@chengchenccc/tui 0.1.1-rc.1

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 (226) hide show
  1. package/dist/autocomplete.d.ts +56 -0
  2. package/dist/autocomplete.d.ts.map +1 -0
  3. package/dist/autocomplete.js +647 -0
  4. package/dist/components/box.d.ts +22 -0
  5. package/dist/components/box.d.ts.map +1 -0
  6. package/dist/components/box.js +103 -0
  7. package/dist/components/card.d.ts +33 -0
  8. package/dist/components/card.d.ts.map +1 -0
  9. package/dist/components/card.js +49 -0
  10. package/dist/components/editor.d.ts +257 -0
  11. package/dist/components/editor.d.ts.map +1 -0
  12. package/dist/components/editor.js +1980 -0
  13. package/dist/components/input.d.ts +37 -0
  14. package/dist/components/input.d.ts.map +1 -0
  15. package/dist/components/input.js +389 -0
  16. package/dist/components/loader.d.ts +33 -0
  17. package/dist/components/loader.d.ts.map +1 -0
  18. package/dist/components/loader.js +100 -0
  19. package/dist/components/markdown.d.ts +108 -0
  20. package/dist/components/markdown.d.ts.map +1 -0
  21. package/dist/components/markdown.js +847 -0
  22. package/dist/components/markdown.test.d.ts +2 -0
  23. package/dist/components/markdown.test.d.ts.map +1 -0
  24. package/dist/components/markdown.test.js +81 -0
  25. package/dist/components/output-block.d.ts +44 -0
  26. package/dist/components/output-block.d.ts.map +1 -0
  27. package/dist/components/output-block.js +131 -0
  28. package/dist/components/select-list.d.ts +50 -0
  29. package/dist/components/select-list.d.ts.map +1 -0
  30. package/dist/components/select-list.js +166 -0
  31. package/dist/components/spacer.d.ts +12 -0
  32. package/dist/components/spacer.d.ts.map +1 -0
  33. package/dist/components/spacer.js +22 -0
  34. package/dist/components/statusline.d.ts +30 -0
  35. package/dist/components/statusline.d.ts.map +1 -0
  36. package/dist/components/statusline.js +46 -0
  37. package/dist/components/text.d.ts +19 -0
  38. package/dist/components/text.d.ts.map +1 -0
  39. package/dist/components/text.js +90 -0
  40. package/dist/components/truncated-text.d.ts +13 -0
  41. package/dist/components/truncated-text.d.ts.map +1 -0
  42. package/dist/components/truncated-text.js +50 -0
  43. package/dist/editor-component.d.ts +39 -0
  44. package/dist/editor-component.d.ts.map +1 -0
  45. package/dist/editor-component.js +1 -0
  46. package/dist/fuzzy.d.ts +16 -0
  47. package/dist/fuzzy.d.ts.map +1 -0
  48. package/dist/fuzzy.js +109 -0
  49. package/dist/index.d.ts +25 -0
  50. package/dist/index.d.ts.map +1 -0
  51. package/dist/index.js +36 -0
  52. package/dist/keybindings.d.ts +193 -0
  53. package/dist/keybindings.d.ts.map +1 -0
  54. package/dist/keybindings.js +191 -0
  55. package/dist/keys.d.ts +184 -0
  56. package/dist/keys.d.ts.map +1 -0
  57. package/dist/keys.js +1191 -0
  58. package/dist/kill-ring.d.ts +28 -0
  59. package/dist/kill-ring.d.ts.map +1 -0
  60. package/dist/kill-ring.js +43 -0
  61. package/dist/mermaid.d.ts +23 -0
  62. package/dist/mermaid.d.ts.map +1 -0
  63. package/dist/mermaid.js +77 -0
  64. package/dist/mouse.d.ts +40 -0
  65. package/dist/mouse.d.ts.map +1 -0
  66. package/dist/mouse.js +39 -0
  67. package/dist/mouse.test.d.ts +2 -0
  68. package/dist/mouse.test.d.ts.map +1 -0
  69. package/dist/mouse.test.js +36 -0
  70. package/dist/native-modifiers.d.ts +3 -0
  71. package/dist/native-modifiers.d.ts.map +1 -0
  72. package/dist/native-modifiers.js +52 -0
  73. package/dist/stdin-buffer.d.ts +50 -0
  74. package/dist/stdin-buffer.d.ts.map +1 -0
  75. package/dist/stdin-buffer.js +360 -0
  76. package/dist/terminal-colors.d.ts +10 -0
  77. package/dist/terminal-colors.d.ts.map +1 -0
  78. package/dist/terminal-colors.js +58 -0
  79. package/dist/terminal-image.d.ts +90 -0
  80. package/dist/terminal-image.d.ts.map +1 -0
  81. package/dist/terminal-image.js +375 -0
  82. package/dist/terminal.d.ts +110 -0
  83. package/dist/terminal.d.ts.map +1 -0
  84. package/dist/terminal.js +432 -0
  85. package/dist/tui-scrollback.test.d.ts +2 -0
  86. package/dist/tui-scrollback.test.d.ts.map +1 -0
  87. package/dist/tui-scrollback.test.js +51 -0
  88. package/dist/tui.d.ts +330 -0
  89. package/dist/tui.d.ts.map +1 -0
  90. package/dist/tui.js +1568 -0
  91. package/dist/undo-stack.d.ts +17 -0
  92. package/dist/undo-stack.d.ts.map +1 -0
  93. package/dist/undo-stack.js +24 -0
  94. package/dist/utils.d.ts +85 -0
  95. package/dist/utils.d.ts.map +1 -0
  96. package/dist/utils.js +1066 -0
  97. package/dist/vendor/mermaid-ascii/ascii/ansi.d.ts +42 -0
  98. package/dist/vendor/mermaid-ascii/ascii/ansi.d.ts.map +1 -0
  99. package/dist/vendor/mermaid-ascii/ascii/ansi.js +361 -0
  100. package/dist/vendor/mermaid-ascii/ascii/canvas.d.ts +90 -0
  101. package/dist/vendor/mermaid-ascii/ascii/canvas.d.ts.map +1 -0
  102. package/dist/vendor/mermaid-ascii/ascii/canvas.js +414 -0
  103. package/dist/vendor/mermaid-ascii/ascii/class-diagram.d.ts +8 -0
  104. package/dist/vendor/mermaid-ascii/ascii/class-diagram.d.ts.map +1 -0
  105. package/dist/vendor/mermaid-ascii/ascii/class-diagram.js +623 -0
  106. package/dist/vendor/mermaid-ascii/ascii/converter.d.ts +13 -0
  107. package/dist/vendor/mermaid-ascii/ascii/converter.d.ts.map +1 -0
  108. package/dist/vendor/mermaid-ascii/ascii/converter.js +233 -0
  109. package/dist/vendor/mermaid-ascii/ascii/draw.d.ts +67 -0
  110. package/dist/vendor/mermaid-ascii/ascii/draw.d.ts.map +1 -0
  111. package/dist/vendor/mermaid-ascii/ascii/draw.js +1313 -0
  112. package/dist/vendor/mermaid-ascii/ascii/edge-bundling.d.ts +49 -0
  113. package/dist/vendor/mermaid-ascii/ascii/edge-bundling.d.ts.map +1 -0
  114. package/dist/vendor/mermaid-ascii/ascii/edge-bundling.js +289 -0
  115. package/dist/vendor/mermaid-ascii/ascii/edge-routing.d.ts +44 -0
  116. package/dist/vendor/mermaid-ascii/ascii/edge-routing.d.ts.map +1 -0
  117. package/dist/vendor/mermaid-ascii/ascii/edge-routing.js +302 -0
  118. package/dist/vendor/mermaid-ascii/ascii/er-diagram.d.ts +8 -0
  119. package/dist/vendor/mermaid-ascii/ascii/er-diagram.d.ts.map +1 -0
  120. package/dist/vendor/mermaid-ascii/ascii/er-diagram.js +380 -0
  121. package/dist/vendor/mermaid-ascii/ascii/grid.d.ts +57 -0
  122. package/dist/vendor/mermaid-ascii/ascii/grid.d.ts.map +1 -0
  123. package/dist/vendor/mermaid-ascii/ascii/grid.js +516 -0
  124. package/dist/vendor/mermaid-ascii/ascii/index.d.ts +66 -0
  125. package/dist/vendor/mermaid-ascii/ascii/index.d.ts.map +1 -0
  126. package/dist/vendor/mermaid-ascii/ascii/index.js +133 -0
  127. package/dist/vendor/mermaid-ascii/ascii/multiline-utils.d.ts +28 -0
  128. package/dist/vendor/mermaid-ascii/ascii/multiline-utils.d.ts.map +1 -0
  129. package/dist/vendor/mermaid-ascii/ascii/multiline-utils.js +60 -0
  130. package/dist/vendor/mermaid-ascii/ascii/pathfinder.d.ts +18 -0
  131. package/dist/vendor/mermaid-ascii/ascii/pathfinder.d.ts.map +1 -0
  132. package/dist/vendor/mermaid-ascii/ascii/pathfinder.js +224 -0
  133. package/dist/vendor/mermaid-ascii/ascii/sequence.d.ts +8 -0
  134. package/dist/vendor/mermaid-ascii/ascii/sequence.d.ts.map +1 -0
  135. package/dist/vendor/mermaid-ascii/ascii/sequence.js +416 -0
  136. package/dist/vendor/mermaid-ascii/ascii/shapes/circle.d.ts +12 -0
  137. package/dist/vendor/mermaid-ascii/ascii/shapes/circle.d.ts.map +1 -0
  138. package/dist/vendor/mermaid-ascii/ascii/shapes/circle.js +22 -0
  139. package/dist/vendor/mermaid-ascii/ascii/shapes/corners.d.ts +35 -0
  140. package/dist/vendor/mermaid-ascii/ascii/shapes/corners.d.ts.map +1 -0
  141. package/dist/vendor/mermaid-ascii/ascii/shapes/corners.js +91 -0
  142. package/dist/vendor/mermaid-ascii/ascii/shapes/diamond.d.ts +12 -0
  143. package/dist/vendor/mermaid-ascii/ascii/shapes/diamond.d.ts.map +1 -0
  144. package/dist/vendor/mermaid-ascii/ascii/shapes/diamond.js +22 -0
  145. package/dist/vendor/mermaid-ascii/ascii/shapes/hexagon.d.ts +12 -0
  146. package/dist/vendor/mermaid-ascii/ascii/shapes/hexagon.d.ts.map +1 -0
  147. package/dist/vendor/mermaid-ascii/ascii/shapes/hexagon.js +22 -0
  148. package/dist/vendor/mermaid-ascii/ascii/shapes/index.d.ts +27 -0
  149. package/dist/vendor/mermaid-ascii/ascii/shapes/index.d.ts.map +1 -0
  150. package/dist/vendor/mermaid-ascii/ascii/shapes/index.js +66 -0
  151. package/dist/vendor/mermaid-ascii/ascii/shapes/rectangle.d.ts +32 -0
  152. package/dist/vendor/mermaid-ascii/ascii/shapes/rectangle.d.ts.map +1 -0
  153. package/dist/vendor/mermaid-ascii/ascii/shapes/rectangle.js +149 -0
  154. package/dist/vendor/mermaid-ascii/ascii/shapes/rounded.d.ts +12 -0
  155. package/dist/vendor/mermaid-ascii/ascii/shapes/rounded.d.ts.map +1 -0
  156. package/dist/vendor/mermaid-ascii/ascii/shapes/rounded.js +22 -0
  157. package/dist/vendor/mermaid-ascii/ascii/shapes/special.d.ts +60 -0
  158. package/dist/vendor/mermaid-ascii/ascii/shapes/special.d.ts.map +1 -0
  159. package/dist/vendor/mermaid-ascii/ascii/shapes/special.js +253 -0
  160. package/dist/vendor/mermaid-ascii/ascii/shapes/stadium.d.ts +18 -0
  161. package/dist/vendor/mermaid-ascii/ascii/shapes/stadium.d.ts.map +1 -0
  162. package/dist/vendor/mermaid-ascii/ascii/shapes/stadium.js +103 -0
  163. package/dist/vendor/mermaid-ascii/ascii/shapes/state.d.ts +31 -0
  164. package/dist/vendor/mermaid-ascii/ascii/shapes/state.d.ts.map +1 -0
  165. package/dist/vendor/mermaid-ascii/ascii/shapes/state.js +167 -0
  166. package/dist/vendor/mermaid-ascii/ascii/shapes/types.d.ts +56 -0
  167. package/dist/vendor/mermaid-ascii/ascii/shapes/types.d.ts.map +1 -0
  168. package/dist/vendor/mermaid-ascii/ascii/shapes/types.js +4 -0
  169. package/dist/vendor/mermaid-ascii/ascii/types.d.ts +207 -0
  170. package/dist/vendor/mermaid-ascii/ascii/types.d.ts.map +1 -0
  171. package/dist/vendor/mermaid-ascii/ascii/types.js +39 -0
  172. package/dist/vendor/mermaid-ascii/ascii/validate.d.ts +52 -0
  173. package/dist/vendor/mermaid-ascii/ascii/validate.d.ts.map +1 -0
  174. package/dist/vendor/mermaid-ascii/ascii/validate.js +97 -0
  175. package/dist/vendor/mermaid-ascii/ascii/xychart.d.ts +3 -0
  176. package/dist/vendor/mermaid-ascii/ascii/xychart.d.ts.map +1 -0
  177. package/dist/vendor/mermaid-ascii/ascii/xychart.js +719 -0
  178. package/dist/vendor/mermaid-ascii/class/parser.d.ts +7 -0
  179. package/dist/vendor/mermaid-ascii/class/parser.d.ts.map +1 -0
  180. package/dist/vendor/mermaid-ascii/class/parser.js +265 -0
  181. package/dist/vendor/mermaid-ascii/class/types.d.ts +103 -0
  182. package/dist/vendor/mermaid-ascii/class/types.d.ts.map +1 -0
  183. package/dist/vendor/mermaid-ascii/class/types.js +7 -0
  184. package/dist/vendor/mermaid-ascii/er/parser.d.ts +7 -0
  185. package/dist/vendor/mermaid-ascii/er/parser.d.ts.map +1 -0
  186. package/dist/vendor/mermaid-ascii/er/parser.js +162 -0
  187. package/dist/vendor/mermaid-ascii/er/types.d.ts +77 -0
  188. package/dist/vendor/mermaid-ascii/er/types.d.ts.map +1 -0
  189. package/dist/vendor/mermaid-ascii/er/types.js +7 -0
  190. package/dist/vendor/mermaid-ascii/index.d.ts +2 -0
  191. package/dist/vendor/mermaid-ascii/index.d.ts.map +1 -0
  192. package/dist/vendor/mermaid-ascii/index.js +13 -0
  193. package/dist/vendor/mermaid-ascii/multiline-utils.d.ts +10 -0
  194. package/dist/vendor/mermaid-ascii/multiline-utils.d.ts.map +1 -0
  195. package/dist/vendor/mermaid-ascii/multiline-utils.js +29 -0
  196. package/dist/vendor/mermaid-ascii/parser.d.ts +8 -0
  197. package/dist/vendor/mermaid-ascii/parser.d.ts.map +1 -0
  198. package/dist/vendor/mermaid-ascii/parser.js +558 -0
  199. package/dist/vendor/mermaid-ascii/sequence/parser.d.ts +7 -0
  200. package/dist/vendor/mermaid-ascii/sequence/parser.d.ts.map +1 -0
  201. package/dist/vendor/mermaid-ascii/sequence/parser.js +182 -0
  202. package/dist/vendor/mermaid-ascii/sequence/types.d.ts +131 -0
  203. package/dist/vendor/mermaid-ascii/sequence/types.d.ts.map +1 -0
  204. package/dist/vendor/mermaid-ascii/sequence/types.js +7 -0
  205. package/dist/vendor/mermaid-ascii/text-metrics.d.ts +24 -0
  206. package/dist/vendor/mermaid-ascii/text-metrics.d.ts.map +1 -0
  207. package/dist/vendor/mermaid-ascii/text-metrics.js +69 -0
  208. package/dist/vendor/mermaid-ascii/types.d.ts +115 -0
  209. package/dist/vendor/mermaid-ascii/types.d.ts.map +1 -0
  210. package/dist/vendor/mermaid-ascii/types.js +4 -0
  211. package/dist/vendor/mermaid-ascii/xychart/colors.d.ts +26 -0
  212. package/dist/vendor/mermaid-ascii/xychart/colors.d.ts.map +1 -0
  213. package/dist/vendor/mermaid-ascii/xychart/colors.js +145 -0
  214. package/dist/vendor/mermaid-ascii/xychart/parser.d.ts +7 -0
  215. package/dist/vendor/mermaid-ascii/xychart/parser.d.ts.map +1 -0
  216. package/dist/vendor/mermaid-ascii/xychart/parser.js +105 -0
  217. package/dist/vendor/mermaid-ascii/xychart/types.d.ts +146 -0
  218. package/dist/vendor/mermaid-ascii/xychart/types.d.ts.map +1 -0
  219. package/dist/vendor/mermaid-ascii/xychart/types.js +8 -0
  220. package/dist/virtual-terminal.d.ts +80 -0
  221. package/dist/virtual-terminal.d.ts.map +1 -0
  222. package/dist/virtual-terminal.js +284 -0
  223. package/dist/word-navigation.d.ts +25 -0
  224. package/dist/word-navigation.d.ts.map +1 -0
  225. package/dist/word-navigation.js +97 -0
  226. package/package.json +38 -0
@@ -0,0 +1,719 @@
1
+ // ============================================================================
2
+ // ASCII renderer — XY Chart
3
+ //
4
+ // Renders xychart-beta diagrams to ASCII/Unicode text art.
5
+ // Uses the parsed XYChart type directly (not PositionedXYChart) since
6
+ // pixel coordinates don't map to character grids.
7
+ //
8
+ // Bar charts: █ (Unicode) or # (ASCII) block characters.
9
+ // Line charts: continuous staircase routing with rounded corners (╭╮╰╯│─).
10
+ //
11
+ // Multi-series support: each series gets a distinct color from a palette.
12
+ // ============================================================================
13
+ import { parseXYChart } from "../xychart/parser.js";
14
+ import { colorizeText } from "./ansi.js";
15
+ import { getSeriesColor, CHART_ACCENT_FALLBACK } from "../xychart/colors.js";
16
+ import { displayWidth, toCells, WIDE_PAD } from "../text-metrics.js";
17
+ // ============================================================================
18
+ // Constants
19
+ // ============================================================================
20
+ const PLOT_WIDTH = 60;
21
+ const PLOT_HEIGHT = 20;
22
+ // Unicode box-drawing characters
23
+ const UNI = {
24
+ hLine: '─',
25
+ vLine: '│',
26
+ origin: '┼',
27
+ yTick: '┤',
28
+ xTick: '┬',
29
+ bar: '█',
30
+ grid: '·',
31
+ cornerTL: '╭', // top-left: down+right
32
+ cornerTR: '╮', // top-right: down+left
33
+ cornerBL: '╰', // bottom-left: up+right
34
+ cornerBR: '╯', // bottom-right: up+left
35
+ };
36
+ // ASCII fallback characters
37
+ const ASC = {
38
+ hLine: '-',
39
+ vLine: '|',
40
+ origin: '+',
41
+ yTick: '+',
42
+ xTick: '+',
43
+ bar: '#',
44
+ grid: '.',
45
+ cornerTL: '+',
46
+ cornerTR: '+',
47
+ cornerBL: '+',
48
+ cornerBR: '+',
49
+ };
50
+ /** Generate an array of hex colors, one per series. */
51
+ function getSeriesColors(total, theme) {
52
+ const accent = theme.accent ?? CHART_ACCENT_FALLBACK;
53
+ if (total <= 1)
54
+ return [accent];
55
+ return Array.from({ length: total }, (_, i) => getSeriesColor(i, accent, theme.bg));
56
+ }
57
+ /** Map a CharRole to its hex color from the theme (for canvasToString fallback). */
58
+ function roleToHex(role, theme) {
59
+ switch (role) {
60
+ case 'text': return theme.fg;
61
+ case 'border': return theme.border;
62
+ case 'line': return theme.line;
63
+ case 'arrow': return theme.arrow;
64
+ case 'corner': return theme.corner ?? theme.line;
65
+ case 'junction': return theme.junction ?? theme.border;
66
+ default: return theme.fg;
67
+ }
68
+ }
69
+ // ============================================================================
70
+ // Public API
71
+ // ============================================================================
72
+ export function renderXYChartAscii(text, config, colorMode, theme) {
73
+ const lines = text.split('\n').map(l => l.trim()).filter(l => l.length > 0 && !l.startsWith('%%'));
74
+ const chart = parseXYChart(lines);
75
+ const ch = config.useAscii ? ASC : UNI;
76
+ if (chart.horizontal) {
77
+ return renderHorizontal(chart, ch, colorMode, theme);
78
+ }
79
+ return renderVertical(chart, ch, colorMode, theme);
80
+ }
81
+ // ============================================================================
82
+ // Vertical chart layout + rendering
83
+ // ============================================================================
84
+ function renderVertical(chart, ch, colorMode, theme) {
85
+ const dataCount = getDataCount(chart);
86
+ if (dataCount === 0)
87
+ return '';
88
+ const yRange = chart.yAxis.range;
89
+ const yTicks = niceTickValues(yRange.min, yRange.max);
90
+ const yLabels = yTicks.map(v => formatTickValue(v));
91
+ const yGutter = Math.max(...yLabels.map(l => displayWidth(l))) + 1;
92
+ const plotW = Math.max(PLOT_WIDTH, dataCount * 6);
93
+ const plotH = PLOT_HEIGHT;
94
+ const bandW = Math.floor(plotW / dataCount);
95
+ const catLabels = getCategoryLabels(chart, dataCount);
96
+ // Canvas dimensions
97
+ const hasTitle = !!chart.title;
98
+ const hasXTitle = !!chart.xAxis.title;
99
+ const hasLegend = chart.series.length > 1;
100
+ const titleRow = hasTitle ? 0 : -1;
101
+ const plotTop = (hasTitle ? 2 : 0) + (hasLegend ? 1 : 0);
102
+ const plotLeft = yGutter + 1; // +1 for axis character
103
+ const totalW = plotLeft + bandW * dataCount + 2;
104
+ const xAxisRow = plotTop + plotH;
105
+ const xLabelRow = xAxisRow + 1;
106
+ const xTitleRow = hasXTitle ? xLabelRow + 1 : -1;
107
+ const totalH = xLabelRow + 1 + (hasXTitle ? 1 : 0) + (hasLegend && !hasTitle ? 0 : 0);
108
+ // Create canvas
109
+ const canvas = createCanvas(totalW, totalH);
110
+ const roles = createRoleCanvas(totalW, totalH);
111
+ const hexColors = createHexCanvas(totalW, totalH);
112
+ // Series colors
113
+ const seriesColors = getSeriesColors(chart.series.length, theme);
114
+ // Scales
115
+ const valueToRow = (v) => {
116
+ const t = (v - yRange.min) / (yRange.max - yRange.min || 1);
117
+ return Math.round(t * (plotH - 1));
118
+ };
119
+ const bandCenter = (i) => plotLeft + Math.floor(bandW * (i + 0.5));
120
+ // 1. Title
121
+ if (hasTitle && titleRow >= 0) {
122
+ writeText(canvas, roles, titleRow, Math.floor(totalW / 2 - displayWidth(chart.title) / 2), chart.title, 'text');
123
+ }
124
+ // 2. Legend
125
+ if (hasLegend) {
126
+ const legendRow = hasTitle ? 1 : 0;
127
+ drawLegend(canvas, roles, hexColors, chart, legendRow, totalW, ch, seriesColors);
128
+ }
129
+ // 3. Y-axis line + ticks + labels
130
+ for (let row = 0; row < plotH; row++) {
131
+ const displayRow = plotTop + (plotH - 1 - row);
132
+ set(canvas, roles, displayRow, plotLeft - 1, ch.vLine, 'border');
133
+ }
134
+ // Origin
135
+ set(canvas, roles, xAxisRow, plotLeft - 1, ch.origin, 'border');
136
+ for (const tick of yTicks) {
137
+ const row = valueToRow(tick);
138
+ if (row < 0 || row >= plotH)
139
+ continue;
140
+ const displayRow = plotTop + (plotH - 1 - row);
141
+ const label = formatTickValue(tick);
142
+ // Tick mark on axis
143
+ set(canvas, roles, displayRow, plotLeft - 1, row === 0 ? ch.origin : ch.yTick, 'border');
144
+ // Label
145
+ const labelStart = yGutter - displayWidth(label);
146
+ writeText(canvas, roles, displayRow, Math.max(0, labelStart), label, 'text');
147
+ }
148
+ // 4. X-axis line + ticks + labels
149
+ for (let c = plotLeft; c < plotLeft + bandW * dataCount; c++) {
150
+ set(canvas, roles, xAxisRow, c, ch.hLine, 'border');
151
+ }
152
+ for (let i = 0; i < dataCount; i++) {
153
+ const cx = bandCenter(i);
154
+ set(canvas, roles, xAxisRow, cx, ch.xTick, 'border');
155
+ // Label below
156
+ const label = catLabels[i];
157
+ const labelStart = cx - Math.floor(displayWidth(label) / 2);
158
+ writeText(canvas, roles, xLabelRow, Math.max(0, labelStart), label, 'text');
159
+ }
160
+ // 5. X-axis title
161
+ if (hasXTitle && xTitleRow >= 0) {
162
+ const title = chart.xAxis.title;
163
+ writeText(canvas, roles, xTitleRow, Math.floor(totalW / 2 - displayWidth(title) / 2), title, 'text');
164
+ }
165
+ // 6. Grid lines (subtle horizontal dots at y-tick positions)
166
+ for (const tick of yTicks) {
167
+ const row = valueToRow(tick);
168
+ if (row < 0 || row >= plotH)
169
+ continue;
170
+ const displayRow = plotTop + (plotH - 1 - row);
171
+ for (let c = plotLeft; c < plotLeft + bandW * dataCount; c++) {
172
+ if (get(canvas, displayRow, c) === ' ') {
173
+ set(canvas, roles, displayRow, c, ch.grid, 'line');
174
+ }
175
+ }
176
+ }
177
+ // 7. Bars — track global series index for per-series colors
178
+ const barEntries = [];
179
+ for (let si = 0; si < chart.series.length; si++) {
180
+ if (chart.series[si].type === 'bar')
181
+ barEntries.push({ data: chart.series[si].data, globalIdx: si });
182
+ }
183
+ if (barEntries.length > 0) {
184
+ const barCount = barEntries.length;
185
+ const usable = Math.max(1, bandW - 2);
186
+ const singleBarW = Math.max(1, Math.min(Math.floor(usable / barCount), 8));
187
+ const groupW = singleBarW * barCount + (barCount - 1);
188
+ const baseRow = valueToRow(Math.max(0, yRange.min));
189
+ for (let bIdx = 0; bIdx < barEntries.length; bIdx++) {
190
+ const entry = barEntries[bIdx];
191
+ const hexColor = seriesColors[entry.globalIdx];
192
+ for (let i = 0; i < entry.data.length; i++) {
193
+ const cx = bandCenter(i);
194
+ const groupLeft = cx - Math.floor(groupW / 2);
195
+ const bx = groupLeft + bIdx * (singleBarW + 1);
196
+ const valRow = valueToRow(entry.data[i]);
197
+ const fromRow = Math.min(baseRow, valRow);
198
+ const toRow = Math.max(baseRow, valRow);
199
+ for (let row = fromRow; row <= toRow; row++) {
200
+ const displayRow = plotTop + (plotH - 1 - row);
201
+ for (let c = bx; c < bx + singleBarW; c++) {
202
+ set(canvas, roles, displayRow, c, ch.bar, 'arrow', hexColors, hexColor);
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+ // 8. Lines (staircase routing with rounded corners)
209
+ const lineEntries = [];
210
+ for (let si = 0; si < chart.series.length; si++) {
211
+ if (chart.series[si].type === 'line')
212
+ lineEntries.push({ data: chart.series[si].data, globalIdx: si });
213
+ }
214
+ for (const entry of lineEntries) {
215
+ if (entry.data.length === 0)
216
+ continue;
217
+ const hexColor = seriesColors[entry.globalIdx];
218
+ drawStaircaseLine(canvas, roles, entry.data, bandCenter, valueToRow, plotTop, plotH, plotLeft, bandW * dataCount, ch, hexColors, hexColor);
219
+ }
220
+ return canvasToString(canvas, roles, hexColors, colorMode, theme);
221
+ }
222
+ // ============================================================================
223
+ // Horizontal chart layout + rendering
224
+ // ============================================================================
225
+ function renderHorizontal(chart, ch, colorMode, theme) {
226
+ const dataCount = getDataCount(chart);
227
+ if (dataCount === 0)
228
+ return '';
229
+ const yRange = chart.yAxis.range;
230
+ const valueTicks = niceTickValues(yRange.min, yRange.max);
231
+ const catLabels = getCategoryLabels(chart, dataCount);
232
+ const catGutter = Math.max(...catLabels.map(l => displayWidth(l))) + 1;
233
+ const plotW = Math.max(PLOT_WIDTH, 40);
234
+ const bandH = Math.max(2, Math.floor(PLOT_HEIGHT / dataCount));
235
+ const plotH = bandH * dataCount;
236
+ const hasTitle = !!chart.title;
237
+ const hasYTitle = !!chart.yAxis.title;
238
+ const hasLegend = chart.series.length > 1;
239
+ const plotTop = (hasTitle ? 2 : 0) + (hasLegend ? 1 : 0);
240
+ const plotLeft = catGutter + 1;
241
+ const totalW = plotLeft + plotW + 2;
242
+ const totalH = plotTop + plotH + 2 + (hasYTitle ? 1 : 0);
243
+ const xAxisRow = plotTop + plotH;
244
+ const canvas = createCanvas(totalW, totalH);
245
+ const roles = createRoleCanvas(totalW, totalH);
246
+ const hexColors = createHexCanvas(totalW, totalH);
247
+ // Series colors
248
+ const seriesColors = getSeriesColors(chart.series.length, theme);
249
+ // Value scale (horizontal)
250
+ const valueToCol = (v) => {
251
+ const t = (v - yRange.min) / (yRange.max - yRange.min || 1);
252
+ return plotLeft + Math.round(t * (plotW - 1));
253
+ };
254
+ const bandMid = (i) => plotTop + Math.floor(bandH * (i + 0.5));
255
+ // Title
256
+ if (hasTitle) {
257
+ writeText(canvas, roles, 0, Math.floor(totalW / 2 - displayWidth(chart.title) / 2), chart.title, 'text');
258
+ }
259
+ // Legend
260
+ if (hasLegend) {
261
+ const legendRow = hasTitle ? 1 : 0;
262
+ drawLegend(canvas, roles, hexColors, chart, legendRow, totalW, ch, seriesColors);
263
+ }
264
+ // Y-axis (category axis on left)
265
+ for (let r = plotTop; r < plotTop + plotH; r++) {
266
+ set(canvas, roles, r, plotLeft - 1, ch.vLine, 'border');
267
+ }
268
+ set(canvas, roles, xAxisRow, plotLeft - 1, ch.origin, 'border');
269
+ for (let i = 0; i < dataCount; i++) {
270
+ const my = bandMid(i);
271
+ const label = catLabels[i];
272
+ const labelStart = catGutter - displayWidth(label);
273
+ writeText(canvas, roles, my, Math.max(0, labelStart), label, 'text');
274
+ }
275
+ // X-axis (value axis on bottom)
276
+ for (let c = plotLeft; c < plotLeft + plotW; c++) {
277
+ set(canvas, roles, xAxisRow, c, ch.hLine, 'border');
278
+ }
279
+ for (const tick of valueTicks) {
280
+ const cx = valueToCol(tick);
281
+ if (cx < plotLeft || cx >= plotLeft + plotW)
282
+ continue;
283
+ set(canvas, roles, xAxisRow, cx, ch.xTick, 'border');
284
+ const label = formatTickValue(tick);
285
+ writeText(canvas, roles, xAxisRow + 1, cx - Math.floor(displayWidth(label) / 2), label, 'text');
286
+ }
287
+ // Y-axis title
288
+ if (hasYTitle) {
289
+ const title = chart.yAxis.title;
290
+ writeText(canvas, roles, totalH - 1, Math.floor(totalW / 2 - displayWidth(title) / 2), title, 'text');
291
+ }
292
+ // Grid lines (vertical at value tick positions)
293
+ for (const tick of valueTicks) {
294
+ const cx = valueToCol(tick);
295
+ if (cx < plotLeft || cx >= plotLeft + plotW)
296
+ continue;
297
+ for (let r = plotTop; r < plotTop + plotH; r++) {
298
+ if (get(canvas, r, cx) === ' ') {
299
+ set(canvas, roles, r, cx, ch.grid, 'line');
300
+ }
301
+ }
302
+ }
303
+ // Bars (horizontal) — with per-series colors
304
+ const barEntries = [];
305
+ for (let si = 0; si < chart.series.length; si++) {
306
+ if (chart.series[si].type === 'bar')
307
+ barEntries.push({ data: chart.series[si].data, globalIdx: si });
308
+ }
309
+ if (barEntries.length > 0) {
310
+ const barCount = barEntries.length;
311
+ const singleBarH = 1;
312
+ const groupH = singleBarH * barCount + (barCount - 1);
313
+ const baseCol = valueToCol(Math.max(0, yRange.min));
314
+ for (let bIdx = 0; bIdx < barEntries.length; bIdx++) {
315
+ const entry = barEntries[bIdx];
316
+ const hexColor = seriesColors[entry.globalIdx];
317
+ for (let i = 0; i < entry.data.length; i++) {
318
+ const my = bandMid(i);
319
+ const groupTop = my - Math.floor(groupH / 2);
320
+ const by = groupTop + bIdx * (singleBarH + 1);
321
+ const valCol = valueToCol(entry.data[i]);
322
+ const fromCol = Math.min(baseCol, valCol);
323
+ const toCol = Math.max(baseCol, valCol);
324
+ for (let r = by; r < by + singleBarH; r++) {
325
+ for (let c = fromCol; c <= toCol; c++) {
326
+ set(canvas, roles, r, c, ch.bar, 'arrow', hexColors, hexColor);
327
+ }
328
+ }
329
+ }
330
+ }
331
+ }
332
+ // Lines (horizontal staircase: value on x, category on y) — with per-series colors
333
+ const lineEntries = [];
334
+ for (let si = 0; si < chart.series.length; si++) {
335
+ if (chart.series[si].type === 'line')
336
+ lineEntries.push({ data: chart.series[si].data, globalIdx: si });
337
+ }
338
+ for (const entry of lineEntries) {
339
+ if (entry.data.length === 0)
340
+ continue;
341
+ const hexColor = seriesColors[entry.globalIdx];
342
+ drawHorizontalStaircaseLine(canvas, roles, entry.data, bandMid, valueToCol, plotTop, plotH, plotLeft, plotW, ch, hexColors, hexColor);
343
+ }
344
+ return canvasToString(canvas, roles, hexColors, colorMode, theme);
345
+ }
346
+ // ============================================================================
347
+ // Staircase line drawing — vertical charts
348
+ //
349
+ // Connects data points with flat segments (─) at each value's row,
350
+ // vertical segments (│) between rows, and rounded corners (╭╮╰╯)
351
+ // at transitions. The vertical step happens at the midpoint column
352
+ // between adjacent data points.
353
+ // ============================================================================
354
+ function drawStaircaseLine(canvas, roles, data, bandCenter, valueToRow, plotTop, plotH, plotLeft, plotTotalW, ch, hexCanvas, hexColor) {
355
+ if (data.length === 0)
356
+ return;
357
+ const points = data.map((v, i) => ({
358
+ col: bandCenter(i),
359
+ row: valueToRow(v),
360
+ }));
361
+ // Helper to draw on the canvas (row 0 = bottom, displayed inverted)
362
+ const drawAt = (col, row, char) => {
363
+ const displayRow = plotTop + (plotH - 1 - row);
364
+ if (displayRow >= 0 && col >= plotLeft && col < plotLeft + plotTotalW) {
365
+ set(canvas, roles, displayRow, col, char, 'arrow', hexCanvas, hexColor);
366
+ }
367
+ };
368
+ // Single point: just draw a flat segment
369
+ if (points.length === 1) {
370
+ drawAt(points[0].col, points[0].row, ch.hLine);
371
+ return;
372
+ }
373
+ for (let i = 0; i < points.length - 1; i++) {
374
+ const p1 = points[i];
375
+ const p2 = points[i + 1];
376
+ if (p1.row === p2.row) {
377
+ // Flat: draw ─ across
378
+ for (let c = p1.col; c <= p2.col; c++) {
379
+ drawAt(c, p1.row, ch.hLine);
380
+ }
381
+ continue;
382
+ }
383
+ const midCol = Math.round((p1.col + p2.col) / 2);
384
+ const goingUp = p2.row > p1.row;
385
+ // 1. Flat at p1's row from p1.col to midCol-1
386
+ for (let c = p1.col; c < midCol; c++) {
387
+ drawAt(c, p1.row, ch.hLine);
388
+ }
389
+ // 2. Corner at (midCol, p1.row)
390
+ // goingUp: ─ from LEFT, │ going UP → LEFT+TOP = ╯ (cornerBR)
391
+ // goingDown: ─ from LEFT, │ going DOWN → LEFT+BOT = ╮ (cornerTR)
392
+ if (goingUp) {
393
+ drawAt(midCol, p1.row, ch.cornerBR); // ╯
394
+ }
395
+ else {
396
+ drawAt(midCol, p1.row, ch.cornerTR); // ╮
397
+ }
398
+ // 3. Vertical from p1.row to p2.row (exclusive of endpoints)
399
+ const minRow = Math.min(p1.row, p2.row);
400
+ const maxRow = Math.max(p1.row, p2.row);
401
+ for (let row = minRow + 1; row < maxRow; row++) {
402
+ drawAt(midCol, row, ch.vLine);
403
+ }
404
+ // 4. Corner at (midCol, p2.row)
405
+ // goingUp: │ from BOTTOM, ─ going RIGHT → BOT+RIGHT = ╭ (cornerTL)
406
+ // goingDown: │ from TOP, ─ going RIGHT → TOP+RIGHT = ╰ (cornerBL)
407
+ if (goingUp) {
408
+ drawAt(midCol, p2.row, ch.cornerTL); // ╭
409
+ }
410
+ else {
411
+ drawAt(midCol, p2.row, ch.cornerBL); // ╰
412
+ }
413
+ // 5. Flat at p2's row from midCol+1 to p2.col
414
+ for (let c = midCol + 1; c <= p2.col; c++) {
415
+ drawAt(c, p2.row, ch.hLine);
416
+ }
417
+ // Leading flat for first segment (before p1.col)
418
+ if (i === 0) {
419
+ const leadStart = Math.max(plotLeft, p1.col - Math.floor((p2.col - p1.col) / 4));
420
+ for (let c = leadStart; c < p1.col; c++) {
421
+ drawAt(c, p1.row, ch.hLine);
422
+ }
423
+ }
424
+ // Trailing flat for last segment (after p2.col)
425
+ if (i === points.length - 2) {
426
+ const trailEnd = Math.min(plotLeft + plotTotalW - 1, p2.col + Math.floor((p2.col - p1.col) / 4));
427
+ for (let c = p2.col + 1; c <= trailEnd; c++) {
428
+ drawAt(c, p2.row, ch.hLine);
429
+ }
430
+ }
431
+ }
432
+ }
433
+ // ============================================================================
434
+ // Staircase line drawing — horizontal charts
435
+ //
436
+ // Same staircase approach but with axes swapped:
437
+ // data values map to columns (horizontal position) and categories map to
438
+ // rows (vertical position). Flat segments are vertical (│), transitions
439
+ // are horizontal (─), with the same rounded corners.
440
+ // ============================================================================
441
+ function drawHorizontalStaircaseLine(canvas, roles, data, bandMid, valueToCol, plotTop, plotH, plotLeft, plotW, ch, hexCanvas, hexColor) {
442
+ if (data.length === 0)
443
+ return;
444
+ const points = data.map((v, i) => ({
445
+ row: bandMid(i),
446
+ col: valueToCol(v),
447
+ }));
448
+ const drawAt = (row, col, char) => {
449
+ if (row >= plotTop && row < plotTop + plotH && col >= plotLeft && col < plotLeft + plotW) {
450
+ set(canvas, roles, row, col, char, 'arrow', hexCanvas, hexColor);
451
+ }
452
+ };
453
+ if (points.length === 1) {
454
+ drawAt(points[0].row, points[0].col, ch.vLine);
455
+ return;
456
+ }
457
+ for (let i = 0; i < points.length - 1; i++) {
458
+ const p1 = points[i];
459
+ const p2 = points[i + 1];
460
+ if (p1.col === p2.col) {
461
+ // Same value: draw │ down
462
+ for (let r = p1.row; r <= p2.row; r++) {
463
+ drawAt(r, p1.col, ch.vLine);
464
+ }
465
+ continue;
466
+ }
467
+ const midRow = Math.round((p1.row + p2.row) / 2);
468
+ const goingRight = p2.col > p1.col;
469
+ // 1. Vertical at p1's col from p1.row to midRow-1
470
+ for (let r = p1.row; r < midRow; r++) {
471
+ drawAt(r, p1.col, ch.vLine);
472
+ }
473
+ // 2. Corner at (midRow, p1.col)
474
+ // goingRight: │ from TOP, ─ going RIGHT → TOP+RIGHT = ╰ (cornerBL)
475
+ // goingLeft: │ from TOP, ─ going LEFT → TOP+LEFT = ╯ (cornerBR)
476
+ if (goingRight) {
477
+ drawAt(midRow, p1.col, ch.cornerBL); // ╰
478
+ }
479
+ else {
480
+ drawAt(midRow, p1.col, ch.cornerBR); // ╯
481
+ }
482
+ // 3. Horizontal from p1.col to p2.col (exclusive)
483
+ const minCol = Math.min(p1.col, p2.col);
484
+ const maxCol = Math.max(p1.col, p2.col);
485
+ for (let c = minCol + 1; c < maxCol; c++) {
486
+ drawAt(midRow, c, ch.hLine);
487
+ }
488
+ // 4. Corner at (midRow, p2.col)
489
+ // goingRight: ─ from LEFT, │ going DOWN → LEFT+BOT = ╮ (cornerTR)
490
+ // goingLeft: ─ from RIGHT, │ going DOWN → RIGHT+BOT = ╭ (cornerTL)
491
+ if (goingRight) {
492
+ drawAt(midRow, p2.col, ch.cornerTR); // ╮
493
+ }
494
+ else {
495
+ drawAt(midRow, p2.col, ch.cornerTL); // ╭
496
+ }
497
+ // 5. Vertical at p2's col from midRow+1 to p2.row
498
+ for (let r = midRow + 1; r <= p2.row; r++) {
499
+ drawAt(r, p2.col, ch.vLine);
500
+ }
501
+ }
502
+ }
503
+ // ============================================================================
504
+ // Legend — shows series symbols with per-series colors
505
+ // ============================================================================
506
+ function drawLegend(canvas, roles, hexCanvas, chart, row, totalW, ch, seriesColors) {
507
+ const items = [];
508
+ let barIdx = 0, lineIdx = 0;
509
+ for (let si = 0; si < chart.series.length; si++) {
510
+ const s = chart.series[si];
511
+ if (s.type === 'bar') {
512
+ items.push({ symbol: ch.bar, label: `Bar ${barIdx + 1}`, globalIdx: si });
513
+ barIdx++;
514
+ }
515
+ else {
516
+ items.push({ symbol: ch.hLine, label: `Line ${lineIdx + 1}`, globalIdx: si });
517
+ lineIdx++;
518
+ }
519
+ }
520
+ // Calculate total legend width: "symbol space label symbol space label ..."
521
+ let totalLen = 0;
522
+ for (let i = 0; i < items.length; i++) {
523
+ if (i > 0)
524
+ totalLen += 2; // gap between items
525
+ totalLen += 1 + 1 + displayWidth(items[i].label); // symbol + space + label
526
+ }
527
+ const startCol = Math.max(0, Math.floor(totalW / 2 - totalLen / 2));
528
+ let col = startCol;
529
+ for (let i = 0; i < items.length; i++) {
530
+ if (i > 0)
531
+ col += 2; // gap
532
+ const item = items[i];
533
+ // Symbol with series-specific color
534
+ set(canvas, roles, row, col, item.symbol, 'arrow', hexCanvas, seriesColors[item.globalIdx]);
535
+ col += 1;
536
+ // Space (already ' ' from canvas init)
537
+ col += 1;
538
+ // Label text
539
+ writeText(canvas, roles, row, col, item.label, 'text');
540
+ col += displayWidth(item.label);
541
+ }
542
+ }
543
+ // ============================================================================
544
+ // Canvas utilities
545
+ // ============================================================================
546
+ function createCanvas(width, height) {
547
+ return Array.from({ length: width }, () => Array.from({ length: height }, () => ' '));
548
+ }
549
+ function createRoleCanvas(width, height) {
550
+ return Array.from({ length: width }, () => Array.from({ length: height }).fill(null));
551
+ }
552
+ function createHexCanvas(width, height) {
553
+ return Array.from({ length: width }, () => Array.from({ length: height }).fill(null));
554
+ }
555
+ function set(canvas, roles, row, col, char, role, hexCanvas, hex) {
556
+ if (col >= 0 && col < canvas.length && row >= 0 && row < canvas[0].length) {
557
+ canvas[col][row] = char;
558
+ roles[col][row] = role;
559
+ if (hexCanvas && hex)
560
+ hexCanvas[col][row] = hex;
561
+ }
562
+ }
563
+ function get(canvas, row, col) {
564
+ if (col >= 0 && col < canvas.length && row >= 0 && row < canvas[0].length) {
565
+ return canvas[col][row];
566
+ }
567
+ return ' ';
568
+ }
569
+ function writeText(canvas, roles, row, startCol, text, role) {
570
+ const cells = toCells(text);
571
+ for (let i = 0; i < cells.length; i++) {
572
+ const cell = cells[i];
573
+ if (cell === WIDE_PAD)
574
+ continue; // written atomically with its lead
575
+ const col = startCol + i;
576
+ const wide = cells[i + 1] === WIDE_PAD;
577
+ // Keep wide-glyph pairs atomic at canvas bounds
578
+ if (col < 0 || col + (wide ? 1 : 0) >= canvas.length)
579
+ continue;
580
+ set(canvas, roles, row, col, cell, role);
581
+ if (wide)
582
+ set(canvas, roles, row, col + 1, WIDE_PAD, role);
583
+ }
584
+ }
585
+ // ============================================================================
586
+ // Canvas → string (with per-cell hex color support)
587
+ // ============================================================================
588
+ function canvasToString(canvas, roles, hexCanvas, colorMode, theme) {
589
+ if (canvas.length === 0)
590
+ return '';
591
+ const height = canvas[0].length;
592
+ const width = canvas.length;
593
+ const lines = [];
594
+ for (let row = 0; row < height; row++) {
595
+ const chars = [];
596
+ const rowRoles = [];
597
+ const rowHex = [];
598
+ for (let col = 0; col < width; col++) {
599
+ const c = canvas[col][row];
600
+ // Skip wide-glyph continuation cells: the glyph itself spans 2 columns
601
+ if (c === WIDE_PAD)
602
+ continue;
603
+ chars.push(c);
604
+ rowRoles.push(roles[col][row]);
605
+ rowHex.push(hexCanvas[col][row]);
606
+ }
607
+ // Trim trailing spaces
608
+ let end = chars.length - 1;
609
+ while (end >= 0 && chars[end] === ' ')
610
+ end--;
611
+ if (end < 0) {
612
+ lines.push('');
613
+ }
614
+ else {
615
+ lines.push(colorizeRow(chars.slice(0, end + 1), rowRoles.slice(0, end + 1), rowHex.slice(0, end + 1), theme, colorMode));
616
+ }
617
+ }
618
+ // Trim trailing empty lines
619
+ while (lines.length > 0 && lines[lines.length - 1] === '') {
620
+ lines.pop();
621
+ }
622
+ return lines.join('\n');
623
+ }
624
+ /**
625
+ * Colorize a row of characters, using hex color overrides where available
626
+ * and falling back to role-based theme colors otherwise.
627
+ * Groups consecutive same-color characters for efficient escape sequences.
628
+ */
629
+ function colorizeRow(chars, roles, hexOverrides, theme, mode) {
630
+ if (mode === 'none')
631
+ return chars.join('');
632
+ let result = '';
633
+ let currentColor = null;
634
+ let buffer = '';
635
+ for (let i = 0; i < chars.length; i++) {
636
+ const char = chars[i];
637
+ if (char === ' ') {
638
+ // Flush buffer before whitespace
639
+ if (buffer.length > 0) {
640
+ result += currentColor ? colorizeText(buffer, currentColor, mode) : buffer;
641
+ buffer = '';
642
+ currentColor = null;
643
+ }
644
+ result += ' ';
645
+ continue;
646
+ }
647
+ // Effective color: hex override > role-based > null
648
+ const hexOvr = hexOverrides[i] ?? null;
649
+ const roleVal = roles[i] ?? null;
650
+ const color = hexOvr ?? (roleVal ? roleToHex(roleVal, theme) : null);
651
+ if (color === currentColor) {
652
+ buffer += char;
653
+ }
654
+ else {
655
+ // Flush previous group
656
+ if (buffer.length > 0) {
657
+ result += currentColor ? colorizeText(buffer, currentColor, mode) : buffer;
658
+ }
659
+ buffer = char;
660
+ currentColor = color;
661
+ }
662
+ }
663
+ // Flush remaining
664
+ if (buffer.length > 0) {
665
+ result += currentColor ? colorizeText(buffer, currentColor, mode) : buffer;
666
+ }
667
+ return result;
668
+ }
669
+ // ============================================================================
670
+ // Helpers (chart-level)
671
+ // ============================================================================
672
+ function getDataCount(chart) {
673
+ if (chart.xAxis.categories)
674
+ return chart.xAxis.categories.length;
675
+ for (const s of chart.series) {
676
+ if (s.data.length > 0)
677
+ return s.data.length;
678
+ }
679
+ return 0;
680
+ }
681
+ function getCategoryLabels(chart, count) {
682
+ if (chart.xAxis.categories)
683
+ return chart.xAxis.categories;
684
+ if (chart.xAxis.range) {
685
+ const { min, max } = chart.xAxis.range;
686
+ const step = count > 1 ? (max - min) / (count - 1) : 0;
687
+ return Array.from({ length: count }, (_, i) => formatTickValue(min + step * i));
688
+ }
689
+ return Array.from({ length: count }, (_, i) => String(i + 1));
690
+ }
691
+ /** Generate nice tick values for a numeric range. */
692
+ function niceTickValues(min, max) {
693
+ const range = max - min;
694
+ if (range <= 0)
695
+ return [min];
696
+ const rawInterval = range / 6;
697
+ const magnitude = Math.pow(10, Math.floor(Math.log10(rawInterval)));
698
+ const residual = rawInterval / magnitude;
699
+ let niceInterval;
700
+ if (residual <= 1.5)
701
+ niceInterval = magnitude;
702
+ else if (residual <= 3)
703
+ niceInterval = 2 * magnitude;
704
+ else if (residual <= 7)
705
+ niceInterval = 5 * magnitude;
706
+ else
707
+ niceInterval = 10 * magnitude;
708
+ const start = Math.ceil(min / niceInterval) * niceInterval;
709
+ const ticks = [];
710
+ for (let v = start; v <= max + niceInterval * 0.001; v += niceInterval) {
711
+ ticks.push(Math.round(v * 1e10) / 1e10);
712
+ }
713
+ return ticks;
714
+ }
715
+ function formatTickValue(v) {
716
+ if (Number.isInteger(v))
717
+ return String(v);
718
+ return v.toFixed(Math.abs(v) < 10 ? 1 : 0);
719
+ }