console-toolkit 1.2.9 → 1.2.11
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 +74 -29
- package/llms-full.txt +429 -0
- package/llms.txt +80 -0
- package/package.json +29 -7
- package/src/alphanumeric/arrows.d.ts +48 -0
- package/src/alphanumeric/arrows.js +23 -0
- package/src/alphanumeric/fractions.d.ts +65 -0
- package/src/alphanumeric/fractions.js +49 -0
- package/src/alphanumeric/number-formatters.d.ts +91 -0
- package/src/alphanumeric/number-formatters.js +45 -1
- package/src/alphanumeric/roman.d.ts +15 -0
- package/src/alphanumeric/roman.js +12 -0
- package/src/alphanumeric/unicode-cultural-numbers.d.ts +65 -0
- package/src/alphanumeric/unicode-cultural-numbers.js +1 -0
- package/src/alphanumeric/unicode-letters.d.ts +32 -0
- package/src/alphanumeric/unicode-letters.js +8 -0
- package/src/alphanumeric/unicode-numbers.d.ts +44 -0
- package/src/alphanumeric/unicode-numbers.js +21 -0
- package/src/alphanumeric/utils.d.ts +45 -0
- package/src/alphanumeric/utils.js +26 -0
- package/src/ansi/csi.d.ts +141 -0
- package/src/ansi/csi.js +51 -2
- package/src/ansi/index.d.ts +26 -0
- package/src/ansi/sgr-constants.d.ts +173 -0
- package/src/ansi/sgr-state.d.ts +91 -0
- package/src/ansi/sgr-state.js +45 -0
- package/src/ansi/sgr.d.ts +587 -0
- package/src/ansi/sgr.js +426 -6
- package/src/box.d.ts +160 -0
- package/src/box.js +113 -12
- package/src/charts/bars/block-frac-grouped.d.ts +12 -0
- package/src/charts/bars/block-frac-grouped.js +6 -0
- package/src/charts/bars/block-frac.d.ts +34 -0
- package/src/charts/bars/block-frac.js +13 -0
- package/src/charts/bars/block-grouped.d.ts +12 -0
- package/src/charts/bars/block-grouped.js +6 -0
- package/src/charts/bars/block.d.ts +43 -0
- package/src/charts/bars/block.js +13 -0
- package/src/charts/bars/draw-grouped.d.ts +46 -0
- package/src/charts/bars/draw-grouped.js +4 -0
- package/src/charts/bars/draw-stacked.d.ts +52 -0
- package/src/charts/bars/draw-stacked.js +4 -0
- package/src/charts/bars/frac-grouped.d.ts +32 -0
- package/src/charts/bars/frac-grouped.js +13 -0
- package/src/charts/bars/plain-grouped.d.ts +12 -0
- package/src/charts/bars/plain-grouped.js +6 -0
- package/src/charts/bars/plain.d.ts +85 -0
- package/src/charts/bars/plain.js +27 -0
- package/src/charts/columns/block-frac-grouped.d.ts +12 -0
- package/src/charts/columns/block-frac-grouped.js +6 -0
- package/src/charts/columns/block-frac.d.ts +39 -0
- package/src/charts/columns/block-frac.js +13 -0
- package/src/charts/columns/block-grouped.d.ts +12 -0
- package/src/charts/columns/block-grouped.js +6 -0
- package/src/charts/columns/block.d.ts +43 -0
- package/src/charts/columns/block.js +13 -0
- package/src/charts/columns/draw-grouped.d.ts +46 -0
- package/src/charts/columns/draw-grouped.js +4 -0
- package/src/charts/columns/draw-stacked.d.ts +44 -0
- package/src/charts/columns/draw-stacked.js +4 -0
- package/src/charts/columns/frac-grouped.d.ts +37 -0
- package/src/charts/columns/frac-grouped.js +13 -0
- package/src/charts/columns/plain-grouped.d.ts +12 -0
- package/src/charts/columns/plain-grouped.js +6 -0
- package/src/charts/columns/plain.d.ts +32 -0
- package/src/charts/columns/plain.js +13 -0
- package/src/charts/themes/default.d.ts +6 -0
- package/src/charts/themes/default.js +1 -0
- package/src/charts/themes/rainbow-reversed.d.ts +6 -0
- package/src/charts/themes/rainbow-reversed.js +2 -1
- package/src/charts/themes/rainbow.d.ts +6 -0
- package/src/charts/themes/rainbow.js +1 -0
- package/src/charts/utils.d.ts +79 -0
- package/src/charts/utils.js +32 -4
- package/src/draw-block-frac.d.ts +16 -0
- package/src/draw-block-frac.js +14 -0
- package/src/draw-block.d.ts +43 -0
- package/src/draw-block.js +25 -1
- package/src/meta.d.ts +84 -0
- package/src/meta.js +64 -0
- package/src/output/show.d.ts +55 -0
- package/src/output/show.js +28 -0
- package/src/output/updater.d.ts +114 -0
- package/src/output/updater.js +58 -4
- package/src/output/writer.d.ts +87 -0
- package/src/output/writer.js +57 -5
- package/src/panel.d.ts +402 -0
- package/src/panel.js +219 -5
- package/src/plot/bitmap.d.ts +80 -0
- package/src/plot/bitmap.js +33 -4
- package/src/plot/draw-line.d.ts +13 -0
- package/src/plot/draw-line.js +8 -0
- package/src/plot/draw-rect.d.ts +13 -0
- package/src/plot/draw-rect.js +38 -30
- package/src/plot/index.d.ts +39 -0
- package/src/plot/index.js +22 -0
- package/src/plot/to-quads.d.ts +10 -0
- package/src/plot/to-quads.js +5 -0
- package/src/spinner/index.d.ts +4 -0
- package/src/spinner/index.js +0 -2
- package/src/spinner/spin.d.ts +13 -0
- package/src/spinner/spin.js +13 -2
- package/src/spinner/spinner.d.ts +69 -0
- package/src/spinner/spinner.js +30 -2
- package/src/spinner/spinners.d.ts +34 -0
- package/src/spinner/spinners.js +23 -9
- package/src/strings/clip.d.ts +21 -0
- package/src/strings/clip.js +10 -0
- package/src/strings/parse.d.ts +23 -0
- package/src/strings/parse.js +7 -0
- package/src/strings/split.d.ts +38 -0
- package/src/strings/split.js +15 -0
- package/src/strings.d.ts +44 -0
- package/src/strings.js +34 -4
- package/src/style.d.ts +462 -0
- package/src/style.js +58 -4
- package/src/symbols.d.ts +167 -0
- package/src/symbols.js +91 -7
- package/src/table/draw-borders.d.ts +38 -0
- package/src/table/draw-borders.js +10 -2
- package/src/table/index.d.ts +8 -0
- package/src/table/index.js +1 -0
- package/src/table/table.d.ts +239 -0
- package/src/table/table.js +59 -1
- package/src/themes/blocks/unicode-half.d.ts +6 -0
- package/src/themes/blocks/unicode-half.js +1 -0
- package/src/themes/blocks/unicode-thin.d.ts +6 -0
- package/src/themes/blocks/unicode-thin.js +1 -0
- package/src/themes/lines/ascii-compact.d.ts +6 -0
- package/src/themes/lines/ascii-compact.js +1 -0
- package/src/themes/lines/ascii-dots.d.ts +6 -0
- package/src/themes/lines/ascii-dots.js +1 -0
- package/src/themes/lines/ascii-girder.d.ts +6 -0
- package/src/themes/lines/ascii-girder.js +1 -0
- package/src/themes/lines/ascii-github.d.ts +6 -0
- package/src/themes/lines/ascii-github.js +1 -0
- package/src/themes/lines/ascii-reddit.d.ts +6 -0
- package/src/themes/lines/ascii-reddit.js +1 -0
- package/src/themes/lines/ascii-rounded.d.ts +6 -0
- package/src/themes/lines/ascii-rounded.js +1 -0
- package/src/themes/lines/ascii.d.ts +6 -0
- package/src/themes/lines/ascii.js +1 -0
- package/src/themes/lines/unicode-bold.d.ts +6 -0
- package/src/themes/lines/unicode-bold.js +1 -0
- package/src/themes/lines/unicode-rounded.d.ts +6 -0
- package/src/themes/lines/unicode-rounded.js +1 -0
- package/src/themes/lines/unicode.d.ts +6 -0
- package/src/themes/lines/unicode.js +1 -0
- package/src/themes/utils.d.ts +33 -0
- package/src/themes/utils.js +7 -0
- package/src/turtle/draw-line-art.d.ts +19 -0
- package/src/turtle/draw-line-art.js +7 -0
- package/src/turtle/draw-unicode.d.ts +19 -0
- package/src/turtle/draw-unicode.js +8 -0
- package/src/turtle/index.d.ts +21 -0
- package/src/turtle/index.js +8 -0
- package/src/turtle/turtle.d.ts +269 -0
- package/src/turtle/turtle.js +124 -4
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import Box from '../box.js';
|
|
2
|
+
import Panel from '../panel.js';
|
|
3
|
+
import {SgrState} from '../ansi/sgr-state.js';
|
|
4
|
+
import {StringsInput} from '../strings.js';
|
|
5
|
+
import {LineTheme} from '../themes/utils.js';
|
|
6
|
+
|
|
7
|
+
/** Input type for a single table cell: a raw value, a CellData object, or null. */
|
|
8
|
+
export type TableCellInput = string | number | boolean | Box | Panel | CellData | null | undefined;
|
|
9
|
+
|
|
10
|
+
/** Data for a single table cell. */
|
|
11
|
+
export interface CellData {
|
|
12
|
+
/** Cell content (string, Box, Panel, or other renderable). */
|
|
13
|
+
value: StringsInput;
|
|
14
|
+
/** Horizontal alignment override for this cell. */
|
|
15
|
+
align?: string;
|
|
16
|
+
/** Column span (number of columns this cell occupies). */
|
|
17
|
+
width?: number;
|
|
18
|
+
/** Row span (number of rows this cell occupies). */
|
|
19
|
+
height?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Cell padding configuration. */
|
|
23
|
+
export interface CellPadding {
|
|
24
|
+
/** Left padding in columns. */
|
|
25
|
+
l?: number;
|
|
26
|
+
/** Right padding in columns. */
|
|
27
|
+
r?: number;
|
|
28
|
+
/** Top padding in rows. */
|
|
29
|
+
t?: number;
|
|
30
|
+
/** Bottom padding in rows. */
|
|
31
|
+
b?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Options for the Table constructor. */
|
|
35
|
+
export interface TableOptions {
|
|
36
|
+
/** Horizontal axis definition (border/separator pattern). */
|
|
37
|
+
hAxis?: string | (string | number)[];
|
|
38
|
+
/** Vertical axis definition (border/separator pattern). */
|
|
39
|
+
vAxis?: string | (string | number)[];
|
|
40
|
+
/** Horizontal alignment per column. */
|
|
41
|
+
hAlign?: string | string[];
|
|
42
|
+
/** Vertical alignment per row. */
|
|
43
|
+
vAlign?: string | string[];
|
|
44
|
+
/** Minimum column widths. */
|
|
45
|
+
hMin?: number | number[];
|
|
46
|
+
/** Minimum row heights. */
|
|
47
|
+
vMin?: number | number[];
|
|
48
|
+
/** Cell padding. */
|
|
49
|
+
cellPadding?: CellPadding;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Options for `Table.draw()`. */
|
|
53
|
+
export interface TableDrawOptions {
|
|
54
|
+
/** SGR state applied to table border lines. */
|
|
55
|
+
lineState?: SgrState;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Options for `Table.generateAxes()`. */
|
|
59
|
+
export interface GenerateAxesOptions {
|
|
60
|
+
/** Horizontal line theme name. */
|
|
61
|
+
hTheme?: string;
|
|
62
|
+
/** Vertical line theme name. */
|
|
63
|
+
vTheme?: string;
|
|
64
|
+
/** Top border style. */
|
|
65
|
+
borderTop?: string | number;
|
|
66
|
+
/** Right border style. */
|
|
67
|
+
borderRight?: string | number;
|
|
68
|
+
/** Left border style. */
|
|
69
|
+
borderLeft?: string | number;
|
|
70
|
+
/** Bottom border style. */
|
|
71
|
+
borderBottom?: string | number;
|
|
72
|
+
/** First row separator style. */
|
|
73
|
+
rowFirst?: string | number;
|
|
74
|
+
/** Last row separator style. */
|
|
75
|
+
rowLast?: string | number;
|
|
76
|
+
/** First column separator style. */
|
|
77
|
+
columnFirst?: string | number;
|
|
78
|
+
/** Last column separator style. */
|
|
79
|
+
columnLast?: string | number;
|
|
80
|
+
/** Horizontal data separator style. */
|
|
81
|
+
hDataSep?: string | number;
|
|
82
|
+
/** Vertical data separator style. */
|
|
83
|
+
vDataSep?: string | number;
|
|
84
|
+
|
|
85
|
+
/** Default horizontal alignment. */
|
|
86
|
+
hAlignDefault?: string;
|
|
87
|
+
/** Column indices aligned left. */
|
|
88
|
+
hLeft?: number[];
|
|
89
|
+
/** Column indices aligned center. */
|
|
90
|
+
hCenter?: number[];
|
|
91
|
+
/** Column indices aligned right. */
|
|
92
|
+
hRight?: number[];
|
|
93
|
+
|
|
94
|
+
/** Default vertical alignment. */
|
|
95
|
+
vAlignDefault?: string;
|
|
96
|
+
/** Row indices aligned top. */
|
|
97
|
+
vTop?: number[];
|
|
98
|
+
/** Row indices aligned center. */
|
|
99
|
+
vCenter?: number[];
|
|
100
|
+
/** Row indices aligned bottom. */
|
|
101
|
+
vBottom?: number[];
|
|
102
|
+
|
|
103
|
+
/** Default minimum column width. */
|
|
104
|
+
hMinDefault?: number;
|
|
105
|
+
/** Minimum widths per column index. */
|
|
106
|
+
hMin?: Record<number, number>;
|
|
107
|
+
|
|
108
|
+
/** Default minimum row height. */
|
|
109
|
+
vMinDefault?: number;
|
|
110
|
+
/** Minimum heights per row index. */
|
|
111
|
+
vMin?: Record<number, number>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** SGR styles applied to table data cells. */
|
|
115
|
+
export interface DataStyleOptions {
|
|
116
|
+
/** Style for the first row. */
|
|
117
|
+
rowFirst?: SgrState;
|
|
118
|
+
/** Style for the last row. */
|
|
119
|
+
rowLast?: SgrState;
|
|
120
|
+
/** Style for the first column. */
|
|
121
|
+
columnFirst?: SgrState;
|
|
122
|
+
/** Style for the last column. */
|
|
123
|
+
columnLast?: SgrState;
|
|
124
|
+
/** Default style for data cells. */
|
|
125
|
+
data?: SgrState;
|
|
126
|
+
/** Style for odd rows. */
|
|
127
|
+
rowOdd?: SgrState;
|
|
128
|
+
/** Style for even rows. */
|
|
129
|
+
rowEven?: SgrState;
|
|
130
|
+
/** Style for odd columns. */
|
|
131
|
+
columnOdd?: SgrState;
|
|
132
|
+
/** Style for even columns. */
|
|
133
|
+
columnEven?: SgrState;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Options for `Table.make()`, combining axis generation and data styling. */
|
|
137
|
+
export interface MakeOptions extends GenerateAxesOptions {
|
|
138
|
+
/** Data cell styles. */
|
|
139
|
+
states?: DataStyleOptions;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Renders tabular data with borders, alignment, and merged cells.
|
|
143
|
+
* @see {@link https://github.com/uhop/console-toolkit/wiki/Package:-table}
|
|
144
|
+
*/
|
|
145
|
+
export class Table {
|
|
146
|
+
/** Number of data rows. */
|
|
147
|
+
height: number;
|
|
148
|
+
/** Number of data columns. */
|
|
149
|
+
width: number;
|
|
150
|
+
/** The raw data grid. */
|
|
151
|
+
data: TableCellInput[][];
|
|
152
|
+
/** Computed column widths. */
|
|
153
|
+
widths: number[];
|
|
154
|
+
/** Computed row heights. */
|
|
155
|
+
heights: number[];
|
|
156
|
+
/** Line theme for borders. */
|
|
157
|
+
lineTheme: LineTheme;
|
|
158
|
+
/** List of cells to skip (merged cell regions). */
|
|
159
|
+
skipList: {x: number; y: number; width: number; height: number}[];
|
|
160
|
+
/** Resolved table options. */
|
|
161
|
+
options: {
|
|
162
|
+
hAxis: string | (string | number)[];
|
|
163
|
+
vAxis: string | (string | number)[];
|
|
164
|
+
hAlign: string[];
|
|
165
|
+
vAlign: string[];
|
|
166
|
+
};
|
|
167
|
+
/** Resolved cell padding. */
|
|
168
|
+
cellPadding: Required<CellPadding>;
|
|
169
|
+
/** Horizontal axis pattern. */
|
|
170
|
+
hAxis: (string | number)[];
|
|
171
|
+
/** Vertical axis pattern. */
|
|
172
|
+
vAxis: (string | number)[];
|
|
173
|
+
/** Processed cell grid. */
|
|
174
|
+
cells: (object | null)[][];
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param data - 2D array of cell data.
|
|
178
|
+
* @param lineTheme - Line theme for borders.
|
|
179
|
+
* @param options - Table options.
|
|
180
|
+
*/
|
|
181
|
+
constructor(data: TableCellInput[][], lineTheme: LineTheme, options?: TableOptions);
|
|
182
|
+
|
|
183
|
+
/** Draws the table as a Panel.
|
|
184
|
+
* @param options - Draw options.
|
|
185
|
+
* @returns A Panel.
|
|
186
|
+
*/
|
|
187
|
+
draw(options?: TableDrawOptions): Panel;
|
|
188
|
+
/** Draws the table as a Panel.
|
|
189
|
+
* @param options - Draw options.
|
|
190
|
+
* @returns A Panel.
|
|
191
|
+
*/
|
|
192
|
+
toPanel(options?: TableDrawOptions): Panel;
|
|
193
|
+
/** Draws the table as a Box.
|
|
194
|
+
* @param options - Draw options.
|
|
195
|
+
* @returns A Box.
|
|
196
|
+
*/
|
|
197
|
+
toBox(options?: TableDrawOptions): Box;
|
|
198
|
+
/** Draws the table as a string array.
|
|
199
|
+
* @param options - Draw options.
|
|
200
|
+
* @returns Array of strings.
|
|
201
|
+
*/
|
|
202
|
+
toStrings(options?: TableDrawOptions): string[];
|
|
203
|
+
|
|
204
|
+
/** Checks if a cell at (x, y) is visible (not hidden by a merged cell).
|
|
205
|
+
* @param x - Column index.
|
|
206
|
+
* @param y - Row index.
|
|
207
|
+
* @returns True if visible.
|
|
208
|
+
*/
|
|
209
|
+
isVisible(x: number, y: number): boolean;
|
|
210
|
+
|
|
211
|
+
/** Generates axis options from dimensions and configuration.
|
|
212
|
+
* @param width - Number of columns.
|
|
213
|
+
* @param height - Number of rows.
|
|
214
|
+
* @param options - Generation options.
|
|
215
|
+
* @returns Table options with axis definitions.
|
|
216
|
+
*/
|
|
217
|
+
static generateAxes(width: number, height: number, options: GenerateAxesOptions): TableOptions;
|
|
218
|
+
/** Processes data cells, applying styles.
|
|
219
|
+
* @param data - 2D array of cell data.
|
|
220
|
+
* @param options - Data style options.
|
|
221
|
+
* @returns Processed data grid.
|
|
222
|
+
*/
|
|
223
|
+
static processData(data: TableCellInput[][], options?: DataStyleOptions): TableCellInput[][];
|
|
224
|
+
/** Factory method that generates axes, processes data, and creates a Table.
|
|
225
|
+
* @param data - 2D array of cell data.
|
|
226
|
+
* @param lineTheme - Line theme for borders.
|
|
227
|
+
* @param options - Make options.
|
|
228
|
+
* @param overrides - Additional table option overrides.
|
|
229
|
+
* @returns A new Table.
|
|
230
|
+
*/
|
|
231
|
+
static make(
|
|
232
|
+
data: TableCellInput[][],
|
|
233
|
+
lineTheme: LineTheme,
|
|
234
|
+
options?: MakeOptions,
|
|
235
|
+
overrides?: Partial<TableOptions>
|
|
236
|
+
): Table;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export default Table;
|
package/src/table/table.js
CHANGED
|
@@ -29,7 +29,22 @@ const dataInstructions = 'rowFirst,rowLast,columnFirst,columnLast,data,rowOdd,ro
|
|
|
29
29
|
|
|
30
30
|
const DIM_STATE = style.dim.getState();
|
|
31
31
|
|
|
32
|
+
/** Creates and renders tables with themes, supporting cell alignment, merged cells, and border drawing.
|
|
33
|
+
* @see {@link https://github.com/uhop/console-toolkit/wiki/Package:-table}
|
|
34
|
+
*/
|
|
32
35
|
export class Table {
|
|
36
|
+
/**
|
|
37
|
+
* @param {any[][]} data - 2D array of cell data. Each cell can be a value, or `{value, align, width, height}`.
|
|
38
|
+
* @param {object} lineTheme - The line theme for borders.
|
|
39
|
+
* @param {object} [options] - Options.
|
|
40
|
+
* @param {string|(string|number)[]} [options.hAxis='1'] - Horizontal axis definition.
|
|
41
|
+
* @param {string|(string|number)[]} [options.vAxis='1'] - Vertical axis definition.
|
|
42
|
+
* @param {string|string[]} [options.hAlign=[]] - Horizontal alignment per column.
|
|
43
|
+
* @param {string|string[]} [options.vAlign=[]] - Vertical alignment per row.
|
|
44
|
+
* @param {number|number[]} [options.hMin=0] - Minimum column widths.
|
|
45
|
+
* @param {number|number[]} [options.vMin=0] - Minimum row heights.
|
|
46
|
+
* @param {{l?: number, r?: number, t?: number, b?: number}} [options.cellPadding={}] - Cell padding.
|
|
47
|
+
*/
|
|
33
48
|
constructor(data, lineTheme, options = {}) {
|
|
34
49
|
let {hAxis = '1', vAxis = '1', hAlign = [], vAlign = [], hMin = 0, vMin = 0, cellPadding = {}} = options;
|
|
35
50
|
|
|
@@ -76,7 +91,10 @@ export class Table {
|
|
|
76
91
|
const isObject = data?.hasOwnProperty('value'),
|
|
77
92
|
value = isObject ? data.value : data,
|
|
78
93
|
align = (isObject && data.align) || '',
|
|
79
|
-
box =
|
|
94
|
+
box =
|
|
95
|
+
value instanceof Box
|
|
96
|
+
? value
|
|
97
|
+
: Box.make(value, {symbol: ' ', align: getCellAlign(align, 0) || hAlign[j] || 'left'}),
|
|
80
98
|
width = box.width,
|
|
81
99
|
height = box.height,
|
|
82
100
|
cellWidth = isObject ? data.width || 1 : 1,
|
|
@@ -122,6 +140,11 @@ export class Table {
|
|
|
122
140
|
}
|
|
123
141
|
|
|
124
142
|
// TODO: accept `states`, draw even/odd rows/columns, support bg on lines optionally
|
|
143
|
+
/** Draws the table as a Panel with borders and cell content.
|
|
144
|
+
* @param {object} [options] - Options.
|
|
145
|
+
* @param {object} [options.lineState] - SGR state for border lines (defaults to dim).
|
|
146
|
+
* @returns {import('../panel.js').default} A Panel with the rendered table.
|
|
147
|
+
*/
|
|
125
148
|
draw({lineState = DIM_STATE} = {}) {
|
|
126
149
|
// prepare axes
|
|
127
150
|
|
|
@@ -177,18 +200,35 @@ export class Table {
|
|
|
177
200
|
return panel;
|
|
178
201
|
}
|
|
179
202
|
|
|
203
|
+
/** Converts the table to a Panel.
|
|
204
|
+
* @param {object} [options] - Options passed to `draw()`.
|
|
205
|
+
* @returns {import('../panel.js').default}
|
|
206
|
+
*/
|
|
180
207
|
toPanel(options) {
|
|
181
208
|
return this.draw(options);
|
|
182
209
|
}
|
|
183
210
|
|
|
211
|
+
/** Converts the table to a Box.
|
|
212
|
+
* @param {object} [options] - Options passed to `draw()`.
|
|
213
|
+
* @returns {import('../box.js').default}
|
|
214
|
+
*/
|
|
184
215
|
toBox(options) {
|
|
185
216
|
return this.toPanel(options).toBox(options);
|
|
186
217
|
}
|
|
187
218
|
|
|
219
|
+
/** Converts the table to an array of strings.
|
|
220
|
+
* @param {object} [options] - Options passed to `draw()`.
|
|
221
|
+
* @returns {string[]}
|
|
222
|
+
*/
|
|
188
223
|
toStrings(options) {
|
|
189
224
|
return this.toBox(options).toStrings();
|
|
190
225
|
}
|
|
191
226
|
|
|
227
|
+
/** Checks if a cell is visible (not hidden by a merged cell).
|
|
228
|
+
* @param {number} x - Column index.
|
|
229
|
+
* @param {number} y - Row index.
|
|
230
|
+
* @returns {boolean}
|
|
231
|
+
*/
|
|
192
232
|
isVisible(x, y) {
|
|
193
233
|
const i = 2 * y + 1,
|
|
194
234
|
j = 2 * x + 1;
|
|
@@ -199,6 +239,12 @@ export class Table {
|
|
|
199
239
|
return true;
|
|
200
240
|
}
|
|
201
241
|
|
|
242
|
+
/** Generates axis definitions and alignment arrays from a declarative options object.
|
|
243
|
+
* @param {number} width - Number of columns.
|
|
244
|
+
* @param {number} height - Number of rows.
|
|
245
|
+
* @param {object} options - Axis generation options.
|
|
246
|
+
* @returns {object} An object with `hAxis`, `vAxis`, `hAlign`, `vAlign`, `hMin`, `vMin`.
|
|
247
|
+
*/
|
|
202
248
|
static generateAxes(
|
|
203
249
|
width,
|
|
204
250
|
height,
|
|
@@ -278,6 +324,11 @@ export class Table {
|
|
|
278
324
|
return {hAxis, vAxis, hAlign, vAlign, hMin: hMinArray, vMin: vMinArray};
|
|
279
325
|
}
|
|
280
326
|
|
|
327
|
+
/** Processes data by applying styles to rows, columns, and special positions.
|
|
328
|
+
* @param {any[][]} data - The raw data.
|
|
329
|
+
* @param {object} [options] - Style options with properties like `rowFirst`, `rowLast`, `data`, `rowOdd`, etc.
|
|
330
|
+
* @returns {any[][]} The styled data.
|
|
331
|
+
*/
|
|
281
332
|
static processData(data, options) {
|
|
282
333
|
if (!options) return data;
|
|
283
334
|
|
|
@@ -322,6 +373,13 @@ export class Table {
|
|
|
322
373
|
});
|
|
323
374
|
}
|
|
324
375
|
|
|
376
|
+
/** Convenience factory that generates axes, processes data, and creates a Table.
|
|
377
|
+
* @param {any[][]} data - The raw data.
|
|
378
|
+
* @param {object} lineTheme - The line theme.
|
|
379
|
+
* @param {object} [options] - Combined axis generation and style options.
|
|
380
|
+
* @param {object} [overrides] - Additional overrides for the Table constructor options.
|
|
381
|
+
* @returns {Table}
|
|
382
|
+
*/
|
|
325
383
|
static make(data, lineTheme, options, overrides) {
|
|
326
384
|
return new Table(Table.processData(data, options?.states), lineTheme, {
|
|
327
385
|
...(options && Table.generateAxes(data.length && data[0].length, data.length, options)),
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Defines the characters used for table borders at various positions. */
|
|
2
|
+
export interface TableDefinition {
|
|
3
|
+
/** Top border characters. */
|
|
4
|
+
t: string;
|
|
5
|
+
/** Middle (row separator) border characters. */
|
|
6
|
+
m: string;
|
|
7
|
+
/** Bottom border characters. */
|
|
8
|
+
b: string;
|
|
9
|
+
/** Vertical border characters. */
|
|
10
|
+
v: string;
|
|
11
|
+
/** Horizontal border characters. */
|
|
12
|
+
h: string;
|
|
13
|
+
/** Character width (default: 1). */
|
|
14
|
+
w?: number;
|
|
15
|
+
/** Fill character. */
|
|
16
|
+
f?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A line theme mapping theme keys to border/line character definitions. */
|
|
20
|
+
export type LineTheme = Record<string, string | number | string[]>;
|
|
21
|
+
|
|
22
|
+
/** Populates a line theme with characters from a table definition.
|
|
23
|
+
* @param lineTheme - The theme object to populate.
|
|
24
|
+
* @param tableDefinition - The table border character definitions.
|
|
25
|
+
* @param hTheme - Horizontal theme identifier.
|
|
26
|
+
* @param vTheme - Vertical theme identifier.
|
|
27
|
+
*/
|
|
28
|
+
export function populateTheme(
|
|
29
|
+
lineTheme: LineTheme,
|
|
30
|
+
tableDefinition: TableDefinition,
|
|
31
|
+
hTheme: number | string,
|
|
32
|
+
vTheme: number | string
|
|
33
|
+
): void;
|
package/src/themes/utils.js
CHANGED
|
@@ -10,6 +10,13 @@
|
|
|
10
10
|
|
|
11
11
|
// The default value for 'w': 1
|
|
12
12
|
|
|
13
|
+
/** Populates a line theme object from a table definition matrix.
|
|
14
|
+
* Sets `t_`, `h_`, `v_`, `w_`, and `f` properties on the lineTheme.
|
|
15
|
+
* @param {object} lineTheme - The line theme object to populate.
|
|
16
|
+
* @param {object} tableDefinition - Definition with `t`, `m`, `b`, `v`, `h`, optional `w` and `f` properties.
|
|
17
|
+
* @param {string|number} hTheme - Horizontal theme identifier.
|
|
18
|
+
* @param {string|number} vTheme - Vertical theme identifier.
|
|
19
|
+
*/
|
|
13
20
|
export const populateTheme = (lineTheme, tableDefinition, hTheme, vTheme) => {
|
|
14
21
|
const w = tableDefinition.w || 1,
|
|
15
22
|
s = [0, w, w << 1],
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Box from '../box.js';
|
|
2
|
+
import Turtle from './turtle.js';
|
|
3
|
+
import {LineTheme} from '../themes/utils.js';
|
|
4
|
+
|
|
5
|
+
/** Options for `draw()`. */
|
|
6
|
+
export interface DrawLineArtOptions {
|
|
7
|
+
/** Character to use for ignored (empty) cells. */
|
|
8
|
+
ignore?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Draws a Turtle's path as line art using a line theme.
|
|
12
|
+
* @param turtle - The Turtle instance.
|
|
13
|
+
* @param lineTheme - Line theme object defining line characters.
|
|
14
|
+
* @param options - Draw options.
|
|
15
|
+
* @returns A Box containing the drawn line art.
|
|
16
|
+
*/
|
|
17
|
+
export function draw(turtle: Turtle, lineTheme: LineTheme, options?: DrawLineArtOptions): Box;
|
|
18
|
+
|
|
19
|
+
export default draw;
|
|
@@ -15,6 +15,13 @@ const getIndex = cell => {
|
|
|
15
15
|
return {skipFlag, hTheme, vTheme, hIndex, vIndex};
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
+
/** Draws a Turtle's path as line art using a line theme.
|
|
19
|
+
* @param {import('./turtle.js').Turtle} turtle - The turtle to draw.
|
|
20
|
+
* @param {object} lineTheme - The line theme defining border characters.
|
|
21
|
+
* @param {object} [options] - Options.
|
|
22
|
+
* @param {string} [options.ignore=' '] - Character for empty cells.
|
|
23
|
+
* @returns {import('../box.js').Box} A Box with the rendered line art.
|
|
24
|
+
*/
|
|
18
25
|
export const draw = (turtle, lineTheme, {ignore = ' '} = {}) =>
|
|
19
26
|
new Box(
|
|
20
27
|
turtle.cells.map(row =>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Box from '../box.js';
|
|
2
|
+
import Turtle from './turtle.js';
|
|
3
|
+
|
|
4
|
+
/** Options for `draw()`. */
|
|
5
|
+
export interface DrawUnicodeOptions {
|
|
6
|
+
/** Character to use for ignored (empty) cells. */
|
|
7
|
+
ignore?: string;
|
|
8
|
+
/** If true, use arc characters for corners instead of sharp corners. */
|
|
9
|
+
useArcs?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Draws a Turtle's path using Unicode box-drawing characters.
|
|
13
|
+
* @param turtle - The Turtle instance.
|
|
14
|
+
* @param options - Draw options.
|
|
15
|
+
* @returns A Box containing the drawn path.
|
|
16
|
+
*/
|
|
17
|
+
export function draw(turtle: Turtle, options?: DrawUnicodeOptions): Box;
|
|
18
|
+
|
|
19
|
+
export default draw;
|
|
@@ -15,6 +15,14 @@ const arcs = {4: '╰', 12: '╭', 28: '╯', 36: '╮'};
|
|
|
15
15
|
|
|
16
16
|
const getIndex = cell => (cell ? (((+cell.l || 0) * 3 + (+cell.d || 0)) * 3 + (+cell.r || 0)) * 3 + (+cell.u || 0) : 0);
|
|
17
17
|
|
|
18
|
+
/** Draws a Turtle's path using Unicode box-drawing characters.
|
|
19
|
+
* Supports themes 1 (thin) and 2 (thick), and optional arc characters for corners.
|
|
20
|
+
* @param {import('./turtle.js').Turtle} turtle - The turtle to draw.
|
|
21
|
+
* @param {object} [options] - Options.
|
|
22
|
+
* @param {string} [options.ignore=' '] - Character for empty cells.
|
|
23
|
+
* @param {boolean} [options.useArcs] - If true, use rounded arc characters for corners.
|
|
24
|
+
* @returns {import('../box.js').Box} A Box with the rendered Unicode drawing.
|
|
25
|
+
*/
|
|
18
26
|
export const draw = (turtle, {ignore = ' ', useArcs} = {}) =>
|
|
19
27
|
new Box(
|
|
20
28
|
turtle.cells.map(row =>
|