@heinrichb/console-toolkit 1.0.7 → 1.0.8
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/package.json +5 -5
- package/dist/components/progress.d.ts +0 -75
- package/dist/components/progress.js +0 -42
- package/dist/components/progress.test.d.ts +0 -1
- package/dist/components/progress.test.js +0 -105
- package/dist/components/spinner.d.ts +0 -36
- package/dist/components/spinner.js +0 -33
- package/dist/components/spinner.test.d.ts +0 -1
- package/dist/components/spinner.test.js +0 -53
- package/dist/core/layout.d.ts +0 -25
- package/dist/core/layout.js +0 -55
- package/dist/core/layout.test.d.ts +0 -1
- package/dist/core/layout.test.js +0 -52
- package/dist/core/printer.d.ts +0 -33
- package/dist/core/printer.js +0 -129
- package/dist/core/printer.test.d.ts +0 -1
- package/dist/core/printer.test.js +0 -124
- package/dist/core/style.d.ts +0 -53
- package/dist/core/style.js +0 -171
- package/dist/core/style.test.d.ts +0 -1
- package/dist/core/style.test.js +0 -108
- package/dist/core/types.d.ts +0 -72
- package/dist/core/types.js +0 -4
- package/dist/core/utils.d.ts +0 -25
- package/dist/core/utils.js +0 -36
- package/dist/core/utils.test.d.ts +0 -1
- package/dist/core/utils.test.js +0 -25
- package/dist/demo.d.ts +0 -1
- package/dist/demo.js +0 -145
- package/dist/demo.test.d.ts +0 -1
- package/dist/demo.test.js +0 -28
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -14
- package/dist/presets/ascii.d.ts +0 -5
- package/dist/presets/ascii.js +0 -35
- package/dist/presets/ascii.test.d.ts +0 -1
- package/dist/presets/ascii.test.js +0 -21
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heinrichb/console-toolkit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "A versatile TypeScript utility library for enhanced console logging, formatting, and layout management.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -14,9 +14,10 @@
|
|
|
14
14
|
],
|
|
15
15
|
"scripts": {
|
|
16
16
|
"demo": "bun run scripts/run-demo.ts",
|
|
17
|
-
"test": "bun
|
|
18
|
-
"test:coverage": "bun
|
|
19
|
-
"build": "bun
|
|
17
|
+
"test": "bun test src",
|
|
18
|
+
"test:coverage": "bun test src --coverage --coverage-threshold=100",
|
|
19
|
+
"build": "bun run scripts/build.ts",
|
|
20
|
+
"typecheck": "bun x tsc --noEmit",
|
|
20
21
|
"format:check": "prettier --check \"src/**/*.{ts,json,md}\"",
|
|
21
22
|
"format:write": "prettier --write \"src/**/*.{ts,json,md}\"",
|
|
22
23
|
"lint": "eslint \"src/**/*.ts\"",
|
|
@@ -37,7 +38,6 @@
|
|
|
37
38
|
"eslint": "^10.0.2",
|
|
38
39
|
"eslint-config-prettier": "^10.1.8",
|
|
39
40
|
"globals": "^17.3.0",
|
|
40
|
-
"jiti": "^2.6.1",
|
|
41
41
|
"prettier": "^3.8.1",
|
|
42
42
|
"typescript": "^5.9.3",
|
|
43
43
|
"typescript-eslint": "^8.56.1"
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { PrintLine, PrintStyle } from "../core/types";
|
|
2
|
-
export interface ProgressBarOptions {
|
|
3
|
-
/**
|
|
4
|
-
* Progress value between 0.0 and 1.0.
|
|
5
|
-
*/
|
|
6
|
-
progress: number;
|
|
7
|
-
/**
|
|
8
|
-
* Width of the progress bar (excluding brackets and percentage).
|
|
9
|
-
* Defaults to 20.
|
|
10
|
-
*/
|
|
11
|
-
width?: number;
|
|
12
|
-
/**
|
|
13
|
-
* Base style for the entire progress bar.
|
|
14
|
-
*/
|
|
15
|
-
style?: PrintStyle;
|
|
16
|
-
/**
|
|
17
|
-
* Style for the brackets (start and end characters).
|
|
18
|
-
* Defaults to `style`.
|
|
19
|
-
*/
|
|
20
|
-
bracketStyle?: PrintStyle;
|
|
21
|
-
/**
|
|
22
|
-
* Specific style for the start bracket. Overrides `bracketStyle`.
|
|
23
|
-
*/
|
|
24
|
-
startStyle?: PrintStyle;
|
|
25
|
-
/**
|
|
26
|
-
* Specific style for the end bracket. Overrides `bracketStyle`.
|
|
27
|
-
*/
|
|
28
|
-
endStyle?: PrintStyle;
|
|
29
|
-
/**
|
|
30
|
-
* Style for the bar (filled and empty parts).
|
|
31
|
-
* Defaults to `style`.
|
|
32
|
-
*/
|
|
33
|
-
barStyle?: PrintStyle;
|
|
34
|
-
/**
|
|
35
|
-
* Specific style for the filled part. Overrides `barStyle`.
|
|
36
|
-
*/
|
|
37
|
-
fillStyle?: PrintStyle;
|
|
38
|
-
/**
|
|
39
|
-
* Specific style for the empty part. Overrides `barStyle`.
|
|
40
|
-
*/
|
|
41
|
-
emptyStyle?: PrintStyle;
|
|
42
|
-
/**
|
|
43
|
-
* Style for the percentage text.
|
|
44
|
-
* Defaults to `style`.
|
|
45
|
-
*/
|
|
46
|
-
percentageStyle?: PrintStyle;
|
|
47
|
-
/**
|
|
48
|
-
* Character to use for the start bracket. Defaults to `[`.
|
|
49
|
-
*/
|
|
50
|
-
startChar?: string;
|
|
51
|
-
/**
|
|
52
|
-
* Character to use for the end bracket. Defaults to `]`.
|
|
53
|
-
*/
|
|
54
|
-
endChar?: string;
|
|
55
|
-
/**
|
|
56
|
-
* Character to use for the filled part. Defaults to `█`.
|
|
57
|
-
*/
|
|
58
|
-
fillChar?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Character to use for the empty part. Defaults to `░`.
|
|
61
|
-
*/
|
|
62
|
-
emptyChar?: string;
|
|
63
|
-
/**
|
|
64
|
-
* Whether to show the percentage text. Defaults to `true`.
|
|
65
|
-
*/
|
|
66
|
-
showPercentage?: boolean;
|
|
67
|
-
/**
|
|
68
|
-
* Custom formatter for the percentage text.
|
|
69
|
-
*/
|
|
70
|
-
formatPercentage?: (progress: number) => string;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Creates a PrintLine representing a progress bar.
|
|
74
|
-
*/
|
|
75
|
-
export declare function createProgressBar(options: ProgressBarOptions): PrintLine;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a PrintLine representing a progress bar.
|
|
3
|
-
*/
|
|
4
|
-
export function createProgressBar(options) {
|
|
5
|
-
const { progress, width = 20, style, bracketStyle, startStyle, endStyle, barStyle, fillStyle, emptyStyle, percentageStyle, startChar = "[", endChar = "]", fillChar = "█", emptyChar = "░", showPercentage = true, formatPercentage } = options;
|
|
6
|
-
// Clamp progress
|
|
7
|
-
const p = Math.max(0, Math.min(1, progress));
|
|
8
|
-
// Calculate filled width
|
|
9
|
-
const filledWidth = Math.round(p * width);
|
|
10
|
-
const emptyWidth = width - filledWidth;
|
|
11
|
-
// Resolve styles (Defaults)
|
|
12
|
-
const resolvedBracketStyle = bracketStyle ?? style;
|
|
13
|
-
const resolvedStartStyle = startStyle ?? resolvedBracketStyle;
|
|
14
|
-
const resolvedEndStyle = endStyle ?? resolvedBracketStyle;
|
|
15
|
-
const resolvedBarStyle = barStyle ?? style;
|
|
16
|
-
const resolvedFillStyle = fillStyle ?? resolvedBarStyle;
|
|
17
|
-
const resolvedEmptyStyle = emptyStyle ?? resolvedBarStyle;
|
|
18
|
-
const resolvedPercentageStyle = percentageStyle ?? style;
|
|
19
|
-
const segments = [];
|
|
20
|
-
// Start Bracket
|
|
21
|
-
if (startChar) {
|
|
22
|
-
segments.push({ text: startChar, style: resolvedStartStyle });
|
|
23
|
-
}
|
|
24
|
-
// Filled Part
|
|
25
|
-
if (filledWidth > 0) {
|
|
26
|
-
segments.push({ text: fillChar.repeat(filledWidth), style: resolvedFillStyle });
|
|
27
|
-
}
|
|
28
|
-
// Empty Part
|
|
29
|
-
if (emptyWidth > 0) {
|
|
30
|
-
segments.push({ text: emptyChar.repeat(emptyWidth), style: resolvedEmptyStyle });
|
|
31
|
-
}
|
|
32
|
-
// End Bracket
|
|
33
|
-
if (endChar) {
|
|
34
|
-
segments.push({ text: endChar, style: resolvedEndStyle });
|
|
35
|
-
}
|
|
36
|
-
// Percentage
|
|
37
|
-
if (showPercentage) {
|
|
38
|
-
const percentageText = formatPercentage ? formatPercentage(p) : ` ${Math.round(p * 100)}%`;
|
|
39
|
-
segments.push({ text: percentageText, style: resolvedPercentageStyle });
|
|
40
|
-
}
|
|
41
|
-
return { segments };
|
|
42
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { expect, test, describe } from "bun:test";
|
|
2
|
-
import { createProgressBar } from "./progress";
|
|
3
|
-
function getText(line) {
|
|
4
|
-
return line.segments.map((s) => s.text).join("");
|
|
5
|
-
}
|
|
6
|
-
describe("createProgressBar", () => {
|
|
7
|
-
test("generates a default progress bar at 0%", () => {
|
|
8
|
-
const line = createProgressBar({ progress: 0 });
|
|
9
|
-
const text = getText(line);
|
|
10
|
-
// Default format: [░░░░░░░░░░░░░░░░░░░░] 0%
|
|
11
|
-
expect(text).toContain("[");
|
|
12
|
-
expect(text).toContain("]");
|
|
13
|
-
expect(text).toContain("0%");
|
|
14
|
-
expect(text).toContain("░");
|
|
15
|
-
});
|
|
16
|
-
test("generates a default progress bar at 50%", () => {
|
|
17
|
-
const line = createProgressBar({ progress: 0.5 });
|
|
18
|
-
const text = getText(line);
|
|
19
|
-
expect(text).toContain("50%");
|
|
20
|
-
expect(text).toContain("█");
|
|
21
|
-
expect(text).toContain("░");
|
|
22
|
-
});
|
|
23
|
-
test("generates a default progress bar at 100%", () => {
|
|
24
|
-
const line = createProgressBar({ progress: 1.0 });
|
|
25
|
-
const text = getText(line);
|
|
26
|
-
expect(text).toContain("100%");
|
|
27
|
-
// Should not contain empty char
|
|
28
|
-
// But wait, width default 20. 100% means 20 filled. 0 empty.
|
|
29
|
-
expect(text).not.toContain("░");
|
|
30
|
-
});
|
|
31
|
-
test("allows custom width", () => {
|
|
32
|
-
const width = 10;
|
|
33
|
-
const line = createProgressBar({ progress: 0.5, width });
|
|
34
|
-
const segments = line.segments;
|
|
35
|
-
const filled = segments.find((s) => s.text.includes("█"));
|
|
36
|
-
const empty = segments.find((s) => s.text.includes("░"));
|
|
37
|
-
// 50% of 10 is 5.
|
|
38
|
-
expect(filled?.text.length).toBe(5);
|
|
39
|
-
expect(empty?.text.length).toBe(5);
|
|
40
|
-
});
|
|
41
|
-
test("applies styles correctly", () => {
|
|
42
|
-
const line = createProgressBar({
|
|
43
|
-
progress: 0.5,
|
|
44
|
-
style: { color: "blue" },
|
|
45
|
-
bracketStyle: { color: "red" },
|
|
46
|
-
barStyle: { color: "green" },
|
|
47
|
-
percentageStyle: { color: "yellow" }
|
|
48
|
-
});
|
|
49
|
-
const start = line.segments.find((s) => s.text === "[");
|
|
50
|
-
const filled = line.segments.find((s) => s.text.includes("█"));
|
|
51
|
-
const end = line.segments.find((s) => s.text === "]");
|
|
52
|
-
const percentage = line.segments.find((s) => s.text.includes("%"));
|
|
53
|
-
expect(start?.style).toEqual({ color: "red" });
|
|
54
|
-
expect(filled?.style).toEqual({ color: "green" });
|
|
55
|
-
expect(end?.style).toEqual({ color: "red" });
|
|
56
|
-
expect(percentage?.style).toEqual({ color: "yellow" });
|
|
57
|
-
});
|
|
58
|
-
test("cascades styles (general style -> specific)", () => {
|
|
59
|
-
const line = createProgressBar({
|
|
60
|
-
progress: 0.5,
|
|
61
|
-
style: { color: "blue" }
|
|
62
|
-
});
|
|
63
|
-
line.segments.forEach((s) => {
|
|
64
|
-
if (s.text.trim().length > 0) {
|
|
65
|
-
expect(s.style).toEqual({ color: "blue" });
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
test("allows custom characters", () => {
|
|
70
|
-
const line = createProgressBar({
|
|
71
|
-
progress: 0.5,
|
|
72
|
-
startChar: "<",
|
|
73
|
-
endChar: ">",
|
|
74
|
-
fillChar: "=",
|
|
75
|
-
emptyChar: "-"
|
|
76
|
-
});
|
|
77
|
-
const text = getText(line);
|
|
78
|
-
expect(text).toContain("<");
|
|
79
|
-
expect(text).toContain(">");
|
|
80
|
-
expect(text).toContain("=");
|
|
81
|
-
expect(text).toContain("-");
|
|
82
|
-
});
|
|
83
|
-
test("hides percentage", () => {
|
|
84
|
-
const line = createProgressBar({
|
|
85
|
-
progress: 0.5,
|
|
86
|
-
showPercentage: false
|
|
87
|
-
});
|
|
88
|
-
const text = getText(line);
|
|
89
|
-
expect(text).not.toContain("%");
|
|
90
|
-
});
|
|
91
|
-
test("formats percentage custom", () => {
|
|
92
|
-
const line = createProgressBar({
|
|
93
|
-
progress: 0.5,
|
|
94
|
-
formatPercentage: (p) => `${p * 10}/10`
|
|
95
|
-
});
|
|
96
|
-
const text = getText(line);
|
|
97
|
-
expect(text).toContain("5/10");
|
|
98
|
-
});
|
|
99
|
-
test("clamping progress", () => {
|
|
100
|
-
const lineLow = createProgressBar({ progress: -0.1 });
|
|
101
|
-
expect(getText(lineLow)).toContain("0%");
|
|
102
|
-
const lineHigh = createProgressBar({ progress: 1.1 });
|
|
103
|
-
expect(getText(lineHigh)).toContain("100%");
|
|
104
|
-
});
|
|
105
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export type SpinnerFrames = string[];
|
|
2
|
-
export interface SpinnerOptions {
|
|
3
|
-
/**
|
|
4
|
-
* The array of frames to cycle through.
|
|
5
|
-
*/
|
|
6
|
-
frames: SpinnerFrames;
|
|
7
|
-
/**
|
|
8
|
-
* The interval in milliseconds between frames.
|
|
9
|
-
* Defaults to 80ms.
|
|
10
|
-
*/
|
|
11
|
-
interval?: number;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Common spinner frame presets.
|
|
15
|
-
*/
|
|
16
|
-
export declare const SPINNERS: {
|
|
17
|
-
dots: string[];
|
|
18
|
-
lines: string[];
|
|
19
|
-
arrows: string[];
|
|
20
|
-
circle: string[];
|
|
21
|
-
square: string[];
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* A stateful spinner that calculates the current frame based on elapsed time.
|
|
25
|
-
* Designed to be used within a render loop (e.g. Printer.print loop).
|
|
26
|
-
*/
|
|
27
|
-
export declare class Spinner {
|
|
28
|
-
private frames;
|
|
29
|
-
private interval;
|
|
30
|
-
private startTime;
|
|
31
|
-
constructor(options: SpinnerOptions);
|
|
32
|
-
/**
|
|
33
|
-
* Returns the current frame based on the elapsed time.
|
|
34
|
-
*/
|
|
35
|
-
getFrame(): string;
|
|
36
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Common spinner frame presets.
|
|
3
|
-
*/
|
|
4
|
-
export const SPINNERS = {
|
|
5
|
-
dots: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
|
|
6
|
-
lines: ["-", "\\", "|", "/"],
|
|
7
|
-
arrows: ["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"],
|
|
8
|
-
circle: ["◐", "◓", "◑", "◒"],
|
|
9
|
-
square: ["▖", "▘", "▝", "▗"]
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* A stateful spinner that calculates the current frame based on elapsed time.
|
|
13
|
-
* Designed to be used within a render loop (e.g. Printer.print loop).
|
|
14
|
-
*/
|
|
15
|
-
export class Spinner {
|
|
16
|
-
frames;
|
|
17
|
-
interval;
|
|
18
|
-
startTime;
|
|
19
|
-
constructor(options) {
|
|
20
|
-
this.frames = options.frames;
|
|
21
|
-
this.interval = options.interval ?? 80;
|
|
22
|
-
this.startTime = Date.now();
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Returns the current frame based on the elapsed time.
|
|
26
|
-
*/
|
|
27
|
-
getFrame() {
|
|
28
|
-
const now = Date.now();
|
|
29
|
-
const elapsed = now - this.startTime;
|
|
30
|
-
const index = Math.floor(elapsed / this.interval) % this.frames.length;
|
|
31
|
-
return this.frames[index];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { expect, test, describe, spyOn, afterEach, beforeEach } from "bun:test";
|
|
2
|
-
import { Spinner, SPINNERS } from "./spinner";
|
|
3
|
-
describe("Spinner Class", () => {
|
|
4
|
-
let now = 1000;
|
|
5
|
-
// Mock Date.now() to control time
|
|
6
|
-
const dateSpy = spyOn(Date, "now").mockImplementation(() => now);
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
now = 1000;
|
|
9
|
-
dateSpy.mockClear();
|
|
10
|
-
dateSpy.mockImplementation(() => now);
|
|
11
|
-
});
|
|
12
|
-
afterEach(() => {
|
|
13
|
-
dateSpy.mockClear();
|
|
14
|
-
});
|
|
15
|
-
test("initializes with correct defaults", () => {
|
|
16
|
-
const spinner = new Spinner({ frames: ["a", "b"] });
|
|
17
|
-
expect(spinner.getFrame()).toBe("a");
|
|
18
|
-
});
|
|
19
|
-
test("advances frames over time", () => {
|
|
20
|
-
const spinner = new Spinner({ frames: ["a", "b", "c"], interval: 100 });
|
|
21
|
-
// t=0 (1000)
|
|
22
|
-
expect(spinner.getFrame()).toBe("a");
|
|
23
|
-
// t=50 (1050) -> still frame 0
|
|
24
|
-
now = 1050;
|
|
25
|
-
expect(spinner.getFrame()).toBe("a");
|
|
26
|
-
// t=100 (1100) -> frame 1
|
|
27
|
-
now = 1100;
|
|
28
|
-
expect(spinner.getFrame()).toBe("b");
|
|
29
|
-
// t=200 (1200) -> frame 2
|
|
30
|
-
now = 1200;
|
|
31
|
-
expect(spinner.getFrame()).toBe("c");
|
|
32
|
-
// t=300 (1300) -> frame 0 (loop)
|
|
33
|
-
now = 1300;
|
|
34
|
-
expect(spinner.getFrame()).toBe("a");
|
|
35
|
-
});
|
|
36
|
-
test("uses custom interval", () => {
|
|
37
|
-
const spinner = new Spinner({ frames: ["a", "b"], interval: 50 });
|
|
38
|
-
// t=0
|
|
39
|
-
expect(spinner.getFrame()).toBe("a");
|
|
40
|
-
// t=50 -> frame 1
|
|
41
|
-
now = 1050;
|
|
42
|
-
expect(spinner.getFrame()).toBe("b");
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
describe("Spinner Presets", () => {
|
|
46
|
-
test("dots preset exists and has frames", () => {
|
|
47
|
-
expect(SPINNERS.dots).toBeDefined();
|
|
48
|
-
expect(SPINNERS.dots.length).toBeGreaterThan(0);
|
|
49
|
-
});
|
|
50
|
-
test("lines preset exists", () => {
|
|
51
|
-
expect(SPINNERS.lines).toBeDefined();
|
|
52
|
-
});
|
|
53
|
-
});
|
package/dist/core/layout.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { PrintLine, PrintStyle } from "./types";
|
|
2
|
-
import { Printer } from "./printer";
|
|
3
|
-
/**
|
|
4
|
-
* Merges multiple columns of PrintLines into a single layout.
|
|
5
|
-
* Ensures proper alignment by padding shorter lines.
|
|
6
|
-
*
|
|
7
|
-
* @param columns - Array of columns, where each column is an array of PrintLines.
|
|
8
|
-
* @param separator - String used to separate columns.
|
|
9
|
-
* @param defaultStyle - Style to apply to the separator and padding.
|
|
10
|
-
* @param widths - Optional fixed widths for each column.
|
|
11
|
-
* @returns A single array of PrintLines representing the merged output.
|
|
12
|
-
*/
|
|
13
|
-
export declare function mergeMultipleColumns(columns: PrintLine[][], separator?: string, defaultStyle?: PrintStyle, widths?: number[]): PrintLine[];
|
|
14
|
-
/**
|
|
15
|
-
* Prints multiple columns of styled content to the console.
|
|
16
|
-
* A convenience wrapper around `mergeMultipleColumns` and `Printer.print`.
|
|
17
|
-
*
|
|
18
|
-
* @param columns - Array of columns to print.
|
|
19
|
-
* @param options - Layout options (widths, separator, custom printer).
|
|
20
|
-
*/
|
|
21
|
-
export declare function printColumns(columns: PrintLine[][], options?: {
|
|
22
|
-
widths?: number[];
|
|
23
|
-
separator?: string;
|
|
24
|
-
printer?: Printer;
|
|
25
|
-
}): void;
|
package/dist/core/layout.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { computeMaxWidth, padLine } from "./utils";
|
|
2
|
-
import { Printer } from "./printer";
|
|
3
|
-
// -----------------
|
|
4
|
-
// Core Layout & Printing
|
|
5
|
-
// -----------------
|
|
6
|
-
/**
|
|
7
|
-
* Merges multiple columns of PrintLines into a single layout.
|
|
8
|
-
* Ensures proper alignment by padding shorter lines.
|
|
9
|
-
*
|
|
10
|
-
* @param columns - Array of columns, where each column is an array of PrintLines.
|
|
11
|
-
* @param separator - String used to separate columns.
|
|
12
|
-
* @param defaultStyle - Style to apply to the separator and padding.
|
|
13
|
-
* @param widths - Optional fixed widths for each column.
|
|
14
|
-
* @returns A single array of PrintLines representing the merged output.
|
|
15
|
-
*/
|
|
16
|
-
export function mergeMultipleColumns(columns, separator = " ", defaultStyle, widths) {
|
|
17
|
-
if (columns.length === 0)
|
|
18
|
-
return [];
|
|
19
|
-
const maxLines = Math.max(...columns.map((c) => c.length));
|
|
20
|
-
const colWidths = columns.map((col, i) => {
|
|
21
|
-
if (widths?.[i] !== undefined)
|
|
22
|
-
return widths[i];
|
|
23
|
-
return computeMaxWidth(col);
|
|
24
|
-
});
|
|
25
|
-
const output = [];
|
|
26
|
-
for (let i = 0; i < maxLines; i++) {
|
|
27
|
-
let segments = [];
|
|
28
|
-
for (let j = 0; j < columns.length; j++) {
|
|
29
|
-
const line = columns[j][i] || { segments: [] };
|
|
30
|
-
// If not the last column, pad it
|
|
31
|
-
if (j < columns.length - 1) {
|
|
32
|
-
const padded = padLine(line, colWidths[j], defaultStyle);
|
|
33
|
-
segments = [...segments, ...padded.segments, { text: separator, style: defaultStyle }];
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
// Last column, just add segments
|
|
37
|
-
segments = [...segments, ...line.segments];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
output.push({ segments });
|
|
41
|
-
}
|
|
42
|
-
return output;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Prints multiple columns of styled content to the console.
|
|
46
|
-
* A convenience wrapper around `mergeMultipleColumns` and `Printer.print`.
|
|
47
|
-
*
|
|
48
|
-
* @param columns - Array of columns to print.
|
|
49
|
-
* @param options - Layout options (widths, separator, custom printer).
|
|
50
|
-
*/
|
|
51
|
-
export function printColumns(columns, options = {}) {
|
|
52
|
-
const { widths, separator = " ", printer = new Printer() } = options;
|
|
53
|
-
const mergedLines = mergeMultipleColumns(columns, separator, undefined, widths);
|
|
54
|
-
printer.print({ lines: mergedLines });
|
|
55
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/core/layout.test.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { expect, test, describe, spyOn, afterEach } from "bun:test";
|
|
2
|
-
import { mergeMultipleColumns, printColumns } from "./layout";
|
|
3
|
-
import { getLineLength } from "./utils";
|
|
4
|
-
const lineA = { segments: [{ text: "Hello" }] };
|
|
5
|
-
const lineB = { segments: [{ text: "World!!" }] };
|
|
6
|
-
describe("Layout Utilities", () => {
|
|
7
|
-
const stdoutSpy = spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
stdoutSpy.mockClear();
|
|
10
|
-
});
|
|
11
|
-
test("mergeMultipleColumns handles asymmetric column lengths", () => {
|
|
12
|
-
// Column 1: [lineA]
|
|
13
|
-
// Column 2: [lineB, lineB]
|
|
14
|
-
// Separator: " | "
|
|
15
|
-
// Default Style: undefined
|
|
16
|
-
// Widths: [10] (first col width 10)
|
|
17
|
-
const merged = mergeMultipleColumns([[lineA], [lineB, lineB]], " | ", undefined, [10]);
|
|
18
|
-
expect(merged.length).toBe(2);
|
|
19
|
-
// Second merged line:
|
|
20
|
-
// Col 1 is empty (pad to 10) + " | " + Col 2 (lineB, length 7)
|
|
21
|
-
// 10 + 3 + 7 = 20
|
|
22
|
-
expect(getLineLength(merged[1])).toBe(20);
|
|
23
|
-
});
|
|
24
|
-
test("printColumns executes correctly", () => {
|
|
25
|
-
printColumns([[lineA], [lineB]]);
|
|
26
|
-
expect(stdoutSpy).toHaveBeenCalled();
|
|
27
|
-
const output = stdoutSpy.mock.calls[0][0];
|
|
28
|
-
expect(output).toContain("Hello");
|
|
29
|
-
expect(output).toContain("World!!");
|
|
30
|
-
});
|
|
31
|
-
test("printColumns handles undefined style in segments", () => {
|
|
32
|
-
const lineNoStyle = { segments: [{ text: "NoStyle" }] };
|
|
33
|
-
printColumns([[lineNoStyle]]);
|
|
34
|
-
expect(stdoutSpy).toHaveBeenCalled();
|
|
35
|
-
const output = stdoutSpy.mock.calls[0][0];
|
|
36
|
-
expect(output).toContain("NoStyle");
|
|
37
|
-
});
|
|
38
|
-
test("printColumns handles empty columns", () => {
|
|
39
|
-
printColumns([]);
|
|
40
|
-
// Should just print nothing or minimal output (if logic handles empty array gracefully)
|
|
41
|
-
// mergeMultipleColumns returns []
|
|
42
|
-
// printer.print({ lines: [] }) -> might output clear sequence or empty string
|
|
43
|
-
expect(stdoutSpy).toHaveBeenCalled();
|
|
44
|
-
});
|
|
45
|
-
test("printColumns handles 3 columns", () => {
|
|
46
|
-
printColumns([[lineA], [lineA], [lineB]]);
|
|
47
|
-
expect(stdoutSpy).toHaveBeenCalled();
|
|
48
|
-
const output = stdoutSpy.mock.calls[0][0];
|
|
49
|
-
expect(output).toContain("Hello");
|
|
50
|
-
expect(output).toContain("World!!");
|
|
51
|
-
});
|
|
52
|
-
});
|
package/dist/core/printer.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { PrintBlock, PrinterOptions } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* Handles rendering PrintBlocks to the terminal with support for interactive/live overwriting.
|
|
4
|
-
*/
|
|
5
|
-
export declare class Printer {
|
|
6
|
-
private linesRendered;
|
|
7
|
-
private isLive;
|
|
8
|
-
private data?;
|
|
9
|
-
constructor(options?: PrinterOptions);
|
|
10
|
-
/**
|
|
11
|
-
* Generates the clear sequence to move cursor and clear previously rendered lines.
|
|
12
|
-
*/
|
|
13
|
-
private getClearSequence;
|
|
14
|
-
/**
|
|
15
|
-
* Clears the console using the stored line count.
|
|
16
|
-
*/
|
|
17
|
-
clear(): void;
|
|
18
|
-
/**
|
|
19
|
-
* Renders the PrintBlock to the standard output.
|
|
20
|
-
* If data is provided, updates the internal state.
|
|
21
|
-
*
|
|
22
|
-
* @param data - Optional data to update the printer with.
|
|
23
|
-
*/
|
|
24
|
-
print(data?: PrintBlock): void;
|
|
25
|
-
/**
|
|
26
|
-
* Resolves the block's vertical gradient (if any) to a solid color for the specific line.
|
|
27
|
-
*/
|
|
28
|
-
private resolveBlockColorForLine;
|
|
29
|
-
/**
|
|
30
|
-
* Renders a single line.
|
|
31
|
-
*/
|
|
32
|
-
private renderLine;
|
|
33
|
-
}
|