@peteanderson/ansi 0.2.2 → 0.2.3

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 CHANGED
@@ -18,6 +18,7 @@ console.log(ansi.bg.blue.combine(ansi.fg.white)("highlighted"));
18
18
  console.log(ansi.style.bold("important"));
19
19
  console.log(ansi.fg.rgb(5, 2, 0)("custom color"));
20
20
  console.log(ansi.strip("styled text")); // remove all ANSI codes
21
+ console.log(ansi.visibleLength(styledText)); // get display width
21
22
  console.log(ansi.slice("styled text", 0, 5)); // slice by visible length
22
23
  console.log(ansi.simplify(styledText)); // combine adjacent SGR sequences
23
24
  ```
@@ -84,7 +85,7 @@ Removes all ANSI CSI sequences from a string.
84
85
  ### `slice(text, start, end)`
85
86
 
86
87
  Slices a string by visible characters, ignoring ANSI sequences. Works like `String.prototype.slice`
87
- but counts only visible characters.
88
+ but counts only visible characters. Properly maintains terminal state at slice boundaries.
88
89
 
89
90
  ### `sanitize(text)`
90
91
 
@@ -95,6 +96,10 @@ Removes "unsafe" CSI sequences (non-SGR), leaving only color and style codes.
95
96
  Simplifies ANSI SGR sequences by combining adjacent sequences and removing redundant codes. Useful
96
97
  for optimizing styled strings.
97
98
 
99
+ ### `visibleLength(text)`
100
+
101
+ Returns the visible length of a string with all ANSI sequences removed.
102
+
98
103
  ## License
99
104
 
100
105
  MIT
package/dist/ansi.d.ts CHANGED
@@ -6,7 +6,8 @@ import { style } from "./sgr/style";
6
6
  import { fg } from "./sgr/color";
7
7
  import { bg } from "./sgr/color";
8
8
  import { strip } from "./strip";
9
+ import { visibleLength } from "./strip";
9
10
  import { sanitize } from "./strip";
10
- import { slice } from "./strip";
11
+ import { slice } from "./slice";
11
12
  import { simplify } from "./simplify/simplify";
12
- export { caret, erase, scroll, reset, style, fg, bg, strip, sanitize, slice, simplify };
13
+ export { caret, erase, scroll, reset, style, fg, bg, strip, visibleLength, sanitize, slice, simplify };
package/dist/ansi.js CHANGED
@@ -4,7 +4,8 @@ const erase = require("./erase");
4
4
  const scroll = require("./scroll");
5
5
  const { bg, fg, reset, style } = require("./sgr");
6
6
  const { simplify } = require("./simplify");
7
- const { sanitize, slice, strip } = require("./strip");
7
+ const { slice } = require("./slice");
8
+ const { sanitize, strip, visibleLength } = require("./strip");
8
9
  const ansi = {
9
10
  caret,
10
11
  erase,
@@ -14,6 +15,7 @@ const ansi = {
14
15
  fg,
15
16
  bg,
16
17
  strip,
18
+ visibleLength,
17
19
  sanitize,
18
20
  slice,
19
21
  simplify,
package/dist/index.d.ts CHANGED
@@ -99,8 +99,9 @@ declare const _exports: {
99
99
  x256: (arg0: number, arg1?: number, arg2?: number) => (text: string) => string;
100
100
  };
101
101
  strip: (text: string) => string;
102
+ visibleLength: (text: string) => number;
102
103
  sanitize: (text: string) => string;
103
- slice: typeof import("./strip").slice;
104
+ slice: typeof import("./slice").slice;
104
105
  simplify: typeof import("./simplify/simplify").simplify;
105
106
  };
106
107
  export = _exports;
@@ -0,0 +1,3 @@
1
+ export const rxSGR: Readonly<RegExp>;
2
+ export const rxCSI: Readonly<RegExp>;
3
+ export const rxUnsafe: Readonly<RegExp>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ /*
3
+ I'm freezing these to prevent accidental mutation of the `lastIndex` property, which could keep
4
+ future calls from starting at the beginning of the string.
5
+
6
+ Code using these objects will have to clone them (with `new RegExp(rx)`) before using functions
7
+ that modify `lastIndex` (such as `exec`). But according to my benchmarks, cloning the regex takes
8
+ mere nanoseconds, and it wasn't even measurable when operating on longer strings. V8 clearly uses a
9
+ global cache of compiled regexes. I assume that SpiderMonkey does the same thing.
10
+
11
+ Here are my `timeit` benchmark results:
12
+
13
+ ```js
14
+ const {timeit} = require("$timeit");
15
+
16
+ const rxSGR = /\x1b\[[\d;]*m/g;
17
+ const short = "\x1b[31mred \x1b[32mgreen \x1b[34mblue \x1b[0m";
18
+ const long = short.repeat(1000);
19
+
20
+ timeit(() => short.replace(rxSGR, ""), {description: "short - no clone "});
21
+ timeit(() => short.replace(new RegExp(rxSGR), ""), {description: "short - clone object "});
22
+ timeit(() => short.replace(new RegExp(rxSGR.source, "g"), ""), {description: "short - clone source "});
23
+
24
+ console.log();
25
+
26
+ timeit(() => long .replace(rxSGR, ""), {description: "long - no clone "});
27
+ timeit(() => long .replace(new RegExp(rxSGR), ""), {description: "long - clone object "});
28
+ timeit(() => long .replace(new RegExp(rxSGR.source, "g"), ""), {description: "long - clone source "});
29
+
30
+ // Output:
31
+ // short - no clone : 82.73 ns (12,087,731 in 1.000016982 s)
32
+ // short - clone object : 142.132 ns (7,035,692 in 1.0000001770000002 s)
33
+ // short - clone source : 127.872 ns (7,820,293 in 1.00000017 s)
34
+ //
35
+ // long - no clone : 47.953 ųs (20,854 in 1.0000098149999999 s)
36
+ // long - clone object : 46.713 ųs (21,408 in 1.0000285520000003 s)
37
+ // long - clone source : 46.491 ųs (21,510 in 1.000026492 s)
38
+ ```
39
+ */
40
+ const rxSGR = Object.freeze(/\x1b\[[\d;]*m/g);
41
+ const rxCSI = Object.freeze(/\x1b\[[0-?]*[ -/]*[@-~]/g);
42
+ const rxUnsafe = Object.freeze(/\x1b\[[0-?]*[ -/]*[@-ln-~]/g); // Everything except SGR codes (m).
43
+ module.exports = { rxSGR, rxCSI, rxUnsafe };
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
+ const { rxSGR } = require("../patterns");
2
3
  const { State } = require("./state");
3
- const rxSGR = /\x1b\[([0-9;]+)m/;
4
4
  /** @type {(text: string) => string} */
5
5
  function simplify(text) {
6
+ // `.split` doesn't modify `lastIndex`, so no clone is necessary here.
6
7
  const parts = text.split(rxSGR);
7
8
  const state = new State();
8
9
  let result = parts[0];
@@ -0,0 +1 @@
1
+ export function slice(text: string, start: number, end: number): string;
package/dist/slice.js ADDED
@@ -0,0 +1,83 @@
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 CHANGED
@@ -3,9 +3,13 @@
3
3
  * @type {(text: string) => string}
4
4
  */
5
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;
6
11
  /**
7
12
  * Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
8
13
  * @type {(text: string) => string}
9
14
  */
10
15
  export const sanitize: (text: string) => string;
11
- export function slice(text: string, start: number, end: number): string;
package/dist/strip.js CHANGED
@@ -1,58 +1,18 @@
1
1
  "use strict";
2
- const { reset } = require("./sgr");
3
- const rxCSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
4
- const rxUnsafe = /\x1b\[[0-?]*[ -/]*[@-ln-~]/g; // Everything except SGR codes (m).
2
+ const { rxCSI, rxUnsafe } = require("./patterns");
5
3
  /**
6
4
  * Remove all ANSI escape codes from the given text.
7
5
  * @type {(text: string) => string}
8
6
  */
9
- const strip = text => text.replace(rxCSI, '');
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;
10
13
  /**
11
14
  * Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
12
15
  * @type {(text: string) => string}
13
16
  */
14
- const sanitize = text => text.replace(rxUnsafe, '');
15
- /** @type {(text: string, start: number, end: number) => string} */
16
- function slice(text, start, end) {
17
- if (start < 0 || end < 0) {
18
- const visibleLength = strip(text).length;
19
- if (start < 0)
20
- start = Math.max(0, visibleLength + start);
21
- if (end < 0)
22
- end = Math.max(0, visibleLength + end);
23
- }
24
- if (start > end)
25
- return "";
26
- let visibleIndex = 0;
27
- let startIndex = 0;
28
- while (startIndex < text.length && visibleIndex < start) {
29
- if (text[startIndex] === "\x1b") {
30
- rxCSI.lastIndex = startIndex;
31
- const match = rxCSI.exec(text);
32
- if (match?.index === startIndex) {
33
- startIndex += match[0].length;
34
- continue;
35
- }
36
- }
37
- startIndex++;
38
- visibleIndex++;
39
- }
40
- let endIndex = startIndex;
41
- let hasAnsi = false;
42
- while (endIndex < text.length && visibleIndex < end) {
43
- if (text[endIndex] === "\x1b") {
44
- rxCSI.lastIndex = endIndex;
45
- const match = rxCSI.exec(text);
46
- if (match?.index === endIndex) {
47
- endIndex += match[0].length;
48
- hasAnsi = true;
49
- continue;
50
- }
51
- }
52
- endIndex++;
53
- visibleIndex++;
54
- }
55
- return text.slice(startIndex, endIndex) + (hasAnsi ? reset : "");
56
- }
57
- ;
58
- module.exports = { strip, sanitize, slice };
17
+ const sanitize = text => text.replace(new RegExp(rxUnsafe), "");
18
+ module.exports = { strip, visibleLength, sanitize };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name" : "@peteanderson/ansi",
3
- "version" : "0.2.2",
3
+ "version" : "0.2.3",
4
4
  "description" : "ANSI escape sequence generation with automatic feature detection",
5
5
  "author" : {
6
6
  "name" : "Pete Anderson",
package/src/ansi.js CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
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 {sanitize, slice, strip} = require("./strip");
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");
9
10
 
10
11
  const ansi = {
11
12
  caret,
@@ -18,6 +19,7 @@ const ansi = {
18
19
  bg,
19
20
 
20
21
  strip,
22
+ visibleLength,
21
23
  sanitize,
22
24
  slice,
23
25
  simplify,
@@ -0,0 +1,44 @@
1
+ /*
2
+ I'm freezing these to prevent accidental mutation of the `lastIndex` property, which could keep
3
+ future calls from starting at the beginning of the string.
4
+
5
+ Code using these objects will have to clone them (with `new RegExp(rx)`) before using functions
6
+ that modify `lastIndex` (such as `exec`). But according to my benchmarks, cloning the regex takes
7
+ mere nanoseconds, and it wasn't even measurable when operating on longer strings. V8 clearly uses a
8
+ global cache of compiled regexes. I assume that SpiderMonkey does the same thing.
9
+
10
+ Here are my `timeit` benchmark results:
11
+
12
+ ```js
13
+ const {timeit} = require("$timeit");
14
+
15
+ const rxSGR = /\x1b\[[\d;]*m/g;
16
+ const short = "\x1b[31mred \x1b[32mgreen \x1b[34mblue \x1b[0m";
17
+ const long = short.repeat(1000);
18
+
19
+ timeit(() => short.replace(rxSGR, ""), {description: "short - no clone "});
20
+ timeit(() => short.replace(new RegExp(rxSGR), ""), {description: "short - clone object "});
21
+ timeit(() => short.replace(new RegExp(rxSGR.source, "g"), ""), {description: "short - clone source "});
22
+
23
+ console.log();
24
+
25
+ timeit(() => long .replace(rxSGR, ""), {description: "long - no clone "});
26
+ timeit(() => long .replace(new RegExp(rxSGR), ""), {description: "long - clone object "});
27
+ timeit(() => long .replace(new RegExp(rxSGR.source, "g"), ""), {description: "long - clone source "});
28
+
29
+ // Output:
30
+ // short - no clone : 82.73 ns (12,087,731 in 1.000016982 s)
31
+ // short - clone object : 142.132 ns (7,035,692 in 1.0000001770000002 s)
32
+ // short - clone source : 127.872 ns (7,820,293 in 1.00000017 s)
33
+ //
34
+ // long - no clone : 47.953 ųs (20,854 in 1.0000098149999999 s)
35
+ // long - clone object : 46.713 ųs (21,408 in 1.0000285520000003 s)
36
+ // long - clone source : 46.491 ųs (21,510 in 1.000026492 s)
37
+ ```
38
+ */
39
+
40
+ const rxSGR = Object.freeze(/\x1b\[[\d;]*m/g);
41
+ const rxCSI = Object.freeze(/\x1b\[[0-?]*[ -/]*[@-~]/g);
42
+ const rxUnsafe = Object.freeze(/\x1b\[[0-?]*[ -/]*[@-ln-~]/g); // Everything except SGR codes (m).
43
+
44
+ module.exports = {rxSGR, rxCSI, rxUnsafe};
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
 
3
+ const {rxSGR} = require("../patterns");
3
4
  const {State} = require("./state");
4
5
 
5
- const rxSGR = /\x1b\[([0-9;]+)m/;
6
-
7
6
  /** @type {(text: string) => string} */
8
7
  function simplify(text) {
8
+ // `.split` doesn't modify `lastIndex`, so no clone is necessary here.
9
9
  const parts = text.split(rxSGR);
10
10
 
11
11
  const state = new State();
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 CHANGED
@@ -1,69 +1,23 @@
1
1
  "use strict";
2
2
 
3
- const {reset} = require("./sgr");
4
-
5
- const rxCSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
6
- const rxUnsafe = /\x1b\[[0-?]*[ -/]*[@-ln-~]/g; // Everything except SGR codes (m).
3
+ const {rxCSI, rxUnsafe} = require("./patterns");
7
4
 
8
5
  /**
9
6
  * Remove all ANSI escape codes from the given text.
10
7
  * @type {(text: string) => string}
11
8
  */
12
- const strip = text => text.replace(rxCSI, '');
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;
13
16
 
14
17
  /**
15
18
  * Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
16
19
  * @type {(text: string) => string}
17
20
  */
18
- const sanitize = text => text.replace(rxUnsafe, '');
19
-
20
- /** @type {(text: string, start: number, end: number) => string} */
21
- function slice(text, start, end) {
22
- if (start < 0 || end < 0) {
23
- const visibleLength = strip(text).length;
24
- if (start < 0)
25
- start = Math.max(0, visibleLength + start);
26
- if (end < 0)
27
- end = Math.max(0, visibleLength + end);
28
- }
29
- if (start > end)
30
- return "";
31
-
32
- let visibleIndex = 0;
33
- let startIndex = 0;
34
-
35
- while (startIndex < text.length && visibleIndex < start) {
36
- if (text[startIndex] === "\x1b") {
37
- rxCSI.lastIndex = startIndex;
38
- const match = rxCSI.exec(text);
39
- if (match?.index === startIndex) {
40
- startIndex += match[0].length;
41
- continue;
42
- }
43
- }
44
-
45
- startIndex++;
46
- visibleIndex++;
47
- }
48
-
49
- let endIndex = startIndex;
50
- let hasAnsi = false;
51
- while (endIndex < text.length && visibleIndex < end) {
52
- if (text[endIndex] === "\x1b") {
53
- rxCSI.lastIndex = endIndex;
54
- const match = rxCSI.exec(text);
55
- if (match?.index === endIndex) {
56
- endIndex += match[0].length;
57
- hasAnsi = true;
58
- continue;
59
- }
60
- }
61
-
62
- endIndex++;
63
- visibleIndex++;
64
- }
65
-
66
- return text.slice(startIndex, endIndex) + (hasAnsi ? reset : "");
67
- };
21
+ const sanitize = text => text.replace(new RegExp(rxUnsafe), "");
68
22
 
69
- module.exports = {strip, sanitize, slice};
23
+ module.exports = {strip, visibleLength, sanitize};