Skip to content

Commit 02fa893

Browse files
SamVerschuerenQix-sindresorhus
authoredOct 28, 2021
Match cursorSave and cursorRestore escape codes (#45)
Co-authored-by: Qix <Qix-@users.noreply.github.com> Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent a28b8e7 commit 02fa893

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed
 

‎fixtures/ansi-codes.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ export const vt52Codes = new Map([
1515
['>', ['Exit alternate keypad mode']],
1616
['1', ['Graphics processor on']],
1717
['2', ['Graphics processor off']],
18-
['<', ['Enter ANSI mode']]
18+
['<', ['Enter ANSI mode']],
19+
['s', ['Cursor save']],
20+
['u', ['Cursor restore']]
1921
]);
2022

21-
// From http://www.umich.edu/~archive/apple2/misc/programmers/vt100.codes.txt
23+
// From https://espterm.github.io/docs/VT100%20escape%20codes.html
2224
export const ansiCompatible = new Map([
2325
['[176A', ['Cursor up Pn lines']],
2426
['[176B', ['Cursor down Pn lines']],

‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default function ansiRegex({onlyFirst = false} = {}) {
22
const pattern = [
33
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
4-
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
4+
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))'
55
].join('|');
66

77
return new RegExp(pattern, onlyFirst ? undefined : 'g');

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"pattern"
5252
],
5353
"devDependencies": {
54+
"ansi-escapes": "^5.0.0",
5455
"ava": "^3.15.0",
5556
"tsd": "^0.14.0",
5657
"xo": "^0.38.2"

‎test.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from 'ava';
2+
import ansiEscapes from 'ansi-escapes';
23
import * as ansiCodes from './fixtures/ansi-codes.js';
34
import ansiRegex from './index.js';
45

@@ -92,3 +93,28 @@ for (const codeSet of Object.keys(ansiCodes)) {
9293
});
9394
}
9495
}
96+
97+
const escapeCodeFunctionArgs = [1, 2];
98+
const escapeCodeIgnoresList = new Set(['beep', 'image', 'iTerm']);
99+
const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArgs[0]]]);
100+
101+
for (const key of Object.keys(ansiEscapes)) {
102+
if (escapeCodeIgnoresList.has(key)) {
103+
continue;
104+
}
105+
106+
const escapeCode = ansiEscapes[key];
107+
108+
const escapeCodeValue = typeof escapeCode === 'function' ?
109+
escapeCode(...escapeCodeFunctionArgs) :
110+
escapeCode;
111+
112+
test(`ansi-escapes ${key}`, t => {
113+
for (const character of consumptionCharacters) {
114+
const string = escapeCodeValue + character;
115+
const result = (escapeCodeResultMap.get(key) || '') + character;
116+
117+
t.is(string.replace(ansiRegex(), ''), result);
118+
}
119+
});
120+
}

0 commit comments

Comments
 (0)
Please sign in to comment.