Skip to content

Commit

Permalink
Match cursorSave and cursorRestore escape codes (#45)
Browse files Browse the repository at this point in the history
Co-authored-by: Qix <[email protected]>
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2021
1 parent a28b8e7 commit 02fa893
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
6 changes: 4 additions & 2 deletions fixtures/ansi-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']],
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"pattern"
],
"devDependencies": {
"ansi-escapes": "^5.0.0",
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
}
});
}

0 comments on commit 02fa893

Please sign in to comment.