Skip to content

Commit 373f660

Browse files
authored
Use node:util stripVTControlCharacters instead of own code (#2486)
1 parent 987f289 commit 373f660

3 files changed

Lines changed: 5 additions & 86 deletions

File tree

lib/command.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import childProcess from 'node:child_process';
33
import path from 'node:path';
44
import fs from 'node:fs';
55
import process from 'node:process';
6+
import { stripVTControlCharacters } from 'node:util';
67

78
import { Argument, humanReadableArgName } from './argument.js';
89
import { CommanderError } from './error.js';
9-
import { Help, stripColor } from './help.js';
10+
import { Help } from './help.js';
1011
import { Option, DualOptions } from './option.js';
1112
import { suggestSimilar } from './suggestSimilar.js';
1213

@@ -70,7 +71,7 @@ export class Command extends EventEmitter {
7071
useColor() ?? (process.stdout.isTTY && process.stdout.hasColors?.()),
7172
getErrHasColors: () =>
7273
useColor() ?? (process.stderr.isTTY && process.stderr.hasColors?.()),
73-
stripColor: (str) => stripColor(str),
74+
stripColor: (str) => stripVTControlCharacters(str),
7475
};
7576

7677
this._hidden = false;

lib/help.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { humanReadableArgName } from './argument.js';
2+
import { stripVTControlCharacters } from 'node:util';
23

34
/**
45
* TypeScript import types for JSDoc, used by Visual Studio Code IntelliSense and `npm run typescript-checkJS`
@@ -533,7 +534,7 @@ export class Help {
533534
* @returns {number}
534535
*/
535536
displayWidth(str) {
536-
return stripColor(str).length;
537+
return stripVTControlCharacters(str).length;
537538
}
538539

539540
/**
@@ -728,17 +729,3 @@ export class Help {
728729
return wrappedLines.join('\n');
729730
}
730731
}
731-
732-
/**
733-
* Strip style ANSI escape sequences from the string. In particular, SGR (Select Graphic Rendition) codes.
734-
*
735-
* @param {string} str
736-
* @returns {string}
737-
* @package
738-
*/
739-
740-
export function stripColor(str) {
741-
// eslint-disable-next-line no-control-regex
742-
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
743-
return str.replace(sgrPattern, '');
744-
}

tests/help.stripAnsi.test.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)