Use node:util stripVTControlCharacters instead of own code (#2486)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent 987f289 · Browse files · 3 files·+5 added−86 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+5 added−86 removed
- tests:+0 added−69 removed
- other:+5 added−17 removed
Provenance
- Commit
373f660- Tree
1130778- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
987f289
3 files changed+5 added−86 removed
modified:lib/command.js+3 added-2 removed
1 unmodified line2 import childProcess from 'node:child_process';3 import path from 'node:path';4 import fs from 'node:fs';5 import process from 'node:process';6+import { stripVTControlCharacters } from 'node:util';7 8 import { Argument, humanReadableArgName } from './argument.js';9 import { CommanderError } from './error.js';9-import { Help, stripColor } from './help.js';10+import { Help } from './help.js';11 import { Option, DualOptions } from './option.js';12 import { suggestSimilar } from './suggestSimilar.js';13 14 export class Command extends EventEmitter {55 unmodified lines70 getOutHasColors: () =>71 useColor() ?? (process.stdout.isTTY && process.stdout.hasColors?.()),72 getErrHasColors: () =>73 useColor() ?? (process.stderr.isTTY && process.stderr.hasColors?.()),73- stripColor: (str) => stripColor(str),74+ stripColor: (str) => stripVTControlCharacters(str),75 };76 77 this._hidden = false;78 /** @type {(Option | null | undefined)} */2712 unmodified linesmodified:lib/help.js+2 added-15 removed
1 import { humanReadableArgName } from './argument.js';2+import { stripVTControlCharacters } from 'node:util';3 4 /**5 * TypeScript import types for JSDoc, used by Visual Studio Code IntelliSense and `npm run typescript-checkJS`6 * https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#import-types526 unmodified lines533 * @param {string} str534 * @returns {number}535 */536 displayWidth(str) {536- return stripColor(str).length;537+ return stripVTControlCharacters(str).length;538 }539 540 /**541 * Style the title for displaying in the help. Called with 'Usage:', 'Options:', etc.186 unmodified lines728 729 return wrappedLines.join('\n');730 }731 }731-732-/**733- * Strip style ANSI escape sequences from the string. In particular, SGR (Select Graphic Rendition) codes.734- *735- * @param {string} str736- * @returns {string}737- * @package738- */739-740-export function stripColor(str) {741- // eslint-disable-next-line no-control-regex742- const sgrPattern = /\x1b\[\d*(;\d*)*m/g;743- return str.replace(sgrPattern, '');744-}deleted:tests/help.stripAnsi.test.js-69 removed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import { stripColor } from '../lib/help.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
// https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
// Test the lowest level routine being used for SGR support.
const ESC = '\u001b';const CSI = ESC + '[';
describe('internal stripColor()', () => { test('SGR only', () => { const str = `${CSI}4m`; assert.equal(stripColor(str), ''); });
test('SGR at start', () => { const str = `${CSI}4mX`; assert.equal(stripColor(str), 'X'); });
test('SGR in middle', () => { const str = `X${CSI}4mY`; assert.equal(stripColor(str), 'XY'); });
test('SGR at end', () => { const str = `${CSI}4mY`; assert.equal(stripColor(str), 'Y'); });
test('SGR pair', () => { // underline and not underlined const str = `${CSI}4mABC${CSI}24m`; assert.equal(stripColor(str), 'ABC'); });
test('explicit reset with zero', () => { const str = `${CSI}0m`; assert.equal(stripColor(str), ''); });
test('implicit reset without zero', () => { const str = `${CSI}m`; assert.equal(stripColor(str), ''); });
test('multiple params: select foreground colour', () => { const str = `${CSI}38;5;4m`; assert.equal(stripColor(str), ''); });
test('multiple params: general', () => { const str = `${CSI}2;3;4m`; assert.equal(stripColor(str), ''); });
test('multiple params: missing param', () => { // CSI sequences can omit number (which is then treated as 0) const str = `${CSI};;m`; assert.equal(stripColor(str), ''); });
test('incomplete SGR sequence', () => { const str = `${CSI}14X`; assert.equal(stripColor(str), `${CSI}14X`); });});