From 91843cd081b716c42c02692015062e5cd383a0a2 Mon Sep 17 00:00:00 2001 From: SamVerschueren Date: Wed, 27 Oct 2021 19:13:00 +0200 Subject: [PATCH 1/5] Match cursorSave and cursorRestore escape codes --- index.js | 2 +- package.json | 1 + test.js | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index bbf6af0..0dfae22 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ export default function ansiRegex({onlyFirst = false} = {}) { 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-ntqry=><~]))' + '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))' ].join('|'); return new RegExp(pattern, onlyFirst ? undefined : 'g'); diff --git a/package.json b/package.json index 7bbb563..6445c00 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "pattern" ], "devDependencies": { + "ansi-escapes": "^5.0.0", "ava": "^3.15.0", "tsd": "^0.14.0", "xo": "^0.38.2" diff --git a/test.js b/test.js index 841678e..111f6f2 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,5 @@ import test from 'ava'; +import ansiEscapes from 'ansi-escapes'; import * as ansiCodes from './fixtures/ansi-codes.js'; import ansiRegex from './index.js'; @@ -92,3 +93,28 @@ for (const codeSet of Object.keys(ansiCodes)) { }); } } + +const escapeCodeFunctionArgs = [1, 2]; +const escapeCodeIgnoresList = new Set(['beep', 'image', 'iTerm']); +const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArgs[0]]]) + +for (const key of Object.keys(ansiEscapes)) { + if (escapeCodeIgnoresList.has(key)) { + continue; + } + + const escapeCode = ansiEscapes[key]; + + const escapeCodeValue = typeof escapeCode === 'function' + ? escapeCode(...escapeCodeFunctionArgs) + : escapeCode; + + test(`ansi-escapes ${key}`, (t) => { + for (const c of consumptionCharacters) { + const string = escapeCodeValue + c; + const result = (escapeCodeResultMap.get(key) || '') + c + + t.is(string.replace(ansiRegex(), ''), result); + } + }); +} From ff2b18d4d03cbbd78771398653a91d7eb04ad296 Mon Sep 17 00:00:00 2001 From: SamVerschueren Date: Wed, 27 Oct 2021 19:20:44 +0200 Subject: [PATCH 2/5] Fix tests --- test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test.js b/test.js index 111f6f2..fa3418a 100644 --- a/test.js +++ b/test.js @@ -95,8 +95,8 @@ for (const codeSet of Object.keys(ansiCodes)) { } const escapeCodeFunctionArgs = [1, 2]; -const escapeCodeIgnoresList = new Set(['beep', 'image', 'iTerm']); -const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArgs[0]]]) +const escapeCodeIgnoresList = new Set(['default', 'beep', 'image', 'iTerm']); +const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArgs[0]]]); for (const key of Object.keys(ansiEscapes)) { if (escapeCodeIgnoresList.has(key)) { @@ -105,14 +105,14 @@ for (const key of Object.keys(ansiEscapes)) { const escapeCode = ansiEscapes[key]; - const escapeCodeValue = typeof escapeCode === 'function' - ? escapeCode(...escapeCodeFunctionArgs) - : escapeCode; + const escapeCodeValue = typeof escapeCode === 'function' ? + escapeCode(...escapeCodeFunctionArgs) : + escapeCode; - test(`ansi-escapes ${key}`, (t) => { + test(`ansi-escapes ${key}`, t => { for (const c of consumptionCharacters) { const string = escapeCodeValue + c; - const result = (escapeCodeResultMap.get(key) || '') + c + const result = (escapeCodeResultMap.get(key) || '') + c; t.is(string.replace(ansiRegex(), ''), result); } From cde7d95befd29c8fb26efd50cf887474d3334c44 Mon Sep 17 00:00:00 2001 From: SamVerschueren Date: Thu, 28 Oct 2021 08:21:49 +0200 Subject: [PATCH 3/5] Remove default and add cursorSave/cursorRestore to fixtures --- fixtures/ansi-codes.js | 6 ++++-- test.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/fixtures/ansi-codes.js b/fixtures/ansi-codes.js index ba60d29..15a4b9a 100644 --- a/fixtures/ansi-codes.js +++ b/fixtures/ansi-codes.js @@ -15,10 +15,12 @@ export const vt52Codes = new Map([ ['>', ['Exit alternate keypad mode']], ['1', ['Graphics processor on']], ['2', ['Graphics processor off']], - ['<', ['Enter ANSI mode']] + ['<', ['Enter ANSI mode']], + ['s', ['Cursor save']], + ['u', ['Cursor restore']] ]); -// From http://www.umich.edu/~archive/apple2/misc/programmers/vt100.codes.txt +// From // From https://espterm.github.io/docs/VT100%20escape%20codes.html export const ansiCompatible = new Map([ ['[176A', ['Cursor up Pn lines']], ['[176B', ['Cursor down Pn lines']], diff --git a/test.js b/test.js index fa3418a..4c2a6f7 100644 --- a/test.js +++ b/test.js @@ -95,7 +95,7 @@ for (const codeSet of Object.keys(ansiCodes)) { } const escapeCodeFunctionArgs = [1, 2]; -const escapeCodeIgnoresList = new Set(['default', 'beep', 'image', 'iTerm']); +const escapeCodeIgnoresList = new Set(['beep', 'image', 'iTerm']); const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArgs[0]]]); for (const key of Object.keys(ansiEscapes)) { From cf4b6b0019cf192471f2d99bcbbfca98e63b10e8 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Thu, 28 Oct 2021 13:42:08 +0200 Subject: [PATCH 4/5] Doc tweaks Co-authored-by: Qix --- fixtures/ansi-codes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fixtures/ansi-codes.js b/fixtures/ansi-codes.js index 15a4b9a..935be0f 100644 --- a/fixtures/ansi-codes.js +++ b/fixtures/ansi-codes.js @@ -20,7 +20,7 @@ export const vt52Codes = new Map([ ['u', ['Cursor restore']] ]); -// From // From https://espterm.github.io/docs/VT100%20escape%20codes.html +// From https://espterm.github.io/docs/VT100%20escape%20codes.html export const ansiCompatible = new Map([ ['[176A', ['Cursor up Pn lines']], ['[176B', ['Cursor down Pn lines']], From 958a1dc86e40c73550bc9102271804dca859a507 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 28 Oct 2021 23:51:52 +0700 Subject: [PATCH 5/5] Update test.js --- test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test.js b/test.js index 4c2a6f7..7cfb248 100644 --- a/test.js +++ b/test.js @@ -110,9 +110,9 @@ for (const key of Object.keys(ansiEscapes)) { escapeCode; test(`ansi-escapes ${key}`, t => { - for (const c of consumptionCharacters) { - const string = escapeCodeValue + c; - const result = (escapeCodeResultMap.get(key) || '') + c; + for (const character of consumptionCharacters) { + const string = escapeCodeValue + character; + const result = (escapeCodeResultMap.get(key) || '') + character; t.is(string.replace(ansiRegex(), ''), result); }