@heinrichb/console-toolkit 1.0.10 → 1.0.13

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
@@ -1,162 +1,320 @@
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
+ # 🎨 Console Toolkit
2
+
3
+ **@heinrichb/console-toolkit** zero-dependency TypeScript library for beautiful console output. True-color gradients, background colors, column layouts, tables, progress bars, spinners, and ASCII art all fully typed, all composable.
4
+
5
+ ---
6
+
7
+ ## 🚀 Features
8
+
9
+ - **🌈 True-Color Styling:** Standard ANSI colors, hex RGB codes, background colors, and text modifiers (bold, dim, italic, etc.).
10
+ - **✨ Gradients:** Horizontal (per-character) and vertical (per-line) gradients with multi-stop support. Includes preset palettes.
11
+ - **📊 Tables:** Styled tables with automatic column sizing, four border styles, and per-section styling.
12
+ - **📈 Live Updates:** Built-in live mode for smooth animations spinners, progress bars, dashboards.
13
+ - **📐 Column Layouts:** Multi-column grid system with automatic padding, fixed widths, and custom separators.
14
+ - **🧩 Components:** Progress bars (17 options) and spinners (5 presets) ready to use.
15
+ - **🐉 ASCII Presets:** Dragon art with customizable multi-stop gradient colors.
16
+ - **🔧 Utilities:** `renderToString` for capturing output and `stripAnsi` for plain text extraction.
17
+ - **TypeScript First:** Fully typed, zero dependencies, works with Bun and Node.js 18+.
18
+
19
+ ---
20
+
21
+ ## 📦 Installation
22
+
23
+ ```bash
24
+ bun add @heinrichb/console-toolkit
25
+ # or
26
+ npm install @heinrichb/console-toolkit
27
+ ```
28
+
29
+ ---
30
+
31
+ ## 🎮 Try the Demo
32
+
33
+ See every feature in action:
34
+
35
+ ```bash
36
+ git clone https://github.com/heinrichb/console-toolkit.git
37
+ cd console-toolkit && bun install
38
+ bun run demo
39
+ ```
40
+
41
+ ---
42
+
43
+ ## ⚡ Quick Start
44
+
45
+ Build output with three functions: `segment` (text + style), `line` (row of segments), `block` (group of lines).
46
+
47
+ ```typescript
48
+ import { Printer, segment, line, block } from "@heinrichb/console-toolkit";
49
+
50
+ const printer = new Printer();
51
+
52
+ printer.print(
53
+ block([
54
+ line([
55
+ segment("Hello, ", { color: "blue", modifiers: ["bold"] }),
56
+ segment("World!", { color: "#10B981", modifiers: ["italic"] })
57
+ ])
58
+ ])
59
+ );
60
+ ```
61
+
62
+ ---
63
+
64
+ ## 🎨 Styling
65
+
66
+ Every style accepts `color`, `bgColor`, and `modifiers`. Colors can be standard names or hex strings. Gradients are just arrays of colors.
67
+
68
+ ### Colors and Modifiers
69
+
70
+ ```typescript
71
+ import { Printer, segment, line, block } from "@heinrichb/console-toolkit";
72
+
73
+ const printer = new Printer();
74
+
75
+ printer.print(
76
+ block([
77
+ line([segment("Bold red text", { color: "red", modifiers: ["bold"] })]),
78
+ line([segment("Hex color", { color: "#FF6B35" })]),
79
+ line([segment("White on blue", { color: "white", bgColor: "#3B82F6" })]),
80
+ line([segment("Bold on purple", { color: "white", bgColor: "#7C3AED", modifiers: ["bold"] })])
81
+ ])
82
+ );
83
+ ```
84
+
85
+ **Standard colors:** `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `gray`, `grey`
86
+
87
+ **Modifiers:** `bold`, `dim`, `italic`, `underline`, `inverse`, `hidden`, `strikethrough`
88
+
89
+ ### Background Colors
90
+
91
+ Background colors support everything foreground colors do — solid colors, hex codes, and gradient arrays:
92
+
93
+ ```typescript
94
+ import { Printer, segment, line, block, GRADIENTS } from "@heinrichb/console-toolkit";
95
+
96
+ const printer = new Printer();
97
+
98
+ printer.print(block([line([segment(" Ocean background gradient ", { color: "white", bgColor: GRADIENTS.ocean })])]));
99
+ ```
100
+
101
+ ---
102
+
103
+ ## ✨ Gradients
104
+
105
+ ### Horizontal Gradients
106
+
107
+ Apply a color array to a line or segment for per-character interpolation:
108
+
109
+ ```typescript
110
+ import { Printer, segment, line, block, GRADIENTS } from "@heinrichb/console-toolkit";
111
+
112
+ const printer = new Printer();
113
+
114
+ // Line-level gradient spans all segments
115
+ printer.print(block([line([segment("Rainbow across the entire line")], { color: GRADIENTS.rainbow })]));
116
+
117
+ // Segment-level gradient affects only that segment
118
+ printer.print(block([line([segment("Normal text "), segment("gradient here", { color: GRADIENTS.sunset })])]));
119
+ ```
120
+
121
+ ### Vertical Gradients
122
+
123
+ Apply a color array to a block for per-line interpolation:
124
+
125
+ ```typescript
126
+ import { Printer, segment, line, block, GRADIENTS } from "@heinrichb/console-toolkit";
127
+
128
+ const printer = new Printer();
129
+
130
+ const lines = Array.from({ length: 6 }, (_, i) => line([segment(` Line ${i + 1} `)]));
131
+ printer.print(block(lines, { color: GRADIENTS.fire }));
132
+ ```
133
+
134
+ ### Gradient Presets (`GRADIENTS`)
135
+
136
+ Ready-made color arrays you can plug into any `color` or `bgColor` property:
137
+
138
+ | Preset | Colors |
139
+ | :----------- | :-------------------------------------- |
140
+ | `rainbow` | Red, amber, emerald, cyan, blue, violet |
141
+ | `ocean` | Deep navy, teal, cyan, light cyan |
142
+ | `fire` | Dark red, red, amber, yellow |
143
+ | `sunset` | Violet, pink, orange, amber |
144
+ | `forest` | Dark emerald, emerald, lime, light lime |
145
+ | `monochrome` | Black, gray, white |
146
+
147
+ ### Custom Gradient Interpolation
148
+
149
+ Use `interpolateGradient` to compute a color at any point in a gradient:
150
+
151
+ ```typescript
152
+ import { interpolateGradient } from "@heinrichb/console-toolkit";
153
+
154
+ const color = interpolateGradient(["#EF4444", "#F59E0B", "#10B981"], 0.5);
155
+ // Returns the hex color at 50% through the gradient
156
+ ```
157
+
158
+ ---
159
+
160
+ ## 📐 Layouts
161
+
162
+ ### `printColumns`
163
+
164
+ Print multiple columns side-by-side with automatic padding:
165
+
166
+ ```typescript
167
+ import { printColumns, line, segment } from "@heinrichb/console-toolkit";
168
+
169
+ const labels = [
170
+ line([segment("Name:", { color: "#A78BFA" })]),
171
+ line([segment("Version:", { color: "#A78BFA" })]),
172
+ line([segment("Status:", { color: "#A78BFA" })])
173
+ ];
174
+ const values = [
175
+ line([segment("console-toolkit", { color: "#60A5FA" })]),
176
+ line([segment("1.0.10", { color: "#34D399" })]),
177
+ line([segment("Operational", { color: "#FBBF24" })])
178
+ ];
179
+
180
+ printColumns([labels, values], {
181
+ separator: " => ",
182
+ widths: [10, 20]
183
+ });
184
+ ```
185
+
186
+ ### `mergeColumns`
187
+
188
+ Returns `PrintLine[]` instead of printing — useful for composing into larger layouts:
189
+
190
+ ```typescript
191
+ import { mergeColumns, Printer, block, line, segment } from "@heinrichb/console-toolkit";
192
+
193
+ const merged = mergeColumns([[line([segment("Left")])], [line([segment("Right")])]], " | ");
194
+
195
+ new Printer().print(block(merged));
196
+ ```
197
+
198
+ ---
199
+
200
+ ## 🧩 Components
201
+
202
+ ### Progress Bars
203
+
204
+ Fully customizable progress bars with 17 configuration options:
205
+
206
+ ```typescript
207
+ import { createProgressBar, Printer, block } from "@heinrichb/console-toolkit";
208
+
209
+ const printer = new Printer({ live: true });
210
+
211
+ for (let i = 0; i <= 100; i += 2) {
212
+ const bar = createProgressBar({
213
+ progress: i / 100,
214
+ width: 40,
215
+ fillStyle: { color: ["#3B82F6", "#EC4899"] },
216
+ emptyStyle: { color: "#4B5563" }
217
+ });
218
+ printer.print(block([bar]));
219
+ await new Promise((r) => setTimeout(r, 25));
220
+ }
221
+ ```
222
+
223
+ ### Spinners
224
+
225
+ Time-based spinners with 5 built-in presets:
226
+
227
+ ```typescript
228
+ import { Spinner, SPINNERS, Printer, line, segment, block } from "@heinrichb/console-toolkit";
229
+
230
+ const spinner = new Spinner({ frames: SPINNERS.dots });
231
+ const printer = new Printer({ live: true });
232
+
233
+ // In your render loop:
234
+ printer.print(block([line([segment(spinner.getFrame(), { color: "cyan" }), segment(" Loading...")])]));
235
+ ```
236
+
237
+ ### Tables
238
+
239
+ Styled tables with automatic column sizing and four border styles (`single`, `double`, `rounded`, `none`):
240
+
241
+ ```typescript
242
+ import { createTable, Printer, block } from "@heinrichb/console-toolkit";
243
+
244
+ const printer = new Printer();
245
+
246
+ const tableLines = createTable({
247
+ headers: ["Feature", "Status", "Since"],
248
+ rows: [
249
+ ["Colors & Hex", "Stable", "v1.0"],
250
+ ["Gradients", "Stable", "v1.0"],
251
+ ["Background Colors", "New", "v1.1"],
252
+ ["Tables", "New", "v1.1"]
253
+ ],
254
+ headerStyle: { color: "cyan", modifiers: ["bold"] },
255
+ style: { color: "white" },
256
+ borderStyle: { color: "#6B7280" },
257
+ border: "rounded"
258
+ });
259
+
260
+ printer.print(block(tableLines));
261
+ ```
262
+
263
+ ---
264
+
265
+ ## 🐉 Presets
266
+
267
+ ### Dragon ASCII Art
268
+
269
+ Vertical gradient dragon art — pass a `Color[]` array for the gradient stops:
270
+
271
+ ```typescript
272
+ import { getDragon, Printer, block, GRADIENTS } from "@heinrichb/console-toolkit";
273
+
274
+ const printer = new Printer();
275
+
276
+ printer.print(block(getDragon())); // Default: red -> amber
277
+ printer.print(block(getDragon(["#3B82F6", "#06B6D4"]))); // Two-color
278
+ printer.print(block(getDragon(GRADIENTS.fire))); // Multi-stop preset
279
+ ```
280
+
281
+ ---
282
+
283
+ ## 🔧 Utilities
284
+
285
+ ### `renderToString`
286
+
287
+ Capture styled output as a string instead of writing to stdout:
288
+
289
+ ```typescript
290
+ import { Printer, segment, line, block } from "@heinrichb/console-toolkit";
291
+
292
+ const printer = new Printer();
293
+ const output = printer.renderToString(block([line([segment("Hello!", { color: "green", modifiers: ["bold"] })])]));
294
+ // output contains the full ANSI-styled string
295
+ ```
296
+
297
+ ### `stripAnsi`
298
+
299
+ Remove all ANSI escape sequences from a string:
300
+
301
+ ```typescript
302
+ import { stripAnsi } from "@heinrichb/console-toolkit";
303
+
304
+ const plain = stripAnsi("\x1b[38;2;255;0;0mRed text\x1b[0m");
305
+ // Returns "Red text"
306
+ ```
307
+
308
+ ---
309
+
310
+ ## 📚 Detailed Documentation
311
+
312
+ - **[Core Engine & Styling](https://github.com/heinrichb/console-toolkit/blob/main/src/core/README.md):** Printer class, data structures, styling system, gradients, layout utilities.
313
+ - **[Components](https://github.com/heinrichb/console-toolkit/blob/main/src/components/README.md):** Full API reference for progress bars, spinners, and tables.
314
+ - **[Presets](https://github.com/heinrichb/console-toolkit/blob/main/src/presets/README.md):** Dragon art, gradient presets, and usage examples.
315
+
316
+ ---
317
+
318
+ ## 📄 License
319
+
320
+ MIT © Brennen Heinrich
@@ -35,6 +35,16 @@ export interface ProgressBarOptions {
35
35
  * Specific style for the filled part. Overrides `barStyle`.
36
36
  */
37
37
  fillStyle?: PrintStyle;
38
+ /**
39
+ * Style to apply to the filled part when progress reaches 100%.
40
+ * Overrides `fillStyle`.
41
+ */
42
+ completeStyle?: PrintStyle;
43
+ /**
44
+ * Character to use for the filled part when progress reaches 100%.
45
+ * Overrides `fillChar`.
46
+ */
47
+ completeChar?: string;
38
48
  /**
39
49
  * Specific style for the empty part. Overrides `barStyle`.
40
50
  */
@@ -0,0 +1,31 @@
1
+ import { PrintLine, PrintStyle } from "../core/types";
2
+ /**
3
+ * Border drawing style for tables.
4
+ */
5
+ export type BorderStyle = "single" | "double" | "rounded" | "none";
6
+ /**
7
+ * Configuration options for creating a styled table.
8
+ */
9
+ export interface TableOptions {
10
+ /** Column headers. If omitted, no header row is rendered. */
11
+ headers?: string[];
12
+ /** Row data. Each row is an array of cell strings. */
13
+ rows: string[][];
14
+ /** Base style for all table cell content. */
15
+ style?: PrintStyle;
16
+ /** Style override for header cells. */
17
+ headerStyle?: PrintStyle;
18
+ /** Style for border characters. */
19
+ borderStyle?: PrintStyle;
20
+ /** Border drawing style. Defaults to "single". */
21
+ border?: BorderStyle;
22
+ /** Fixed column widths (content area, excluding padding). Auto-computed from content if omitted. */
23
+ columnWidths?: number[];
24
+ /** Padding inside each cell (spaces on each side). Defaults to 1. */
25
+ cellPadding?: number;
26
+ }
27
+ /**
28
+ * Creates a styled table as an array of PrintLines.
29
+ * Composable with Printer: `printer.print(block(createTable(options)))`.
30
+ */
31
+ export declare function createTable(options: TableOptions): PrintLine[];
@@ -21,5 +21,6 @@ export declare function mergeColumns(columns: PrintLine[][], separator?: string,
21
21
  export declare function printColumns(columns: PrintLine[][], options?: {
22
22
  widths?: number[];
23
23
  separator?: string;
24
+ defaultStyle?: PrintStyle;
24
25
  printer?: Printer;
25
26
  }): void;
@@ -15,6 +15,14 @@ export declare class Printer {
15
15
  * Clears the console using the stored line count.
16
16
  */
17
17
  clear(): void;
18
+ /**
19
+ * Renders a PrintBlock to a string without writing to stdout or affecting live mode state.
20
+ * Useful for capturing output, testing, or composing before display.
21
+ *
22
+ * @param data - Optional data to update the printer with.
23
+ * @returns The rendered ANSI string.
24
+ */
25
+ renderToString(data?: PrintBlock): string;
18
26
  /**
19
27
  * Renders the PrintBlock to the standard output.
20
28
  * If data is provided, updates the internal state.
@@ -23,9 +31,17 @@ export declare class Printer {
23
31
  */
24
32
  print(data?: PrintBlock): void;
25
33
  /**
26
- * Resolves the block's vertical gradient (if any) to a solid color for the specific line.
34
+ * Shared rendering core builds the ANSI output string for a PrintBlock.
35
+ */
36
+ private renderBlock;
37
+ /**
38
+ * Resolves the block's vertical gradients (color and bgColor) to solid colors for a specific line.
39
+ */
40
+ private resolveBlockStyleForLine;
41
+ /**
42
+ * Resolves a single color/gradient value to a solid color for a specific line position.
27
43
  */
28
- private resolveBlockColorForLine;
44
+ private resolveGradientForLine;
29
45
  /**
30
46
  * Renders a single line.
31
47
  */
@@ -1,4 +1,5 @@
1
- import { Color, HexColor, PrintStyle, StyleModifier } from "./types";
1
+ import { Color, HexColor, PrintStyle, StyleModifier, RGB } from "./types";
2
+ export declare const ESC = "\u001B";
2
3
  /**
3
4
  * ANSI escape sequence to reset all styles.
4
5
  */
@@ -10,19 +11,23 @@ export declare function colorToHex(color: Color): string;
10
11
  /**
11
12
  * Converts a hex color string to an RGB object.
12
13
  */
13
- export declare function hexToRgb(hex: string): {
14
- r: number;
15
- g: number;
16
- b: number;
17
- };
14
+ export declare function hexToRgb(hex: string): RGB;
18
15
  /**
19
16
  * Converts RGB to a 24-bit ANSI foreground color escape sequence.
20
17
  */
21
18
  export declare function rgbToAnsi(r: number, g: number, b: number): string;
19
+ /**
20
+ * Converts RGB to a 24-bit ANSI background color escape sequence.
21
+ */
22
+ export declare function rgbToBgAnsi(r: number, g: number, b: number): string;
22
23
  /**
23
24
  * Converts any Color to an ANSI escape sequence.
24
25
  */
25
26
  export declare function resolveColorToAnsi(color: Color): string;
27
+ /**
28
+ * Resolves a Color (Standard or Hex) to an RGB object.
29
+ */
30
+ export declare function resolveColorToRgb(color: Color): RGB;
26
31
  /**
27
32
  * Converts a list of modifiers to an ANSI escape sequence.
28
33
  */
@@ -32,13 +37,20 @@ export declare function resolveModifiersToAnsi(modifiers?: StyleModifier[]): str
32
37
  */
33
38
  export declare function interpolateHex(color1: string, color2: string, factor: number): string;
34
39
  /**
35
- * Public interpolation function that accepts any Color type.
40
+ * Gets a foreground color from a multi-stop gradient as an ANSI escape sequence.
41
+ * Uses pre-resolved RGB colors for performance in per-character hot paths.
42
+ */
43
+ export declare function getGradientColorFromRgb(colors: RGB[], factor: number): string;
44
+ /**
45
+ * Gets a background color from a multi-stop gradient as an ANSI escape sequence.
46
+ * Uses pre-resolved RGB colors for performance in per-character hot paths.
36
47
  */
37
- export declare function interpolateColor(color1: Color, color2: Color, factor: number): HexColor;
48
+ export declare function getGradientBgColorFromRgb(colors: RGB[], factor: number): string;
38
49
  /**
39
- * logic to get a specific color from a multi-stop gradient array at a specific factor (0-1).
50
+ * Interpolates a multi-stop gradient at a given factor (0-1), returning a HexColor.
51
+ * Useful for computing vertical gradient colors per-line or for any custom gradient logic.
40
52
  */
41
- export declare function getGradientColor(colors: Color[], factor: number): string;
53
+ export declare function interpolateGradient(colors: Color[], factor: number): HexColor;
42
54
  /**
43
55
  * Merges a child style into a parent style.
44
56
  * - Modifiers are combined (union).