@peteanderson/ansi 0.2.0 → 0.2.2
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 +9 -3
- package/dist/ansi.d.ts +6 -5
- package/dist/ansi.js +2 -0
- package/dist/index.d.ts +46 -174
- package/dist/sgr/color.d.ts +62 -0
- package/dist/sgr/color.js +111 -0
- package/dist/sgr/index.d.ts +59 -0
- package/dist/sgr/index.js +6 -0
- package/dist/sgr/reset.d.ts +1 -0
- package/dist/sgr/reset.js +3 -0
- package/dist/sgr/sgr.d.ts +14 -0
- package/dist/sgr/sgr.js +47 -0
- package/dist/sgr/style.d.ts +13 -0
- package/dist/sgr/style.js +16 -0
- package/dist/sgr/utils.d.ts +2 -0
- package/dist/sgr/utils.js +16 -0
- package/dist/simplify/atom.d.ts +3 -0
- package/dist/simplify/atom.js +28 -0
- package/dist/simplify/index.d.ts +2 -0
- package/dist/simplify/index.js +2 -0
- package/dist/simplify/lookup.d.ts +5 -0
- package/dist/simplify/lookup.js +77 -0
- package/dist/simplify/simplify.d.ts +1 -0
- package/dist/simplify/simplify.js +24 -0
- package/dist/simplify/state.d.ts +10 -0
- package/dist/simplify/state.js +57 -0
- package/dist/simplify/utils.d.ts +2 -0
- package/dist/simplify/utils.js +31 -0
- package/dist/strip.d.ts +1 -1
- package/dist/strip.js +2 -2
- package/package.json +1 -1
- package/src/ansi.js +2 -0
- package/src/sgr/color.js +130 -0
- package/src/sgr/index.js +5 -0
- package/src/sgr/reset.js +3 -0
- package/src/sgr/sgr.js +66 -0
- package/src/sgr/style.js +19 -0
- package/src/sgr/utils.js +20 -0
- package/src/simplify/atom.js +34 -0
- package/src/simplify/index.js +1 -0
- package/src/simplify/lookup.js +86 -0
- package/src/simplify/simplify.js +33 -0
- package/src/simplify/state.js +73 -0
- package/src/simplify/types.d.ts +28 -0
- package/src/simplify/utils.js +41 -0
- package/src/strip.js +2 -2
- package/dist/sgr.d.ts +0 -205
- package/dist/sgr.js +0 -89
- package/src/sgr.js +0 -113
|
@@ -0,0 +1,16 @@
|
|
|
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 };
|
|
@@ -0,0 +1,3 @@
|
|
|
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;
|
|
@@ -0,0 +1,28 @@
|
|
|
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 };
|
|
@@ -0,0 +1,5 @@
|
|
|
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;
|
|
@@ -0,0 +1,77 @@
|
|
|
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 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function simplify(text: string): string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const { State } = require("./state");
|
|
3
|
+
const rxSGR = /\x1b\[([0-9;]+)m/;
|
|
4
|
+
/** @type {(text: string) => string} */
|
|
5
|
+
function simplify(text) {
|
|
6
|
+
const parts = text.split(rxSGR);
|
|
7
|
+
const state = new State();
|
|
8
|
+
let result = parts[0];
|
|
9
|
+
for (let i = 1; i < parts.length;) {
|
|
10
|
+
let codes = parts[i++].split(";").map(Number);
|
|
11
|
+
let chunk = parts[i++];
|
|
12
|
+
while (!chunk && i < parts.length) {
|
|
13
|
+
// Merge consecutive SGR sequences.
|
|
14
|
+
codes.push(...parts[i++].split(";").map(Number));
|
|
15
|
+
chunk = parts[i++];
|
|
16
|
+
}
|
|
17
|
+
codes = state.update(codes);
|
|
18
|
+
if (codes.length)
|
|
19
|
+
result += `\x1b[${codes.join(";")}m`;
|
|
20
|
+
result += chunk;
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
module.exports = { simplify };
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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 };
|
|
@@ -0,0 +1,31 @@
|
|
|
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/strip.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export const strip: (text: string) => string;
|
|
6
6
|
/**
|
|
7
|
-
* Remove all ANSI escape codes except SGR (m)
|
|
7
|
+
* Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
|
|
8
8
|
* @type {(text: string) => string}
|
|
9
9
|
*/
|
|
10
10
|
export const sanitize: (text: string) => string;
|
package/dist/strip.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const { reset } = require("./sgr");
|
|
3
3
|
const rxCSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
|
|
4
|
-
const rxUnsafe = /\x1b\[[0-?]*[ -/]*[@-ln-~]/g; // Everything except SGR (m)
|
|
4
|
+
const rxUnsafe = /\x1b\[[0-?]*[ -/]*[@-ln-~]/g; // Everything except SGR codes (m).
|
|
5
5
|
/**
|
|
6
6
|
* Remove all ANSI escape codes from the given text.
|
|
7
7
|
* @type {(text: string) => string}
|
|
8
8
|
*/
|
|
9
9
|
const strip = text => text.replace(rxCSI, '');
|
|
10
10
|
/**
|
|
11
|
-
* Remove all ANSI escape codes except SGR (m)
|
|
11
|
+
* Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
|
|
12
12
|
* @type {(text: string) => string}
|
|
13
13
|
*/
|
|
14
14
|
const sanitize = text => text.replace(rxUnsafe, '');
|
package/package.json
CHANGED
package/src/ansi.js
CHANGED
|
@@ -4,6 +4,7 @@ const caret = require("./caret");
|
|
|
4
4
|
const erase = require("./erase");
|
|
5
5
|
const scroll = require("./scroll");
|
|
6
6
|
const {bg, fg, reset, style} = require("./sgr");
|
|
7
|
+
const {simplify} = require("./simplify");
|
|
7
8
|
const {sanitize, slice, strip} = require("./strip");
|
|
8
9
|
|
|
9
10
|
const ansi = {
|
|
@@ -19,6 +20,7 @@ const ansi = {
|
|
|
19
20
|
strip,
|
|
20
21
|
sanitize,
|
|
21
22
|
slice,
|
|
23
|
+
simplify,
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
module.exports = ansi;
|
package/src/sgr/color.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {sgr} = require("./sgr");
|
|
4
|
+
|
|
5
|
+
/** @type {(r: number, g: number, b: number, open: number, close: number) => (text: string) => string} */
|
|
6
|
+
const rgb = (r, g, b, open, close) => sgr([open, 2, r, g, b], close);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @overload
|
|
10
|
+
* @param {number} code
|
|
11
|
+
* @param {number} open
|
|
12
|
+
* @param {number} close
|
|
13
|
+
* @returns {(text: string) => string}
|
|
14
|
+
*//**
|
|
15
|
+
* @overload
|
|
16
|
+
* @param {number} r
|
|
17
|
+
* @param {number} g
|
|
18
|
+
* @param {number} b
|
|
19
|
+
* @param {number} open
|
|
20
|
+
* @param {number} close
|
|
21
|
+
* @returns {(text: string) => string}
|
|
22
|
+
*//**
|
|
23
|
+
@type {
|
|
24
|
+
(
|
|
25
|
+
...args: [number, number, number] | [number, number, number, number, number]
|
|
26
|
+
) => (text: string) => string}
|
|
27
|
+
*/
|
|
28
|
+
function x256(...args) {
|
|
29
|
+
if (args.length === 3)
|
|
30
|
+
return sgr([args[1], 5, args[0]], args[2]);
|
|
31
|
+
|
|
32
|
+
const [r, g, b, open, close] = args;
|
|
33
|
+
if (r === g && g === b) {
|
|
34
|
+
// grayscale
|
|
35
|
+
return sgr([open, 5, 232 + Math.floor(r * 24 / 256)], close);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 6x6x6 color cube
|
|
39
|
+
return sgr(
|
|
40
|
+
[
|
|
41
|
+
open,
|
|
42
|
+
5,
|
|
43
|
+
16 +
|
|
44
|
+
Math.floor(r * 6 / 256) * 36 +
|
|
45
|
+
Math.floor(g * 6 / 256) * 6 +
|
|
46
|
+
Math.floor(b * 6 / 256)
|
|
47
|
+
],
|
|
48
|
+
close,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const fg = {
|
|
53
|
+
black : sgr(30, 39),
|
|
54
|
+
red : sgr(31, 39),
|
|
55
|
+
green : sgr(32, 39),
|
|
56
|
+
yellow : sgr(33, 39),
|
|
57
|
+
blue : sgr(34, 39),
|
|
58
|
+
magenta : sgr(35, 39),
|
|
59
|
+
cyan : sgr(36, 39),
|
|
60
|
+
white : sgr(37, 39),
|
|
61
|
+
|
|
62
|
+
brightBlack : sgr(90, 39),
|
|
63
|
+
brightRed : sgr(91, 39),
|
|
64
|
+
brightGreen : sgr(92, 39),
|
|
65
|
+
brightYellow : sgr(93, 39),
|
|
66
|
+
brightBlue : sgr(94, 39),
|
|
67
|
+
brightMagenta : sgr(95, 39),
|
|
68
|
+
brightCyan : sgr(96, 39),
|
|
69
|
+
brightWhite : sgr(97, 39),
|
|
70
|
+
|
|
71
|
+
default : "\x1b[39m",
|
|
72
|
+
|
|
73
|
+
/** @type {(r: number, g: number, b: number) => (text: string) => string} */
|
|
74
|
+
rgb : (r, g, b) => rgb(r, g, b, 38, 39),
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @overload
|
|
78
|
+
* @param {number} code
|
|
79
|
+
* @returns {(text: string) => string}
|
|
80
|
+
*//**
|
|
81
|
+
* @param {number} r
|
|
82
|
+
* @param {number} g
|
|
83
|
+
* @param {number} b
|
|
84
|
+
* @returns {(text: string) => string}
|
|
85
|
+
*//** @type {(arg0: number, arg1?: number, arg2?: number) => (text: string) => string} */
|
|
86
|
+
x256 : (arg0, arg1, arg2) => arg1 === undefined || arg2 === undefined
|
|
87
|
+
? x256(arg0 , 38, 39)
|
|
88
|
+
: x256(arg0, arg1, arg2, 38, 39),
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const bg = {
|
|
92
|
+
black : sgr(40, 49),
|
|
93
|
+
red : sgr(41, 49),
|
|
94
|
+
green : sgr(42, 49),
|
|
95
|
+
yellow : sgr(43, 49),
|
|
96
|
+
blue : sgr(44, 49),
|
|
97
|
+
magenta : sgr(45, 49),
|
|
98
|
+
cyan : sgr(46, 49),
|
|
99
|
+
white : sgr(47, 49),
|
|
100
|
+
|
|
101
|
+
brightBlack : sgr(100, 49),
|
|
102
|
+
brightRed : sgr(101, 49),
|
|
103
|
+
brightGreen : sgr(102, 49),
|
|
104
|
+
brightYellow : sgr(103, 49),
|
|
105
|
+
brightBlue : sgr(104, 49),
|
|
106
|
+
brightMagenta : sgr(105, 49),
|
|
107
|
+
brightCyan : sgr(106, 49),
|
|
108
|
+
brightWhite : sgr(107, 49),
|
|
109
|
+
|
|
110
|
+
default : "\x1b[49m",
|
|
111
|
+
|
|
112
|
+
/** @type {(r: number, g: number, b: number) => (text: string) => string} */
|
|
113
|
+
rgb : (r, g, b) => rgb(r, g, b, 48, 49),
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @overload
|
|
117
|
+
* @param {number} code
|
|
118
|
+
* @returns {(text: string) => string}
|
|
119
|
+
*//**
|
|
120
|
+
* @param {number} r
|
|
121
|
+
* @param {number} g
|
|
122
|
+
* @param {number} b
|
|
123
|
+
* @returns {(text: string) => string}
|
|
124
|
+
*//** @type {(arg0: number, arg1?: number, arg2?: number) => (text: string) => string} */
|
|
125
|
+
x256 : (arg0, arg1, arg2) => arg1 === undefined || arg2 === undefined
|
|
126
|
+
? x256(arg0 , 48, 49)
|
|
127
|
+
: x256(arg0, arg1, arg2, 48, 49),
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
module.exports = {fg, bg};
|
package/src/sgr/index.js
ADDED
package/src/sgr/reset.js
ADDED
package/src/sgr/sgr.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const $codes = Symbol();
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
@typedef {number | number[]} Code
|
|
7
|
+
@typedef {{
|
|
8
|
+
(text: string): string;
|
|
9
|
+
open : string;
|
|
10
|
+
close : string;
|
|
11
|
+
[$codes] : {open: Code, close: Code};
|
|
12
|
+
combine : (...styles: SGR[]) => SGR;
|
|
13
|
+
}} SGR
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** @type {(code: Code, codes: Code[]) => Code} */
|
|
17
|
+
function combine(code, codes, ) {
|
|
18
|
+
if (!codes.length)
|
|
19
|
+
return code;
|
|
20
|
+
|
|
21
|
+
const rtn = Array.isArray(code) ? [...code] : [code];
|
|
22
|
+
|
|
23
|
+
for (const c of codes) {
|
|
24
|
+
if (Array.isArray(c))
|
|
25
|
+
rtn.push(...c);
|
|
26
|
+
else
|
|
27
|
+
rtn.push(c);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return rtn;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @type {(code: Code) => string | number} */
|
|
34
|
+
const codeSequence = code => Array.isArray(code) ? code.join(';') : code;
|
|
35
|
+
|
|
36
|
+
/** @type {(open: Code, close: Code, reset?: boolean) => SGR} */
|
|
37
|
+
function sgr(open, close, reset = false) {
|
|
38
|
+
const openCode = codeSequence(open);
|
|
39
|
+
const closeCode = codeSequence(close);
|
|
40
|
+
const openSequence = `\x1b[${openCode}m`;
|
|
41
|
+
const closeSequence = `\x1b[${closeCode}m`;
|
|
42
|
+
const reopenCode = reset ? `${closeCode};${openCode}` : openCode;
|
|
43
|
+
|
|
44
|
+
const rxClose = new RegExp(
|
|
45
|
+
`(?<start>\\x1b\\[(?:\\d+;)*)${closeCode}(?<end>(?:;\\d+)*m)`,
|
|
46
|
+
"g"
|
|
47
|
+
);
|
|
48
|
+
const replace = `$<start>${reopenCode}$<end>`;
|
|
49
|
+
|
|
50
|
+
/** @type {(text: string) => string} */
|
|
51
|
+
const func = s => openSequence + s.replace(rxClose, replace) + closeSequence
|
|
52
|
+
|
|
53
|
+
return Object.assign(func, {
|
|
54
|
+
[$codes] : {open, close},
|
|
55
|
+
open : openSequence,
|
|
56
|
+
close : closeSequence,
|
|
57
|
+
|
|
58
|
+
/** @type {(...styles: SGR[]) => SGR} */
|
|
59
|
+
combine: (...styles) => sgr(
|
|
60
|
+
combine(open, styles.map(s => s[$codes].open)),
|
|
61
|
+
combine(close, styles.map(s => s[$codes].close)),
|
|
62
|
+
),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = {sgr};
|
package/src/sgr/style.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {sgr} = require("./sgr");
|
|
4
|
+
|
|
5
|
+
const style = {
|
|
6
|
+
bold : sgr(1, 22, true),
|
|
7
|
+
dim : sgr(2, 22, true),
|
|
8
|
+
italic : sgr(3, 23),
|
|
9
|
+
underline : sgr(4, 24),
|
|
10
|
+
inverse : sgr(7, 27),
|
|
11
|
+
hidden : sgr(8, 28),
|
|
12
|
+
strikethrough : sgr(9, 29),
|
|
13
|
+
doubleUnderline : sgr(21, 24),
|
|
14
|
+
frame : sgr(51, 54),
|
|
15
|
+
encircle : sgr(52, 54),
|
|
16
|
+
overline : sgr(53, 55),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
module.exports = {style};
|
package/src/sgr/utils.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** @typedef {number | number[]} Code */
|
|
2
|
+
|
|
3
|
+
/** @type {(code: Code, codes: Code[]) => Code} */
|
|
4
|
+
function combine(code, codes, ) {
|
|
5
|
+
if (!codes.length)
|
|
6
|
+
return code;
|
|
7
|
+
|
|
8
|
+
const rtn = Array.isArray(code) ? [...code] : [code];
|
|
9
|
+
|
|
10
|
+
for (const c of codes) {
|
|
11
|
+
if (Array.isArray(c))
|
|
12
|
+
rtn.push(...c);
|
|
13
|
+
else
|
|
14
|
+
rtn.push(c);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return rtn;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = {combine};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** @typedef {import("./types").BuildAtom} BuildAtom */
|
|
4
|
+
|
|
5
|
+
/** @type {BuildAtom} */
|
|
6
|
+
function buildExtendedColorAtom(attribute, codes, index) {
|
|
7
|
+
switch (codes[index + 1]) {
|
|
8
|
+
case 2:
|
|
9
|
+
if (index + 5 >= codes.length)
|
|
10
|
+
return {attribute, code: codes.slice(index), skip: codes.length - index - 1};
|
|
11
|
+
return {attribute, code: codes.slice(index, index + 5), skip: 4};
|
|
12
|
+
case 5:
|
|
13
|
+
if (index + 3 >= codes.length)
|
|
14
|
+
return {attribute, code: codes.slice(index), skip: codes.length - index - 1};
|
|
15
|
+
return {attribute, code: codes.slice(index, index + 3), skip: 2};
|
|
16
|
+
default:
|
|
17
|
+
return {attribute, code: codes.slice(index, index + 2), skip: 1};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @type {BuildAtom} */
|
|
22
|
+
function buildIntensityAtom(attribute, codes, index, state) {
|
|
23
|
+
const code = codes[index];
|
|
24
|
+
if (code === 22)
|
|
25
|
+
return {attribute, code: [code]};
|
|
26
|
+
|
|
27
|
+
const current = state.get("intensity");
|
|
28
|
+
if (!current || current.length == 1 && current[0] === code || current[0] === 22)
|
|
29
|
+
return {attribute, code: [code]};
|
|
30
|
+
|
|
31
|
+
return {attribute, code: [1, 2]};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = {buildExtendedColorAtom, buildIntensityAtom};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./simplify");
|