@peteanderson/ansi 0.2.4 → 0.3.0
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 +119 -28
- package/dist/ansi.d.ts +46 -0
- package/dist/ansi.js +49 -0
- package/dist/caret.d.ts +28 -0
- package/dist/caret.js +70 -0
- package/dist/erase.d.ts +13 -0
- package/dist/erase.js +30 -0
- package/dist/features/build.d.ts +11 -0
- package/dist/features/build.js +50 -0
- package/dist/features/detect.d.ts +4 -0
- package/dist/features/detect.js +33 -0
- package/dist/features/features.d.ts +5 -0
- package/dist/features/features.js +39 -0
- package/dist/features/index.d.ts +2 -0
- package/dist/features/index.js +2 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +2 -0
- package/dist/patterns.d.ts +3 -0
- package/dist/patterns.js +43 -0
- package/dist/scroll.d.ts +6 -0
- package/dist/scroll.js +14 -0
- package/dist/sgr/color/color.d.ts +9 -0
- package/dist/sgr/color/color.js +76 -0
- package/dist/sgr/color/index.d.ts +2 -0
- package/dist/sgr/color/index.js +2 -0
- package/dist/sgr/color/utils.d.ts +27 -0
- package/dist/sgr/color/utils.js +67 -0
- package/dist/sgr/index.d.ts +9 -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 +57 -0
- package/dist/sgr/style.d.ts +13 -0
- package/dist/sgr/style.js +19 -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 +25 -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/slice.d.ts +1 -0
- package/dist/slice.js +83 -0
- package/dist/strip.d.ts +15 -0
- package/dist/strip.js +18 -0
- package/package.json +13 -20
- package/src/ansi.js +58 -0
- package/src/caret.js +86 -0
- package/src/erase.js +34 -0
- package/src/features/build.js +60 -0
- package/src/features/detect.js +39 -0
- package/src/features/features.js +53 -0
- package/src/features/index.js +1 -0
- package/src/features/types.d.ts +9 -0
- package/src/index.js +1 -0
- package/src/patterns.js +44 -0
- package/src/scroll.js +18 -0
- package/src/sgr/color/color.js +104 -0
- package/src/sgr/color/index.js +1 -0
- package/src/sgr/color/types.d.ts +28 -0
- package/src/sgr/color/utils.js +81 -0
- package/src/sgr/index.js +5 -0
- package/src/sgr/reset.js +3 -0
- package/src/sgr/sgr.js +77 -0
- package/src/sgr/style.js +23 -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/slice.js +99 -0
- package/src/strip.js +23 -0
- package/ansi.js +0 -28
package/src/slice.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {rxCSI, rxSGR} = require("./patterns");
|
|
4
|
+
const {simplify} = require("./simplify");
|
|
5
|
+
const {visibleLength} = require("./strip");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
@type {
|
|
9
|
+
(
|
|
10
|
+
text : string,
|
|
11
|
+
fromStringIndex : number,
|
|
12
|
+
visibleCount : number,
|
|
13
|
+
) => {index: number, length: number}
|
|
14
|
+
}
|
|
15
|
+
*/
|
|
16
|
+
function scanCSI(text, fromStringIndex, visibleCount) {
|
|
17
|
+
// We intentionally mutate `lastIndex` here, so we need a clone.
|
|
18
|
+
const rx = new RegExp(rxCSI);
|
|
19
|
+
|
|
20
|
+
let length = 0;
|
|
21
|
+
let index = fromStringIndex;
|
|
22
|
+
|
|
23
|
+
while (index < text.length && length < visibleCount) {
|
|
24
|
+
if (text[index] === "\x1b") {
|
|
25
|
+
rx.lastIndex = index;
|
|
26
|
+
const match = rx.exec(text);
|
|
27
|
+
if (match?.index === index) {
|
|
28
|
+
index += match[0].length;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
index++;
|
|
34
|
+
length++;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {index, length};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @type {(text: string, start: number, end?: number) => string} */
|
|
41
|
+
function skippedSequences(text, start, end) {
|
|
42
|
+
const sequences = text.slice(start, end).match(new RegExp(rxSGR));
|
|
43
|
+
return sequences ? simplify(sequences.join("")) : "";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Return a slice of the given text, counting only visible characters (i.e. ignoring ANSI escape
|
|
48
|
+
* codes). The returned slice will include any ANSI escape codes from within the slice. In addition,
|
|
49
|
+
* SGR codes (m) from before and after the slice will be combined, simplified, and included so that
|
|
50
|
+
* the terminal state before and after the slice will be the same as if the whole string were
|
|
51
|
+
* printed. If you compose two or more slices together, there may be redundant SGR sequences at the
|
|
52
|
+
* boundaries, but the result will be correct. You can use `simplify` to remove the redundancies.
|
|
53
|
+
*
|
|
54
|
+
* Note the following caveats:
|
|
55
|
+
*
|
|
56
|
+
* * Any non-SGR ANSI codes prior to or after the slice will be ignored.
|
|
57
|
+
* * Control characters (such as newlines and backspaces) both outside and inside the slice are
|
|
58
|
+
* counted as visible characters.
|
|
59
|
+
*
|
|
60
|
+
* These caveats mean that text may not appear on screen to be the same number of characters as the
|
|
61
|
+
* length of the slice, and that the terminal state may not be exactly the same at the start and end
|
|
62
|
+
* of the slice as it would be if the whole string were printed. This function is intended mostly
|
|
63
|
+
* for text that contains SGR codes, but no other ANSI codes or control characters.
|
|
64
|
+
*
|
|
65
|
+
* @type {(text: string, start: number, end: number) => string}
|
|
66
|
+
*/
|
|
67
|
+
function slice(text, start, end) {
|
|
68
|
+
/** @type {number | undefined} */
|
|
69
|
+
let textVisibleLength;
|
|
70
|
+
const getVisibleLength = () => textVisibleLength ??= visibleLength(text);
|
|
71
|
+
|
|
72
|
+
if (start < 0 || end < 0) {
|
|
73
|
+
textVisibleLength = getVisibleLength();
|
|
74
|
+
if (start < 0)
|
|
75
|
+
start = Math.max(0, textVisibleLength + start);
|
|
76
|
+
if (end < 0)
|
|
77
|
+
end = Math.max(0, textVisibleLength + end);
|
|
78
|
+
}
|
|
79
|
+
if (start > end)
|
|
80
|
+
return "";
|
|
81
|
+
|
|
82
|
+
if (start === 0 && (end >= text.length || end >= getVisibleLength()))
|
|
83
|
+
return text;
|
|
84
|
+
|
|
85
|
+
const {index: startIndex, length: visibleSkipped} = scanCSI(text, 0, start);
|
|
86
|
+
|
|
87
|
+
const endIndex =
|
|
88
|
+
startIndex < text.length && visibleSkipped < end
|
|
89
|
+
? scanCSI(text, startIndex, end - visibleSkipped).index
|
|
90
|
+
: text.length;
|
|
91
|
+
|
|
92
|
+
// Start and end the string in the same terminal state it would be in if the string weren't sliced.
|
|
93
|
+
const leadingSequences = skippedSequences(text, 0, startIndex);
|
|
94
|
+
const trailingSequences = skippedSequences(text, endIndex);
|
|
95
|
+
|
|
96
|
+
return leadingSequences + text.slice(startIndex, endIndex) + trailingSequences;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = {slice};
|
package/src/strip.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {rxCSI, rxUnsafe} = require("./patterns");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Remove all ANSI escape codes from the given text.
|
|
7
|
+
* @type {(text: string) => string}
|
|
8
|
+
*/
|
|
9
|
+
const strip = text => text.replace(new RegExp(rxCSI), "");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Calculate the visible length of a string with ANSI escape codes stripped.
|
|
13
|
+
* @type {(text: string) => number}
|
|
14
|
+
*/
|
|
15
|
+
const visibleLength = text => strip(text).length;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
|
|
19
|
+
* @type {(text: string) => string}
|
|
20
|
+
*/
|
|
21
|
+
const sanitize = text => text.replace(new RegExp(rxUnsafe), "");
|
|
22
|
+
|
|
23
|
+
module.exports = {strip, visibleLength, sanitize};
|
package/ansi.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const caret = require("./caret");
|
|
4
|
-
const erase = require("./erase");
|
|
5
|
-
const scroll = require("./scroll");
|
|
6
|
-
const {bg, fg, reset, style} = require("./sgr");
|
|
7
|
-
const {simplify} = require("./simplify");
|
|
8
|
-
const {slice} = require("./slice");
|
|
9
|
-
const {sanitize, strip, visibleLength} = require("./strip");
|
|
10
|
-
|
|
11
|
-
const ansi = {
|
|
12
|
-
caret,
|
|
13
|
-
erase,
|
|
14
|
-
scroll,
|
|
15
|
-
|
|
16
|
-
reset,
|
|
17
|
-
style,
|
|
18
|
-
fg,
|
|
19
|
-
bg,
|
|
20
|
-
|
|
21
|
-
strip,
|
|
22
|
-
visibleLength,
|
|
23
|
-
sanitize,
|
|
24
|
-
slice,
|
|
25
|
-
simplify,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
module.exports = ansi;
|