Skip to content

Commit

Permalink
fix: πŸ› properly wrap long ansi lines to preseve CSI output
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 22, 2020
1 parent 3fb2a5c commit 9f7c4cd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"@types/jest": "^24.0.25",
"@types/node": "^13.1.8",
"@types/supports-color": "^5.3.0",
"@types/wrap-ansi": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^2.17.0",
"@typescript-eslint/parser": "^2.17.0",
"commitlint": "^8.3.5",
Expand Down Expand Up @@ -108,6 +109,8 @@
"cli-spinners": "^2.2.0",
"commander": "^4.1.0",
"shellwords-ts": "^2.0.4",
"supports-color": "^7.1.0"
"string-width": "^4.2.0",
"supports-color": "^7.1.0",
"wrap-ansi": "^6.2.0"
}
}
27 changes: 25 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { OutputSpinner, Spinner } from "./spinner"
import supportsColor from "supports-color"
import { Spawner } from "./spawn"

import wrapAnsi from "wrap-ansi"
// eslint-disable-next-line import/default
import stringWidth from "string-width"

type CliOptions = { [key: string]: string | boolean }

export class Runner {
Expand Down Expand Up @@ -96,13 +100,32 @@ export class Runner {

if (this.options.flat)
spawner.onLine = (line: string) => {
line = chalk.grey.dim(`[${basename(cmd)}]`) + " " + line
const prefix = chalk.grey.dim(`[${basename(cmd)}]`) + " "
line = wrapAnsi(
`${line}`,
process.stdout.columns - stringWidth(prefix) - 1,
{
trim: false,
wordWrap: true,
hard: false,
}
)
line = prefix + line.replace(/\n/g, `\n${prefix}`)
output += line + "\n"
if (!this.options.silent) console.log(line)
}
else
spawner.onData = (data: string) => {
let ret = `${data}`.replace(/\n/g, `\n${prefix}`)
let ret = wrapAnsi(
`${data}`,
process.stdout.columns - stringWidth(prefix),
{
trim: false,
wordWrap: true,
hard: false,
}
)
ret = ret.replace(/\n/g, `\n${prefix}`)
if (!output.length) ret = prefix + ret
output += ret
if (!this.options.silent && spinner) {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,11 @@
resolved "https://registry.yarnpkg.com/@types/supports-color/-/supports-color-5.3.0.tgz#eb6a52e9531fb3ebcd401cec774d1bdfb571f793"
integrity sha512-WxwTXnHTIsk7srax1icjLgX+6w1MUAJbhyCpRP/45paEElsPDQUJZDgr1UpKuL2S3Tb+ZyX9MjWwmcSD4bUoOQ==

"@types/wrap-ansi@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/wrap-ansi/-/wrap-ansi-3.0.0.tgz#18b97a972f94f60a679fd5c796d96421b9abb9fd"
integrity sha512-ltIpx+kM7g/MLRZfkbL7EsCEjfzCcScLpkg37eXEtx5kmrAKBkTJwd1GIAjDSL8wTpM6Hzn5YO4pSb91BEwu1g==

"@types/yargs-parser@*":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
Expand Down

0 comments on commit 9f7c4cd

Please sign in to comment.