Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match cursorSave and cursorRestore escape codes #45

Merged
merged 5 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Qix- marked this conversation as resolved.
Show resolved Hide resolved
const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArgs[0]]]);

for (const key of Object.keys(ansiEscapes)) {
Qix- marked this conversation as resolved.
Show resolved Hide resolved
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);
}
});
}