@heinrichb/console-toolkit 1.0.3 → 1.0.6

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 (37) hide show
  1. package/README.md +162 -2
  2. package/dist/{progress.d.ts → components/progress.d.ts} +12 -12
  3. package/dist/{progress.js → components/progress.js} +5 -6
  4. package/dist/{progress.test.js → components/progress.test.js} +14 -10
  5. package/dist/components/spinner.d.ts +36 -0
  6. package/dist/components/spinner.js +33 -0
  7. package/dist/components/spinner.test.js +53 -0
  8. package/dist/core/layout.d.ts +25 -0
  9. package/dist/core/layout.js +55 -0
  10. package/dist/core/layout.test.d.ts +1 -0
  11. package/dist/core/layout.test.js +52 -0
  12. package/dist/core/printer.d.ts +33 -0
  13. package/dist/core/printer.js +129 -0
  14. package/dist/core/printer.test.d.ts +1 -0
  15. package/dist/core/printer.test.js +124 -0
  16. package/dist/core/style.d.ts +53 -0
  17. package/dist/core/style.js +171 -0
  18. package/dist/core/style.test.d.ts +1 -0
  19. package/dist/core/style.test.js +108 -0
  20. package/dist/core/types.d.ts +72 -0
  21. package/dist/core/types.js +4 -0
  22. package/dist/core/utils.d.ts +25 -0
  23. package/dist/core/utils.js +36 -0
  24. package/dist/core/utils.test.d.ts +1 -0
  25. package/dist/core/utils.test.js +25 -0
  26. package/dist/demo.d.ts +0 -4
  27. package/dist/demo.js +104 -43
  28. package/dist/index.d.ts +8 -99
  29. package/dist/index.js +14 -234
  30. package/dist/presets/ascii.d.ts +5 -0
  31. package/dist/presets/ascii.js +35 -0
  32. package/dist/presets/ascii.test.d.ts +1 -0
  33. package/dist/presets/ascii.test.js +21 -0
  34. package/package.json +46 -44
  35. package/dist/index.test.js +0 -126
  36. /package/dist/{progress.test.d.ts → components/progress.test.d.ts} +0 -0
  37. /package/dist/{index.test.d.ts → components/spinner.test.d.ts} +0 -0
package/README.md CHANGED
@@ -1,2 +1,162 @@
1
- # console-toolkit
2
- A versatile TypeScript utility library for enhanced console logging, formatting, and layout management.
1
+ # 🎨 Console Toolkit
2
+
3
+ **@heinrichb/console-toolkit** is a powerful, lightweight TypeScript library for creating beautiful, interactive, and structured command-line interfaces. From simple colored text to complex multi-column layouts and live-updating progress bars, this toolkit has everything you need to elevate your CLI experience.
4
+
5
+ ---
6
+
7
+ ## 🚀 Features
8
+
9
+ - **🌈 Rich Styling:** Support for standard ANSI colors, true-color Hex codes, and text modifiers (bold, dim, italic, etc.).
10
+ - **✨ Gradients:** Easy-to-use linear gradients for text and backgrounds.
11
+ - **live-updating:** Built-in support for live-updating displays (perfect for spinners and progress bars).
12
+ - **📐 Flexible Layouts:** powerful grid system for multi-column layouts with automatic padding and alignment.
13
+ - **🧩 Components:** Pre-built, customizable components like **Progress Bars** and **Spinners**.
14
+ - **🐉 Presets:** Fun ASCII art presets (like dragons!) to spice up your output.
15
+ - **TypeScript First:** Fully typed for a great developer experience.
16
+
17
+ ---
18
+
19
+ ## 📦 Installation
20
+
21
+ This library is designed for use with **Bun** or **Node.js**.
22
+
23
+ ```bash
24
+ bun add @heinrichb/console-toolkit
25
+ # or
26
+ npm install @heinrichb/console-toolkit
27
+ ```
28
+
29
+ ---
30
+
31
+ ## ⚡ Quick Start
32
+
33
+ Get up and running in seconds:
34
+
35
+ ```typescript
36
+ import { Printer } from "@heinrichb/console-toolkit";
37
+
38
+ const printer = new Printer();
39
+
40
+ printer.print({
41
+ lines: [
42
+ {
43
+ segments: [
44
+ { text: "Hello, ", style: { color: "blue", modifiers: ["bold"] } },
45
+ { text: "World!", style: { color: "#10B981", modifiers: ["italic"] } } // Hex color support!
46
+ ]
47
+ }
48
+ ]
49
+ });
50
+ ```
51
+
52
+ ---
53
+
54
+ ## 🎨 Styling
55
+
56
+ We support a flexible styling system that works with both standard terminal colors and full RGB Hex codes.
57
+
58
+ ### Basic Colors & Modifiers
59
+
60
+ ```typescript
61
+ import { PrintStyle } from "@heinrichb/console-toolkit";
62
+
63
+ const myStyle: PrintStyle = {
64
+ color: "red", // Standard color
65
+ modifiers: ["bold", "underline"]
66
+ };
67
+ ```
68
+
69
+ ### Hex Colors & Gradients
70
+
71
+ You can use any hex color string. For gradients, simply provide an array of colors!
72
+
73
+ ```typescript
74
+ const gradientStyle: PrintStyle = {
75
+ // Creates a gradient from Red to Blue
76
+ color: ["#EF4444", "#3B82F6"]
77
+ };
78
+ ```
79
+
80
+ ---
81
+
82
+ ## 📐 Layouts
83
+
84
+ Creating multi-column layouts is a breeze.
85
+
86
+ ```typescript
87
+ import { printColumns } from "@heinrichb/console-toolkit";
88
+
89
+ printColumns(
90
+ [
91
+ [
92
+ // Column 1
93
+ { segments: [{ text: "Item 1" }] },
94
+ { segments: [{ text: "Item 2" }] }
95
+ ],
96
+ [
97
+ // Column 2
98
+ { segments: [{ text: "Description 1", style: { color: "gray" } }] },
99
+ { segments: [{ text: "Description 2", style: { color: "gray" } }] }
100
+ ]
101
+ ],
102
+ { separator: " | " }
103
+ );
104
+ ```
105
+
106
+ ---
107
+
108
+ ## 🧩 Components
109
+
110
+ ### Progress Bars
111
+
112
+ Create customizable progress bars with ease.
113
+
114
+ ```typescript
115
+ import { createProgressBar, Printer } from "@heinrichb/console-toolkit";
116
+
117
+ const printer = new Printer({ live: true });
118
+ const bar = createProgressBar({
119
+ progress: 0.75,
120
+ width: 30,
121
+ fillStyle: { color: "green" },
122
+ emptyStyle: { color: "gray" }
123
+ });
124
+
125
+ printer.print({ lines: [bar] });
126
+ ```
127
+
128
+ ### Spinners
129
+
130
+ Add activity indicators to your long-running tasks.
131
+
132
+ ```typescript
133
+ import { Spinner, SPINNERS, Printer } from "@heinrichb/console-toolkit";
134
+
135
+ const spinner = new Spinner({ frames: SPINNERS.dots });
136
+ const printer = new Printer({ live: true });
137
+
138
+ // In your loop:
139
+ printer.print({
140
+ lines: [
141
+ {
142
+ segments: [{ text: spinner.getFrame(), style: { color: "cyan" } }]
143
+ }
144
+ ]
145
+ });
146
+ ```
147
+
148
+ ---
149
+
150
+ ## 📚 Detailed Documentation
151
+
152
+ For more in-depth information on specific parts of the library, check out the detailed guides below:
153
+
154
+ - **[Core Engine & Styling](src/core/README.md):** Deep dive into `Printer`, `PrintBlock`, `PrintStyle`, and advanced layout techniques.
155
+ - **[Components](src/components/README.md):** Full API reference for `ProgressBar`, `Spinner`, and how to build your own components.
156
+ - **[Presets](src/presets/README.md):** Explore available ASCII art and other presets.
157
+
158
+ ---
159
+
160
+ ## 📄 License
161
+
162
+ MIT © Brennen Heinrich
@@ -1,4 +1,4 @@
1
- import { StyledLine, Style } from "./index";
1
+ import { PrintLine, PrintStyle } from "../core/types";
2
2
  export interface ProgressBarOptions {
3
3
  /**
4
4
  * Progress value between 0.0 and 1.0.
@@ -12,38 +12,38 @@ export interface ProgressBarOptions {
12
12
  /**
13
13
  * Base style for the entire progress bar.
14
14
  */
15
- style?: Style | Style[];
15
+ style?: PrintStyle;
16
16
  /**
17
17
  * Style for the brackets (start and end characters).
18
- * Defaults to `style` or gray.
18
+ * Defaults to `style`.
19
19
  */
20
- bracketStyle?: Style | Style[];
20
+ bracketStyle?: PrintStyle;
21
21
  /**
22
22
  * Specific style for the start bracket. Overrides `bracketStyle`.
23
23
  */
24
- startStyle?: Style | Style[];
24
+ startStyle?: PrintStyle;
25
25
  /**
26
26
  * Specific style for the end bracket. Overrides `bracketStyle`.
27
27
  */
28
- endStyle?: Style | Style[];
28
+ endStyle?: PrintStyle;
29
29
  /**
30
30
  * Style for the bar (filled and empty parts).
31
31
  * Defaults to `style`.
32
32
  */
33
- barStyle?: Style | Style[];
33
+ barStyle?: PrintStyle;
34
34
  /**
35
35
  * Specific style for the filled part. Overrides `barStyle`.
36
36
  */
37
- fillStyle?: Style | Style[];
37
+ fillStyle?: PrintStyle;
38
38
  /**
39
39
  * Specific style for the empty part. Overrides `barStyle`.
40
40
  */
41
- emptyStyle?: Style | Style[];
41
+ emptyStyle?: PrintStyle;
42
42
  /**
43
43
  * Style for the percentage text.
44
44
  * Defaults to `style`.
45
45
  */
46
- percentageStyle?: Style | Style[];
46
+ percentageStyle?: PrintStyle;
47
47
  /**
48
48
  * Character to use for the start bracket. Defaults to `[`.
49
49
  */
@@ -70,6 +70,6 @@ export interface ProgressBarOptions {
70
70
  formatPercentage?: (progress: number) => string;
71
71
  }
72
72
  /**
73
- * Creates a StyledLine representing a progress bar.
73
+ * Creates a PrintLine representing a progress bar.
74
74
  */
75
- export declare function createProgressBar(options: ProgressBarOptions): StyledLine;
75
+ export declare function createProgressBar(options: ProgressBarOptions): PrintLine;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Creates a StyledLine representing a progress bar.
2
+ * Creates a PrintLine representing a progress bar.
3
3
  */
4
4
  export function createProgressBar(options) {
5
5
  const { progress, width = 20, style, bracketStyle, startStyle, endStyle, barStyle, fillStyle, emptyStyle, percentageStyle, startChar = "[", endChar = "]", fillChar = "█", emptyChar = "░", showPercentage = true, formatPercentage } = options;
@@ -8,15 +8,14 @@ export function createProgressBar(options) {
8
8
  // Calculate filled width
9
9
  const filledWidth = Math.round(p * width);
10
10
  const emptyWidth = width - filledWidth;
11
- // Resolve styles
12
- const baseStyle = style ?? [];
13
- const resolvedBracketStyle = bracketStyle ?? baseStyle;
11
+ // Resolve styles (Defaults)
12
+ const resolvedBracketStyle = bracketStyle ?? style;
14
13
  const resolvedStartStyle = startStyle ?? resolvedBracketStyle;
15
14
  const resolvedEndStyle = endStyle ?? resolvedBracketStyle;
16
- const resolvedBarStyle = barStyle ?? baseStyle;
15
+ const resolvedBarStyle = barStyle ?? style;
17
16
  const resolvedFillStyle = fillStyle ?? resolvedBarStyle;
18
17
  const resolvedEmptyStyle = emptyStyle ?? resolvedBarStyle;
19
- const resolvedPercentageStyle = percentageStyle ?? baseStyle;
18
+ const resolvedPercentageStyle = percentageStyle ?? style;
20
19
  const segments = [];
21
20
  // Start Bracket
22
21
  if (startChar) {
@@ -7,6 +7,7 @@ describe("createProgressBar", () => {
7
7
  test("generates a default progress bar at 0%", () => {
8
8
  const line = createProgressBar({ progress: 0 });
9
9
  const text = getText(line);
10
+ // Default format: [░░░░░░░░░░░░░░░░░░░░] 0%
10
11
  expect(text).toContain("[");
11
12
  expect(text).toContain("]");
12
13
  expect(text).toContain("0%");
@@ -23,6 +24,8 @@ describe("createProgressBar", () => {
23
24
  const line = createProgressBar({ progress: 1.0 });
24
25
  const text = getText(line);
25
26
  expect(text).toContain("100%");
27
+ // Should not contain empty char
28
+ // But wait, width default 20. 100% means 20 filled. 0 empty.
26
29
  expect(text).not.toContain("░");
27
30
  });
28
31
  test("allows custom width", () => {
@@ -31,34 +34,35 @@ describe("createProgressBar", () => {
31
34
  const segments = line.segments;
32
35
  const filled = segments.find((s) => s.text.includes("█"));
33
36
  const empty = segments.find((s) => s.text.includes("░"));
37
+ // 50% of 10 is 5.
34
38
  expect(filled?.text.length).toBe(5);
35
39
  expect(empty?.text.length).toBe(5);
36
40
  });
37
41
  test("applies styles correctly", () => {
38
42
  const line = createProgressBar({
39
43
  progress: 0.5,
40
- style: "blue",
41
- bracketStyle: "red",
42
- barStyle: "green",
43
- percentageStyle: "yellow"
44
+ style: { color: "blue" },
45
+ bracketStyle: { color: "red" },
46
+ barStyle: { color: "green" },
47
+ percentageStyle: { color: "yellow" }
44
48
  });
45
49
  const start = line.segments.find((s) => s.text === "[");
46
50
  const filled = line.segments.find((s) => s.text.includes("█"));
47
51
  const end = line.segments.find((s) => s.text === "]");
48
52
  const percentage = line.segments.find((s) => s.text.includes("%"));
49
- expect(start?.style).toBe("red");
50
- expect(filled?.style).toBe("green");
51
- expect(end?.style).toBe("red");
52
- expect(percentage?.style).toBe("yellow");
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" });
53
57
  });
54
58
  test("cascades styles (general style -> specific)", () => {
55
59
  const line = createProgressBar({
56
60
  progress: 0.5,
57
- style: "blue"
61
+ style: { color: "blue" }
58
62
  });
59
63
  line.segments.forEach((s) => {
60
64
  if (s.text.trim().length > 0) {
61
- expect(s.style).toBe("blue");
65
+ expect(s.style).toEqual({ color: "blue" });
62
66
  }
63
67
  });
64
68
  });
@@ -0,0 +1,36 @@
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
+ }
@@ -0,0 +1,33 @@
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
+ }
@@ -0,0 +1,53 @@
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
+ });
@@ -0,0 +1,25 @@
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;
@@ -0,0 +1,55 @@
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
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,52 @@
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
+ });
@@ -0,0 +1,33 @@
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
+ }