@peteanderson/ansi 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -70
- package/package.json +20 -13
- package/dist/ansi.d.ts +0 -13
- package/dist/ansi.js +0 -23
- package/dist/caret.d.ts +0 -25
- package/dist/caret.js +0 -40
- package/dist/erase.d.ts +0 -14
- package/dist/erase.js +0 -15
- package/dist/index.d.ts +0 -107
- package/dist/index.js +0 -2
- package/dist/patterns.d.ts +0 -3
- package/dist/patterns.js +0 -43
- package/dist/scroll.d.ts +0 -2
- package/dist/scroll.js +0 -8
- package/dist/sgr/color.d.ts +0 -62
- package/dist/sgr/color.js +0 -111
- package/dist/sgr/index.d.ts +0 -59
- package/dist/sgr/index.js +0 -6
- package/dist/sgr/reset.d.ts +0 -1
- package/dist/sgr/reset.js +0 -3
- package/dist/sgr/sgr.d.ts +0 -14
- package/dist/sgr/sgr.js +0 -47
- package/dist/sgr/style.d.ts +0 -13
- package/dist/sgr/style.js +0 -16
- package/dist/sgr/utils.d.ts +0 -2
- package/dist/sgr/utils.js +0 -16
- package/dist/simplify/atom.d.ts +0 -3
- package/dist/simplify/atom.js +0 -28
- package/dist/simplify/index.d.ts +0 -2
- package/dist/simplify/index.js +0 -2
- package/dist/simplify/lookup.d.ts +0 -5
- package/dist/simplify/lookup.js +0 -77
- package/dist/simplify/simplify.d.ts +0 -1
- package/dist/simplify/simplify.js +0 -25
- package/dist/simplify/state.d.ts +0 -10
- package/dist/simplify/state.js +0 -57
- package/dist/simplify/utils.d.ts +0 -2
- package/dist/simplify/utils.js +0 -31
- package/dist/slice.d.ts +0 -1
- package/dist/slice.js +0 -83
- package/dist/strip.d.ts +0 -15
- package/dist/strip.js +0 -18
- package/src/caret.js +0 -49
- package/src/erase.js +0 -17
- package/src/index.js +0 -1
- package/src/patterns.js +0 -44
- package/src/scroll.js +0 -10
- package/src/sgr/color.js +0 -130
- package/src/sgr/index.js +0 -5
- package/src/sgr/reset.js +0 -3
- package/src/sgr/sgr.js +0 -66
- package/src/sgr/style.js +0 -19
- package/src/sgr/utils.js +0 -20
- package/src/simplify/atom.js +0 -34
- package/src/simplify/index.js +0 -1
- package/src/simplify/lookup.js +0 -86
- package/src/simplify/simplify.js +0 -33
- package/src/simplify/state.js +0 -73
- package/src/simplify/types.d.ts +0 -28
- package/src/simplify/utils.js +0 -41
- package/src/slice.js +0 -99
- package/src/strip.js +0 -23
- /package/{src/ansi.js → ansi.js} +0 -0
package/dist/sgr/index.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
declare const _exports: {
|
|
2
|
-
fg: {
|
|
3
|
-
black: import("./sgr").SGR;
|
|
4
|
-
red: import("./sgr").SGR;
|
|
5
|
-
green: import("./sgr").SGR;
|
|
6
|
-
yellow: import("./sgr").SGR;
|
|
7
|
-
blue: import("./sgr").SGR;
|
|
8
|
-
magenta: import("./sgr").SGR;
|
|
9
|
-
cyan: import("./sgr").SGR;
|
|
10
|
-
white: import("./sgr").SGR;
|
|
11
|
-
brightBlack: import("./sgr").SGR;
|
|
12
|
-
brightRed: import("./sgr").SGR;
|
|
13
|
-
brightGreen: import("./sgr").SGR;
|
|
14
|
-
brightYellow: import("./sgr").SGR;
|
|
15
|
-
brightBlue: import("./sgr").SGR;
|
|
16
|
-
brightMagenta: import("./sgr").SGR;
|
|
17
|
-
brightCyan: import("./sgr").SGR;
|
|
18
|
-
brightWhite: import("./sgr").SGR;
|
|
19
|
-
default: string;
|
|
20
|
-
rgb: (r: number, g: number, b: number) => (text: string) => string;
|
|
21
|
-
x256: (arg0: number, arg1?: number, arg2?: number) => (text: string) => string;
|
|
22
|
-
};
|
|
23
|
-
bg: {
|
|
24
|
-
black: import("./sgr").SGR;
|
|
25
|
-
red: import("./sgr").SGR;
|
|
26
|
-
green: import("./sgr").SGR;
|
|
27
|
-
yellow: import("./sgr").SGR;
|
|
28
|
-
blue: import("./sgr").SGR;
|
|
29
|
-
magenta: import("./sgr").SGR;
|
|
30
|
-
cyan: import("./sgr").SGR;
|
|
31
|
-
white: import("./sgr").SGR;
|
|
32
|
-
brightBlack: import("./sgr").SGR;
|
|
33
|
-
brightRed: import("./sgr").SGR;
|
|
34
|
-
brightGreen: import("./sgr").SGR;
|
|
35
|
-
brightYellow: import("./sgr").SGR;
|
|
36
|
-
brightBlue: import("./sgr").SGR;
|
|
37
|
-
brightMagenta: import("./sgr").SGR;
|
|
38
|
-
brightCyan: import("./sgr").SGR;
|
|
39
|
-
brightWhite: import("./sgr").SGR;
|
|
40
|
-
default: string;
|
|
41
|
-
rgb: (r: number, g: number, b: number) => (text: string) => string;
|
|
42
|
-
x256: (arg0: number, arg1?: number, arg2?: number) => (text: string) => string;
|
|
43
|
-
};
|
|
44
|
-
style: {
|
|
45
|
-
bold: import("./sgr").SGR;
|
|
46
|
-
dim: import("./sgr").SGR;
|
|
47
|
-
italic: import("./sgr").SGR;
|
|
48
|
-
underline: import("./sgr").SGR;
|
|
49
|
-
inverse: import("./sgr").SGR;
|
|
50
|
-
hidden: import("./sgr").SGR;
|
|
51
|
-
strikethrough: import("./sgr").SGR;
|
|
52
|
-
doubleUnderline: import("./sgr").SGR;
|
|
53
|
-
frame: import("./sgr").SGR;
|
|
54
|
-
encircle: import("./sgr").SGR;
|
|
55
|
-
overline: import("./sgr").SGR;
|
|
56
|
-
};
|
|
57
|
-
reset: "\u001B[0m";
|
|
58
|
-
};
|
|
59
|
-
export = _exports;
|
package/dist/sgr/index.js
DELETED
package/dist/sgr/reset.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const reset: "\u001B[0m";
|
package/dist/sgr/reset.js
DELETED
package/dist/sgr/sgr.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export type Code = number | number[];
|
|
2
|
-
export type SGR = {
|
|
3
|
-
(text: string): string;
|
|
4
|
-
open: string;
|
|
5
|
-
close: string;
|
|
6
|
-
[$codes]: {
|
|
7
|
-
open: Code;
|
|
8
|
-
close: Code;
|
|
9
|
-
};
|
|
10
|
-
combine: (...styles: SGR[]) => SGR;
|
|
11
|
-
};
|
|
12
|
-
export function sgr(open: Code, close: Code, reset?: boolean): SGR;
|
|
13
|
-
declare const $codes: unique symbol;
|
|
14
|
-
export {};
|
package/dist/sgr/sgr.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const $codes = Symbol();
|
|
3
|
-
/**
|
|
4
|
-
@typedef {number | number[]} Code
|
|
5
|
-
@typedef {{
|
|
6
|
-
(text: string): string;
|
|
7
|
-
open : string;
|
|
8
|
-
close : string;
|
|
9
|
-
[$codes] : {open: Code, close: Code};
|
|
10
|
-
combine : (...styles: SGR[]) => SGR;
|
|
11
|
-
}} SGR
|
|
12
|
-
*/
|
|
13
|
-
/** @type {(code: Code, codes: Code[]) => Code} */
|
|
14
|
-
function combine(code, codes) {
|
|
15
|
-
if (!codes.length)
|
|
16
|
-
return code;
|
|
17
|
-
const rtn = Array.isArray(code) ? [...code] : [code];
|
|
18
|
-
for (const c of codes) {
|
|
19
|
-
if (Array.isArray(c))
|
|
20
|
-
rtn.push(...c);
|
|
21
|
-
else
|
|
22
|
-
rtn.push(c);
|
|
23
|
-
}
|
|
24
|
-
return rtn;
|
|
25
|
-
}
|
|
26
|
-
/** @type {(code: Code) => string | number} */
|
|
27
|
-
const codeSequence = code => Array.isArray(code) ? code.join(';') : code;
|
|
28
|
-
/** @type {(open: Code, close: Code, reset?: boolean) => SGR} */
|
|
29
|
-
function sgr(open, close, reset = false) {
|
|
30
|
-
const openCode = codeSequence(open);
|
|
31
|
-
const closeCode = codeSequence(close);
|
|
32
|
-
const openSequence = `\x1b[${openCode}m`;
|
|
33
|
-
const closeSequence = `\x1b[${closeCode}m`;
|
|
34
|
-
const reopenCode = reset ? `${closeCode};${openCode}` : openCode;
|
|
35
|
-
const rxClose = new RegExp(`(?<start>\\x1b\\[(?:\\d+;)*)${closeCode}(?<end>(?:;\\d+)*m)`, "g");
|
|
36
|
-
const replace = `$<start>${reopenCode}$<end>`;
|
|
37
|
-
/** @type {(text: string) => string} */
|
|
38
|
-
const func = s => openSequence + s.replace(rxClose, replace) + closeSequence;
|
|
39
|
-
return Object.assign(func, {
|
|
40
|
-
[$codes]: { open, close },
|
|
41
|
-
open: openSequence,
|
|
42
|
-
close: closeSequence,
|
|
43
|
-
/** @type {(...styles: SGR[]) => SGR} */
|
|
44
|
-
combine: (...styles) => sgr(combine(open, styles.map(s => s[$codes].open)), combine(close, styles.map(s => s[$codes].close))),
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
module.exports = { sgr };
|
package/dist/sgr/style.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export namespace style {
|
|
2
|
-
let bold: import("./sgr").SGR;
|
|
3
|
-
let dim: import("./sgr").SGR;
|
|
4
|
-
let italic: import("./sgr").SGR;
|
|
5
|
-
let underline: import("./sgr").SGR;
|
|
6
|
-
let inverse: import("./sgr").SGR;
|
|
7
|
-
let hidden: import("./sgr").SGR;
|
|
8
|
-
let strikethrough: import("./sgr").SGR;
|
|
9
|
-
let doubleUnderline: import("./sgr").SGR;
|
|
10
|
-
let frame: import("./sgr").SGR;
|
|
11
|
-
let encircle: import("./sgr").SGR;
|
|
12
|
-
let overline: import("./sgr").SGR;
|
|
13
|
-
}
|
package/dist/sgr/style.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const { sgr } = require("./sgr");
|
|
3
|
-
const style = {
|
|
4
|
-
bold: sgr(1, 22, true),
|
|
5
|
-
dim: sgr(2, 22, true),
|
|
6
|
-
italic: sgr(3, 23),
|
|
7
|
-
underline: sgr(4, 24),
|
|
8
|
-
inverse: sgr(7, 27),
|
|
9
|
-
hidden: sgr(8, 28),
|
|
10
|
-
strikethrough: sgr(9, 29),
|
|
11
|
-
doubleUnderline: sgr(21, 24),
|
|
12
|
-
frame: sgr(51, 54),
|
|
13
|
-
encircle: sgr(52, 54),
|
|
14
|
-
overline: sgr(53, 55),
|
|
15
|
-
};
|
|
16
|
-
module.exports = { style };
|
package/dist/sgr/utils.d.ts
DELETED
package/dist/sgr/utils.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/** @typedef {number | number[]} Code */
|
|
3
|
-
/** @type {(code: Code, codes: Code[]) => Code} */
|
|
4
|
-
function combine(code, codes) {
|
|
5
|
-
if (!codes.length)
|
|
6
|
-
return code;
|
|
7
|
-
const rtn = Array.isArray(code) ? [...code] : [code];
|
|
8
|
-
for (const c of codes) {
|
|
9
|
-
if (Array.isArray(c))
|
|
10
|
-
rtn.push(...c);
|
|
11
|
-
else
|
|
12
|
-
rtn.push(c);
|
|
13
|
-
}
|
|
14
|
-
return rtn;
|
|
15
|
-
}
|
|
16
|
-
module.exports = { combine };
|
package/dist/simplify/atom.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export type BuildAtom = import("./types").BuildAtom;
|
|
2
|
-
export function buildExtendedColorAtom(attribute: import("./types").Attribute, codes: number[], index: number, state: import("./types").AttributeMap): import("./types").Atom;
|
|
3
|
-
export function buildIntensityAtom(attribute: import("./types").Attribute, codes: number[], index: number, state: import("./types").AttributeMap): import("./types").Atom;
|
package/dist/simplify/atom.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/** @typedef {import("./types").BuildAtom} BuildAtom */
|
|
3
|
-
/** @type {BuildAtom} */
|
|
4
|
-
function buildExtendedColorAtom(attribute, codes, index) {
|
|
5
|
-
switch (codes[index + 1]) {
|
|
6
|
-
case 2:
|
|
7
|
-
if (index + 5 >= codes.length)
|
|
8
|
-
return { attribute, code: codes.slice(index), skip: codes.length - index - 1 };
|
|
9
|
-
return { attribute, code: codes.slice(index, index + 5), skip: 4 };
|
|
10
|
-
case 5:
|
|
11
|
-
if (index + 3 >= codes.length)
|
|
12
|
-
return { attribute, code: codes.slice(index), skip: codes.length - index - 1 };
|
|
13
|
-
return { attribute, code: codes.slice(index, index + 3), skip: 2 };
|
|
14
|
-
default:
|
|
15
|
-
return { attribute, code: codes.slice(index, index + 2), skip: 1 };
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
/** @type {BuildAtom} */
|
|
19
|
-
function buildIntensityAtom(attribute, codes, index, state) {
|
|
20
|
-
const code = codes[index];
|
|
21
|
-
if (code === 22)
|
|
22
|
-
return { attribute, code: [code] };
|
|
23
|
-
const current = state.get("intensity");
|
|
24
|
-
if (!current || current.length == 1 && current[0] === code || current[0] === 22)
|
|
25
|
-
return { attribute, code: [code] };
|
|
26
|
-
return { attribute, code: [1, 2] };
|
|
27
|
-
}
|
|
28
|
-
module.exports = { buildExtendedColorAtom, buildIntensityAtom };
|
package/dist/simplify/index.d.ts
DELETED
package/dist/simplify/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export type Atom = import("./types").Atom;
|
|
2
|
-
export type Attribute = import("./types").Attribute;
|
|
3
|
-
export type AttributeMap = import("./types").AttributeMap;
|
|
4
|
-
export type BuildAtom = import("./types").BuildAtom;
|
|
5
|
-
export function lookUpAtom(codes: number[], index: number, state: AttributeMap): Atom;
|
package/dist/simplify/lookup.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const { buildExtendedColorAtom, buildIntensityAtom } = require("./atom");
|
|
3
|
-
/** @typedef {import("./types").Atom} Atom */
|
|
4
|
-
/** @typedef {import("./types").Attribute} Attribute */
|
|
5
|
-
/** @typedef {import("./types").AttributeMap} AttributeMap */
|
|
6
|
-
/** @typedef {import("./types").BuildAtom} BuildAtom */
|
|
7
|
-
/** @type {Record<number, [attribute: Attribute, buildAtom?: BuildAtom]>} */
|
|
8
|
-
const codeMap = {
|
|
9
|
-
0: ["reset"],
|
|
10
|
-
1: ["intensity", buildIntensityAtom],
|
|
11
|
-
2: ["intensity", buildIntensityAtom],
|
|
12
|
-
3: ["italic"],
|
|
13
|
-
4: ["underline"],
|
|
14
|
-
7: ["inverse"],
|
|
15
|
-
8: ["hidden"],
|
|
16
|
-
9: ["strikethrough"],
|
|
17
|
-
21: ["underline"],
|
|
18
|
-
22: ["intensity", buildIntensityAtom],
|
|
19
|
-
23: ["italic"],
|
|
20
|
-
24: ["underline"],
|
|
21
|
-
27: ["inverse"],
|
|
22
|
-
28: ["hidden"],
|
|
23
|
-
29: ["strikethrough"],
|
|
24
|
-
30: ["fg"],
|
|
25
|
-
31: ["fg"],
|
|
26
|
-
32: ["fg"],
|
|
27
|
-
33: ["fg"],
|
|
28
|
-
34: ["fg"],
|
|
29
|
-
35: ["fg"],
|
|
30
|
-
36: ["fg"],
|
|
31
|
-
37: ["fg"],
|
|
32
|
-
38: ["fg", buildExtendedColorAtom],
|
|
33
|
-
39: ["fg"],
|
|
34
|
-
40: ["bg"],
|
|
35
|
-
41: ["bg"],
|
|
36
|
-
42: ["bg"],
|
|
37
|
-
43: ["bg"],
|
|
38
|
-
44: ["bg"],
|
|
39
|
-
45: ["bg"],
|
|
40
|
-
46: ["bg"],
|
|
41
|
-
47: ["bg"],
|
|
42
|
-
48: ["bg", buildExtendedColorAtom],
|
|
43
|
-
49: ["bg"],
|
|
44
|
-
51: ["frame"],
|
|
45
|
-
52: ["frame"],
|
|
46
|
-
53: ["overline"],
|
|
47
|
-
54: ["frame"],
|
|
48
|
-
55: ["overline"],
|
|
49
|
-
90: ["fg"],
|
|
50
|
-
91: ["fg"],
|
|
51
|
-
92: ["fg"],
|
|
52
|
-
93: ["fg"],
|
|
53
|
-
94: ["fg"],
|
|
54
|
-
95: ["fg"],
|
|
55
|
-
96: ["fg"],
|
|
56
|
-
97: ["fg"],
|
|
57
|
-
100: ["bg"],
|
|
58
|
-
101: ["bg"],
|
|
59
|
-
102: ["bg"],
|
|
60
|
-
103: ["bg"],
|
|
61
|
-
104: ["bg"],
|
|
62
|
-
105: ["bg"],
|
|
63
|
-
106: ["bg"],
|
|
64
|
-
107: ["bg"],
|
|
65
|
-
};
|
|
66
|
-
/** @type {(codes: number[], index: number, state: AttributeMap) => Atom} */
|
|
67
|
-
function lookUpAtom(codes, index, state) {
|
|
68
|
-
const code = codes[index];
|
|
69
|
-
const entry = codeMap[code];
|
|
70
|
-
if (!entry)
|
|
71
|
-
return { attribute: code, code: [code] };
|
|
72
|
-
const [attribute, buildAtom] = entry;
|
|
73
|
-
if (buildAtom)
|
|
74
|
-
return buildAtom(attribute, codes, index, state);
|
|
75
|
-
return { attribute, code: [code] };
|
|
76
|
-
}
|
|
77
|
-
module.exports = { lookUpAtom };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export function simplify(text: string): string;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const { rxSGR } = require("../patterns");
|
|
3
|
-
const { State } = require("./state");
|
|
4
|
-
/** @type {(text: string) => string} */
|
|
5
|
-
function simplify(text) {
|
|
6
|
-
// `.split` doesn't modify `lastIndex`, so no clone is necessary here.
|
|
7
|
-
const parts = text.split(rxSGR);
|
|
8
|
-
const state = new State();
|
|
9
|
-
let result = parts[0];
|
|
10
|
-
for (let i = 1; i < parts.length;) {
|
|
11
|
-
let codes = parts[i++].split(";").map(Number);
|
|
12
|
-
let chunk = parts[i++];
|
|
13
|
-
while (!chunk && i < parts.length) {
|
|
14
|
-
// Merge consecutive SGR sequences.
|
|
15
|
-
codes.push(...parts[i++].split(";").map(Number));
|
|
16
|
-
chunk = parts[i++];
|
|
17
|
-
}
|
|
18
|
-
codes = state.update(codes);
|
|
19
|
-
if (codes.length)
|
|
20
|
-
result += `\x1b[${codes.join(";")}m`;
|
|
21
|
-
result += chunk;
|
|
22
|
-
}
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
module.exports = { simplify };
|
package/dist/simplify/state.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export type Attribute = import("./types").Attribute;
|
|
2
|
-
export type AttributeMap = import("./types").AttributeMap;
|
|
3
|
-
/** @type {AttributeMap} */
|
|
4
|
-
export class State extends Map<any, any> {
|
|
5
|
-
constructor();
|
|
6
|
-
constructor(entries?: readonly (readonly [any, any])[] | null | undefined);
|
|
7
|
-
constructor();
|
|
8
|
-
constructor(iterable?: Iterable<readonly [any, any]> | null | undefined);
|
|
9
|
-
update(codes: number[]): number[];
|
|
10
|
-
}
|
package/dist/simplify/state.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const { lookUpAtom } = require("./lookup");
|
|
3
|
-
const { arraysEqual, transitionIntensity } = require("./utils");
|
|
4
|
-
/** @typedef {import("./types").Attribute} Attribute */
|
|
5
|
-
/** @typedef {import("./types").AttributeMap} AttributeMap */
|
|
6
|
-
/** @type {Map<Attribute, number[]>} */
|
|
7
|
-
const defaultCodes = new Map([
|
|
8
|
-
["intensity", [22]],
|
|
9
|
-
["italic", [23]],
|
|
10
|
-
["underline", [24]],
|
|
11
|
-
["inverse", [27]],
|
|
12
|
-
["hidden", [28]],
|
|
13
|
-
["strikethrough", [29]],
|
|
14
|
-
["fg", [39]],
|
|
15
|
-
["bg", [49]],
|
|
16
|
-
["frame", [54]],
|
|
17
|
-
["overline", [55]],
|
|
18
|
-
]);
|
|
19
|
-
/** @type {AttributeMap} */
|
|
20
|
-
class State extends Map {
|
|
21
|
-
/** @type {(codes: number[]) => number[]} */
|
|
22
|
-
update(codes) {
|
|
23
|
-
/** @type {AttributeMap} */
|
|
24
|
-
let snapshot = new Map(this);
|
|
25
|
-
/** @type {Set<Attribute>} */
|
|
26
|
-
const attributes = new Set();
|
|
27
|
-
for (let i = 0; i < codes.length; i++) {
|
|
28
|
-
const { attribute, code, skip } = lookUpAtom(codes, i, this);
|
|
29
|
-
if (attribute === "reset") {
|
|
30
|
-
attributes.clear();
|
|
31
|
-
this.clear();
|
|
32
|
-
attributes.add("reset");
|
|
33
|
-
for (const [attr, defaultCode] of defaultCodes)
|
|
34
|
-
this.set(attr, defaultCode);
|
|
35
|
-
snapshot = new Map(this);
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
attributes.add(attribute);
|
|
39
|
-
this.set(attribute, code);
|
|
40
|
-
if (skip)
|
|
41
|
-
i += skip;
|
|
42
|
-
}
|
|
43
|
-
return Array.from(attributes).flatMap(attribute => {
|
|
44
|
-
if (attribute === "reset")
|
|
45
|
-
return [0];
|
|
46
|
-
const code = this.get(attribute);
|
|
47
|
-
if (arraysEqual(code, snapshot.get(attribute)))
|
|
48
|
-
return [];
|
|
49
|
-
if (attribute === "intensity") {
|
|
50
|
-
const previous = snapshot.get(attribute);
|
|
51
|
-
return transitionIntensity(previous, code);
|
|
52
|
-
}
|
|
53
|
-
return code;
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
module.exports = { State };
|
package/dist/simplify/utils.d.ts
DELETED
package/dist/simplify/utils.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/** @type {<T>(a: T[] | undefined, b: T[] | undefined) => boolean} */
|
|
3
|
-
function arraysEqual(a, b) {
|
|
4
|
-
if (a === b)
|
|
5
|
-
return true;
|
|
6
|
-
if (!a || !b || a.length !== b.length)
|
|
7
|
-
return false;
|
|
8
|
-
for (let i = 0; i < a.length; i++) {
|
|
9
|
-
if (a[i] !== b[i])
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
/** @type {(previous: number[] | undefined, current: number[]) => number[]} */
|
|
15
|
-
function transitionIntensity(previous, current) {
|
|
16
|
-
if (!previous || previous[0] === 22 || current[0] === 22)
|
|
17
|
-
return current;
|
|
18
|
-
const oldBold = previous[0] === 1;
|
|
19
|
-
const newBold = current[0] === 1;
|
|
20
|
-
const oldDim = previous[previous.length - 1] === 2;
|
|
21
|
-
const newDim = current[current.length - 1] === 2;
|
|
22
|
-
const result = [];
|
|
23
|
-
if (oldBold && !newBold || oldDim && !newDim)
|
|
24
|
-
result.push(22);
|
|
25
|
-
if (newBold && (!oldBold || result[0] === 22))
|
|
26
|
-
result.push(1);
|
|
27
|
-
if (newDim && (!oldDim || result[0] === 22))
|
|
28
|
-
result.push(2);
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
module.exports = { arraysEqual, transitionIntensity };
|
package/dist/slice.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export function slice(text: string, start: number, end: number): string;
|
package/dist/slice.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const { rxCSI, rxSGR } = require("./patterns");
|
|
3
|
-
const { simplify } = require("./simplify");
|
|
4
|
-
const { visibleLength } = require("./strip");
|
|
5
|
-
/**
|
|
6
|
-
@type {
|
|
7
|
-
(
|
|
8
|
-
text : string,
|
|
9
|
-
fromStringIndex : number,
|
|
10
|
-
visibleCount : number,
|
|
11
|
-
) => {index: number, length: number}
|
|
12
|
-
}
|
|
13
|
-
*/
|
|
14
|
-
function scanCSI(text, fromStringIndex, visibleCount) {
|
|
15
|
-
// We intentionally mutate `lastIndex` here, so we need a clone.
|
|
16
|
-
const rx = new RegExp(rxCSI);
|
|
17
|
-
let length = 0;
|
|
18
|
-
let index = fromStringIndex;
|
|
19
|
-
while (index < text.length && length < visibleCount) {
|
|
20
|
-
if (text[index] === "\x1b") {
|
|
21
|
-
rx.lastIndex = index;
|
|
22
|
-
const match = rx.exec(text);
|
|
23
|
-
if (match?.index === index) {
|
|
24
|
-
index += match[0].length;
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
index++;
|
|
29
|
-
length++;
|
|
30
|
-
}
|
|
31
|
-
return { index, length };
|
|
32
|
-
}
|
|
33
|
-
/** @type {(text: string, start: number, end?: number) => string} */
|
|
34
|
-
function skippedSequences(text, start, end) {
|
|
35
|
-
const sequences = text.slice(start, end).match(new RegExp(rxSGR));
|
|
36
|
-
return sequences ? simplify(sequences.join("")) : "";
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Return a slice of the given text, counting only visible characters (i.e. ignoring ANSI escape
|
|
40
|
-
* codes). The returned slice will include any ANSI escape codes from within the slice. In addition,
|
|
41
|
-
* SGR codes (m) from before and after the slice will be combined, simplified, and included so that
|
|
42
|
-
* the terminal state before and after the slice will be the same as if the whole string were
|
|
43
|
-
* printed. If you compose two or more slices together, there may be redundant SGR sequences at the
|
|
44
|
-
* boundaries, but the result will be correct. You can use `simplify` to remove the redundancies.
|
|
45
|
-
*
|
|
46
|
-
* Note the following caveats:
|
|
47
|
-
*
|
|
48
|
-
* * Any non-SGR ANSI codes prior to or after the slice will be ignored.
|
|
49
|
-
* * Control characters (such as newlines and backspaces) both outside and inside the slice are
|
|
50
|
-
* counted as visible characters.
|
|
51
|
-
*
|
|
52
|
-
* These caveats mean that text may not appear on screen to be the same number of characters as the
|
|
53
|
-
* length of the slice, and that the terminal state may not be exactly the same at the start and end
|
|
54
|
-
* of the slice as it would be if the whole string were printed. This function is intended mostly
|
|
55
|
-
* for text that contains SGR codes, but no other ANSI codes or control characters.
|
|
56
|
-
*
|
|
57
|
-
* @type {(text: string, start: number, end: number) => string}
|
|
58
|
-
*/
|
|
59
|
-
function slice(text, start, end) {
|
|
60
|
-
/** @type {number | undefined} */
|
|
61
|
-
let textVisibleLength;
|
|
62
|
-
const getVisibleLength = () => textVisibleLength ??= visibleLength(text);
|
|
63
|
-
if (start < 0 || end < 0) {
|
|
64
|
-
textVisibleLength = getVisibleLength();
|
|
65
|
-
if (start < 0)
|
|
66
|
-
start = Math.max(0, textVisibleLength + start);
|
|
67
|
-
if (end < 0)
|
|
68
|
-
end = Math.max(0, textVisibleLength + end);
|
|
69
|
-
}
|
|
70
|
-
if (start > end)
|
|
71
|
-
return "";
|
|
72
|
-
if (start === 0 && (end >= text.length || end >= getVisibleLength()))
|
|
73
|
-
return text;
|
|
74
|
-
const { index: startIndex, length: visibleSkipped } = scanCSI(text, 0, start);
|
|
75
|
-
const endIndex = startIndex < text.length && visibleSkipped < end
|
|
76
|
-
? scanCSI(text, startIndex, end - visibleSkipped).index
|
|
77
|
-
: text.length;
|
|
78
|
-
// Start and end the string in the same terminal state it would be in if the string weren't sliced.
|
|
79
|
-
const leadingSequences = skippedSequences(text, 0, startIndex);
|
|
80
|
-
const trailingSequences = skippedSequences(text, endIndex);
|
|
81
|
-
return leadingSequences + text.slice(startIndex, endIndex) + trailingSequences;
|
|
82
|
-
}
|
|
83
|
-
module.exports = { slice };
|
package/dist/strip.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remove all ANSI escape codes from the given text.
|
|
3
|
-
* @type {(text: string) => string}
|
|
4
|
-
*/
|
|
5
|
-
export const strip: (text: string) => string;
|
|
6
|
-
/**
|
|
7
|
-
* Calculate the visible length of a string with ANSI escape codes stripped.
|
|
8
|
-
* @type {(text: string) => number}
|
|
9
|
-
*/
|
|
10
|
-
export const visibleLength: (text: string) => number;
|
|
11
|
-
/**
|
|
12
|
-
* Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
|
|
13
|
-
* @type {(text: string) => string}
|
|
14
|
-
*/
|
|
15
|
-
export const sanitize: (text: string) => string;
|
package/dist/strip.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const { rxCSI, rxUnsafe } = require("./patterns");
|
|
3
|
-
/**
|
|
4
|
-
* Remove all ANSI escape codes from the given text.
|
|
5
|
-
* @type {(text: string) => string}
|
|
6
|
-
*/
|
|
7
|
-
const strip = text => text.replace(new RegExp(rxCSI), "");
|
|
8
|
-
/**
|
|
9
|
-
* Calculate the visible length of a string with ANSI escape codes stripped.
|
|
10
|
-
* @type {(text: string) => number}
|
|
11
|
-
*/
|
|
12
|
-
const visibleLength = text => strip(text).length;
|
|
13
|
-
/**
|
|
14
|
-
* Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
|
|
15
|
-
* @type {(text: string) => string}
|
|
16
|
-
*/
|
|
17
|
-
const sanitize = text => text.replace(new RegExp(rxUnsafe), "");
|
|
18
|
-
module.exports = { strip, visibleLength, sanitize };
|
package/src/caret.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/** @type {(code: string) => (count?: number) => string} */
|
|
4
|
-
const move = code =>
|
|
5
|
-
count => `\x1b[${count === undefined ? "" : count ? count : "?"}${code}`;
|
|
6
|
-
|
|
7
|
-
const caret = {
|
|
8
|
-
up : move("A"),
|
|
9
|
-
down : move("B"),
|
|
10
|
-
forward : move("C"),
|
|
11
|
-
backward : move("D"),
|
|
12
|
-
nextLine : move("E"),
|
|
13
|
-
prevLine : move("F"),
|
|
14
|
-
|
|
15
|
-
/** @type {(x: number) => string} */
|
|
16
|
-
x: x => `\x1b[${x}G`,
|
|
17
|
-
|
|
18
|
-
// These are the old VT100 codes, but they're widely supported by old and new terminals. The
|
|
19
|
-
// newer VT220 codes are standardized by ECMA and ISO, and are also widely supported, but might
|
|
20
|
-
// not work in some older terminals. The VT220 codes are:
|
|
21
|
-
//
|
|
22
|
-
// ```
|
|
23
|
-
// save : "\x1b[s",
|
|
24
|
-
// restore : "\x1b[u",
|
|
25
|
-
// ```
|
|
26
|
-
save : "\x1b7",
|
|
27
|
-
restore : "\x1b8",
|
|
28
|
-
|
|
29
|
-
hide : "\x1b[?25l",
|
|
30
|
-
show : "\x1b[?25h",
|
|
31
|
-
|
|
32
|
-
position: {
|
|
33
|
-
get: "\x1b[6n",
|
|
34
|
-
/** @type {(x: number, y: number) => string} */
|
|
35
|
-
set: (x, y) => `\x1b[${y};${x}H`,
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
shape: {
|
|
39
|
-
steadyBlock : "\x1b[2 q",
|
|
40
|
-
steadyBar : "\x1b[6 q",
|
|
41
|
-
steadyUnderline : "\x1b[4 q",
|
|
42
|
-
blinkingBlock : "\x1b[1 q",
|
|
43
|
-
blinkingBar : "\x1b[5 q",
|
|
44
|
-
blinkingUnderline : "\x1b[3 q",
|
|
45
|
-
default : "\x1b[0 q",
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
module.exports = caret;
|
package/src/erase.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const erase = {
|
|
4
|
-
line: {
|
|
5
|
-
toStart: "\x1b[1K",
|
|
6
|
-
toEnd : "\x1b[0K",
|
|
7
|
-
full : "\x1b[2K",
|
|
8
|
-
},
|
|
9
|
-
screen: {
|
|
10
|
-
toStart : "\x1b[1J",
|
|
11
|
-
toEnd : "\x1b[0J",
|
|
12
|
-
full : "\x1b[2J",
|
|
13
|
-
scrollback : "\x1b[3J",
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
module.exports = erase;
|
package/src/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require("./ansi");
|