@diagc/cli 1.4.0 → 1.6.0

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 (89) hide show
  1. package/assets/guide/index.md +1 -1
  2. package/assets/skill/SKILL.md +1 -1
  3. package/assets/studio/assets/{index-B4EbTBLO.js → index-DRsUukzt.js} +48 -46
  4. package/assets/studio/index.html +1 -1
  5. package/dist/api/diagrams.d.ts +5 -0
  6. package/dist/api/diagrams.js +37 -24
  7. package/dist/api/dispatch.d.ts +3 -0
  8. package/dist/api/handlers.d.ts +1 -0
  9. package/dist/api/handlers.js +1 -0
  10. package/dist/api/pages.d.ts +21 -0
  11. package/dist/api/pages.js +50 -0
  12. package/dist/api/routes.d.ts +2 -0
  13. package/dist/api/routes.js +6 -0
  14. package/dist/cli/io.d.ts +5 -0
  15. package/dist/cli/io.js +4 -0
  16. package/dist/cli/parse.d.ts +7 -0
  17. package/dist/cli/parse.js +25 -0
  18. package/dist/cli/run.js +4 -0
  19. package/dist/cli.js +23 -1
  20. package/dist/commands/shared.d.ts +4 -0
  21. package/dist/commands/shared.js +13 -0
  22. package/dist/commands/show.d.ts +5 -0
  23. package/dist/commands/show.js +115 -0
  24. package/dist/commands/tui.d.ts +3 -0
  25. package/dist/commands/tui.js +36 -0
  26. package/dist/init.d.ts +1 -1
  27. package/dist/init.js +1 -1
  28. package/dist/publish/publish.d.ts +15 -1
  29. package/dist/publish/publish.js +22 -15
  30. package/dist/serve.d.ts +3 -0
  31. package/dist/serve.js +2 -2
  32. package/dist/studio.js +1 -0
  33. package/dist/text/accents.d.ts +9 -0
  34. package/dist/text/accents.js +83 -0
  35. package/dist/text/ansi.d.ts +28 -0
  36. package/dist/text/ansi.js +111 -0
  37. package/dist/text/braille.d.ts +28 -0
  38. package/dist/text/braille.js +58 -0
  39. package/dist/text/canvas.d.ts +65 -0
  40. package/dist/text/canvas.js +155 -0
  41. package/dist/text/content.d.ts +32 -0
  42. package/dist/text/content.js +90 -0
  43. package/dist/text/fish.d.ts +27 -0
  44. package/dist/text/fish.js +209 -0
  45. package/dist/text/folds.d.ts +14 -0
  46. package/dist/text/folds.js +45 -0
  47. package/dist/text/glyphs.d.ts +31 -0
  48. package/dist/text/glyphs.js +84 -0
  49. package/dist/text/layout.d.ts +61 -0
  50. package/dist/text/layout.js +334 -0
  51. package/dist/text/legend.d.ts +44 -0
  52. package/dist/text/legend.js +252 -0
  53. package/dist/text/paint.d.ts +25 -0
  54. package/dist/text/paint.js +208 -0
  55. package/dist/text/render.d.ts +11 -0
  56. package/dist/text/render.js +28 -0
  57. package/dist/text/scene.d.ts +40 -0
  58. package/dist/text/scene.js +50 -0
  59. package/dist/text/vocabulary.d.ts +37 -0
  60. package/dist/text/vocabulary.js +211 -0
  61. package/dist/text/width.d.ts +15 -0
  62. package/dist/text/width.js +181 -0
  63. package/dist/tui/actions.d.ts +10 -0
  64. package/dist/tui/actions.js +297 -0
  65. package/dist/tui/app.d.ts +31 -0
  66. package/dist/tui/app.js +389 -0
  67. package/dist/tui/details.d.ts +3 -0
  68. package/dist/tui/details.js +54 -0
  69. package/dist/tui/keys.d.ts +33 -0
  70. package/dist/tui/keys.js +113 -0
  71. package/dist/tui/kitty.d.ts +36 -0
  72. package/dist/tui/kitty.js +65 -0
  73. package/dist/tui/legend.d.ts +14 -0
  74. package/dist/tui/legend.js +18 -0
  75. package/dist/tui/nav.d.ts +25 -0
  76. package/dist/tui/nav.js +79 -0
  77. package/dist/tui/overview.d.ts +46 -0
  78. package/dist/tui/overview.js +115 -0
  79. package/dist/tui/pixels.d.ts +28 -0
  80. package/dist/tui/pixels.js +80 -0
  81. package/dist/tui/screen.d.ts +44 -0
  82. package/dist/tui/screen.js +335 -0
  83. package/dist/tui/state.d.ts +100 -0
  84. package/dist/tui/state.js +72 -0
  85. package/dist/tui/terminal.d.ts +21 -0
  86. package/dist/tui/terminal.js +53 -0
  87. package/dist/watch.d.ts +2 -0
  88. package/dist/watch.js +1 -0
  89. package/package.json +3 -2
@@ -0,0 +1,335 @@
1
+ import { serialize } from '../text/ansi.js';
2
+ import { UNICODE } from '../text/glyphs.js';
3
+ import { legendCells, legendSize } from '../text/legend.js';
4
+ import { subtitleOf } from '../text/vocabulary.js';
5
+ import { cellWidth, charWidth, fitCells, isControl, printable } from '../text/width.js';
6
+ import { detailLines } from './details.js';
7
+ import { legendPlace } from './legend.js';
8
+ import { overviewCells, overviewPixels, overviewPlace } from './overview.js';
9
+ // One frame of the TUI, as lines exactly the terminal's size: a title row, the canvas
10
+ // (a window onto the painted picture, the overview and the legend in their corners,
11
+ // and the details panel beside it), and a status row. Built cell by cell, so a wide character at a
12
+ // window's edge can be blanked instead of shifting the rest of the row.
13
+ /** The details panel's columns, its separator included. */
14
+ export const PANEL_WIDTH = 40;
15
+ /** The panel takes its columns from the canvas only on a terminal at least this wide;
16
+ * on a narrower one it takes the whole canvas while it is open. */
17
+ const PANEL_MIN_COLUMNS = 80;
18
+ /** [keys, what they do], for the help */
19
+ const HELP = [
20
+ ['arrows, h j k l', 'move between boxes'],
21
+ ['Tab, Shift-Tab', 'next / previous box'],
22
+ ['Enter, Space', 'unfold / fold the box'],
23
+ ['e, c', 'unfold / fold everything'],
24
+ ['>, <', 'go into the box / back out'],
25
+ ['p, P', 'next / previous plane'],
26
+ ['1 … 9', 'turn a layer on or off'],
27
+ ['/, n, N', 'search; next / previous match'],
28
+ ['i', 'details of the box'],
29
+ ['o', 'overview on / off'],
30
+ ['L', 'legend on / off'],
31
+ ['Shift-arrows, PgUp, PgDn', 'scroll'],
32
+ ['Home, End, mouse wheel', 'scroll'],
33
+ ['click, double-click, drag', 'select, fold, pan'],
34
+ ['click, drag on the overview', 'go there'],
35
+ ['r', 'reload'],
36
+ ['Esc', 'close; back to the list'],
37
+ ['q, Ctrl-C', 'quit'],
38
+ ];
39
+ const INVERSE = { inverse: true };
40
+ const DIM = { dim: true };
41
+ const blankRow = (columns) => Array.from({ length: columns }, () => ({ ch: ' ', style: {} }));
42
+ /** Before cell `i` is overwritten: if it is half of a wide character, blank the other
43
+ * half, so an overlay over a wide character never leaves the row a cell short or
44
+ * long. The canvas's `unwide`, for rows. */
45
+ function unwide(row, i) {
46
+ const left = row[i - 1];
47
+ if (row[i]?.ch === '' && left !== undefined)
48
+ row[i - 1] = { ch: ' ', style: left.style };
49
+ const right = row[i + 1];
50
+ if (right?.ch === '')
51
+ row[i + 1] = { ch: ' ', style: right.style };
52
+ }
53
+ /** Writes `text` into `row` from `x`, never past `maxX` (the row's end by default)
54
+ * and never half a wide character. Returns where it stopped. */
55
+ function write(row, x, text, { style = {}, maxX = row.length } = {}) {
56
+ let at = x;
57
+ for (const ch of text) {
58
+ const cp = ch.codePointAt(0);
59
+ // never sent to the terminal, where it would move the cursor instead of drawing
60
+ if (isControl(cp))
61
+ continue;
62
+ const w = charWidth(cp);
63
+ if (w === 0) {
64
+ if (at > x && row[at - 1] !== undefined)
65
+ row[at - 1].ch += ch;
66
+ continue;
67
+ }
68
+ if (at + w > Math.min(maxX, row.length))
69
+ break;
70
+ unwide(row, at);
71
+ row[at] = { ch, style };
72
+ if (w === 2) {
73
+ // the cell the second half takes may start another wide character
74
+ const after = row[at + 2];
75
+ if (row[at + 1]?.ch !== '' && after?.ch === '')
76
+ row[at + 2] = { ch: ' ', style: after.style };
77
+ row[at + 1] = { ch: '', style };
78
+ }
79
+ at += w;
80
+ }
81
+ return at;
82
+ }
83
+ /** A whole row: `text` from the second cell, the rest blank, all in `style`. */
84
+ function bar(columns, text, style) {
85
+ const row = blankRow(columns).map((c) => ({ ...c, style }));
86
+ write(row, 1, text, { style });
87
+ return row;
88
+ }
89
+ /** Copies the picture's cells under the window at `scroll` into `rows`, `area` wide
90
+ * from column 0 and from row `top`. A wide character cut by either edge is a blank. */
91
+ function blit(rows, picture, scroll, area) {
92
+ for (let r = 0; r < area.height; r++) {
93
+ const src = picture[scroll.y + r];
94
+ const dst = rows[area.top + r];
95
+ if (src === undefined || dst === undefined)
96
+ continue;
97
+ for (let c = 0; c < area.width; c++) {
98
+ const cell = src[scroll.x + c];
99
+ if (cell === undefined)
100
+ break;
101
+ const cutLeft = cell.ch === '' && c === 0;
102
+ const cutRight = src[scroll.x + c + 1]?.ch === '' && cell.ch !== '' && c === area.width - 1;
103
+ dst[c] = cutLeft || cutRight ? { ch: ' ', style: cell.style } : cell;
104
+ }
105
+ }
106
+ }
107
+ /** `text` in the middle of the canvas. */
108
+ function centred(rows, area, text) {
109
+ const row = rows[area.top + (area.height >> 1)];
110
+ if (row !== undefined)
111
+ write(row, Math.max(0, (area.width - cellWidth(text)) >> 1), text, { style: DIM, maxX: area.width });
112
+ }
113
+ /** The canvas area the picture is drawn in: the rows between the title and the status,
114
+ * less the panel's columns when it sits beside the picture. `reduce` reads the same. */
115
+ export function canvasSize(state, size) {
116
+ const beside = state.diagram?.panel === true && size.columns >= PANEL_MIN_COLUMNS;
117
+ return { top: 1, width: Math.max(0, size.columns - (beside ? PANEL_WIDTH : 0)), height: Math.max(0, size.rows - 2) };
118
+ }
119
+ /** Whether the canvas shows a picture for the overview and the legend to lie over: not
120
+ * off the diagram screen, without one, or where the panel covers the canvas. */
121
+ function pictureShown(state, scene, size) {
122
+ const d = state.diagram;
123
+ if (state.screen !== 'diagram' || d === undefined || scene === undefined)
124
+ return false;
125
+ return scene.layout.boxes.size > 0 && !(d.panel && size.columns < PANEL_MIN_COLUMNS);
126
+ }
127
+ /** Where the overview goes on this screen, whether or not it is on. `reduce` reads the
128
+ * same, for clicks. */
129
+ export function overviewPlaceOf(state, scene, size) {
130
+ return pictureShown(state, scene, size) ? overviewPlace(scene.layout, canvasSize(state, size)) : undefined;
131
+ }
132
+ /** Where the legend goes on this screen, its border included, whether or not it is on:
133
+ * kept off the overview while that is on. `reduce` reads the same, for clicks. */
134
+ export function legendPlaceOf(state, scene, size) {
135
+ if (!pictureShown(state, scene, size) || scene.legend === undefined)
136
+ return undefined;
137
+ const overview = state.diagram?.overview === true ? overviewPlaceOf(state, scene, size) : undefined;
138
+ const framed = overview && {
139
+ x: overview.x - 1,
140
+ y: overview.y - 1,
141
+ width: overview.width + 2,
142
+ height: overview.height + 2,
143
+ };
144
+ return legendPlace(legendSize(scene.legend), scene.legend.position, canvasSize(state, size), framed);
145
+ }
146
+ /** Where the overview is drawn on this frame: on, with a place, over a picture. */
147
+ function overviewDrawn(state, input, size) {
148
+ if (state.diagram?.overview !== true || input.canvas === undefined)
149
+ return undefined;
150
+ return overviewPlaceOf(state, input.scene, size);
151
+ }
152
+ /** Whether the overview goes as an image: the terminal shows them, and the help is not
153
+ * up — it lies over the overview, and an image sits above the text. */
154
+ const asImage = (d, input) => input.graphics !== undefined && !d.help;
155
+ /** The overview as an image, where it is drawn as one on this frame. */
156
+ export function overviewImage(state, input, size) {
157
+ const [d, place, scene, graphics] = [state.diagram, overviewDrawn(state, input, size), input.scene, input.graphics];
158
+ if (d === undefined || place === undefined || scene === undefined || graphics === undefined || !asImage(d, input))
159
+ return undefined;
160
+ const { width, height } = canvasSize(state, size);
161
+ const window = { scroll: d.scroll, width, height };
162
+ const pixels = overviewPixels(scene.layout, { ...place, cell: graphics.cell }, window, d.selected);
163
+ return { place, width: pixels.width, height: pixels.height, rgba: pixels.rgba(graphics.ink) };
164
+ }
165
+ function titleOf(d, model) {
166
+ if (model === undefined)
167
+ return d.name;
168
+ const name = (id) => printable(model.nodes.find((n) => n.id === id)?.name ?? id);
169
+ const parts = [[d.name, ...d.path.map(name)].join(' › ')];
170
+ const plane = model.planes.find((p) => p.id === d.plane) ?? model.planes[0];
171
+ if (plane !== undefined)
172
+ parts.push(printable(plane.name));
173
+ const layers = model.layers
174
+ .slice(0, 9)
175
+ .map((l, i) => `[${i + 1}] ${printable(l.name)}${d.layers.includes(l.id) ? ' ✓' : ''}`)
176
+ .join(' ');
177
+ if (layers !== '')
178
+ parts.push(`layers ${layers}`);
179
+ return parts.join(' · ');
180
+ }
181
+ function statusOf(d, input) {
182
+ if (d.search?.editing === true)
183
+ return [`/${d.search.query}_`, {}];
184
+ if (d.message !== undefined)
185
+ return [d.message.text, d.message.error ? { fg: { ansi: 9 }, bold: true } : {}];
186
+ const node = d.selected === undefined ? undefined : input.model?.nodes.find((n) => n.id === d.selected);
187
+ if (node === undefined)
188
+ return ['arrows move · Enter fold · > go in · / search · i details · ? help · q quit', DIM];
189
+ const subtitle = printable(subtitleOf(node, input.scene?.notation));
190
+ const name = printable(node.name);
191
+ return [`${name}${subtitle === '' ? '' : ` ${subtitle}`} · Enter fold · i details · ? help`, {}];
192
+ }
193
+ function drawPanel(rows, d, input, size) {
194
+ const beside = size.columns >= PANEL_MIN_COLUMNS;
195
+ const left = beside ? size.columns - PANEL_WIDTH : 0;
196
+ const width = beside ? PANEL_WIDTH - 3 : size.columns - 2;
197
+ const lines = d.selected !== undefined && input.model !== undefined
198
+ ? detailLines(input.model, d.selected, width, input.scene?.notation)
199
+ : ['Select a box to see its details.'];
200
+ for (let r = 1; r < size.rows - 1; r++) {
201
+ const row = rows[r];
202
+ for (let c = left; c < size.columns; c++)
203
+ row[c] = { ch: ' ', style: {} };
204
+ if (beside)
205
+ row[left] = { ch: '│', style: DIM };
206
+ const line = lines[r - 1];
207
+ if (line !== undefined)
208
+ write(row, left + (beside ? 2 : 1), line, { style: r === 1 ? { bold: true } : {} });
209
+ }
210
+ }
211
+ /** The overview, opaque over the canvas, in a border a cell wide around `place`; blank
212
+ * inside where an image goes over it. */
213
+ function drawOverview(rows, d, scene, places) {
214
+ const { canvas, overview: place, image } = places;
215
+ const { width, height } = canvas;
216
+ const cells = image
217
+ ? Array.from({ length: place.height }, () => blankRow(place.width))
218
+ : overviewCells(scene.layout, place, { scroll: d.scroll, width, height }, d.selected);
219
+ const [left, right] = [place.x - 1, place.x + place.width];
220
+ for (let r = -1; r <= place.height; r++) {
221
+ const row = rows[place.y + r];
222
+ if (row === undefined)
223
+ continue;
224
+ if (r === -1 || r === place.height) {
225
+ const [l, rt] = r === -1 ? ['┌', '┐'] : ['└', '┘'];
226
+ write(row, left, `${l}${'─'.repeat(place.width)}${rt}`, { style: DIM });
227
+ continue;
228
+ }
229
+ write(row, left, '│', { style: DIM });
230
+ cells[r].forEach((cell, c) => write(row, place.x + c, cell.ch, { style: cell.style }));
231
+ write(row, right, '│', { style: DIM });
232
+ }
233
+ const title = ' overview ';
234
+ if (cellWidth(title) <= place.width - 2)
235
+ write(rows[place.y - 1], left + 2, title, { style: DIM });
236
+ }
237
+ /** The legend, opaque over the canvas, cut to `place`. */
238
+ function drawLegend(rows, scene, place) {
239
+ if (scene.legend === undefined)
240
+ return;
241
+ legendCells(scene.legend, UNICODE, place).forEach((cells, r) => {
242
+ const row = rows[place.y + r];
243
+ if (row !== undefined)
244
+ cells.forEach((cell, c) => write(row, place.x + c, cell.ch, { style: cell.style }));
245
+ });
246
+ }
247
+ function drawHelp(rows, area) {
248
+ const keyWidth = Math.max(...HELP.map(([k]) => cellWidth(k)));
249
+ const lines = HELP.map(([k, what]) => `${k}${' '.repeat(keyWidth - cellWidth(k))} ${what}`);
250
+ const width = Math.min(area.width, Math.max(...lines.map(cellWidth)) + 4);
251
+ const height = Math.min(area.height, lines.length + 2);
252
+ if (width < 3 || height < 2)
253
+ return;
254
+ const x = (area.width - width) >> 1;
255
+ const y = area.top + ((area.height - height) >> 1);
256
+ for (let r = 0; r < height; r++) {
257
+ const row = rows[y + r];
258
+ const edge = r === 0 || r === height - 1;
259
+ const [l, fill, rt] = r === 0 ? ['┌', '─', '┐'] : r === height - 1 ? ['└', '─', '┘'] : ['│', ' ', '│'];
260
+ write(row, x, `${l}${fill.repeat(width - 2)}${rt}`);
261
+ if (!edge)
262
+ write(row, x + 2, lines[r - 1] ?? '', { maxX: x + width - 1 });
263
+ }
264
+ write(rows[y], x + 2, ' Keys ', { style: { bold: true }, maxX: x + width - 1 });
265
+ }
266
+ function composeDiagram(d, input, size, places) {
267
+ const rows = Array.from({ length: size.rows }, () => blankRow(size.columns));
268
+ const area = places.canvas;
269
+ rows[0] = bar(size.columns, titleOf(d, input.model), INVERSE);
270
+ if (input.scene === undefined || input.canvas === undefined)
271
+ centred(rows, area, input.notice ?? 'Loading…');
272
+ else if (input.scene.layout.boxes.size === 0)
273
+ centred(rows, area, 'Nothing on this plane');
274
+ else {
275
+ blit(rows, input.canvas, d.scroll, area);
276
+ const place = places.overview;
277
+ if (d.overview && place !== undefined) {
278
+ drawOverview(rows, d, input.scene, { canvas: area, overview: place, image: asImage(d, input) });
279
+ }
280
+ if (d.legend && places.legend !== undefined)
281
+ drawLegend(rows, input.scene, places.legend);
282
+ }
283
+ if (d.panel && size.rows > 2)
284
+ drawPanel(rows, d, input, size);
285
+ if (d.help)
286
+ drawHelp(rows, area);
287
+ if (size.rows > 1) {
288
+ const [text, style] = statusOf(d, input);
289
+ rows[size.rows - 1] = bar(size.columns, fitCells(text, Math.max(0, size.columns - 2)), style);
290
+ }
291
+ return rows;
292
+ }
293
+ function composeList(state, input, size) {
294
+ const rows = Array.from({ length: size.rows }, () => blankRow(size.columns));
295
+ const count = state.names.length;
296
+ rows[0] = bar(size.columns, `diagc · ${count} diagram${count === 1 ? '' : 's'}`, INVERSE);
297
+ const area = { top: 1, width: size.columns, height: Math.max(0, size.rows - 2) };
298
+ if (count === 0)
299
+ centred(rows, area, input.notice ?? 'No diagrams under .diagrams/src');
300
+ // keep the current name on screen: scroll the list once it passes the bottom
301
+ const first = Math.max(0, state.index - area.height + 1);
302
+ for (let r = 0; r < area.height && first + r < count; r++) {
303
+ const current = first + r === state.index;
304
+ // the marker as well as the inverse bar: a terminal may show no attributes at all
305
+ const row = current ? bar(size.columns, `› ${state.names[first + r]}`, INVERSE) : blankRow(size.columns);
306
+ if (!current)
307
+ write(row, 3, state.names[first + r]);
308
+ rows[area.top + r] = row;
309
+ }
310
+ // with names to show, a notice is what the last pick ran into, said in the status row
311
+ const [status, style] = count > 0 && input.notice !== undefined
312
+ ? [input.notice, { fg: { ansi: 9 }, bold: true }]
313
+ : ['Enter open · ↑↓ move · r reload · q quit', DIM];
314
+ if (size.rows > 1)
315
+ rows[size.rows - 1] = bar(size.columns, fitCells(status, Math.max(0, size.columns - 2)), style);
316
+ return rows;
317
+ }
318
+ /** The frame for `state`: `size.rows` lines of `size.columns` cells each. */
319
+ export function composeFrame(state, input, size, mode) {
320
+ const safe = { columns: Math.max(0, size.columns), rows: Math.max(0, size.rows) };
321
+ const rows = state.screen === 'diagram' && state.diagram !== undefined
322
+ ? composeDiagram(state.diagram, input, safe, {
323
+ canvas: canvasSize(state, safe),
324
+ overview: overviewPlaceOf(state, input.scene, safe),
325
+ legend: legendPlaceOf(state, input.scene, safe),
326
+ })
327
+ : composeList(state, input, safe);
328
+ return serialize(rows.slice(0, safe.rows), mode, false);
329
+ }
330
+ /** The escapes that put `lines` on the screen in one go: each line at its row, then
331
+ * `images`' commands, inside a synchronized update, so a terminal that has them never
332
+ * shows a frame half drawn. */
333
+ export function frameText(lines, images = '') {
334
+ return `\x1b[?2026h${lines.map((line, i) => `\x1b[${i + 1};1H${line}`).join('')}${images}\x1b[?2026l`;
335
+ }
@@ -0,0 +1,100 @@
1
+ import { type DiagramModel, type LayoutOverlay } from '@diagc/core/internal';
2
+ import type { CellPoint, CellRect } from '../text/canvas.js';
3
+ import { type Pins } from '../text/folds.js';
4
+ import type { SceneInput, Scene } from '../text/scene.js';
5
+ import type { UserInput } from './keys.js';
6
+ import type { OverviewPlace } from './overview.js';
7
+ /** One open diagram, as the reader sees it. None of it is saved. */
8
+ export interface DiagramState {
9
+ name: string;
10
+ /** absent = the model's first plane */
11
+ plane?: string;
12
+ layers: string[];
13
+ pins: Pins;
14
+ /** the drill path; its last entry is the container whose inside is drawn */
15
+ path: string[];
16
+ selected?: string;
17
+ /** the picture's cell at the canvas's top-left corner */
18
+ scroll: CellPoint;
19
+ /** the details panel */
20
+ panel: boolean;
21
+ help: boolean;
22
+ /** the overview, which shows while the picture is bigger than the screen */
23
+ overview: boolean;
24
+ /** the legend, on as a diagram opens where it declares one */
25
+ legend: boolean;
26
+ search?: {
27
+ query: string;
28
+ editing: boolean;
29
+ };
30
+ /** one line for the status row, cleared by the next key */
31
+ message?: {
32
+ text: string;
33
+ error: boolean;
34
+ };
35
+ /** the last mouse press, for telling a double click */
36
+ lastPress?: {
37
+ x: number;
38
+ y: number;
39
+ at: number;
40
+ };
41
+ /** where a drag last was, while it pans the canvas or, started on the overview, moves
42
+ * the screen about it */
43
+ drag?: CellPoint & {
44
+ overview?: true;
45
+ };
46
+ /** scroll the selection into view once the layout it waits for arrives (`settle`) */
47
+ follow?: boolean;
48
+ }
49
+ export interface TuiState {
50
+ screen: 'list' | 'diagram';
51
+ /** the diagrams there are to open */
52
+ names: string[];
53
+ /** the list's current row */
54
+ index: number;
55
+ diagram?: DiagramState;
56
+ }
57
+ /** What an event is read against: the open diagram's model and laid-out picture, the
58
+ * canvas's place on the screen, and when the event came (ms). */
59
+ export interface Ctx {
60
+ model?: DiagramModel;
61
+ overlay?: LayoutOverlay;
62
+ scene?: Scene;
63
+ /** the canvas: its first screen row and its size */
64
+ view: {
65
+ top: number;
66
+ width: number;
67
+ height: number;
68
+ };
69
+ /** where the overview goes on this screen, whether or not it is on */
70
+ overview?: OverviewPlace;
71
+ /** where the legend goes on this screen, its border included, whether or not it is on */
72
+ legend?: CellRect;
73
+ at: number;
74
+ }
75
+ export type Effect = {
76
+ kind: 'quit';
77
+ } | {
78
+ kind: 'open';
79
+ name: string;
80
+ } | {
81
+ kind: 'relayout';
82
+ } | {
83
+ kind: 'reload';
84
+ };
85
+ export interface Reduced {
86
+ state: TuiState;
87
+ effects: Effect[];
88
+ }
89
+ /** A diagram as it opens, and as a plane switch reopens it: the plane's own layers and
90
+ * the folds its layout saved, as the published page opens it. */
91
+ export declare function openDiagram(name: string, model: DiagramModel, overlay: LayoutOverlay | undefined, plane?: string): DiagramState;
92
+ /** What to lay out for `state`. */
93
+ export declare function sceneInput(state: DiagramState, model: DiagramModel, overlay: LayoutOverlay | undefined): SceneInput;
94
+ /** `state` fitted to a new picture: a selection the picture no longer draws is gone,
95
+ * one that asked to be followed is scrolled to, and the scroll stays on the picture. */
96
+ export declare function settle(state: DiagramState, scene: Scene, view: {
97
+ width: number;
98
+ height: number;
99
+ }): DiagramState;
100
+ export declare function reduce(state: TuiState, event: UserInput, ctx: Ctx): Reduced;
@@ -0,0 +1,72 @@
1
+ import { presetLayers } from '@diagc/core/internal';
2
+ import { readerPins } from '../text/folds.js';
3
+ import { onKey, onMouse } from './actions.js';
4
+ import { clampScroll, follow } from './nav.js';
5
+ /** A diagram as it opens, and as a plane switch reopens it: the plane's own layers and
6
+ * the folds its layout saved, as the published page opens it. */
7
+ export function openDiagram(name, model, overlay, plane) {
8
+ return {
9
+ name,
10
+ ...(plane !== undefined ? { plane } : {}),
11
+ layers: presetLayers(model.planes, plane),
12
+ pins: readerPins(model, overlay, plane),
13
+ path: [],
14
+ scroll: { x: 0, y: 0 },
15
+ panel: false,
16
+ help: false,
17
+ overview: true,
18
+ legend: model.legend !== undefined,
19
+ };
20
+ }
21
+ /** What to lay out for `state`. */
22
+ export function sceneInput(state, model, overlay) {
23
+ const root = state.path.at(-1);
24
+ return {
25
+ model,
26
+ pins: state.pins,
27
+ layers: state.layers,
28
+ ...(overlay !== undefined ? { overlay } : {}),
29
+ ...(state.plane !== undefined ? { plane: state.plane } : {}),
30
+ ...(root !== undefined ? { root } : {}),
31
+ };
32
+ }
33
+ /** `state` fitted to a new picture: a selection the picture no longer draws is gone,
34
+ * one that asked to be followed is scrolled to, and the scroll stays on the picture. */
35
+ export function settle(state, scene, view) {
36
+ const { follow: wanted, selected, ...rest } = state;
37
+ const box = selected === undefined ? undefined : scene.layout.boxes.get(selected);
38
+ const scroll = wanted === true && box !== undefined ? follow(state.scroll, box, view) : state.scroll;
39
+ return {
40
+ ...rest,
41
+ ...(box !== undefined ? { selected } : {}),
42
+ scroll: clampScroll(scroll, scene.layout, view),
43
+ };
44
+ }
45
+ function reduceList(state, event) {
46
+ const stay = (index = state.index) => ({ state: { ...state, index }, effects: [] });
47
+ if (event.kind !== 'key')
48
+ return stay();
49
+ const last = Math.max(0, state.names.length - 1);
50
+ if (event.name === 'q' || (event.ctrl === true && event.name === 'c'))
51
+ return { state, effects: [{ kind: 'quit' }] };
52
+ if (event.name === 'up' || event.name === 'k')
53
+ return stay(Math.max(0, state.index - 1));
54
+ if (event.name === 'down' || event.name === 'j')
55
+ return stay(Math.min(last, state.index + 1));
56
+ if (event.name === 'r')
57
+ return { state, effects: [{ kind: 'reload' }] };
58
+ const name = state.names[state.index];
59
+ if (event.name === 'enter' && name !== undefined)
60
+ return { state, effects: [{ kind: 'open', name }] };
61
+ return stay();
62
+ }
63
+ export function reduce(state, event, ctx) {
64
+ if (state.screen === 'list' || state.diagram === undefined)
65
+ return reduceList(state, event);
66
+ const { message: _cleared, ...diagram } = state.diagram;
67
+ const step = event.kind === 'mouse' ? onMouse(diagram, ctx, event) : onKey(diagram, ctx, event);
68
+ return {
69
+ state: { ...state, diagram: step.diagram, ...(step.toList === true ? { screen: 'list' } : {}) },
70
+ effects: step.effects,
71
+ };
72
+ }
@@ -0,0 +1,21 @@
1
+ /** The alternate screen (the shell's own screen comes back untouched), the cursor
2
+ * hidden, and mouse reports: presses, drags (1002) in the SGR encoding (1006),
3
+ * which has no 223-column limit. */
4
+ export declare const ENTER_SEQUENCE = "\u001B[?1049h\u001B[?25l\u001B[?1000h\u001B[?1002h\u001B[?1006h";
5
+ /** Everything `ENTER_SEQUENCE` turned on, off again, in the reverse order. */
6
+ export declare const LEAVE_SEQUENCE = "\u001B[?1006l\u001B[?1002l\u001B[?1000l\u001B[?25h\u001B[?1049l";
7
+ export interface Terminal {
8
+ readonly columns: number;
9
+ readonly rows: number;
10
+ write(text: string): void;
11
+ /** called with what each read brings, while started */
12
+ onInput(listener: (data: string) => void): void;
13
+ /** called when the window changes size, while started */
14
+ onResize(listener: () => void): void;
15
+ /** raw input and the alternate screen */
16
+ start(): void;
17
+ /** the terminal as it was; safe to call twice, or without a start */
18
+ stop(): void;
19
+ }
20
+ /** The process's own terminal. */
21
+ export declare function processTerminal(stdin: NodeJS.ReadStream, stdout: NodeJS.WriteStream): Terminal;
@@ -0,0 +1,53 @@
1
+ // The terminal the TUI draws on, behind an interface small enough for a test to fake.
2
+ /** The alternate screen (the shell's own screen comes back untouched), the cursor
3
+ * hidden, and mouse reports: presses, drags (1002) in the SGR encoding (1006),
4
+ * which has no 223-column limit. */
5
+ export const ENTER_SEQUENCE = '\x1b[?1049h\x1b[?25l\x1b[?1000h\x1b[?1002h\x1b[?1006h';
6
+ /** Everything `ENTER_SEQUENCE` turned on, off again, in the reverse order. */
7
+ export const LEAVE_SEQUENCE = '\x1b[?1006l\x1b[?1002l\x1b[?1000l\x1b[?25h\x1b[?1049l';
8
+ /** The process's own terminal. */
9
+ export function processTerminal(stdin, stdout) {
10
+ const inputs = [];
11
+ const resizes = [];
12
+ let started = false;
13
+ const onData = (data) => {
14
+ for (const listener of inputs)
15
+ listener(data.toString());
16
+ };
17
+ const onResize = () => {
18
+ for (const listener of resizes)
19
+ listener();
20
+ };
21
+ return {
22
+ get columns() {
23
+ return stdout.columns ?? 80;
24
+ },
25
+ get rows() {
26
+ return stdout.rows ?? 24;
27
+ },
28
+ write: (text) => void stdout.write(text),
29
+ onInput: (listener) => void inputs.push(listener),
30
+ onResize: (listener) => void resizes.push(listener),
31
+ start() {
32
+ if (started)
33
+ return;
34
+ started = true;
35
+ stdin.setRawMode?.(true);
36
+ stdin.setEncoding('utf8');
37
+ stdin.on('data', onData);
38
+ stdout.on('resize', onResize);
39
+ stdin.resume();
40
+ stdout.write(ENTER_SEQUENCE);
41
+ },
42
+ stop() {
43
+ if (!started)
44
+ return;
45
+ started = false;
46
+ stdout.write(LEAVE_SEQUENCE);
47
+ stdin.off('data', onData);
48
+ stdout.off('resize', onResize);
49
+ stdin.setRawMode?.(false);
50
+ stdin.pause();
51
+ },
52
+ };
53
+ }
package/dist/watch.d.ts CHANGED
@@ -32,6 +32,8 @@ export declare function startWatch(dir: string, outDir: string, opts?: {
32
32
  onEvent?: (e: WatchEvent) => void;
33
33
  coreEntry?: string;
34
34
  resolver?: IncludeResolver;
35
+ /** where a compile's warnings go, one line each; the console's by default */
36
+ warn?: (line: string) => void;
35
37
  }): {
36
38
  close(): Promise<void>;
37
39
  };
package/dist/watch.js CHANGED
@@ -57,6 +57,7 @@ export function startWatch(dir, outDir, opts = {}) {
57
57
  rootDir: dir,
58
58
  ...(opts.coreEntry !== undefined ? { coreEntry: opts.coreEntry } : {}),
59
59
  ...(opts.resolver !== undefined ? { resolver: opts.resolver } : {}),
60
+ ...(opts.warn !== undefined ? { warn: opts.warn } : {}),
60
61
  });
61
62
  opts.onEvent?.({ file, ok: true, artifact });
62
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagc/cli",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "Author software-architecture diagrams as code, compile them to a validated model, edit them in a local studio, and publish pages and images.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "author": "Bogdan Frankovskyi",
@@ -41,10 +41,11 @@
41
41
  ],
42
42
  "dependencies": {
43
43
  "chokidar": "^4.0.0",
44
+ "elkjs": "^0.12.0",
44
45
  "fast-glob": "^3.3.0",
45
46
  "jiti": "^2.0.0",
46
47
  "playwright-core": "^1.62.0",
47
- "@diagc/core": "1.4.0"
48
+ "@diagc/core": "1.6.0"
48
49
  },
49
50
  "publishConfig": {
50
51
  "access": "public"