diff --git a/fixtures/ansi-codes.js b/fixtures/ansi-codes.js index ba60d29..935be0f 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 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/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..7cfb248 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 character of consumptionCharacters) { + const string = escapeCodeValue + character; + const result = (escapeCodeResultMap.get(key) || '') + character; + + t.is(string.replace(ansiRegex(), ''), result); + } + }); +}