@peteanderson/ansi 0.3.0 → 0.3.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.
package/README.md CHANGED
@@ -137,6 +137,11 @@ available).
137
137
 
138
138
  Returns the visible length of a string with all ANSI sequences removed (always available).
139
139
 
140
+ ### `padStart(text, targetLength)` and `padEnd(text, targetLength)`
141
+
142
+ Pads a string to a target visible length using spaces. Accounts for ANSI sequences when calculating
143
+ length.
144
+
140
145
  ### `features`
141
146
 
142
147
  Object containing the detected feature configuration:
package/dist/ansi.d.ts CHANGED
@@ -8,6 +8,8 @@ declare const ansi: typeof makeAnsi & {
8
8
  scroll: ReturnType<typeof makeScroll>;
9
9
  reset: string;
10
10
  style: ReturnType<typeof makeStyle>;
11
+ padEnd: typeof padEnd;
12
+ padStart: typeof padStart;
11
13
  strip: typeof strip;
12
14
  visibleLength: typeof visibleLength;
13
15
  sanitize: typeof sanitize;
@@ -23,6 +25,8 @@ import { makeCaret } from "./caret";
23
25
  import { makeErase } from "./erase";
24
26
  import { makeScroll } from "./scroll";
25
27
  import { makeStyle } from "./sgr/style";
28
+ import { padEnd } from "./pad";
29
+ import { padStart } from "./pad";
26
30
  import { strip } from "./strip";
27
31
  import { visibleLength } from "./strip";
28
32
  import { sanitize } from "./strip";
@@ -36,6 +40,8 @@ type Ansi = ReturnType<typeof makeColor> & {
36
40
  scroll: ReturnType<typeof makeScroll>;
37
41
  reset: string;
38
42
  style: ReturnType<typeof makeStyle>;
43
+ padEnd: typeof padEnd;
44
+ padStart: typeof padStart;
39
45
  strip: typeof strip;
40
46
  visibleLength: typeof visibleLength;
41
47
  sanitize: typeof sanitize;
package/dist/ansi.js CHANGED
@@ -2,6 +2,7 @@
2
2
  const { makeCaret } = require("./caret");
3
3
  const { makeErase } = require("./erase");
4
4
  const { getFeatures } = require("./features");
5
+ const { padEnd, padStart } = require("./pad");
5
6
  const { makeScroll } = require("./scroll");
6
7
  const { makeColor, makeReset, makeStyle } = require("./sgr");
7
8
  const { simplify } = require("./simplify");
@@ -18,6 +19,9 @@ const { sanitize, strip, visibleLength } = require("./strip");
18
19
  reset : string;
19
20
  style : ReturnType<typeof makeStyle>;
20
21
 
22
+ padEnd : typeof padEnd;
23
+ padStart : typeof padStart;
24
+
21
25
  strip : typeof strip;
22
26
  visibleLength : typeof visibleLength;
23
27
  sanitize : typeof sanitize;
@@ -37,6 +41,8 @@ function makeAnsi(...args) {
37
41
  reset: makeReset(features.colorDepth > 1),
38
42
  style: makeStyle(features.style),
39
43
  ...makeColor(features.colorDepth),
44
+ padEnd,
45
+ padStart,
40
46
  strip,
41
47
  visibleLength,
42
48
  sanitize,
package/dist/index.d.ts CHANGED
@@ -47,6 +47,8 @@ declare const _exports: ((...args: Args) => Ansi) & {
47
47
  }>;
48
48
  reset: string;
49
49
  style: ReturnType<typeof import("./sgr/style").makeStyle>;
50
+ padEnd: (text: string, targetLength: number, fillString?: string) => string;
51
+ padStart: (text: string, targetLength: number, fillString?: string) => string;
50
52
  strip: (text: string) => string;
51
53
  visibleLength: (text: string) => number;
52
54
  sanitize: (text: string) => string;
package/dist/pad.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Pad the given text at the end with the specified fill string to the target visible length without
3
+ * counting ANSI escape codes.
4
+ * @type {(text: string, targetLength: number, fillString?: string) => string}
5
+ */
6
+ export const padEnd: (text: string, targetLength: number, fillString?: string) => string;
7
+ /**
8
+ * Pad the given text at the start with the specified fill string to the target visible length
9
+ * without counting ANSI escape codes.
10
+ * @type {(text: string, targetLength: number, fillString?: string) => string}
11
+ */
12
+ export const padStart: (text: string, targetLength: number, fillString?: string) => string;
package/dist/pad.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ const { visibleLength } = require("./strip");
3
+ /**
4
+ * Pad the given text at the end with the specified fill string to the target visible length without
5
+ * counting ANSI escape codes.
6
+ * @type {(text: string, targetLength: number, fillString?: string) => string}
7
+ */
8
+ const padEnd = (text, targetLength, fillString = " ") => text + fillString.repeat(Math.max(0, targetLength - visibleLength(text)) / fillString.length);
9
+ /**
10
+ * Pad the given text at the start with the specified fill string to the target visible length
11
+ * without counting ANSI escape codes.
12
+ * @type {(text: string, targetLength: number, fillString?: string) => string}
13
+ */
14
+ const padStart = (text, targetLength, fillString = " ") => fillString.repeat(Math.max(0, targetLength - visibleLength(text)) / fillString.length) + text;
15
+ module.exports = { padEnd, padStart };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name" : "@peteanderson/ansi",
3
- "version" : "0.3.0",
3
+ "version" : "0.3.1",
4
4
  "description" : "ANSI escape sequence generation with automatic feature detection",
5
5
  "author" : {
6
6
  "name" : "Pete Anderson",
package/src/ansi.js CHANGED
@@ -3,6 +3,7 @@
3
3
  const {makeCaret} = require("./caret");
4
4
  const {makeErase} = require("./erase");
5
5
  const {getFeatures} = require("./features");
6
+ const {padEnd, padStart} = require("./pad");
6
7
  const {makeScroll} = require("./scroll");
7
8
  const {makeColor, makeReset, makeStyle} = require("./sgr");
8
9
  const {simplify} = require("./simplify");
@@ -20,6 +21,9 @@ const {sanitize, strip, visibleLength} = require("./strip");
20
21
  reset : string;
21
22
  style : ReturnType<typeof makeStyle>;
22
23
 
24
+ padEnd : typeof padEnd;
25
+ padStart : typeof padStart;
26
+
23
27
  strip : typeof strip;
24
28
  visibleLength : typeof visibleLength;
25
29
  sanitize : typeof sanitize;
@@ -43,6 +47,9 @@ function makeAnsi(...args) {
43
47
  style : makeStyle(features.style),
44
48
  ...makeColor(features.colorDepth),
45
49
 
50
+ padEnd,
51
+ padStart,
52
+
46
53
  strip,
47
54
  visibleLength,
48
55
  sanitize,
package/src/pad.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ const {visibleLength} = require("./strip");
4
+
5
+ /**
6
+ * Pad the given text at the end with the specified fill string to the target visible length without
7
+ * counting ANSI escape codes.
8
+ * @type {(text: string, targetLength: number, fillString?: string) => string}
9
+ */
10
+ const padEnd = (text, targetLength, fillString = " ") =>
11
+ text + fillString.repeat(Math.max(0, targetLength - visibleLength(text)) / fillString.length);
12
+
13
+ /**
14
+ * Pad the given text at the start with the specified fill string to the target visible length
15
+ * without counting ANSI escape codes.
16
+ * @type {(text: string, targetLength: number, fillString?: string) => string}
17
+ */
18
+ const padStart = (text, targetLength, fillString = " ") =>
19
+ fillString.repeat(Math.max(0, targetLength - visibleLength(text)) / fillString.length) + text;
20
+
21
+ module.exports = {padEnd, padStart};