console-toolkit 1.2.8 → 1.2.10

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 (156) hide show
  1. package/README.md +70 -25
  2. package/package.json +26 -6
  3. package/src/alphanumeric/arrows.d.ts +48 -0
  4. package/src/alphanumeric/arrows.js +23 -0
  5. package/src/alphanumeric/fractions.d.ts +65 -0
  6. package/src/alphanumeric/fractions.js +49 -0
  7. package/src/alphanumeric/number-formatters.d.ts +91 -0
  8. package/src/alphanumeric/number-formatters.js +45 -1
  9. package/src/alphanumeric/roman.d.ts +15 -0
  10. package/src/alphanumeric/roman.js +12 -0
  11. package/src/alphanumeric/unicode-cultural-numbers.d.ts +65 -0
  12. package/src/alphanumeric/unicode-cultural-numbers.js +1 -0
  13. package/src/alphanumeric/unicode-letters.d.ts +32 -0
  14. package/src/alphanumeric/unicode-letters.js +8 -0
  15. package/src/alphanumeric/unicode-numbers.d.ts +44 -0
  16. package/src/alphanumeric/unicode-numbers.js +21 -0
  17. package/src/alphanumeric/utils.d.ts +45 -0
  18. package/src/alphanumeric/utils.js +26 -0
  19. package/src/ansi/csi.d.ts +141 -0
  20. package/src/ansi/csi.js +51 -2
  21. package/src/ansi/index.d.ts +26 -0
  22. package/src/ansi/sgr-constants.d.ts +173 -0
  23. package/src/ansi/sgr-state.d.ts +91 -0
  24. package/src/ansi/sgr-state.js +45 -0
  25. package/src/ansi/sgr.d.ts +587 -0
  26. package/src/ansi/sgr.js +426 -6
  27. package/src/box.d.ts +160 -0
  28. package/src/box.js +113 -12
  29. package/src/charts/bars/block-frac-grouped.d.ts +12 -0
  30. package/src/charts/bars/block-frac-grouped.js +6 -0
  31. package/src/charts/bars/block-frac.d.ts +34 -0
  32. package/src/charts/bars/block-frac.js +13 -0
  33. package/src/charts/bars/block-grouped.d.ts +12 -0
  34. package/src/charts/bars/block-grouped.js +6 -0
  35. package/src/charts/bars/block.d.ts +43 -0
  36. package/src/charts/bars/block.js +13 -0
  37. package/src/charts/bars/draw-grouped.d.ts +41 -0
  38. package/src/charts/bars/draw-grouped.js +4 -0
  39. package/src/charts/bars/draw-stacked.d.ts +47 -0
  40. package/src/charts/bars/draw-stacked.js +4 -0
  41. package/src/charts/bars/frac-grouped.d.ts +32 -0
  42. package/src/charts/bars/frac-grouped.js +13 -0
  43. package/src/charts/bars/plain-grouped.d.ts +12 -0
  44. package/src/charts/bars/plain-grouped.js +6 -0
  45. package/src/charts/bars/plain.d.ts +75 -0
  46. package/src/charts/bars/plain.js +27 -0
  47. package/src/charts/columns/block-frac-grouped.d.ts +12 -0
  48. package/src/charts/columns/block-frac-grouped.js +6 -0
  49. package/src/charts/columns/block-frac.d.ts +39 -0
  50. package/src/charts/columns/block-frac.js +13 -0
  51. package/src/charts/columns/block-grouped.d.ts +12 -0
  52. package/src/charts/columns/block-grouped.js +6 -0
  53. package/src/charts/columns/block.d.ts +43 -0
  54. package/src/charts/columns/block.js +13 -0
  55. package/src/charts/columns/draw-grouped.d.ts +41 -0
  56. package/src/charts/columns/draw-grouped.js +4 -0
  57. package/src/charts/columns/draw-stacked.d.ts +39 -0
  58. package/src/charts/columns/draw-stacked.js +4 -0
  59. package/src/charts/columns/frac-grouped.d.ts +37 -0
  60. package/src/charts/columns/frac-grouped.js +13 -0
  61. package/src/charts/columns/plain-grouped.d.ts +12 -0
  62. package/src/charts/columns/plain-grouped.js +6 -0
  63. package/src/charts/columns/plain.d.ts +32 -0
  64. package/src/charts/columns/plain.js +13 -0
  65. package/src/charts/themes/default.d.ts +6 -0
  66. package/src/charts/themes/default.js +1 -0
  67. package/src/charts/themes/rainbow-reversed.d.ts +6 -0
  68. package/src/charts/themes/rainbow-reversed.js +2 -1
  69. package/src/charts/themes/rainbow.d.ts +6 -0
  70. package/src/charts/themes/rainbow.js +1 -0
  71. package/src/charts/utils.d.ts +79 -0
  72. package/src/charts/utils.js +32 -4
  73. package/src/draw-block-frac.d.ts +16 -0
  74. package/src/draw-block-frac.js +14 -0
  75. package/src/draw-block.d.ts +53 -0
  76. package/src/draw-block.js +25 -1
  77. package/src/meta.d.ts +84 -0
  78. package/src/meta.js +64 -0
  79. package/src/output/show.d.ts +55 -0
  80. package/src/output/show.js +28 -0
  81. package/src/output/updater.d.ts +114 -0
  82. package/src/output/updater.js +58 -4
  83. package/src/output/writer.d.ts +87 -0
  84. package/src/output/writer.js +57 -5
  85. package/src/panel.d.ts +402 -0
  86. package/src/panel.js +219 -5
  87. package/src/plot/bitmap.d.ts +80 -0
  88. package/src/plot/bitmap.js +33 -4
  89. package/src/plot/draw-line.d.ts +13 -0
  90. package/src/plot/draw-line.js +8 -0
  91. package/src/plot/draw-rect.d.ts +13 -0
  92. package/src/plot/draw-rect.js +38 -30
  93. package/src/plot/index.d.ts +39 -0
  94. package/src/plot/index.js +22 -0
  95. package/src/plot/to-quads.d.ts +10 -0
  96. package/src/plot/to-quads.js +5 -0
  97. package/src/spinner/index.d.ts +4 -0
  98. package/src/spinner/index.js +0 -2
  99. package/src/spinner/spin.d.ts +13 -0
  100. package/src/spinner/spin.js +13 -2
  101. package/src/spinner/spinner.d.ts +69 -0
  102. package/src/spinner/spinner.js +30 -2
  103. package/src/spinner/spinners.d.ts +34 -0
  104. package/src/spinner/spinners.js +23 -9
  105. package/src/strings/clip.d.ts +21 -0
  106. package/src/strings/clip.js +10 -0
  107. package/src/strings/parse.d.ts +23 -0
  108. package/src/strings/parse.js +7 -0
  109. package/src/strings/split.d.ts +38 -0
  110. package/src/strings/split.js +15 -0
  111. package/src/strings.d.ts +44 -0
  112. package/src/strings.js +34 -4
  113. package/src/style.d.ts +462 -0
  114. package/src/style.js +58 -4
  115. package/src/symbols.d.ts +167 -0
  116. package/src/symbols.js +91 -7
  117. package/src/table/draw-borders.d.ts +38 -0
  118. package/src/table/draw-borders.js +10 -2
  119. package/src/table/index.d.ts +8 -0
  120. package/src/table/index.js +1 -0
  121. package/src/table/table.d.ts +234 -0
  122. package/src/table/table.js +59 -1
  123. package/src/themes/blocks/unicode-half.d.ts +6 -0
  124. package/src/themes/blocks/unicode-half.js +1 -0
  125. package/src/themes/blocks/unicode-thin.d.ts +6 -0
  126. package/src/themes/blocks/unicode-thin.js +1 -0
  127. package/src/themes/lines/ascii-compact.d.ts +6 -0
  128. package/src/themes/lines/ascii-compact.js +1 -0
  129. package/src/themes/lines/ascii-dots.d.ts +6 -0
  130. package/src/themes/lines/ascii-dots.js +1 -0
  131. package/src/themes/lines/ascii-girder.d.ts +6 -0
  132. package/src/themes/lines/ascii-girder.js +1 -0
  133. package/src/themes/lines/ascii-github.d.ts +6 -0
  134. package/src/themes/lines/ascii-github.js +1 -0
  135. package/src/themes/lines/ascii-reddit.d.ts +6 -0
  136. package/src/themes/lines/ascii-reddit.js +1 -0
  137. package/src/themes/lines/ascii-rounded.d.ts +6 -0
  138. package/src/themes/lines/ascii-rounded.js +1 -0
  139. package/src/themes/lines/ascii.d.ts +6 -0
  140. package/src/themes/lines/ascii.js +1 -0
  141. package/src/themes/lines/unicode-bold.d.ts +6 -0
  142. package/src/themes/lines/unicode-bold.js +1 -0
  143. package/src/themes/lines/unicode-rounded.d.ts +6 -0
  144. package/src/themes/lines/unicode-rounded.js +1 -0
  145. package/src/themes/lines/unicode.d.ts +6 -0
  146. package/src/themes/lines/unicode.js +1 -0
  147. package/src/themes/utils.d.ts +33 -0
  148. package/src/themes/utils.js +7 -0
  149. package/src/turtle/draw-line-art.d.ts +19 -0
  150. package/src/turtle/draw-line-art.js +7 -0
  151. package/src/turtle/draw-unicode.d.ts +19 -0
  152. package/src/turtle/draw-unicode.js +8 -0
  153. package/src/turtle/index.d.ts +21 -0
  154. package/src/turtle/index.js +8 -0
  155. package/src/turtle/turtle.d.ts +269 -0
  156. package/src/turtle/turtle.js +124 -4
@@ -0,0 +1,32 @@
1
+ import {ChartDatum, ChartDataInput} from '../utils.js';
2
+ import {SgrState} from '../../ansi/sgr-state.js';
3
+ import {StackedColumnChartOptions} from './draw-stacked.js';
4
+
5
+ /** Options for plain column charts. */
6
+ export interface PlainColumnOptions extends StackedColumnChartOptions {
7
+ /** Size of each rectangle in characters. */
8
+ rectSize?: number;
9
+ /** Initial SGR state. */
10
+ initState?: SgrState | string | null;
11
+ /** If true, reverse the drawing direction. */
12
+ reverse?: boolean;
13
+ }
14
+
15
+ /** Draws a single stacked column using plain symbols.
16
+ * @param data - Chart data items.
17
+ * @param width - Available width.
18
+ * @param maxValue - Maximum value.
19
+ * @param options - Options.
20
+ * @returns Array of strings for the column.
21
+ */
22
+ export function drawColumn(data: ChartDatum[], width: number, maxValue: number, options?: PlainColumnOptions): string[];
23
+
24
+ /** Draws a complete plain stacked column chart.
25
+ * @param values - Chart data.
26
+ * @param width - Available width.
27
+ * @param options - Options.
28
+ * @returns Array of strings representing the chart.
29
+ */
30
+ export function drawChart(values: ChartDataInput, width: number, options?: PlainColumnOptions): string[];
31
+
32
+ export default drawChart;
@@ -11,6 +11,13 @@ import defaultTheme from '../themes/default.js';
11
11
 
12
12
  const defaultSymbol = hBlocks8th[7];
13
13
 
14
+ /** Draws a single stacked column using plain symbols.
15
+ * @param {object[]} data - Normalized data series.
16
+ * @param {number} width - Total height.
17
+ * @param {number} maxValue - Maximum value for scaling.
18
+ * @param {object} [options] - Options including `reverse`, `rectSize`, `initState`, `theme`.
19
+ * @returns {string[]} The drawn column lines.
20
+ */
14
21
  export const drawColumn = (data, width, maxValue, options = {}) => {
15
22
  const {reverse, rectSize = 1, initState = {}, theme = defaultTheme} = options,
16
23
  {symbol = ' ', state = null, colorState} = theme?.empty || {},
@@ -34,6 +41,12 @@ export const drawColumn = (data, width, maxValue, options = {}) => {
34
41
  return result.map(line => optimize(line));
35
42
  };
36
43
 
44
+ /** Draws a complete plain stacked column chart.
45
+ * @param {any[]} values - Chart data.
46
+ * @param {number} width - Available height.
47
+ * @param {object} [options] - Options.
48
+ * @returns {string[]} Array of strings representing the chart.
49
+ */
37
50
  export const drawChart = drawStackedChart(drawColumn);
38
51
 
39
52
  export default drawChart;
@@ -0,0 +1,6 @@
1
+ import {ChartTheme} from '../utils.js';
2
+
3
+ /** The default chart theme with standard colors. */
4
+ export const chartTheme: ChartTheme;
5
+
6
+ export default chartTheme;
@@ -3,6 +3,7 @@ import style from '../../style.js';
3
3
 
4
4
  const seriesColors = 'cyan,magenta,blue,yellow,green,red'.split(',');
5
5
 
6
+ /** The default chart theme with standard colors. */
6
7
  export const chartTheme = [
7
8
  ...seriesColors.map(name => ({colorState: style['bright' + capitalize(name)].getState()})),
8
9
  ...seriesColors.map(name => ({colorState: style[name].getState()}))
@@ -0,0 +1,6 @@
1
+ import {ChartDatum} from '../utils.js';
2
+
3
+ /** Rainbow chart theme with reversed spectrum colors. */
4
+ export const chartTheme: ChartDatum[];
5
+
6
+ export default chartTheme;
@@ -1,5 +1,6 @@
1
- import rainbow from "./rainbow.js";
1
+ import rainbow from './rainbow.js';
2
2
 
3
+ /** Rainbow chart theme with reversed spectrum colors. */
3
4
  export const chartTheme = rainbow.slice().reverse();
4
5
 
5
6
  export default chartTheme;
@@ -0,0 +1,6 @@
1
+ import {ChartDatum} from '../utils.js';
2
+
3
+ /** Rainbow chart theme with spectrum colors. */
4
+ export const chartTheme: ChartDatum[];
5
+
6
+ export default chartTheme;
@@ -3,6 +3,7 @@ import style from '../../style.js';
3
3
  // red, orange, yellow, green, blue, indigo, violet
4
4
  const colors = [0xff0000, 0xffa500, 0xffff00, 0x008000, 0x0000ff, 0x4b0082, 0xee82ee];
5
5
 
6
+ /** Rainbow chart theme with spectrum colors. */
6
7
  export const chartTheme = colors.map(color => ({colorState: style.hex(color).getState()}));
7
8
 
8
9
  export default chartTheme;
@@ -0,0 +1,79 @@
1
+ import {SgrState} from '../ansi/sgr-state.js';
2
+
3
+ /** Converts a foreground color state to a background color state.
4
+ * @param state - The SGR state with foreground color.
5
+ * @returns An object with the background property.
6
+ */
7
+ export function makeBgFromFg(state: SgrState): {background: string | string[] | null};
8
+ /** Converts a background color state to a foreground color state.
9
+ * @param state - The SGR state with background color.
10
+ * @returns An object with the foreground property.
11
+ */
12
+ export function makeFgFromBg(state: SgrState): {foreground: string | string[] | null};
13
+
14
+ /** Sums the values in a data series.
15
+ * @param series - Array of data items with optional `value` properties.
16
+ * @returns The sum of all values.
17
+ */
18
+ export function sumValues(series: {value?: number}[]): number;
19
+
20
+ /** A single data point in a chart. */
21
+ export interface ChartDatum {
22
+ /** The numeric value. */
23
+ value: number;
24
+ /** SGR state for the color fill. */
25
+ colorState?: SgrState;
26
+ /** Character used for the bar/column fill. */
27
+ symbol?: string;
28
+ /** SGR state for the item. */
29
+ state?: SgrState;
30
+ /** Text label for the item. */
31
+ label?: string;
32
+ /** Additional custom properties. */
33
+ [key: string]: unknown;
34
+ }
35
+
36
+ /** Input type for chart data: each series can be a number, a ChartDatum, or an array of either. */
37
+ export type ChartDataInput = (number | ChartDatum | (number | ChartDatum)[])[];
38
+
39
+ /** Context object passed to custom `drawItem` callbacks. */
40
+ export interface DrawItemInfo {
41
+ /** Index of the current datum within the series. */
42
+ index: number;
43
+ /** The full data series. */
44
+ data: ChartDatum[];
45
+ /** Allocated sizes per datum. */
46
+ sizes: number[];
47
+ /** Maximum value for scaling. */
48
+ maxValue: number;
49
+ /** Available width in characters. */
50
+ width: number;
51
+ }
52
+
53
+ /** A chart theme: an array of ChartDatum defaults with an optional `empty` style. */
54
+ export type ChartTheme = ChartDatum[] & {empty?: {symbol?: string; state?: SgrState | null; colorState?: SgrState}};
55
+
56
+ /** Normalizes chart data, merging with default and custom themes.
57
+ * @param data - Raw chart data (numbers, ChartDatum objects, or arrays thereof).
58
+ * @param theme - Default theme to merge with.
59
+ * @returns Normalized 2D array of ChartDatum.
60
+ */
61
+ export function normalizeData(
62
+ data: (number | ChartDatum | (number | ChartDatum)[])[],
63
+ theme: ChartDatum[]
64
+ ): ChartDatum[][];
65
+
66
+ /** Allocates pixel/character sizes to data values proportionally.
67
+ * @param data - Array of chart data items.
68
+ * @param maxValue - Maximum value for scaling.
69
+ * @param size - Total available size in characters.
70
+ * @returns Array of allocated sizes.
71
+ */
72
+ export function allocateSizes(data: ChartDatum[], maxValue: number, size: number): number[];
73
+
74
+ /** Calculates the integer size needed for a fractional value.
75
+ * @param value - The fractional value.
76
+ * @param drawEmptyBorder - If true, add space for an empty border.
77
+ * @returns The integer size.
78
+ */
79
+ export function getFracSize(value: number, drawEmptyBorder?: boolean): number;
@@ -1,24 +1,41 @@
1
1
  import {Commands} from '../ansi/sgr.js';
2
2
  import defaultTheme from './themes/default.js';
3
3
 
4
+ /** Converts a foreground color state to a background color state.
5
+ * @param {object} state - SGR state with a `foreground` property.
6
+ * @returns {object} State with a `background` property.
7
+ */
4
8
  export const makeBgFromFg = state => ({
5
9
  background: !state.foreground
6
10
  ? null
7
11
  : Array.isArray(state.foreground)
8
- ? [Commands.BG_EXTENDED_COLOR, ...state.foreground.slice(1)]
9
- : Number(state.foreground) + 10
12
+ ? [Commands.BG_EXTENDED_COLOR, ...state.foreground.slice(1)]
13
+ : Number(state.foreground) + 10
10
14
  });
11
15
 
16
+ /** Converts a background color state to a foreground color state.
17
+ * @param {object} state - SGR state with a `background` property.
18
+ * @returns {object} State with a `foreground` property.
19
+ */
12
20
  export const makeFgFromBg = state => ({
13
21
  foreground: !state.background
14
22
  ? null
15
23
  : Array.isArray(state.background)
16
- ? [Commands.EXTENDED_COLOR, ...state.background.slice(1)]
17
- : Number(state.background) - 10
24
+ ? [Commands.EXTENDED_COLOR, ...state.background.slice(1)]
25
+ : Number(state.background) - 10
18
26
  });
19
27
 
28
+ /** Sums the values in a data series.
29
+ * @param {{value?: number}[]} series
30
+ * @returns {number}
31
+ */
20
32
  export const sumValues = series => series.reduce((acc, datum) => acc + (datum?.value || 0), 0);
21
33
 
34
+ /** Normalizes chart data, merging with default and custom themes.
35
+ * @param {(number|object|Array)[]} data - Raw chart data.
36
+ * @param {object[]} theme - Theme array for series styling.
37
+ * @returns {object[][]} Normalized data.
38
+ */
22
39
  export const normalizeData = (data, theme) =>
23
40
  data.map(series => {
24
41
  if (!Array.isArray(series)) series = [series];
@@ -31,6 +48,12 @@ export const normalizeData = (data, theme) =>
31
48
  });
32
49
  });
33
50
 
51
+ /** Allocates pixel/character sizes to data values proportionally.
52
+ * @param {{value?: number}[]} data - Data series.
53
+ * @param {number} maxValue - Maximum value (-1 for auto).
54
+ * @param {number} size - Total available size.
55
+ * @returns {number[]} Allocated sizes per datum plus one extra for remainder.
56
+ */
34
57
  export const allocateSizes = (data, maxValue, size) => {
35
58
  const values = data.map((datum, index) => ({value: datum?.value || 0, index})),
36
59
  seriesValue = values.reduce((acc, datum) => acc + datum.value, 0);
@@ -65,6 +88,11 @@ export const allocateSizes = (data, maxValue, size) => {
65
88
  return sizes;
66
89
  };
67
90
 
91
+ /** Calculates the integer size needed for a fractional value.
92
+ * @param {number} value - The fractional value.
93
+ * @param {boolean} [drawEmptyBorder] - Whether to draw a border for empty fractional parts.
94
+ * @returns {number} The integer size.
95
+ */
68
96
  export const getFracSize = (value, drawEmptyBorder) => {
69
97
  const intValue = Math.floor(value),
70
98
  hasFrac = value - intValue > 0,
@@ -0,0 +1,16 @@
1
+ import Box from './box.js';
2
+
3
+ /** Draws a block with fractional width using 1/8th Unicode block characters.
4
+ * @param realWidth - Fractional width (e.g., 3.5 for 3.5 columns).
5
+ * @param height - Height in rows.
6
+ * @param drawEmptyBorder - If true, add an empty border column when the fractional part is close to 0 but not exactly 0.
7
+ * @returns A Box containing the drawn block.
8
+ */
9
+ export function drawRealWidthBlock(realWidth: number, height: number, drawEmptyBorder?: boolean): Box;
10
+ /** Draws a block with fractional height using 1/8th Unicode block characters.
11
+ * @param width - Width in columns.
12
+ * @param realHeight - Fractional height (e.g., 3.5 for 3.5 rows).
13
+ * @param drawEmptyBorder - If true, add an empty border row when the fractional part is close to 0 but not exactly 0.
14
+ * @returns A Box containing the drawn block.
15
+ */
16
+ export function drawRealHeightBlock(width: number, realHeight: number, drawEmptyBorder?: boolean): Box;
@@ -1,6 +1,13 @@
1
1
  import {fullBlock, hBlocks8th, vBlocks8th} from './symbols.js';
2
2
  import Box from './box.js';
3
3
 
4
+ /** Draws a block with a fractional width using 1/8th Unicode block characters.
5
+ * The integer part is filled with full blocks; the fractional part appears on the right.
6
+ * @param {number} realWidth - The real width (float). Fractional part is interpreted in 1/8th steps.
7
+ * @param {number} height - The integer height of the block.
8
+ * @param {boolean} [drawEmptyBorder=false] - If true, add an empty border column when the fractional part is close to 0 but not exactly 0.
9
+ * @returns {import('./box.js').Box} A Box containing the drawn block.
10
+ */
4
11
  export const drawRealWidthBlock = (realWidth, height, drawEmptyBorder) => {
5
12
  realWidth = Math.max(0, realWidth);
6
13
  height = Math.max(0, Math.floor(height));
@@ -15,6 +22,13 @@ export const drawRealWidthBlock = (realWidth, height, drawEmptyBorder) => {
15
22
  return new Box(new Array(height).fill(line), true);
16
23
  };
17
24
 
25
+ /** Draws a block with a fractional height using 1/8th Unicode block characters.
26
+ * The integer part is filled with full blocks; the fractional part appears on the top.
27
+ * @param {number} width - The integer width of the block.
28
+ * @param {number} realHeight - The real height (float). Fractional part is interpreted in 1/8th steps.
29
+ * @param {boolean} [drawEmptyBorder=false] - If true, add an empty border row when the fractional part is close to 0 but not exactly 0.
30
+ * @returns {import('./box.js').Box} A Box containing the drawn block.
31
+ */
18
32
  export const drawRealHeightBlock = (width, realHeight, drawEmptyBorder) => {
19
33
  width = Math.max(0, Math.floor(width));
20
34
  realHeight = Math.max(0, realHeight);
@@ -0,0 +1,53 @@
1
+ import Box from './box.js';
2
+ import {LineTheme} from './themes/utils.js';
3
+
4
+ /** Options for `drawBlock()` and `drawFrame()`. */
5
+ export interface DrawBlockOptions {
6
+ /** Sub-theme for the top border (defaults to `hTheme`, then `theme`, then `1`). */
7
+ top?: number;
8
+ /** Sub-theme for the bottom border (defaults to `hTheme`, then `theme`, then `1`). */
9
+ bottom?: number;
10
+ /** Sub-theme for the left border (defaults to `vTheme`, then `theme`, then `1`). */
11
+ left?: number;
12
+ /** Sub-theme for the right border (defaults to `vTheme`, then `theme`, then `1`). */
13
+ right?: number;
14
+ /** Sub-theme for vertical borders (left and right). Defaults to `theme`, then `1`. */
15
+ vTheme?: number;
16
+ /** Sub-theme for horizontal borders (top and bottom). Defaults to `theme`, then `1`. */
17
+ hTheme?: number;
18
+ /** Sub-theme for all borders (default: `1`). */
19
+ theme?: number;
20
+ /** Fill character for the interior. `drawBlock()` defaults to the theme's `f` property or space; `drawFrame()` defaults to space. */
21
+ symbol?: string;
22
+ }
23
+
24
+ /** Draws a filled rectangular block using a block theme.
25
+ * The interior is filled with a symbol from the theme's `f` property, or space by default.
26
+ * @param width - Interior width in columns.
27
+ * @param height - Interior height in rows.
28
+ * @param blockTheme - Block theme object defining border characters.
29
+ * @param options - Drawing options.
30
+ * @returns A Box containing the drawn block.
31
+ * @see {@link https://github.com/uhop/console-toolkit/wiki/Module:-draw-block}
32
+ */
33
+ export function drawBlock(
34
+ width: number,
35
+ height: number,
36
+ blockTheme: LineTheme,
37
+ options?: DrawBlockOptions
38
+ ): Box;
39
+ /** Draws a rectangular frame using a block theme. Same as `drawBlock()` but defaults the interior fill to space if `symbol` is not specified.
40
+ * @param width - Interior width in columns.
41
+ * @param height - Interior height in rows.
42
+ * @param blockTheme - Block theme object defining border characters.
43
+ * @param options - Drawing options.
44
+ * @returns A Box containing the drawn frame.
45
+ */
46
+ export function drawFrame(
47
+ width: number,
48
+ height: number,
49
+ blockTheme: LineTheme,
50
+ options?: DrawBlockOptions
51
+ ): Box;
52
+
53
+ export default drawBlock;
package/src/draw-block.js CHANGED
@@ -11,6 +11,23 @@ const T = 1,
11
11
  LB = getIndex(L, B),
12
12
  RB = getIndex(R, B);
13
13
 
14
+ /** Draws a filled rectangular block using a block theme.
15
+ * The interior is filled with a symbol from the theme's `f` property, or space by default.
16
+ * @param {number} width - Interior width in columns.
17
+ * @param {number} height - Interior height in rows.
18
+ * @param {object} blockTheme - Block theme object defining border characters.
19
+ * @param {object} [options] - Drawing options.
20
+ * @param {number} [options.top] - Sub-theme for the top border (defaults to `hTheme`, then `theme`, then `1`).
21
+ * @param {number} [options.bottom] - Sub-theme for the bottom border (defaults to `hTheme`, then `theme`, then `1`).
22
+ * @param {number} [options.left] - Sub-theme for the left border (defaults to `vTheme`, then `theme`, then `1`).
23
+ * @param {number} [options.right] - Sub-theme for the right border (defaults to `vTheme`, then `theme`, then `1`).
24
+ * @param {number} [options.vTheme] - Sub-theme for vertical borders (left and right). Defaults to `theme`, then `1`.
25
+ * @param {number} [options.hTheme] - Sub-theme for horizontal borders (top and bottom). Defaults to `theme`, then `1`.
26
+ * @param {number} [options.theme] - Sub-theme for all borders (default: `1`).
27
+ * @param {string} [options.symbol] - Fill character for the interior. Defaults to the theme's `f` property or space.
28
+ * @returns {import('./box.js').Box} A Box containing the drawn block.
29
+ * @see {@link https://github.com/uhop/console-toolkit/wiki/Module:-draw-block}
30
+ */
14
31
  export const drawBlock = (
15
32
  width,
16
33
  height,
@@ -47,9 +64,16 @@ export const drawBlock = (
47
64
  );
48
65
  };
49
66
 
67
+ /** Draws a rectangular frame using a block theme. Same as `drawBlock()` but defaults the interior fill to space if `symbol` is not specified.
68
+ * @param {number} width - Interior width in columns.
69
+ * @param {number} height - Interior height in rows.
70
+ * @param {object} blockTheme - Block theme object defining border characters.
71
+ * @param {object} [options] - Drawing options (same as `drawBlock()`).
72
+ * @returns {import('./box.js').Box} A Box containing the drawn frame.
73
+ */
50
74
  export const drawFrame = (width, height, blockTheme, options) => {
51
75
  if (!options?.symbol) options = {...options, symbol: ' '};
52
76
  return drawBlock(width, height, blockTheme, options);
53
- }
77
+ };
54
78
 
55
79
  export default drawBlock;
package/src/meta.d.ts ADDED
@@ -0,0 +1,84 @@
1
+ /** Capitalizes the first letter of a string and lowercases the rest.
2
+ * @param name - The string to capitalize.
3
+ * @returns The capitalized string.
4
+ */
5
+ export function capitalize(name: string): string;
6
+
7
+ /** Converts an array of name parts to camelCase.
8
+ * @param names - Name parts.
9
+ * @returns The camelCase string.
10
+ */
11
+ export function toCamelCase(names: string[]): string;
12
+ /** Splits a camelCase string into an array of name parts.
13
+ * @param name - The camelCase string.
14
+ * @returns Array of name parts.
15
+ */
16
+ export function fromCamelCase(name: string): string[];
17
+
18
+ /** Converts an array of name parts to PascalCase.
19
+ * @param names - Name parts.
20
+ * @returns The PascalCase string.
21
+ */
22
+ export function toPascalCase(names: string[]): string;
23
+ /** Splits a PascalCase string into an array of name parts.
24
+ * @param name - The PascalCase string.
25
+ * @returns Array of name parts.
26
+ */
27
+ export function fromPascalCase(name: string): string[];
28
+
29
+ /** Converts an array of name parts to ALL_CAPS_SNAKE_CASE.
30
+ * @param names - Name parts.
31
+ * @returns The ALL_CAPS_SNAKE_CASE string.
32
+ */
33
+ export function toAllCapsSnakeCase(names: string[]): string;
34
+ /** Converts an array of name parts to snake_case.
35
+ * @param names - Name parts.
36
+ * @returns The snake_case string.
37
+ */
38
+ export function toSnakeCase(names: string[]): string;
39
+ /** Splits a snake_case string into an array of name parts.
40
+ * @param name - The snake_case string.
41
+ * @returns Array of name parts.
42
+ */
43
+ export function fromSnakeCase(name: string): string[];
44
+
45
+ /** Converts an array of name parts to kebab-case.
46
+ * @param names - Name parts.
47
+ * @returns The kebab-case string.
48
+ */
49
+ export function toKebabCase(names: string[]): string;
50
+ /** Splits a kebab-case string into an array of name parts.
51
+ * @param name - The kebab-case string.
52
+ * @returns Array of name parts.
53
+ */
54
+ export function fromKebabCase(name: string): string[];
55
+
56
+ /** Adds a getter property to a class prototype or object.
57
+ * @param Class - The class or object to modify.
58
+ * @param name - Property name.
59
+ * @param getter - Getter function.
60
+ * @param force - If true, overwrite existing properties.
61
+ * @returns The modified class/object.
62
+ */
63
+ export function addGetter<T>(Class: T, name: string, getter: () => unknown, force?: boolean): T;
64
+ /** Adds multiple getter properties to a class prototype or object.
65
+ * @param Class - The class or object to modify.
66
+ * @param getters - Map of property names to getter functions.
67
+ * @param force - If true, overwrite existing properties.
68
+ */
69
+ export function addGetters<T>(Class: T, getters: Record<string, () => unknown>, force?: boolean): void;
70
+
71
+ /** Adds an alias property that mirrors an existing property descriptor.
72
+ * @param Class - The class or object to modify.
73
+ * @param name - New property name.
74
+ * @param oldName - Existing property name to alias.
75
+ * @param force - If true, overwrite existing properties.
76
+ * @returns The modified class/object.
77
+ */
78
+ export function addAlias<T>(Class: T, name: string, oldName: string, force?: boolean): T;
79
+ /** Adds multiple alias properties to a class prototype or object.
80
+ * @param Class - The class or object to modify.
81
+ * @param aliases - Map of new names to existing names.
82
+ * @param force - If true, overwrite existing properties.
83
+ */
84
+ export function addAliases<T>(Class: T, aliases: Record<string, string>, force?: boolean): void;
package/src/meta.js CHANGED
@@ -1,31 +1,90 @@
1
+ /** Capitalizes the first letter of a string and lowercases the rest.
2
+ * @param {string} name - The string to capitalize.
3
+ * @returns {string} The capitalized string.
4
+ */
1
5
  export const capitalize = name => (name ? name[0].toUpperCase() + name.substring(1).toLowerCase() : name);
2
6
 
7
+ /** Converts an array of name parts to camelCase.
8
+ * @param {string[]} names - The name parts.
9
+ * @returns {string} The camelCase string.
10
+ */
3
11
  export const toCamelCase = names =>
4
12
  names.map((name, index) => (index ? capitalize(name) : name.toLowerCase())).join('');
13
+ /** Splits a camelCase string into an array of name parts.
14
+ * @param {string} name - The camelCase string.
15
+ * @returns {string[]} The name parts.
16
+ */
5
17
  export const fromCamelCase = name => name.split(/(?=[A-Z])/g);
6
18
 
19
+ /** Converts an array of name parts to PascalCase.
20
+ * @param {string[]} names - The name parts.
21
+ * @returns {string} The PascalCase string.
22
+ */
7
23
  export const toPascalCase = names => names.map(name => capitalize(name)).join('');
24
+ /** Splits a PascalCase string into an array of name parts.
25
+ * @param {string} name - The PascalCase string.
26
+ * @returns {string[]} The name parts.
27
+ */
8
28
  export const fromPascalCase = name => name.split(/(?=[A-Z])/g);
9
29
 
30
+ /** Converts an array of name parts to ALL_CAPS_SNAKE_CASE.
31
+ * @param {string[]} names - The name parts.
32
+ * @returns {string} The ALL_CAPS_SNAKE_CASE string.
33
+ */
10
34
  export const toAllCapsSnakeCase = names => names.map(name => name.toUpperCase()).join('_');
35
+ /** Converts an array of name parts to snake_case.
36
+ * @param {string[]} names - The name parts.
37
+ * @returns {string} The snake_case string.
38
+ */
11
39
  export const toSnakeCase = names => names.map(name => name.toLowerCase()).join('_');
40
+ /** Splits a snake_case string into an array of name parts.
41
+ * @param {string} name - The snake_case string.
42
+ * @returns {string[]} The name parts.
43
+ */
12
44
  export const fromSnakeCase = name => name.split('_');
13
45
 
46
+ /** Converts an array of name parts to kebab-case.
47
+ * @param {string[]} names - The name parts.
48
+ * @returns {string} The kebab-case string.
49
+ */
14
50
  export const toKebabCase = names => names.map(name => name.toLowerCase()).join('-');
51
+ /** Splits a kebab-case string into an array of name parts.
52
+ * @param {string} name - The kebab-case string.
53
+ * @returns {string[]} The name parts.
54
+ */
15
55
  export const fromKebabCase = name => name.split('-');
16
56
 
57
+ /** Adds a getter property to a class prototype or object.
58
+ * @param {Function|object} Class - The class or object to add the getter to.
59
+ * @param {string} name - The property name.
60
+ * @param {Function} getter - The getter function.
61
+ * @param {boolean} [force] - If true, overwrite existing properties.
62
+ * @returns {object} The modified prototype or object.
63
+ */
17
64
  export const addGetter = (Class, name, getter, force) => {
18
65
  const object = Class.prototype || Class;
19
66
  if (!force && object.hasOwnProperty(name)) return object;
20
67
  return Object.defineProperty(object, name, {configurable: true, enumerable: true, get: getter});
21
68
  };
22
69
 
70
+ /** Adds multiple getter properties to a class prototype or object.
71
+ * @param {Function|object} Class - The class or object to add getters to.
72
+ * @param {Record<string, Function>} getters - An object mapping property names to getter functions.
73
+ * @param {boolean} [force] - If true, overwrite existing properties.
74
+ */
23
75
  export const addGetters = (Class, getters, force) => {
24
76
  for (const [name, value] of Object.entries(getters)) {
25
77
  addGetter(Class, name, value, force);
26
78
  }
27
79
  };
28
80
 
81
+ /** Adds an alias property that mirrors an existing property descriptor.
82
+ * @param {Function|object} Class - The class or object to add the alias to.
83
+ * @param {string} name - The new alias name.
84
+ * @param {string} oldName - The existing property name to alias.
85
+ * @param {boolean} [force] - If true, overwrite existing properties.
86
+ * @returns {object} The modified prototype or object.
87
+ */
29
88
  export const addAlias = (Class, name, oldName, force) => {
30
89
  const object = Class.prototype || Class;
31
90
  if (!force && object.hasOwnProperty(name)) return object;
@@ -34,6 +93,11 @@ export const addAlias = (Class, name, oldName, force) => {
34
93
  return Object.defineProperty(object, name, descriptor);
35
94
  };
36
95
 
96
+ /** Adds multiple alias properties to a class prototype or object.
97
+ * @param {Function|object} Class - The class or object to add aliases to.
98
+ * @param {Record<string, string>} aliases - An object mapping new names to existing property names.
99
+ * @param {boolean} [force] - If true, overwrite existing properties.
100
+ */
37
101
  export const addAliases = (Class, aliases, force) => {
38
102
  for (const [name, oldName] of Object.entries(aliases)) {
39
103
  addAlias(Class, name, oldName, force);
@@ -0,0 +1,55 @@
1
+ import {Writable} from 'node:stream';
2
+ import {StringsInput} from '../strings.js';
3
+
4
+ /** Options for `log()`. */
5
+ export interface LogOptions {
6
+ /** ANSI command appended at the end of each line. */
7
+ endOfLineCommand?: string;
8
+ /** Color depth (1, 4, 8, or 24). If < 4, ANSI codes are stripped. */
9
+ colorDepth?: number;
10
+ }
11
+
12
+ /** Options for `out()`. */
13
+ export interface OutOptions {
14
+ /** Writable stream to output to (default: stdout). */
15
+ stream?: Writable;
16
+ /** ANSI command appended at the end of each line. */
17
+ endOfLineCommand?: string;
18
+ /** Color depth (1, 4, 8, or 24). If < 4, ANSI codes are stripped. */
19
+ colorDepth?: number;
20
+ }
21
+
22
+ /** Logs a text container to the console via `console.log()`. Strips ANSI codes if colorDepth < 4.
23
+ * @param s - Input: Box, string, string array, or object with `toStrings()`.
24
+ * @param options - Log options.
25
+ */
26
+ export function log(s: StringsInput, options?: LogOptions): void;
27
+ /** Writes a text container to a stream (default: stdout).
28
+ * @param s - Input: Box, string, string array, or object with `toStrings()`.
29
+ * @param options - Output options.
30
+ */
31
+ export function out(s: StringsInput, options?: OutOptions): void;
32
+
33
+ /** Wraps a writable stream for outputting styled text. */
34
+ export class Out {
35
+ /** The underlying writable stream. */
36
+ stream: Writable;
37
+ /** The detected or configured color depth. */
38
+ colorDepth: number;
39
+
40
+ /**
41
+ * @param stream - The writable stream to wrap.
42
+ */
43
+ constructor(stream: Writable);
44
+
45
+ /** Writes a text container to the stream.
46
+ * @param s - Input: Box, string, string array, or object with `toStrings()`.
47
+ * @param options - Output options.
48
+ */
49
+ out(s: StringsInput, options?: {endOfLineCommand?: string; colorDepth?: number}): void;
50
+ }
51
+
52
+ /** Logs a string with non-printable characters visualized as hex escape sequences via `console.log()`.
53
+ * @param string - The string to debug.
54
+ */
55
+ export function debug(string: string): void;