From 1904e57d5a98a1d82b1cd57b42533677d4f9fd8c Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Sat, 4 Jan 2025 01:32:32 -0600 Subject: [PATCH] Replace custom ANSI regex logic with Node built-in (#224) --- .changeset/slimy-dolphins-press.md | 5 +++++ packages/prompts/LICENSE | 14 -------------- packages/prompts/src/index.ts | 13 +------------ 3 files changed, 6 insertions(+), 26 deletions(-) create mode 100644 .changeset/slimy-dolphins-press.md diff --git a/.changeset/slimy-dolphins-press.md b/.changeset/slimy-dolphins-press.md new file mode 100644 index 00000000..54be6c29 --- /dev/null +++ b/.changeset/slimy-dolphins-press.md @@ -0,0 +1,5 @@ +--- +"@clack/prompts": patch +--- + +Replace custom utility for stripping ANSI control sequences with Node's built-in [`stripVTControlCharacters`](https://nodejs.org/docs/latest/api/util.html#utilstripvtcontrolcharactersstr) utility. diff --git a/packages/prompts/LICENSE b/packages/prompts/LICENSE index a95d8e47..1ca4cf20 100644 --- a/packages/prompts/LICENSE +++ b/packages/prompts/LICENSE @@ -7,17 +7,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---- - -`ansi-regex` is adapted from https://github.com/chalk/ansi-regex - -MIT License - -Copyright (c) Sindre Sorhus (https://sindresorhus.com) - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 4f261c09..81c32736 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -1,3 +1,4 @@ +import { stripVTControlCharacters as strip } from 'node:util'; import { ConfirmPrompt, GroupMultiSelectPrompt, @@ -569,7 +570,6 @@ export const groupMultiselect = (opts: GroupMultiSelectOptions) => }).prompt() as Promise; }; -const strip = (str: string) => str.replace(ansiRegex(), ''); export const note = (message = '', title = '') => { const lines = `\n${message}\n`.split('\n'); const titleLen = strip(title).length; @@ -740,17 +740,6 @@ export const spinner = () => { }; }; -// Adapted from https://github.com/chalk/ansi-regex -// @see LICENSE -function ansiRegex() { - const pattern = [ - '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', - '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))', - ].join('|'); - - return new RegExp(pattern, 'g'); -} - export type PromptGroupAwaitedReturn = { [P in keyof T]: Exclude, symbol>; };