@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.
Files changed (63) hide show
  1. package/README.md +30 -70
  2. package/package.json +20 -13
  3. package/dist/ansi.d.ts +0 -13
  4. package/dist/ansi.js +0 -23
  5. package/dist/caret.d.ts +0 -25
  6. package/dist/caret.js +0 -40
  7. package/dist/erase.d.ts +0 -14
  8. package/dist/erase.js +0 -15
  9. package/dist/index.d.ts +0 -107
  10. package/dist/index.js +0 -2
  11. package/dist/patterns.d.ts +0 -3
  12. package/dist/patterns.js +0 -43
  13. package/dist/scroll.d.ts +0 -2
  14. package/dist/scroll.js +0 -8
  15. package/dist/sgr/color.d.ts +0 -62
  16. package/dist/sgr/color.js +0 -111
  17. package/dist/sgr/index.d.ts +0 -59
  18. package/dist/sgr/index.js +0 -6
  19. package/dist/sgr/reset.d.ts +0 -1
  20. package/dist/sgr/reset.js +0 -3
  21. package/dist/sgr/sgr.d.ts +0 -14
  22. package/dist/sgr/sgr.js +0 -47
  23. package/dist/sgr/style.d.ts +0 -13
  24. package/dist/sgr/style.js +0 -16
  25. package/dist/sgr/utils.d.ts +0 -2
  26. package/dist/sgr/utils.js +0 -16
  27. package/dist/simplify/atom.d.ts +0 -3
  28. package/dist/simplify/atom.js +0 -28
  29. package/dist/simplify/index.d.ts +0 -2
  30. package/dist/simplify/index.js +0 -2
  31. package/dist/simplify/lookup.d.ts +0 -5
  32. package/dist/simplify/lookup.js +0 -77
  33. package/dist/simplify/simplify.d.ts +0 -1
  34. package/dist/simplify/simplify.js +0 -25
  35. package/dist/simplify/state.d.ts +0 -10
  36. package/dist/simplify/state.js +0 -57
  37. package/dist/simplify/utils.d.ts +0 -2
  38. package/dist/simplify/utils.js +0 -31
  39. package/dist/slice.d.ts +0 -1
  40. package/dist/slice.js +0 -83
  41. package/dist/strip.d.ts +0 -15
  42. package/dist/strip.js +0 -18
  43. package/src/caret.js +0 -49
  44. package/src/erase.js +0 -17
  45. package/src/index.js +0 -1
  46. package/src/patterns.js +0 -44
  47. package/src/scroll.js +0 -10
  48. package/src/sgr/color.js +0 -130
  49. package/src/sgr/index.js +0 -5
  50. package/src/sgr/reset.js +0 -3
  51. package/src/sgr/sgr.js +0 -66
  52. package/src/sgr/style.js +0 -19
  53. package/src/sgr/utils.js +0 -20
  54. package/src/simplify/atom.js +0 -34
  55. package/src/simplify/index.js +0 -1
  56. package/src/simplify/lookup.js +0 -86
  57. package/src/simplify/simplify.js +0 -33
  58. package/src/simplify/state.js +0 -73
  59. package/src/simplify/types.d.ts +0 -28
  60. package/src/simplify/utils.js +0 -41
  61. package/src/slice.js +0 -99
  62. package/src/strip.js +0 -23
  63. /package/{src/ansi.js → ansi.js} +0 -0
package/src/slice.js DELETED
@@ -1,99 +0,0 @@
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 DELETED
@@ -1,23 +0,0 @@
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};
File without changes