From 9f7c4cd6575262cd684858bcca0226b85161a6d8 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 22 Jan 2020 12:03:49 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20properly=20wrap=20long=20?= =?UTF-8?q?ansi=20lines=20to=20preseve=20CSI=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 5 ++++- src/runner.ts | 27 +++++++++++++++++++++++++-- yarn.lock | 5 +++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 7a90da28..e16ac459 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/src/runner.ts b/src/runner.ts index 8242fd76..a0b15ad7 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -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 { @@ -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) { diff --git a/yarn.lock b/yarn.lock index f3fdef1e..8348d30d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"