@peteanderson/ansi 0.3.2 → 1.0.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.
Files changed (182) 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 +24 -34
  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 -12
  38. package/dist/pad.d.ts.map +1 -0
  39. package/dist/pad.js +8 -14
  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/test-rgb.d.ts +3 -0
  58. package/dist/sgr/color/test-rgb.d.ts.map +1 -0
  59. package/dist/sgr/color/test-rgb.js +149 -0
  60. package/dist/sgr/color/test-rgb.js.map +1 -0
  61. package/dist/sgr/color/types.d.ts +30 -0
  62. package/dist/sgr/color/types.d.ts.map +1 -0
  63. package/dist/sgr/color/types.js +3 -0
  64. package/dist/sgr/color/types.js.map +1 -0
  65. package/dist/sgr/color/utils.d.ts +7 -27
  66. package/dist/sgr/color/utils.d.ts.map +1 -0
  67. package/dist/sgr/color/utils.js +17 -32
  68. package/dist/sgr/color/utils.js.map +1 -0
  69. package/dist/sgr/index.d.ts +4 -9
  70. package/dist/sgr/index.d.ts.map +1 -0
  71. package/dist/sgr/index.js +18 -4
  72. package/dist/sgr/index.js.map +1 -0
  73. package/dist/sgr/reset.d.ts +2 -1
  74. package/dist/sgr/reset.d.ts.map +1 -0
  75. package/dist/sgr/reset.js +4 -1
  76. package/dist/sgr/reset.js.map +1 -0
  77. package/dist/sgr/sgr.d.ts +4 -3
  78. package/dist/sgr/sgr.d.ts.map +1 -0
  79. package/dist/sgr/sgr.js +8 -22
  80. package/dist/sgr/sgr.js.map +1 -0
  81. package/dist/sgr/style.d.ts +3 -1
  82. package/dist/sgr/style.d.ts.map +1 -0
  83. package/dist/sgr/style.js +5 -3
  84. package/dist/sgr/style.js.map +1 -0
  85. package/dist/sgr/utils.d.ts +3 -2
  86. package/dist/sgr/utils.d.ts.map +1 -0
  87. package/dist/sgr/utils.js +3 -3
  88. package/dist/sgr/utils.js.map +1 -0
  89. package/dist/simplify/atom.d.ts +4 -3
  90. package/dist/simplify/atom.d.ts.map +1 -0
  91. package/dist/simplify/atom.js +12 -9
  92. package/dist/simplify/atom.js.map +1 -0
  93. package/dist/simplify/index.d.ts +2 -2
  94. package/dist/simplify/index.d.ts.map +1 -0
  95. package/dist/simplify/index.js +17 -1
  96. package/dist/simplify/index.js.map +1 -0
  97. package/dist/simplify/lookup.d.ts +3 -5
  98. package/dist/simplify/lookup.d.ts.map +1 -0
  99. package/dist/simplify/lookup.js +9 -13
  100. package/dist/simplify/lookup.js.map +1 -0
  101. package/dist/simplify/simplify.d.ts +2 -1
  102. package/dist/simplify/simplify.d.ts.map +1 -0
  103. package/dist/simplify/simplify.js +7 -7
  104. package/dist/simplify/simplify.js.map +1 -0
  105. package/dist/simplify/state.d.ts +3 -8
  106. package/dist/simplify/state.d.ts.map +1 -0
  107. package/dist/simplify/state.js +9 -13
  108. package/dist/simplify/state.js.map +1 -0
  109. package/dist/simplify/test.d.ts +2 -0
  110. package/dist/simplify/test.d.ts.map +1 -0
  111. package/dist/simplify/test.js +64 -0
  112. package/dist/simplify/test.js.map +1 -0
  113. package/dist/simplify/types.d.ts +9 -0
  114. package/dist/simplify/types.d.ts.map +1 -0
  115. package/dist/simplify/types.js +3 -0
  116. package/dist/simplify/types.js.map +1 -0
  117. package/dist/simplify/utils.d.ts +3 -2
  118. package/dist/simplify/utils.d.ts.map +1 -0
  119. package/dist/simplify/utils.js +4 -3
  120. package/dist/simplify/utils.js.map +1 -0
  121. package/dist/slice.d.ts +21 -1
  122. package/dist/slice.d.ts.map +1 -0
  123. package/dist/slice.js +10 -22
  124. package/dist/slice.js.map +1 -0
  125. package/dist/strip.d.ts +4 -15
  126. package/dist/strip.d.ts.map +1 -0
  127. package/dist/strip.js +10 -17
  128. package/dist/strip.js.map +1 -0
  129. package/dist/terminal.d.ts +5 -1
  130. package/dist/terminal.d.ts.map +1 -0
  131. package/dist/terminal.js +4 -2
  132. package/dist/terminal.js.map +1 -0
  133. package/package.json +9 -22
  134. package/src/ansi.ts +71 -0
  135. package/src/{caret.js → caret.ts} +9 -15
  136. package/src/{erase.js → erase.ts} +7 -10
  137. package/src/features/build.ts +52 -0
  138. package/src/features/detect.ts +33 -0
  139. package/src/features/features.ts +48 -0
  140. package/src/features/index.ts +1 -0
  141. package/src/features/{types.d.ts → types.ts} +1 -1
  142. package/src/index.ts +2 -0
  143. package/src/pad.ts +7 -0
  144. package/src/{patterns.js → patterns.ts} +3 -5
  145. package/src/scroll.ts +13 -0
  146. package/src/sgr/color/{color.js → color.ts} +26 -35
  147. package/src/sgr/color/index.ts +1 -0
  148. package/src/sgr/color/{types.d.ts → types.ts} +6 -2
  149. package/src/sgr/color/utils.ts +58 -0
  150. package/src/sgr/index.ts +3 -0
  151. package/src/sgr/reset.ts +1 -0
  152. package/src/sgr/sgr.ts +68 -0
  153. package/src/sgr/{style.js → style.ts} +3 -5
  154. package/src/sgr/{utils.js → utils.ts} +3 -6
  155. package/src/simplify/{atom.js → atom.ts} +11 -13
  156. package/src/simplify/index.ts +1 -0
  157. package/src/simplify/{lookup.js → lookup.ts} +5 -14
  158. package/src/simplify/{simplify.js → simplify.ts} +4 -10
  159. package/src/simplify/state.ts +63 -0
  160. package/src/simplify/{types.d.ts → types.ts} +5 -5
  161. package/src/simplify/{utils.js → utils.ts} +4 -10
  162. package/src/{slice.js → slice.ts} +12 -26
  163. package/src/strip.ts +5 -0
  164. package/src/{terminal.js → terminal.ts} +4 -7
  165. package/LICENSE +0 -7
  166. package/README.md +0 -174
  167. package/src/ansi.js +0 -70
  168. package/src/features/build.js +0 -68
  169. package/src/features/detect.js +0 -40
  170. package/src/features/features.js +0 -53
  171. package/src/features/index.js +0 -1
  172. package/src/index.js +0 -1
  173. package/src/pad.js +0 -21
  174. package/src/scroll.js +0 -18
  175. package/src/sgr/color/index.js +0 -1
  176. package/src/sgr/color/utils.js +0 -81
  177. package/src/sgr/index.js +0 -5
  178. package/src/sgr/reset.js +0 -3
  179. package/src/sgr/sgr.js +0 -77
  180. package/src/simplify/index.js +0 -1
  181. package/src/simplify/state.js +0 -73
  182. package/src/strip.js +0 -23
@@ -1,9 +1,8 @@
1
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[]>} */
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.State = void 0;
4
+ const lookup_1 = require("./lookup");
5
+ const utils_1 = require("./utils");
7
6
  const defaultCodes = new Map([
8
7
  ["intensity", [22]],
9
8
  ["italic", [23]],
@@ -16,16 +15,12 @@ const defaultCodes = new Map([
16
15
  ["frame", [54]],
17
16
  ["overline", [55]],
18
17
  ]);
19
- /** @type {AttributeMap} */
20
18
  class State extends Map {
21
- /** @type {(codes: number[]) => number[]} */
22
19
  update(codes) {
23
- /** @type {AttributeMap} */
24
20
  let snapshot = new Map(this);
25
- /** @type {Set<Attribute>} */
26
21
  const attributes = new Set();
27
22
  for (let i = 0; i < codes.length; i++) {
28
- const { attribute, code, skip } = lookUpAtom(codes, i, this);
23
+ const { attribute, code, skip } = (0, lookup_1.lookUpAtom)(codes, i, this);
29
24
  if (attribute === "reset") {
30
25
  attributes.clear();
31
26
  this.clear();
@@ -44,14 +39,15 @@ class State extends Map {
44
39
  if (attribute === "reset")
45
40
  return [0];
46
41
  const code = this.get(attribute);
47
- if (arraysEqual(code, snapshot.get(attribute)))
42
+ if ((0, utils_1.arraysEqual)(code, snapshot.get(attribute)))
48
43
  return [];
49
44
  if (attribute === "intensity") {
50
45
  const previous = snapshot.get(attribute);
51
- return transitionIntensity(previous, code);
46
+ return (0, utils_1.transitionIntensity)(previous, code);
52
47
  }
53
48
  return code;
54
49
  });
55
50
  }
56
51
  }
57
- module.exports = { State };
52
+ exports.State = State;
53
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/simplify/state.ts"],"names":[],"mappings":";;;AAAA,qCAA0D;AAC1D,mCAAyD;AAIzD,MAAM,YAAY,GAA6B,IAAI,GAAG,CAAC;IACtD,CAAC,WAAW,EAAM,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,QAAQ,EAAS,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,WAAW,EAAM,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,SAAS,EAAQ,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,QAAQ,EAAS,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,eAAe,EAAE,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,IAAI,EAAa,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,IAAI,EAAa,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,OAAO,EAAU,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC,UAAU,EAAO,CAAC,EAAE,CAAC,CAAC;CACvB,CAAC,CAAC;AAEH,MAAa,KAAM,SAAQ,GAAwB;IAClD,MAAM,CAAC,KAAe;QACrB,IAAI,QAAQ,GAAiB,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC;QAE3C,MAAM,UAAU,GAAG,IAAI,GAAG,EAAa,CAAC;QAExC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACvC,MAAM,EAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAC,GAAG,IAAA,mBAAU,EAAC,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;YAE3D,IAAI,SAAS,KAAK,OAAO,EAAE,CAAC;gBAC3B,UAAU,CAAC,KAAK,EAAE,CAAC;gBACn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use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const chalk_1 = __importDefault(require("/tmp/chalk/node_modules/chalk"));
7
+ const __1 = __importDefault(require(".."));
8
+ const patterns_1 = require("../patterns");
9
+ const simplify_1 = require("./simplify");
10
+ function showCodes(s) {
11
+ let result = ` ${__1.default.fg.green(JSON.stringify(s).replace(/\\u00/g, "\\x"))}\n`;
12
+ const parts = s.split(patterns_1.rxSGR);
13
+ result += ` [\n ${__1.default.fg.green(JSON.stringify(parts[0]))},\n`;
14
+ let longest = 0;
15
+ for (let i = 1; i < parts.length; i += 2) {
16
+ const text = parts[i].replace(/;/g, ", ");
17
+ if (text.length > longest)
18
+ longest = text.length;
19
+ }
20
+ for (let i = 1; i < parts.length; i += 2) {
21
+ const codes = parts[i].split(";");
22
+ const codeLength = codes.join(", ").length;
23
+ const padding = " ".repeat(longest - codeLength);
24
+ result += ` [${codes.map(__1.default.fg.yellow).join(", ")}], ${padding}${__1.default.fg.green(JSON.stringify(parts[i + 1]))},\n`;
25
+ }
26
+ result += " ]";
27
+ return result;
28
+ }
29
+ function test(s) {
30
+ const simplified = (0, simplify_1.simplify)(s);
31
+ console.log("original :\n" + showCodes(s));
32
+ console.log("simplified :\n" + showCodes(simplified));
33
+ console.log();
34
+ console.log("original :", s + __1.default.reset);
35
+ console.log("simplified :", simplified + __1.default.reset);
36
+ console.log("------------------------------");
37
+ }
38
+ const style = (name) => __1.default.style[name](name);
39
+ const fg = Object.assign((name) => __1.default.fg[name](name), {
40
+ rgb: (r, g, b) => __1.default.fg.rgb(r, g, b)(`rgb(${r}, ${g}, ${b})`),
41
+ x256: (r, g, b) => __1.default.fg.x256(r, g, b)(`x256(${r}, ${g}, ${b})`),
42
+ });
43
+ const bg = Object.assign((name) => __1.default.bg[name](`bg:${name}`), {
44
+ rgb: (r, g, b) => __1.default.bg.rgb(r, g, b)(`bg:rgb(${r}, ${g}, ${b})`),
45
+ x256: (r, g, b) => __1.default.bg.x256(r, g, b)(`bg:x256(${r}, ${g}, ${b})`),
46
+ });
47
+ debugger;
48
+ test("1: " + style("bold") + style("dim") + "default");
49
+ test("2: " + style("italic") + style("underline") + "default");
50
+ test("3: " + style("inverse") + style("hidden") + "default");
51
+ test("4: " + style("strikethrough") + "default");
52
+ test("5: " + fg("red") + fg("blue") + "default");
53
+ test("6: " + fg("red") + bg("blue") + "default");
54
+ test("7: " + fg.rgb(255, 0, 0) + bg.rgb(0, 0, 255) + "default");
55
+ test("8: " + fg.x256(0, 80, 0) + bg.x256(0, 0, 80) + "default");
56
+ test(`${__1.default.style.bold(`bold ${__1.default.style.dim("dim-bold")} bold again`)} ${__1.default.style.dim(`dim ${__1.default.style.bold(`bold-dim ${__1.default.style.dim("still dim")} still bold-dim`)} dim again`)} default`);
57
+ test(`${chalk_1.default.bold(`bold ${chalk_1.default.dim("dim-bold")} bold again`)} ${chalk_1.default.dim(`dim ${chalk_1.default.bold(`bold-dim ${chalk_1.default.dim("still dim")} still bold-dim`)} dim again`)} default`);
58
+ test(`reset in the middle: ${style("bold")}${__1.default.reset} reset`);
59
+ test(`reset at the end: ${style("bold")} default${__1.default.reset}`);
60
+ test(`multiple resets: ${style("bold")} default${__1.default.reset}${__1.default.reset} default`);
61
+ test(`codes after a reset: ${style("bold")} default${__1.default.reset}${__1.default.fg.default}${style("italic")} ${style("bold")} default`);
62
+ test(__1.default.fg.brightWhite(__1.default.bg.red(__1.default.style.underline("bright white on red with underline"))));
63
+ test(chalk_1.default.whiteBright.bgRed.underline("bright white on red with underline"));
64
+ //# sourceMappingURL=test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.js","sourceRoot":"","sources":["../../src/simplify/test.ts"],"names":[],"mappings":";;;;;AAAA,0EAAuD;AACvD,2CAA4B;AAC5B,0CAAqC;AACrC,yCAAoC;AAEpC,SAAS,SAAS,CAAC,CAAS;IAC3B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K,CAAC,MAAM,CAAC,WAAW,WAAI,CAAC,KAAK,GAAG,WAAI,CAAC,KAAK,UAAU,CAAC,CAAC;AAEpF,IAAI,CAAC,wBAAwB,KAAK,CAAC,MAAM,CAAC,WAAW,WAAI,CAAC,KAAK,GAAG,WAAI,CAAC,EAAE,CAAC,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;AAEhI,IAAI,CAAC,WAAI,CAAC,EAAE,CAAC,WAAW,CAAC,WAAI,CAAC,EAAE,CAAC,GAAG,CAAC,WAAI,CAAC,KAAK,CAAC,SAAS,CAAC,oCAAoC,CAAC,CAAC,CAAC,CAAC,CAAC;AACnG,IAAI,CAAC,eAAK,CAAC,WAAW,CAAC,KAAK,CAAC,SAAS,CAAC,oCAAoC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ export type Attribute = "intensity" | "italic" | "underline" | "inverse" | "hidden" | "strikethrough" | "fg" | "bg" | "frame" | "overline" | "reset" | number;
2
+ export type AttributeMap = Map<Attribute, number[]>;
3
+ export interface Atom {
4
+ attribute: Attribute;
5
+ code: number[];
6
+ skip?: number;
7
+ }
8
+ export type BuildAtom = (attribute: Attribute, codes: number[], index: number, state: AttributeMap) => Atom;
9
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/simplify/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAClB,WAAW,GACX,QAAQ,GACR,WAAW,GACX,SAAS,GACT,QAAQ,GACR,eAAe,GACf,IAAI,GACJ,IAAI,GACJ,OAAO,GACP,UAAU,GACV,OAAO,GACP,MAAM,CAAC;AAEV,MAAM,MAAM,YAAY,GAAG,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;AAEpD,MAAM,WAAW,IAAI;IACpB,SAAS,EAAG,SAAS,CAAC;IACtB,IAAI,EAAQ,MAAM,EAAE,CAAC;IACrB,IAAI,CAAC,EAAO,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,SAAS,GAAG,CACvB,SAAS,EAAG,SAAS,EACrB,KAAK,EAAO,MAAM,EAAE,EACpB,KAAK,EAAO,MAAM,EAClB,KAAK,EAAO,YAAY,KACpB,IAAI,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/simplify/types.ts"],"names":[],"mappings":""}
@@ -1,2 +1,3 @@
1
- export function arraysEqual<T>(a: T[] | undefined, b: T[] | undefined): boolean;
2
- export function transitionIntensity(previous: number[] | undefined, current: number[]): number[];
1
+ export declare function arraysEqual<T>(a: T[] | undefined, b: T[] | undefined): boolean;
2
+ export declare function transitionIntensity(previous: number[] | undefined, current: number[]): number[];
3
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/simplify/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,SAAS,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,SAAS,GAAG,OAAO,CAa9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAmB/F"}
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
- /** @type {<T>(a: T[] | undefined, b: T[] | undefined) => boolean} */
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.arraysEqual = arraysEqual;
4
+ exports.transitionIntensity = transitionIntensity;
3
5
  function arraysEqual(a, b) {
4
6
  if (a === b)
5
7
  return true;
@@ -11,7 +13,6 @@ function arraysEqual(a, b) {
11
13
  }
12
14
  return true;
13
15
  }
14
- /** @type {(previous: number[] | undefined, current: number[]) => number[]} */
15
16
  function transitionIntensity(previous, current) {
16
17
  if (!previous || previous[0] === 22 || current[0] === 22)
17
18
  return current;
@@ -28,4 +29,4 @@ function transitionIntensity(previous, current) {
28
29
  result.push(2);
29
30
  return result;
30
31
  }
31
- module.exports = { arraysEqual, transitionIntensity };
32
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/simplify/utils.ts"],"names":[],"mappings":";;AAAA,kCAaC;AAED,kDAmBC;AAlCD,SAAgB,WAAW,CAAI,CAAkB,EAAE,CAAkB;IACpE,IAAI,CAAC,KAAK,CAAC;QACV,OAAO,IAAI,CAAC;IAEb,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QACpC,OAAO,KAAK,CAAC;IAEd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAChB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC;AAED,SAAgB,mBAAmB,CAAC,QAA8B,EAAE,OAAiB;IACpF,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,CAAC,CAAC,KAAK,EAAE,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE;QACvD,OAAO,OAAO,CAAC;IAEhB,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IACjC,MAAM,MAAM,GAAI,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IACpD,MAAM,MAAM,GAAI,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IAElD,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,IAAI,OAAO,IAAI,CAAC,OAAO,IAAI,MAAM,IAAI,CAAC,MAAM;QAC3C,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjB,IAAI,OAAO,IAAI,CAAC,CAAC,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;QAC5C,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAChB,IAAI,MAAM,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;QAC1C,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEhB,OAAO,MAAM,CAAC;AACf,CAAC"}
package/dist/slice.d.ts CHANGED
@@ -1 +1,21 @@
1
- export function slice(text: string, start: number, end: number): string;
1
+ /**
2
+ * Return a slice of the given text, counting only visible characters (i.e. ignoring ANSI escape
3
+ * codes). The returned slice will include any ANSI escape codes from within the slice. In addition,
4
+ * SGR codes (m) from before and after the slice will be combined, simplified, and included so that
5
+ * the terminal state before and after the slice will be the same as if the whole string were
6
+ * printed. If you compose two or more slices together, there may be redundant SGR sequences at the
7
+ * boundaries, but the result will be correct. You can use `simplify` to remove the redundancies.
8
+ *
9
+ * Note the following caveats:
10
+ *
11
+ * * Any non-SGR ANSI codes prior to or after the slice will be ignored.
12
+ * * Control characters (such as newlines and backspaces) both outside and inside the slice are
13
+ * counted as visible characters.
14
+ *
15
+ * These caveats mean that text may not appear on screen to be the same number of characters as the
16
+ * length of the slice, and that the terminal state may not be exactly the same at the start and end
17
+ * of the slice as it would be if the whole string were printed. This function is intended mostly
18
+ * for text that contains SGR codes, but no other ANSI codes or control characters.
19
+ */
20
+ export declare function slice(text: string, start: number, end: number): string;
21
+ //# sourceMappingURL=slice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slice.d.ts","sourceRoot":"","sources":["../src/slice.ts"],"names":[],"mappings":"AAqCA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CA4BtE"}
package/dist/slice.js CHANGED
@@ -1,19 +1,12 @@
1
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
- */
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.slice = slice;
4
+ const patterns_1 = require("./patterns");
5
+ const simplify_1 = require("./simplify");
6
+ const strip_1 = require("./strip");
14
7
  function scanCSI(text, fromStringIndex, visibleCount) {
15
8
  // We intentionally mutate `lastIndex` here, so we need a clone.
16
- const rx = new RegExp(rxCSI);
9
+ const rx = new RegExp(patterns_1.rxCSI);
17
10
  let length = 0;
18
11
  let index = fromStringIndex;
19
12
  while (index < text.length && length < visibleCount) {
@@ -30,10 +23,9 @@ function scanCSI(text, fromStringIndex, visibleCount) {
30
23
  }
31
24
  return { index, length };
32
25
  }
33
- /** @type {(text: string, start: number, end?: number) => string} */
34
26
  function skippedSequences(text, start, end) {
35
- const sequences = text.slice(start, end).match(new RegExp(rxSGR));
36
- return sequences ? simplify(sequences.join("")) : "";
27
+ const sequences = text.slice(start, end).match(new RegExp(patterns_1.rxSGR));
28
+ return sequences ? (0, simplify_1.simplify)(sequences.join("")) : "";
37
29
  }
38
30
  /**
39
31
  * Return a slice of the given text, counting only visible characters (i.e. ignoring ANSI escape
@@ -53,13 +45,10 @@ function skippedSequences(text, start, end) {
53
45
  * length of the slice, and that the terminal state may not be exactly the same at the start and end
54
46
  * of the slice as it would be if the whole string were printed. This function is intended mostly
55
47
  * 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
48
  */
59
49
  function slice(text, start, end) {
60
- /** @type {number | undefined} */
61
50
  let textVisibleLength;
62
- const getVisibleLength = () => textVisibleLength ??= visibleLength(text);
51
+ const getVisibleLength = () => textVisibleLength ?? (textVisibleLength = (0, strip_1.visibleLength)(text));
63
52
  if (start < 0 || end < 0) {
64
53
  textVisibleLength = getVisibleLength();
65
54
  if (start < 0)
@@ -75,9 +64,8 @@ function slice(text, start, end) {
75
64
  const endIndex = startIndex < text.length && visibleSkipped < end
76
65
  ? scanCSI(text, startIndex, end - visibleSkipped).index
77
66
  : text.length;
78
- // Start and end the string in the same terminal state it would be in if the string weren't sliced.
79
67
  const leadingSequences = skippedSequences(text, 0, startIndex);
80
68
  const trailingSequences = skippedSequences(text, endIndex);
81
69
  return leadingSequences + text.slice(startIndex, endIndex) + trailingSequences;
82
70
  }
83
- module.exports = { slice };
71
+ //# sourceMappingURL=slice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slice.js","sourceRoot":"","sources":["../src/slice.ts"],"names":[],"mappings":";;AAwDA,sBA4BC;AApFD,yCAAyC;AACzC,yCAAyC;AACzC,mCAAsC;AAEtC,SAAS,OAAO,CACf,IAAwB,EACxB,eAAwB,EACxB,YAAwB;IAExB,gEAAgE;IAChE,MAAM,EAAE,GAAG,IAAI,MAAM,CAAC,gBAAK,CAAC,CAAC;IAE7B,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,KAAK,GAAI,eAAe,CAAC;IAE7B,OAAO,KAAK,GAAG,IAAI,CAAC,MAAM,IAAI,MAAM,GAAG,YAAY,EAAE,CAAC;QACrD,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,MAAM,EAAE,CAAC;YAC5B,EAAE,CAAC,SAAS,GAAG,KAAK,CAAC;YACrB,MAAM,KAAK,GAAG,EAAE,CAAC,IAA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AG,GAAG;QACd,OAAO,EAAE,CAAC;IAEX,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,IAAI,GAAG,IAAI,gBAAgB,EAAE,CAAC;QACnE,OAAO,IAAI,CAAC;IAEb,MAAM,EAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,cAAc,EAAC,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAE5E,MAAM,QAAQ,GACb,UAAU,GAAG,IAAI,CAAC,MAAM,IAAI,cAAc,GAAG,GAAG;QAC/C,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,GAAG,GAAG,cAAc,CAAC,CAAC,KAAK;QACvD,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC;IAEhB,MAAM,gBAAgB,GAAI,gBAAgB,CAAC,IAAI,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;IAChE,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IAE3D,OAAO,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,QAAQ,CAAC,GAAG,iBAAiB,CAAC;AAChF,CAAC"}
package/dist/strip.d.ts CHANGED
@@ -1,15 +1,4 @@
1
- /**
2
- * Remove all ANSI escape codes from the given text.
3
- * @type {(text: string) => string}
4
- */
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;
11
- /**
12
- * Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
13
- * @type {(text: string) => string}
14
- */
15
- export const sanitize: (text: string) => string;
1
+ export declare const strip: (text: string) => string;
2
+ export declare const visibleLength: (text: string) => number;
3
+ export declare const sanitize: (text: string) => string;
4
+ //# sourceMappingURL=strip.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strip.d.ts","sourceRoot":"","sources":["../src/strip.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,KAAK,GAAY,MAAM,MAAM,KAAG,MAA6C,CAAC;AAC3F,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,KAAG,MAA4B,CAAC;AAC1E,eAAO,MAAM,QAAQ,GAAS,MAAM,MAAM,KAAG,MAAgD,CAAC"}
package/dist/strip.js CHANGED
@@ -1,18 +1,11 @@
1
1
  "use strict";
2
- const { rxCSI, rxUnsafe } = require("./patterns");
3
- /**
4
- * Remove all ANSI escape codes from the given text.
5
- * @type {(text: string) => string}
6
- */
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;
13
- /**
14
- * Remove all ANSI escape codes except SGR codes (m), which are used for color and styling.
15
- * @type {(text: string) => string}
16
- */
17
- const sanitize = text => text.replace(new RegExp(rxUnsafe), "");
18
- module.exports = { strip, visibleLength, sanitize };
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sanitize = exports.visibleLength = exports.strip = void 0;
4
+ const patterns_1 = require("./patterns");
5
+ const strip = (text) => text.replace(new RegExp(patterns_1.rxCSI), "");
6
+ exports.strip = strip;
7
+ const visibleLength = (text) => (0, exports.strip)(text).length;
8
+ exports.visibleLength = visibleLength;
9
+ const sanitize = (text) => text.replace(new RegExp(patterns_1.rxUnsafe), "");
10
+ exports.sanitize = sanitize;
11
+ //# sourceMappingURL=strip.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strip.js","sourceRoot":"","sources":["../src/strip.ts"],"names":[],"mappings":";;;AAAA,yCAA2C;AAEpC,MAAM,KAAK,GAAW,CAAC,IAAY,EAAU,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,gBAAK,CAAC,EAAE,EAAE,CAAC,CAAC;AAA9E,QAAA,KAAK,SAAyE;AACpF,MAAM,aAAa,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,IAAA,aAAK,EAAC,IAAI,CAAC,CAAC,MAAM,CAAC;AAA7D,QAAA,aAAa,iBAAgD;AACnE,MAAM,QAAQ,GAAQ,CAAC,IAAY,EAAU,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,mBAAQ,CAAC,EAAE,EAAE,CAAC,CAAC;AAAjF,QAAA,QAAQ,YAAyE"}
@@ -1,4 +1,4 @@
1
- export function makeTerminal(enabled?: boolean): {
1
+ declare const terminal: {
2
2
  focusReporting: {
3
3
  enable: string;
4
4
  disable: string;
@@ -20,3 +20,7 @@ export function makeTerminal(enabled?: boolean): {
20
20
  end: string;
21
21
  };
22
22
  };
23
+ export type Terminal = typeof terminal;
24
+ export declare const makeTerminal: (enabled?: boolean) => Terminal;
25
+ export {};
26
+ //# sourceMappingURL=terminal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"terminal.d.ts","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;CAwBb,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,OAAO,QAAQ,CAAC;AA4BvC,eAAO,MAAM,YAAY,GAAI,iBAAc,KAAG,QAAyC,CAAC"}
package/dist/terminal.js CHANGED
@@ -1,4 +1,6 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeTerminal = void 0;
2
4
  const terminal = {
3
5
  focusReporting: {
4
6
  enable: "\x1b[?1004h",
@@ -21,7 +23,6 @@ const terminal = {
21
23
  end: "\x1b[201~",
22
24
  },
23
25
  };
24
- /** @type {typeof terminal} */
25
26
  const disabled = {
26
27
  focusReporting: {
27
28
  enable: "",
@@ -45,4 +46,5 @@ const disabled = {
45
46
  },
46
47
  };
47
48
  const makeTerminal = (enabled = true) => enabled ? terminal : disabled;
48
- module.exports = { makeTerminal };
49
+ exports.makeTerminal = makeTerminal;
50
+ //# sourceMappingURL=terminal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"terminal.js","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":";;;AAAA,MAAM,QAAQ,GAAG;IAChB,cAAc,EAAE;QACf,MAAM,EAAI,aAAa;QACvB,OAAO,EAAG,aAAa;QACvB,KAAK,EAAK,QAAQ;QAClB,IAAI,EAAM,QAAQ;KAClB;IAED,eAAe,EAAE;QAChB,MAAM,EAAI,aAAa;QACvB,OAAO,EAAG,aAAa;QAEvB,MAAM,EAAE;YACP,MAAM,EAAI,WAAW;YACrB,OAAO,EAAG,WAAW;SACrB;KACD;IAED,cAAc,EAAE;QACf,MAAM,EAAI,aAAa;QACvB,OAAO,EAAG,aAAa;QACvB,KAAK,EAAK,WAAW;QACrB,GAAG,EAAO,WAAW;KACrB;CACD,CAAC;AAIF,MAAM,QAAQ,GAAa;IAC1B,cAAc,EAAE;QACf,MAAM,EAAI,EAAE;QACZ,OAAO,EAAG,EAAE;QACZ,KAAK,EAAK,EAAE;QACZ,IAAI,EAAM,EAAE;KACZ;IAED,eAAe,EAAE;QAChB,MAAM,EAAI,EAAE;QACZ,OAAO,EAAG,EAAE;QAEZ,MAAM,EAAE;YACP,MAAM,EAAI,EAAE;YACZ,OAAO,EAAG,EAAE;SACZ;KACD;IAED,cAAc,EAAE;QACf,MAAM,EAAI,EAAE;QACZ,OAAO,EAAG,EAAE;QACZ,KAAK,EAAK,EAAE;QACZ,GAAG,EAAO,EAAE;KACZ;CACD,CAAC;AAEK,MAAM,YAAY,GAAG,CAAC,OAAO,GAAG,IAAI,EAAY,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC;AAA3E,QAAA,YAAY,gBAA+D"}
package/package.json CHANGED
@@ -1,32 +1,19 @@
1
1
  {
2
2
  "name" : "@peteanderson/ansi",
3
- "version" : "0.3.2",
3
+ "version" : "1.0.0",
4
4
  "description" : "ANSI escape sequence generation with automatic feature detection",
5
- "author" : {
6
- "name" : "Pete Anderson",
7
- "email" : "pete.the.anderson@gmail.com"
8
- },
9
- "repository" : {
10
- "type" : "git",
11
- "url" : "git+https://github.com/anderson-pete/ansi.git"
12
- },
13
- "keywords" : ["ansi", "nodejs", "color", "terminal", "cli"],
14
- "engines" : {"node": ">=18"},
15
5
  "files" : ["dist", "src"],
16
- "main" : "dist/ansi.js",
17
- "types" : "dist/ansi.d.ts",
18
6
  "exports" : {
19
7
  ".": {
20
- "types" : "./dist/ansi.d.ts",
21
- "require" : "./dist/ansi.js"
22
- }
23
- },
24
- "license" : "MIT",
8
+ "require" : "./dist/index.js",
9
+ "types" : "./dist/index.d.ts"
10
+ }},
11
+ "main" : "dist/index.js",
12
+ "types" : "dist/index.d.ts",
13
+ "license" : "UNLICENSED",
25
14
  "scripts" : {
26
- "prebuild" : "node -e \"require('node:fs').rmSync('dist', {force: true, recursive: true})\"",
27
- "build" : "tsc -p tsconfig.build.json",
28
- "prepack" : "npm run build",
29
- "tag" : "git tag -a v$npm_package_version -m v$npm_package_version"
15
+ "build" : "tsc",
16
+ "build:watch" : "tsc --watch"
30
17
  },
31
18
  "devDependencies" : {
32
19
  "@types/node" : "^26.0.0",
package/src/ansi.ts ADDED
@@ -0,0 +1,71 @@
1
+ import {makeCaret} from "./caret";
2
+ import {makeErase} from "./erase";
3
+ import {getFeatures} from "./features";
4
+ import {padEnd, padStart} from "./pad";
5
+ import {makeScroll} from "./scroll";
6
+ import {makeColor, makeReset, makeStyle} from "./sgr";
7
+ import {simplify} from "./simplify";
8
+ import {slice} from "./slice";
9
+ import {sanitize, strip, visibleLength} from "./strip";
10
+ import {makeTerminal} from "./terminal";
11
+
12
+ import type {Args} from "./features/features";
13
+ import type {Caret} from "./caret";
14
+ import type {Erase} from "./erase";
15
+ import type {Features} from "./features/types";
16
+ import type {Scroll} from "./scroll";
17
+ import type {Color, Style} from "./sgr";
18
+ import type {Terminal} from "./terminal";
19
+
20
+ export type Ansi = Color & {
21
+ caret : Caret;
22
+ erase : Erase;
23
+ scroll : Scroll;
24
+
25
+ reset : string;
26
+ style : Style;
27
+
28
+ terminal : Terminal;
29
+
30
+ padEnd : typeof padEnd;
31
+ padStart : typeof padStart;
32
+
33
+ strip : typeof strip;
34
+ visibleLength : typeof visibleLength;
35
+ sanitize : typeof sanitize;
36
+ slice : typeof slice;
37
+ simplify : typeof simplify;
38
+
39
+ features : Features;
40
+ };
41
+
42
+ export function makeAnsi(...args: Args): Ansi {
43
+ const features = getFeatures(...args);
44
+
45
+ return {
46
+ caret : makeCaret(features.caret),
47
+ erase : makeErase(features.erase),
48
+ scroll : makeScroll(features.scroll),
49
+
50
+ reset : makeReset(features.colorDepth > 1),
51
+ style : makeStyle(features.style),
52
+ ...makeColor(features.colorDepth),
53
+
54
+ terminal: makeTerminal(features.terminal),
55
+
56
+ padEnd,
57
+ padStart,
58
+
59
+ strip,
60
+ visibleLength,
61
+ sanitize,
62
+ slice,
63
+ simplify,
64
+
65
+ features,
66
+ };
67
+ }
68
+
69
+ const ansi = Object.assign(makeAnsi, makeAnsi());
70
+
71
+ export default ansi;
@@ -1,10 +1,7 @@
1
- "use strict";
1
+ const move = (code: string) => (count?: number): string =>
2
+ `\x1b[${count === undefined ? "" : count ? count : "?"}${code}`;
2
3
 
3
- /** @type {(code: string) => (count?: number) => string} */
4
- const move = code => count => `\x1b[${count === undefined ? "" : count ? count : "?"}${code}`;
5
-
6
- /** @type {(count?: number) => string} */
7
- const noop = () => "";
4
+ const noop = (): string => "";
8
5
 
9
6
  const caret = {
10
7
  up : move("A"),
@@ -14,8 +11,7 @@ const caret = {
14
11
  nextLine : move("E"),
15
12
  prevLine : move("F"),
16
13
 
17
- /** @type {(x: number) => string} */
18
- x: x => `\x1b[${x}G`,
14
+ x: (x: number): string => `\x1b[${x}G`,
19
15
 
20
16
  // These are the old VT100 codes, but they're widely supported by old and new terminals. The
21
17
  // newer VT220 codes are standardized by ECMA and ISO, and are also widely supported, but might
@@ -33,8 +29,7 @@ const caret = {
33
29
 
34
30
  position: {
35
31
  get: "\x1b[6n",
36
- /** @type {(x: number, y: number) => string} */
37
- set: (x, y) => `\x1b[${y};${x}H`,
32
+ set: (x: number, y: number): string => `\x1b[${y};${x}H`,
38
33
  },
39
34
 
40
35
  shape: {
@@ -48,8 +43,9 @@ const caret = {
48
43
  },
49
44
  };
50
45
 
51
- /** @type {typeof caret} */
52
- const disabled = {
46
+ export type Caret = typeof caret;
47
+
48
+ const disabled: Caret = {
53
49
  up : noop,
54
50
  down : noop,
55
51
  forward : noop,
@@ -81,6 +77,4 @@ const disabled = {
81
77
  },
82
78
  };
83
79
 
84
- const makeCaret = (enabled = true) => enabled ? caret : disabled;
85
-
86
- module.exports = {makeCaret};
80
+ export const makeCaret = (enabled = true): Caret => enabled ? caret : disabled;
@@ -1,10 +1,8 @@
1
- "use strict";
2
-
3
1
  const erase = {
4
2
  line: {
5
- toStart: "\x1b[1K",
6
- toEnd : "\x1b[0K",
7
- full : "\x1b[2K",
3
+ toStart : "\x1b[1K",
4
+ toEnd : "\x1b[0K",
5
+ full : "\x1b[2K",
8
6
  },
9
7
  screen: {
10
8
  toStart : "\x1b[1J",
@@ -14,8 +12,9 @@ const erase = {
14
12
  },
15
13
  };
16
14
 
17
- /** @type {typeof erase} */
18
- const disabled = {
15
+ export type Erase = typeof erase;
16
+
17
+ const disabled: Erase = {
19
18
  line: {
20
19
  toStart : "",
21
20
  toEnd : "",
@@ -29,6 +28,4 @@ const disabled = {
29
28
  },
30
29
  };
31
30
 
32
- const makeErase = (enabled = true) => enabled ? erase : disabled;
33
-
34
- module.exports = {makeErase};
31
+ export const makeErase = (enabled = true): Erase => enabled ? erase : disabled;