@peteanderson/ansi 0.3.3 → 1.0.1

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 (169) hide show
  1. package/dist/ansi.d.ts +28 -37
  2. package/dist/ansi.d.ts.map +1 -0
  3. package/dist/ansi.js +29 -52
  4. package/dist/ansi.js.map +1 -0
  5. package/dist/caret.d.ts +5 -3
  6. package/dist/caret.d.ts.map +1 -0
  7. package/dist/caret.js +6 -8
  8. package/dist/caret.js.map +1 -0
  9. package/dist/erase.d.ts +5 -1
  10. package/dist/erase.d.ts.map +1 -0
  11. package/dist/erase.js +4 -2
  12. package/dist/erase.js.map +1 -0
  13. package/dist/features/build.d.ts +7 -11
  14. package/dist/features/build.d.ts.map +1 -0
  15. package/dist/features/build.js +12 -25
  16. package/dist/features/build.js.map +1 -0
  17. package/dist/features/detect.d.ts +4 -4
  18. package/dist/features/detect.d.ts.map +1 -0
  19. package/dist/features/detect.js +5 -6
  20. package/dist/features/detect.js.map +1 -0
  21. package/dist/features/features.d.ts +4 -4
  22. package/dist/features/features.d.ts.map +1 -0
  23. package/dist/features/features.js +17 -28
  24. package/dist/features/features.js.map +1 -0
  25. package/dist/features/index.d.ts +2 -2
  26. package/dist/features/index.d.ts.map +1 -0
  27. package/dist/features/index.js +17 -1
  28. package/dist/features/index.js.map +1 -0
  29. package/dist/features/types.d.ts +10 -0
  30. package/dist/features/types.d.ts.map +1 -0
  31. package/dist/features/types.js +3 -0
  32. package/dist/features/types.js.map +1 -0
  33. package/dist/index.d.ts +3 -81
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +23 -1
  36. package/dist/index.js.map +1 -0
  37. package/dist/pad.d.ts +3 -4
  38. package/dist/pad.d.ts.map +1 -0
  39. package/dist/pad.js +8 -6
  40. package/dist/pad.js.map +1 -0
  41. package/dist/patterns.d.ts +4 -3
  42. package/dist/patterns.d.ts.map +1 -0
  43. package/dist/patterns.js +6 -4
  44. package/dist/patterns.js.map +1 -0
  45. package/dist/scroll.d.ts +5 -3
  46. package/dist/scroll.d.ts.map +1 -0
  47. package/dist/scroll.js +6 -6
  48. package/dist/scroll.js.map +1 -0
  49. package/dist/sgr/color/color.d.ts +5 -9
  50. package/dist/sgr/color/color.d.ts.map +1 -0
  51. package/dist/sgr/color/color.js +22 -29
  52. package/dist/sgr/color/color.js.map +1 -0
  53. package/dist/sgr/color/index.d.ts +2 -2
  54. package/dist/sgr/color/index.d.ts.map +1 -0
  55. package/dist/sgr/color/index.js +17 -1
  56. package/dist/sgr/color/index.js.map +1 -0
  57. package/dist/sgr/color/types.d.ts +30 -0
  58. package/dist/sgr/color/types.d.ts.map +1 -0
  59. package/dist/sgr/color/types.js +3 -0
  60. package/dist/sgr/color/types.js.map +1 -0
  61. package/dist/sgr/color/utils.d.ts +7 -27
  62. package/dist/sgr/color/utils.d.ts.map +1 -0
  63. package/dist/sgr/color/utils.js +16 -23
  64. package/dist/sgr/color/utils.js.map +1 -0
  65. package/dist/sgr/index.d.ts +4 -9
  66. package/dist/sgr/index.d.ts.map +1 -0
  67. package/dist/sgr/index.js +18 -4
  68. package/dist/sgr/index.js.map +1 -0
  69. package/dist/sgr/reset.d.ts +2 -1
  70. package/dist/sgr/reset.d.ts.map +1 -0
  71. package/dist/sgr/reset.js +4 -1
  72. package/dist/sgr/reset.js.map +1 -0
  73. package/dist/sgr/sgr.d.ts +4 -3
  74. package/dist/sgr/sgr.d.ts.map +1 -0
  75. package/dist/sgr/sgr.js +14 -27
  76. package/dist/sgr/sgr.js.map +1 -0
  77. package/dist/sgr/style.d.ts +3 -1
  78. package/dist/sgr/style.d.ts.map +1 -0
  79. package/dist/sgr/style.js +5 -3
  80. package/dist/sgr/style.js.map +1 -0
  81. package/dist/sgr/utils.d.ts +3 -2
  82. package/dist/sgr/utils.d.ts.map +1 -0
  83. package/dist/sgr/utils.js +3 -3
  84. package/dist/sgr/utils.js.map +1 -0
  85. package/dist/simplify/atom.d.ts +4 -3
  86. package/dist/simplify/atom.d.ts.map +1 -0
  87. package/dist/simplify/atom.js +9 -8
  88. package/dist/simplify/atom.js.map +1 -0
  89. package/dist/simplify/index.d.ts +2 -2
  90. package/dist/simplify/index.d.ts.map +1 -0
  91. package/dist/simplify/index.js +17 -1
  92. package/dist/simplify/index.js.map +1 -0
  93. package/dist/simplify/lookup.d.ts +3 -5
  94. package/dist/simplify/lookup.d.ts.map +1 -0
  95. package/dist/simplify/lookup.js +9 -13
  96. package/dist/simplify/lookup.js.map +1 -0
  97. package/dist/simplify/simplify.d.ts +2 -1
  98. package/dist/simplify/simplify.d.ts.map +1 -0
  99. package/dist/simplify/simplify.js +7 -7
  100. package/dist/simplify/simplify.js.map +1 -0
  101. package/dist/simplify/state.d.ts +3 -8
  102. package/dist/simplify/state.d.ts.map +1 -0
  103. package/dist/simplify/state.js +9 -13
  104. package/dist/simplify/state.js.map +1 -0
  105. package/dist/simplify/types.d.ts +9 -0
  106. package/dist/simplify/types.d.ts.map +1 -0
  107. package/dist/simplify/types.js +3 -0
  108. package/dist/simplify/types.js.map +1 -0
  109. package/dist/simplify/utils.d.ts +3 -2
  110. package/dist/simplify/utils.d.ts.map +1 -0
  111. package/dist/simplify/utils.js +4 -3
  112. package/dist/simplify/utils.js.map +1 -0
  113. package/dist/slice.d.ts +21 -1
  114. package/dist/slice.d.ts.map +1 -0
  115. package/dist/slice.js +10 -21
  116. package/dist/slice.js.map +1 -0
  117. package/dist/strip.d.ts +7 -15
  118. package/dist/strip.d.ts.map +1 -0
  119. package/dist/strip.js +13 -17
  120. package/dist/strip.js.map +1 -0
  121. package/dist/terminal.d.ts +5 -1
  122. package/dist/terminal.d.ts.map +1 -0
  123. package/dist/terminal.js +4 -2
  124. package/dist/terminal.js.map +1 -0
  125. package/package.json +8 -6
  126. package/src/ansi.ts +71 -0
  127. package/src/{caret.js → caret.ts} +9 -15
  128. package/src/{erase.js → erase.ts} +7 -10
  129. package/src/features/build.ts +53 -0
  130. package/src/features/detect.ts +36 -0
  131. package/src/features/{features.js → features.ts} +9 -18
  132. package/src/features/index.ts +1 -0
  133. package/src/index.ts +2 -0
  134. package/src/{pad.js → pad.ts} +4 -10
  135. package/src/{patterns.js → patterns.ts} +3 -5
  136. package/src/scroll.ts +13 -0
  137. package/src/sgr/color/{color.js → color.ts} +26 -35
  138. package/src/sgr/color/index.ts +1 -0
  139. package/src/sgr/color/{types.d.ts → types.ts} +6 -2
  140. package/src/sgr/color/{utils.js → utils.ts} +17 -30
  141. package/src/sgr/index.ts +3 -0
  142. package/src/sgr/reset.ts +1 -0
  143. package/src/sgr/sgr.ts +65 -0
  144. package/src/sgr/{style.js → style.ts} +3 -5
  145. package/src/sgr/{utils.js → utils.ts} +3 -6
  146. package/src/simplify/{atom.js → atom.ts} +5 -11
  147. package/src/simplify/index.ts +1 -0
  148. package/src/simplify/{lookup.js → lookup.ts} +5 -14
  149. package/src/simplify/{simplify.js → simplify.ts} +4 -10
  150. package/src/simplify/state.ts +63 -0
  151. package/src/simplify/{types.d.ts → types.ts} +5 -5
  152. package/src/simplify/{utils.js → utils.ts} +4 -10
  153. package/src/{slice.js → slice.ts} +12 -25
  154. package/src/strip.ts +10 -0
  155. package/src/{terminal.js → terminal.ts} +4 -7
  156. package/src/ansi.js +0 -70
  157. package/src/features/build.js +0 -68
  158. package/src/features/detect.js +0 -40
  159. package/src/features/index.js +0 -1
  160. package/src/index.js +0 -1
  161. package/src/scroll.js +0 -18
  162. package/src/sgr/color/index.js +0 -1
  163. package/src/sgr/index.js +0 -5
  164. package/src/sgr/reset.js +0 -3
  165. package/src/sgr/sgr.js +0 -77
  166. package/src/simplify/index.js +0 -1
  167. package/src/simplify/state.js +0 -73
  168. package/src/strip.js +0 -23
  169. /package/src/features/{types.d.ts → types.ts} +0 -0
@@ -1,7 +1,4 @@
1
- /** @typedef {import("../sgr").Code} Code */
2
-
3
- /** @type {(r: number, g: number, b: number) => number} */
4
- function rgbToX256(r, g, b) {
1
+ export function rgbToX256(r: number, g: number, b: number): number {
5
2
  if (r === g && g === b) {
6
3
  // grayscale
7
4
  return 232 + Math.floor(r * 24 / 256);
@@ -16,8 +13,7 @@ function rgbToX256(r, g, b) {
16
13
  );
17
14
  }
18
15
 
19
- /** @type {(code: number) => [number, number, number]} */
20
- function x256ToRgb(code) {
16
+ export function x256ToRgb(code: number): [number, number, number] {
21
17
  if (code >= 232) {
22
18
  // grayscale
23
19
  const gray = Math.round((code - 232) * 255 / 24);
@@ -31,10 +27,8 @@ function x256ToRgb(code) {
31
27
  return [r, g, b];
32
28
  }
33
29
 
34
- /** @type {(r: number, g: number, b: number) => number} */
35
- function rgbToX16(r, g, b) {
30
+ export function rgbToX16(r: number, g: number, b: number): number {
36
31
  // First calculate it as a 4x4x4 cube.
37
-
38
32
  r = Math.min(2, Math.floor(r / 85));
39
33
  g = Math.min(2, Math.floor(g / 85));
40
34
  b = Math.min(2, Math.floor(b / 85));
@@ -51,8 +45,7 @@ function rgbToX16(r, g, b) {
51
45
  return index + (bright ? 60 : 0);
52
46
  }
53
47
 
54
- /** @type {(r: number, g: number, b: number) => number} */
55
- function rgbToX8(r, g, b) {
48
+ export function rgbToX8(r: number, g: number, b: number): number {
56
49
  r = r >= 128 ? 1 : 0;
57
50
  g = g >= 128 ? 1 : 0;
58
51
  b = b >= 128 ? 1 : 0;
@@ -60,22 +53,16 @@ function rgbToX8(r, g, b) {
60
53
  return (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0);
61
54
  }
62
55
 
63
- /**
64
- * @overload
65
- * @param {number} x
66
- * @returns {number}
67
- * @overload
68
- * @param {number} r
69
- * @param {number} g
70
- * @param {number} b
71
- * @returns {[number, number, number]}
72
- *//** @param {[number] | [number, number, number]} args */
73
- const clip = (...args) => args.length === 1 ?
74
- Math.max(0, Math.min(255, Math.floor(args[0]))) :
75
- [
76
- Math.max(0, Math.min(255, Math.floor(args[0]))),
77
- Math.max(0, Math.min(255, Math.floor(args[1]))),
78
- Math.max(0, Math.min(255, Math.floor(args[2]))),
79
- ];
80
-
81
- module.exports = {rgbToX256, x256ToRgb, rgbToX16, rgbToX8, clip};
56
+ export function clip(x: number): number;
57
+ export function clip(r: number, g: number, b: number): [number, number, number];
58
+ export function clip(
59
+ ...args: [number] | [number, number, number]
60
+ ): number | [number, number, number] {
61
+ return args.length === 1 ?
62
+ Math.max(0, Math.min(255, Math.floor(args[0]))) :
63
+ [
64
+ Math.max(0, Math.min(255, Math.floor(args[0]))),
65
+ Math.max(0, Math.min(255, Math.floor(args[1]))),
66
+ Math.max(0, Math.min(255, Math.floor(args[2]))),
67
+ ];
68
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./reset";
2
+ export * from "./style";
3
+ export * from "./color";
@@ -0,0 +1 @@
1
+ export const makeReset = (enabled = true): string => enabled ? "\x1b[0m" : "";
package/src/sgr/sgr.ts ADDED
@@ -0,0 +1,65 @@
1
+ export type Code = number | number[];
2
+
3
+ export interface SGR {
4
+ (text: string): string;
5
+ open : string;
6
+ close : string;
7
+ codes : {open: Code; close: Code};
8
+ combine : (...styles: SGR[]) => SGR;
9
+ }
10
+
11
+ function combine(code: Code, codes: Code[]): Code {
12
+ if (!codes.length)
13
+ return code;
14
+
15
+ const set = new Set(Array.isArray(code) ? [...code] : [code]);
16
+
17
+ for (const c of codes) {
18
+ if (Array.isArray(c)) {
19
+ for (const code of c)
20
+ set.add(code);
21
+ } else
22
+ set.add(c);
23
+ }
24
+
25
+ return Array.from(set);
26
+ }
27
+
28
+ const codeSequence = (code: Code): string | number => Array.isArray(code) ? code.join(";") : code;
29
+
30
+ function sgr(open: Code, close: Code, reset = false): SGR {
31
+ const openCode = codeSequence(open);
32
+ const closeCode = codeSequence(close);
33
+ const openSequence = `\x1b[${openCode}m`;
34
+ const closeSequence = `\x1b[${closeCode}m`;
35
+ const reopenCode = reset ? `${closeCode};${openCode}` : openCode;
36
+
37
+ const rxClose = new RegExp(
38
+ `(?<start>\\x1b\\[(?:\\d+;)*)${closeCode}(?<end>(?:;\\d+)*m)`,
39
+ "g"
40
+ );
41
+ const replace = `$<start>${reopenCode}$<end>`;
42
+
43
+ const func = (s: string) => s ? openSequence + s.replace(rxClose, replace) + closeSequence : s;
44
+
45
+ return Object.assign(func, {
46
+ codes : {open, close},
47
+ open : openSequence,
48
+ close : closeSequence,
49
+ combine : (...styles: SGR[]) => sgr(
50
+ combine(open, styles.map(s => s.codes.open)),
51
+ combine(close, styles.map(s => s.codes.close)),
52
+ ),
53
+ });
54
+ }
55
+
56
+ const disabledSGR: SGR = Object.assign((s: string) => s, {
57
+ codes : {open: 0, close: 0},
58
+ open : "",
59
+ close : "",
60
+ combine : (): SGR => disabledSGR,
61
+ });
62
+
63
+ const disabled: typeof sgr = () => disabledSGR;
64
+
65
+ export const makeSGR = (enabled = true): typeof sgr => enabled ? sgr : disabled;
@@ -1,8 +1,6 @@
1
- "use strict";
1
+ import {makeSGR} from "./sgr";
2
2
 
3
- const {makeSGR} = require("./sgr");
4
-
5
- function makeStyle(enabled = true) {
3
+ export function makeStyle(enabled = true) {
6
4
  const sgr = makeSGR(enabled);
7
5
 
8
6
  return {
@@ -20,4 +18,4 @@ function makeStyle(enabled = true) {
20
18
  };
21
19
  }
22
20
 
23
- module.exports = {makeStyle};
21
+ export type Style = ReturnType<typeof makeStyle>;
@@ -1,7 +1,6 @@
1
- /** @typedef {number | number[]} Code */
1
+ import type {Code} from "./sgr";
2
2
 
3
- /** @type {(code: Code, codes: Code[]) => Code} */
4
- function combine(code, codes, ) {
3
+ export function combine(code: Code, codes: Code[]): Code {
5
4
  if (!codes.length)
6
5
  return code;
7
6
 
@@ -15,6 +14,4 @@ function combine(code, codes, ) {
15
14
  }
16
15
 
17
16
  return rtn;
18
- }
19
-
20
- module.exports = {combine};
17
+ }
@@ -1,9 +1,6 @@
1
- "use strict";
1
+ import type {BuildAtom} from "./types";
2
2
 
3
- /** @typedef {import("./types").BuildAtom} BuildAtom */
4
-
5
- /** @type {BuildAtom} */
6
- function buildExtendedColorAtom(attribute, codes, index) {
3
+ export const buildExtendedColorAtom: BuildAtom = (attribute, codes, index) => {
7
4
  switch (codes[index + 1]) {
8
5
  case 2:
9
6
  if (index + 5 >= codes.length)
@@ -16,10 +13,9 @@ function buildExtendedColorAtom(attribute, codes, index) {
16
13
  default:
17
14
  return {attribute, code: codes.slice(index, index + 2), skip: 1};
18
15
  }
19
- }
16
+ };
20
17
 
21
- /** @type {BuildAtom} */
22
- function buildIntensityAtom(attribute, codes, index, state) {
18
+ export const buildIntensityAtom: BuildAtom = (attribute, codes, index, state) => {
23
19
  const code = codes[index];
24
20
  if (code === 22)
25
21
  return {attribute, code: [code]};
@@ -39,6 +35,4 @@ function buildIntensityAtom(attribute, codes, index, state) {
39
35
 
40
36
  // Otherwise, we need to set both intensity codes.
41
37
  return {attribute, code: [1, 2]};
42
- }
43
-
44
- module.exports = {buildExtendedColorAtom, buildIntensityAtom};
38
+ };
@@ -0,0 +1 @@
1
+ export * from "./simplify";
@@ -1,14 +1,8 @@
1
- "use strict";
1
+ import {buildExtendedColorAtom, buildIntensityAtom} from "./atom";
2
2
 
3
- const {buildExtendedColorAtom, buildIntensityAtom} = require("./atom");
3
+ import type {Atom, Attribute, AttributeMap, BuildAtom} from "./types";
4
4
 
5
- /** @typedef {import("./types").Atom} Atom */
6
- /** @typedef {import("./types").Attribute} Attribute */
7
- /** @typedef {import("./types").AttributeMap} AttributeMap */
8
- /** @typedef {import("./types").BuildAtom} BuildAtom */
9
-
10
- /** @type {Record<number, [attribute: Attribute, buildAtom?: BuildAtom]>} */
11
- const codeMap = {
5
+ const codeMap: Record<number, [attribute: Attribute, buildAtom?: BuildAtom]> = {
12
6
  0 : ["reset"],
13
7
  1 : ["intensity", buildIntensityAtom],
14
8
  2 : ["intensity", buildIntensityAtom],
@@ -67,8 +61,7 @@ const codeMap = {
67
61
  107 : ["bg"],
68
62
  };
69
63
 
70
- /** @type {(codes: number[], index: number, state: AttributeMap) => Atom} */
71
- function lookUpAtom(codes, index, state) {
64
+ export function lookUpAtom(codes: number[], index: number, state: AttributeMap): Atom {
72
65
  const code = codes[index];
73
66
  const entry = codeMap[code];
74
67
 
@@ -81,6 +74,4 @@ function lookUpAtom(codes, index, state) {
81
74
  return buildAtom(attribute, codes, index, state);
82
75
 
83
76
  return {attribute, code: [code]};
84
- }
85
-
86
- module.exports = {lookUpAtom};
77
+ }
@@ -1,10 +1,7 @@
1
- "use strict";
1
+ import {rxSGR} from "../patterns";
2
+ import {State} from "./state";
2
3
 
3
- const {rxSGR} = require("../patterns");
4
- const {State} = require("./state");
5
-
6
- /** @type {(text: string) => string} */
7
- function simplify(text) {
4
+ export function simplify(text: string): string {
8
5
  // `.split` doesn't modify `lastIndex`, so no clone is necessary here.
9
6
  const parts = text.split(rxSGR);
10
7
 
@@ -16,7 +13,6 @@ function simplify(text) {
16
13
  let chunk = parts[i++];
17
14
 
18
15
  while (!chunk && i < parts.length) {
19
- // Merge consecutive SGR sequences.
20
16
  codes.push(...parts[i++].split(";").map(Number));
21
17
  chunk = parts[i++];
22
18
  }
@@ -28,6 +24,4 @@ function simplify(text) {
28
24
  }
29
25
 
30
26
  return result;
31
- }
32
-
33
- module.exports = {simplify};
27
+ }
@@ -0,0 +1,63 @@
1
+ import {lookUpAtom} from "./lookup";
2
+ import {arraysEqual, transitionIntensity} from "./utils";
3
+
4
+ import type {Attribute, AttributeMap} from "./types";
5
+
6
+ const defaultCodes: Map<Attribute, number[]> = new Map([
7
+ ["intensity", [22]],
8
+ ["italic", [23]],
9
+ ["underline", [24]],
10
+ ["inverse", [27]],
11
+ ["hidden", [28]],
12
+ ["strikethrough", [29]],
13
+ ["fg", [39]],
14
+ ["bg", [49]],
15
+ ["frame", [54]],
16
+ ["overline", [55]],
17
+ ]);
18
+
19
+ export class State extends Map<Attribute, number[]> implements AttributeMap {
20
+ update(codes: number[]): number[] {
21
+ let snapshot: AttributeMap = new Map(this);
22
+
23
+ const attributes = new Set<Attribute>();
24
+
25
+ for (let i = 0; i < codes.length; i++) {
26
+ const {attribute, code, skip} = lookUpAtom(codes, i, this);
27
+
28
+ if (attribute === "reset") {
29
+ attributes.clear();
30
+ this.clear();
31
+ attributes.add("reset");
32
+ for (const [attr, defaultCode] of defaultCodes)
33
+ this.set(attr, defaultCode);
34
+
35
+ snapshot = new Map(this);
36
+ continue;
37
+ }
38
+
39
+ attributes.add(attribute);
40
+ this.set(attribute, code);
41
+
42
+ if (skip)
43
+ i += skip;
44
+ }
45
+
46
+ return Array.from(attributes).flatMap(attribute => {
47
+ if (attribute === "reset")
48
+ return [0];
49
+
50
+ const code = this.get(attribute)!;
51
+
52
+ if (arraysEqual(code, snapshot.get(attribute)))
53
+ return [];
54
+
55
+ if (attribute === "intensity") {
56
+ const previous = snapshot.get(attribute);
57
+ return transitionIntensity(previous, code);
58
+ }
59
+
60
+ return code;
61
+ });
62
+ }
63
+ }
@@ -12,7 +12,7 @@ export type Attribute =
12
12
  | "reset"
13
13
  | number;
14
14
 
15
- type AttributeMap = Map<Attribute, number[]>;
15
+ export type AttributeMap = Map<Attribute, number[]>;
16
16
 
17
17
  export interface Atom {
18
18
  attribute : Attribute;
@@ -21,8 +21,8 @@ export interface Atom {
21
21
  }
22
22
 
23
23
  export type BuildAtom = (
24
- attribute: Attribute,
25
- codes : number[],
26
- index : number,
27
- state : AttributeMap,
24
+ attribute : Attribute,
25
+ codes : number[],
26
+ index : number,
27
+ state : AttributeMap,
28
28
  ) => Atom;
@@ -1,7 +1,4 @@
1
- "use strict";
2
-
3
- /** @type {<T>(a: T[] | undefined, b: T[] | undefined) => boolean} */
4
- function arraysEqual(a, b) {
1
+ export function arraysEqual<T>(a: T[] | undefined, b: T[] | undefined): boolean {
5
2
  if (a === b)
6
3
  return true;
7
4
 
@@ -16,8 +13,7 @@ function arraysEqual(a, b) {
16
13
  return true;
17
14
  }
18
15
 
19
- /** @type {(previous: number[] | undefined, current: number[]) => number[]} */
20
- function transitionIntensity(previous, current) {
16
+ export function transitionIntensity(previous: number[] | undefined, current: number[]): number[] {
21
17
  if (!previous || previous[0] === 22 || current[0] === 22)
22
18
  return current;
23
19
 
@@ -26,7 +22,7 @@ function transitionIntensity(previous, current) {
26
22
  const oldDim = previous[previous.length - 1] === 2;
27
23
  const newDim = current[current.length - 1] === 2;
28
24
 
29
- const result = [];
25
+ const result: number[] = [];
30
26
 
31
27
  if (oldBold && !newBold || oldDim && !newDim)
32
28
  result.push(22);
@@ -36,6 +32,4 @@ function transitionIntensity(previous, current) {
36
32
  result.push(2);
37
33
 
38
34
  return result;
39
- }
40
-
41
- module.exports = {arraysEqual, transitionIntensity};
35
+ }
@@ -1,19 +1,12 @@
1
- "use strict";
1
+ import {rxCSI, rxSGR} from "./patterns";
2
+ import {simplify} from "./simplify";
3
+ import {visibleLength} from "./strip";
2
4
 
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) {
5
+ function scanCSI(
6
+ text : string,
7
+ fromStringIndex : number,
8
+ visibleCount : number,
9
+ ): {index: number; length: number} {
17
10
  // We intentionally mutate `lastIndex` here, so we need a clone.
18
11
  const rx = new RegExp(rxCSI);
19
12
 
@@ -37,8 +30,7 @@ function scanCSI(text, fromStringIndex, visibleCount) {
37
30
  return {index, length};
38
31
  }
39
32
 
40
- /** @type {(text: string, start: number, end?: number) => string} */
41
- function skippedSequences(text, start, end) {
33
+ function skippedSequences(text: string, start: number, end?: number): string {
42
34
  const sequences = text.slice(start, end).match(new RegExp(rxSGR));
43
35
  return sequences ? simplify(sequences.join("")) : "";
44
36
  }
@@ -61,12 +53,9 @@ function skippedSequences(text, start, end) {
61
53
  * length of the slice, and that the terminal state may not be exactly the same at the start and end
62
54
  * of the slice as it would be if the whole string were printed. This function is intended mostly
63
55
  * 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
56
  */
67
- function slice(text, start, end) {
68
- /** @type {number | undefined} */
69
- let textVisibleLength;
57
+ export function slice(text: string, start: number, end: number): string {
58
+ let textVisibleLength: number | undefined;
70
59
  const getVisibleLength = () => textVisibleLength ??= visibleLength(text);
71
60
 
72
61
  if (start < 0 || end < 0) {
@@ -94,6 +83,4 @@ function slice(text, start, end) {
94
83
  const trailingSequences = skippedSequences(text, endIndex);
95
84
 
96
85
  return leadingSequences + text.slice(startIndex, endIndex) + trailingSequences;
97
- }
98
-
99
- module.exports = {slice};
86
+ }
package/src/strip.ts ADDED
@@ -0,0 +1,10 @@
1
+ import {rxCSI, rxUnsafe} from "./patterns";
2
+
3
+ /** Remove all ANSI escape codes from the given text. */
4
+ export const strip = (text: string): string => text.replace(new RegExp(rxCSI), "");
5
+
6
+ /** Calculate the visible length of a string with ANSI escape codes stripped. */
7
+ export const visibleLength = (text: string): number => strip(text).length;
8
+
9
+ /** Remove all ANSI escape codes except SGR codes (m), which are used for color and styling. */
10
+ export const sanitize = (text: string): string => text.replace(new RegExp(rxUnsafe), "");
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  const terminal = {
4
2
  focusReporting: {
5
3
  enable : "\x1b[?1004h",
@@ -26,8 +24,9 @@ const terminal = {
26
24
  },
27
25
  };
28
26
 
29
- /** @type {typeof terminal} */
30
- const disabled = {
27
+ export type Terminal = typeof terminal;
28
+
29
+ const disabled: Terminal = {
31
30
  focusReporting: {
32
31
  enable : "",
33
32
  disable : "",
@@ -53,6 +52,4 @@ const disabled = {
53
52
  },
54
53
  };
55
54
 
56
- const makeTerminal = (enabled = true) => enabled ? terminal : disabled;
57
-
58
- module.exports = {makeTerminal};
55
+ export const makeTerminal = (enabled = true): Terminal => enabled ? terminal : disabled;
package/src/ansi.js DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
-
3
- const {makeCaret} = require("./caret");
4
- const {makeErase} = require("./erase");
5
- const {getFeatures} = require("./features");
6
- const {padEnd, padStart} = require("./pad");
7
- const {makeScroll} = require("./scroll");
8
- const {makeColor, makeReset, makeStyle} = require("./sgr");
9
- const {simplify} = require("./simplify");
10
- const {slice} = require("./slice");
11
- const {sanitize, strip, visibleLength} = require("./strip");
12
- const {makeTerminal} = require("./terminal");
13
-
14
- /**
15
- @typedef {import("./features/features").Args} Args
16
-
17
- @typedef {ReturnType<typeof makeColor> & {
18
- caret : ReturnType<typeof makeCaret>;
19
- erase : ReturnType<typeof makeErase>;
20
- scroll : ReturnType<typeof makeScroll>;
21
-
22
- reset : string;
23
- style : ReturnType<typeof makeStyle>;
24
-
25
- terminal : ReturnType<typeof makeTerminal>;
26
-
27
- padEnd : typeof padEnd;
28
- padStart : typeof padStart;
29
-
30
- strip : typeof strip;
31
- visibleLength : typeof visibleLength;
32
- sanitize : typeof sanitize;
33
- slice : typeof slice;
34
- simplify : typeof simplify;
35
-
36
- features : ReturnType<typeof getFeatures>;
37
- }} Ansi
38
- */
39
-
40
- /** @type {(...args: Args) => Ansi}*/
41
- function makeAnsi(...args) {
42
- const features = getFeatures(...args);
43
-
44
- return {
45
- caret : makeCaret(features.caret),
46
- erase : makeErase(features.erase),
47
- scroll : makeScroll(features.scroll),
48
-
49
- reset : makeReset(features.colorDepth > 1),
50
- style : makeStyle(features.style),
51
- ...makeColor(features.colorDepth),
52
-
53
- terminal: makeTerminal(features.terminal),
54
-
55
- padEnd,
56
- padStart,
57
-
58
- strip,
59
- visibleLength,
60
- sanitize,
61
- slice,
62
- simplify,
63
-
64
- features,
65
- };
66
- }
67
-
68
- const ansi = Object.assign(makeAnsi, makeAnsi());
69
-
70
- module.exports = ansi;
@@ -1,68 +0,0 @@
1
- "use strict";
2
-
3
- const {getColorDepth, detectFeatureSupport} = require("./detect");
4
-
5
- /** @typedef {import("./types").ColorDepth} ColorDepth */
6
- /** @typedef {import("./types").Features} Features */
7
-
8
- /** @type {(colorDepth: number) => ColorDepth} */
9
- function normalizeColorDepth(colorDepth) {
10
- colorDepth = Math.floor(colorDepth);
11
- if (colorDepth >= 24)
12
- return 24;
13
- if (colorDepth >= 8)
14
- return 8;
15
- if (colorDepth >= 4)
16
- return 4;
17
- if (colorDepth === 3)
18
- return 3;
19
- return 1;
20
- }
21
-
22
- /**
23
- @type {
24
- (
25
- isTTY : boolean,
26
- colorDepth : ColorDepth,
27
- style? : boolean,
28
- caret? : boolean,
29
- erase? : boolean,
30
- scroll? : boolean,
31
- terminal? : boolean,
32
- ) => Features
33
- }
34
- */
35
- function build(isTTY, colorDepth, style, caret, erase, scroll, terminal) {
36
- if (
37
- style !== undefined &&
38
- caret !== undefined &&
39
- erase !== undefined &&
40
- scroll !== undefined &&
41
- terminal !== undefined
42
- )
43
- return {colorDepth: normalizeColorDepth(colorDepth), style, caret, erase, scroll, terminal};
44
-
45
- const support = detectFeatureSupport(isTTY, colorDepth);
46
- return {
47
- colorDepth : normalizeColorDepth(colorDepth),
48
- style : style ?? support.style,
49
- caret : caret ?? support.caret,
50
- erase : erase ?? support.erase,
51
- scroll : scroll ?? support.scroll,
52
- terminal : terminal ?? support.terminal,
53
- };
54
- }
55
-
56
- /** @type {(isTTY: boolean, enabled: boolean) => Features} */
57
- const boolean = (isTTY, enabled) => build(isTTY, enabled ? 4 : 1);
58
-
59
- /** @type {(isTTY: boolean, colorDepth: ColorDepth) => Features} */
60
- const number = (isTTY, colorDepth) => build(isTTY, colorDepth);
61
-
62
- /** @type {(stream: NodeJS.WriteStream) => Features} */
63
- const tty = stream => build(true, getColorDepth(stream));
64
-
65
- /** @type {() => Features} */
66
- const pipe = () => build(false, getColorDepth());
67
-
68
- module.exports = {build, boolean, number, tty, pipe};