@diagc/cli 1.5.0 → 1.7.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.
- package/README.md +8 -3
- package/assets/guide/index.md +1 -1
- package/assets/skill/SKILL.md +1 -1
- package/dist/cli/io.d.ts +5 -0
- package/dist/cli/io.js +4 -0
- package/dist/cli/parse.d.ts +7 -0
- package/dist/cli/parse.js +25 -0
- package/dist/cli/run.js +4 -0
- package/dist/cli.js +24 -2
- package/dist/commands/shared.d.ts +4 -0
- package/dist/commands/shared.js +13 -0
- package/dist/commands/show.d.ts +5 -0
- package/dist/commands/show.js +115 -0
- package/dist/commands/tui.d.ts +3 -0
- package/dist/commands/tui.js +44 -0
- package/dist/init.d.ts +1 -1
- package/dist/init.js +1 -1
- package/dist/text/accents.d.ts +9 -0
- package/dist/text/accents.js +83 -0
- package/dist/text/ansi.d.ts +28 -0
- package/dist/text/ansi.js +117 -0
- package/dist/text/braille.d.ts +28 -0
- package/dist/text/braille.js +58 -0
- package/dist/text/canvas.d.ts +72 -0
- package/dist/text/canvas.js +173 -0
- package/dist/text/content.d.ts +32 -0
- package/dist/text/content.js +90 -0
- package/dist/text/dos.d.ts +69 -0
- package/dist/text/dos.js +93 -0
- package/dist/text/fish.d.ts +27 -0
- package/dist/text/fish.js +209 -0
- package/dist/text/folds.d.ts +14 -0
- package/dist/text/folds.js +45 -0
- package/dist/text/glyphs.d.ts +32 -0
- package/dist/text/glyphs.js +100 -0
- package/dist/text/layout.d.ts +70 -0
- package/dist/text/layout.js +337 -0
- package/dist/text/legend.d.ts +44 -0
- package/dist/text/legend.js +256 -0
- package/dist/text/paint.d.ts +27 -0
- package/dist/text/paint.js +260 -0
- package/dist/text/render.d.ts +11 -0
- package/dist/text/render.js +28 -0
- package/dist/text/scene.d.ts +44 -0
- package/dist/text/scene.js +58 -0
- package/dist/text/vocabulary.d.ts +37 -0
- package/dist/text/vocabulary.js +211 -0
- package/dist/text/width.d.ts +15 -0
- package/dist/text/width.js +181 -0
- package/dist/tui/actions.d.ts +15 -0
- package/dist/tui/actions.js +300 -0
- package/dist/tui/app.d.ts +34 -0
- package/dist/tui/app.js +402 -0
- package/dist/tui/details.d.ts +3 -0
- package/dist/tui/details.js +54 -0
- package/dist/tui/keys.d.ts +33 -0
- package/dist/tui/keys.js +113 -0
- package/dist/tui/kitty.d.ts +36 -0
- package/dist/tui/kitty.js +65 -0
- package/dist/tui/legend.d.ts +14 -0
- package/dist/tui/legend.js +18 -0
- package/dist/tui/nav.d.ts +31 -0
- package/dist/tui/nav.js +101 -0
- package/dist/tui/overview.d.ts +48 -0
- package/dist/tui/overview.js +145 -0
- package/dist/tui/pixels.d.ts +28 -0
- package/dist/tui/pixels.js +80 -0
- package/dist/tui/screen.d.ts +54 -0
- package/dist/tui/screen.js +416 -0
- package/dist/tui/state.d.ts +102 -0
- package/dist/tui/state.js +80 -0
- package/dist/tui/terminal.d.ts +21 -0
- package/dist/tui/terminal.js +53 -0
- package/dist/watch.d.ts +2 -0
- package/dist/watch.js +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import { serialize } from '../text/ansi.js';
|
|
2
|
+
import { BLACK, CYAN, DESKTOP, GREY, RED, SHADOW, SHADOW_REACH, WHITE } from '../text/dos.js';
|
|
3
|
+
import { UNICODE } from '../text/glyphs.js';
|
|
4
|
+
import { legendCells, legendSize } from '../text/legend.js';
|
|
5
|
+
import { subtitleOf } from '../text/vocabulary.js';
|
|
6
|
+
import { cellWidth, charWidth, fitCells, isControl, printable } from '../text/width.js';
|
|
7
|
+
import { detailLines } from './details.js';
|
|
8
|
+
import { legendPlace } from './legend.js';
|
|
9
|
+
import { overviewCells, overviewPixels, overviewPlace } from './overview.js';
|
|
10
|
+
// One frame of the TUI, as lines exactly the terminal's size: a title row, the canvas
|
|
11
|
+
// (a window onto the painted picture, the overview and the legend in their corners,
|
|
12
|
+
// and the details panel beside it), and a status row. Built cell by cell, so a wide character at a
|
|
13
|
+
// window's edge can be blanked instead of shifting the rest of the row.
|
|
14
|
+
/** The details panel's columns, its separator included. */
|
|
15
|
+
export const PANEL_WIDTH = 40;
|
|
16
|
+
/** The panel takes its columns from the canvas only on a terminal at least this wide;
|
|
17
|
+
* on a narrower one it takes the whole canvas while it is open. */
|
|
18
|
+
const PANEL_MIN_COLUMNS = 80;
|
|
19
|
+
/** [keys, what they do], for the help */
|
|
20
|
+
const HELP = [
|
|
21
|
+
['arrows, h j k l', 'move between boxes'],
|
|
22
|
+
['Tab, Shift-Tab', 'next / previous box'],
|
|
23
|
+
['Enter, Space', 'unfold / fold the box'],
|
|
24
|
+
['e, c', 'unfold / fold everything'],
|
|
25
|
+
['>, <', 'go into the box / back out'],
|
|
26
|
+
['p, P', 'next / previous plane'],
|
|
27
|
+
['1 … 9', 'turn a layer on or off'],
|
|
28
|
+
['/, n, N', 'search; next / previous match'],
|
|
29
|
+
['i', 'details of the box'],
|
|
30
|
+
['o', 'overview on / off'],
|
|
31
|
+
['L', 'legend on / off'],
|
|
32
|
+
['s', 'DOS style on / off'],
|
|
33
|
+
['Shift-arrows, PgUp, PgDn', 'scroll'],
|
|
34
|
+
['Home, End, mouse wheel', 'scroll'],
|
|
35
|
+
['click, double-click, drag', 'select, fold, pan'],
|
|
36
|
+
['click, drag on the overview', 'go there'],
|
|
37
|
+
['r', 'reload'],
|
|
38
|
+
['Esc', 'close; back to the list'],
|
|
39
|
+
['q, Ctrl-C', 'quit'],
|
|
40
|
+
];
|
|
41
|
+
const INVERSE = { inverse: true };
|
|
42
|
+
const DIM = { dim: true };
|
|
43
|
+
/** The terminal's own look: its colours, with inverse for the bars. */
|
|
44
|
+
const CLEAN = {
|
|
45
|
+
title: INVERSE,
|
|
46
|
+
status: {},
|
|
47
|
+
hint: DIM,
|
|
48
|
+
error: { fg: { ansi: 9 }, bold: true },
|
|
49
|
+
ground: {},
|
|
50
|
+
current: INVERSE,
|
|
51
|
+
panel: {},
|
|
52
|
+
rule: DIM,
|
|
53
|
+
dialog: {},
|
|
54
|
+
border: {},
|
|
55
|
+
frame: ['┌', '┐', '└', '┘', '─', '│'],
|
|
56
|
+
};
|
|
57
|
+
const BAR = { fg: BLACK, bg: GREY };
|
|
58
|
+
/** Turbo Vision's: a grey menu bar and status line over the blue desktop, the list's
|
|
59
|
+
* row and the panel a cyan window, the help a grey dialog in a double frame. */
|
|
60
|
+
const DOS = {
|
|
61
|
+
title: BAR,
|
|
62
|
+
status: BAR,
|
|
63
|
+
hint: BAR,
|
|
64
|
+
error: { ...BAR, fg: RED, bold: true },
|
|
65
|
+
ground: DESKTOP,
|
|
66
|
+
current: { fg: BLACK, bg: CYAN },
|
|
67
|
+
panel: { fg: BLACK, bg: CYAN },
|
|
68
|
+
rule: { fg: BLACK, bg: CYAN },
|
|
69
|
+
dialog: BAR,
|
|
70
|
+
border: { fg: WHITE, bg: GREY },
|
|
71
|
+
frame: ['╔', '╗', '╚', '╝', '═', '║'],
|
|
72
|
+
shadow: SHADOW,
|
|
73
|
+
ink: WHITE.rgb,
|
|
74
|
+
};
|
|
75
|
+
const chromeOf = (input) => (input.style === 'dos' ? DOS : CLEAN);
|
|
76
|
+
const blankRow = (columns, style = {}) => Array.from({ length: columns }, () => ({ ch: ' ', style }));
|
|
77
|
+
/** Before cell `i` is overwritten: if it is half of a wide character, blank the other
|
|
78
|
+
* half, so an overlay over a wide character never leaves the row a cell short or
|
|
79
|
+
* long. The canvas's `unwide`, for rows. */
|
|
80
|
+
function unwide(row, i) {
|
|
81
|
+
const left = row[i - 1];
|
|
82
|
+
if (row[i]?.ch === '' && left !== undefined)
|
|
83
|
+
row[i - 1] = { ch: ' ', style: left.style };
|
|
84
|
+
const right = row[i + 1];
|
|
85
|
+
if (right?.ch === '')
|
|
86
|
+
row[i + 1] = { ch: ' ', style: right.style };
|
|
87
|
+
}
|
|
88
|
+
/** Writes `text` into `row` from `x`, never past `maxX` (the row's end by default)
|
|
89
|
+
* and never half a wide character. Returns where it stopped. */
|
|
90
|
+
function write(row, x, text, { style = {}, maxX = row.length } = {}) {
|
|
91
|
+
let at = x;
|
|
92
|
+
for (const ch of text) {
|
|
93
|
+
const cp = ch.codePointAt(0);
|
|
94
|
+
// never sent to the terminal, where it would move the cursor instead of drawing
|
|
95
|
+
if (isControl(cp))
|
|
96
|
+
continue;
|
|
97
|
+
const w = charWidth(cp);
|
|
98
|
+
if (w === 0) {
|
|
99
|
+
if (at > x && row[at - 1] !== undefined)
|
|
100
|
+
row[at - 1].ch += ch;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (at + w > Math.min(maxX, row.length))
|
|
104
|
+
break;
|
|
105
|
+
unwide(row, at);
|
|
106
|
+
row[at] = { ch, style };
|
|
107
|
+
if (w === 2) {
|
|
108
|
+
// the cell the second half takes may start another wide character
|
|
109
|
+
const after = row[at + 2];
|
|
110
|
+
if (row[at + 1]?.ch !== '' && after?.ch === '')
|
|
111
|
+
row[at + 2] = { ch: ' ', style: after.style };
|
|
112
|
+
row[at + 1] = { ch: '', style };
|
|
113
|
+
}
|
|
114
|
+
at += w;
|
|
115
|
+
}
|
|
116
|
+
return at;
|
|
117
|
+
}
|
|
118
|
+
/** A whole row: `text` from the second cell, the rest blank, all in `style`. */
|
|
119
|
+
function bar(columns, text, style) {
|
|
120
|
+
const row = blankRow(columns).map((c) => ({ ...c, style }));
|
|
121
|
+
write(row, 1, text, { style });
|
|
122
|
+
return row;
|
|
123
|
+
}
|
|
124
|
+
/** Copies the picture's cells under the window at `scroll` into `rows`, `area` wide
|
|
125
|
+
* from column 0 and from row `top`. A wide character cut by either edge is a blank. */
|
|
126
|
+
function blit(rows, picture, scroll, area) {
|
|
127
|
+
for (let r = 0; r < area.height; r++) {
|
|
128
|
+
const src = picture[scroll.y + r];
|
|
129
|
+
const dst = rows[area.top + r];
|
|
130
|
+
if (src === undefined || dst === undefined)
|
|
131
|
+
continue;
|
|
132
|
+
for (let c = 0; c < area.width; c++) {
|
|
133
|
+
const cell = src[scroll.x + c];
|
|
134
|
+
if (cell === undefined)
|
|
135
|
+
break;
|
|
136
|
+
const cutLeft = cell.ch === '' && c === 0;
|
|
137
|
+
const cutRight = src[scroll.x + c + 1]?.ch === '' && cell.ch !== '' && c === area.width - 1;
|
|
138
|
+
dst[c] = cutLeft || cutRight ? { ch: ' ', style: cell.style } : cell;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** `text` in the middle of the canvas. */
|
|
143
|
+
function centred(rows, area, text) {
|
|
144
|
+
const row = rows[area.top + (area.height >> 1)];
|
|
145
|
+
if (row !== undefined)
|
|
146
|
+
write(row, Math.max(0, (area.width - cellWidth(text)) >> 1), text, { style: DIM, maxX: area.width });
|
|
147
|
+
}
|
|
148
|
+
/** The canvas area the picture is drawn in: the rows between the title and the status,
|
|
149
|
+
* less the panel's columns when it sits beside the picture. `reduce` reads the same. */
|
|
150
|
+
export function canvasSize(state, size) {
|
|
151
|
+
const beside = state.diagram?.panel === true && size.columns >= PANEL_MIN_COLUMNS;
|
|
152
|
+
return { top: 1, width: Math.max(0, size.columns - (beside ? PANEL_WIDTH : 0)), height: Math.max(0, size.rows - 2) };
|
|
153
|
+
}
|
|
154
|
+
/** Whether the canvas shows a picture for the overview and the legend to lie over: not
|
|
155
|
+
* off the diagram screen, without one, or where the panel covers the canvas. */
|
|
156
|
+
function pictureShown(state, scene, size) {
|
|
157
|
+
const d = state.diagram;
|
|
158
|
+
if (state.screen !== 'diagram' || d === undefined || scene === undefined)
|
|
159
|
+
return false;
|
|
160
|
+
return scene.layout.boxes.size > 0 && !(d.panel && size.columns < PANEL_MIN_COLUMNS);
|
|
161
|
+
}
|
|
162
|
+
/** Where the overview goes on this screen, whether or not it is on. `reduce` reads the
|
|
163
|
+
* same, for clicks. */
|
|
164
|
+
export function overviewPlaceOf(state, scene, size) {
|
|
165
|
+
return pictureShown(state, scene, size) ? overviewPlace(scene.layout, canvasSize(state, size)) : undefined;
|
|
166
|
+
}
|
|
167
|
+
/** The canvas as the scroll reads it: its size, and the corner the overview's frame
|
|
168
|
+
* covers while it is on, which a selected box is kept out of and the scroll may run
|
|
169
|
+
* past the picture to uncover. `reduce` and `settle` read it. */
|
|
170
|
+
export function viewOf(state, scene, size) {
|
|
171
|
+
const canvas = canvasSize(state, size);
|
|
172
|
+
const place = state.diagram?.overview === true ? overviewPlaceOf(state, scene, size) : undefined;
|
|
173
|
+
return place === undefined ? canvas : { ...canvas, corner: { width: place.width + 2, height: place.height + 2 } };
|
|
174
|
+
}
|
|
175
|
+
/** Where the legend goes on this screen, its border included, whether or not it is on:
|
|
176
|
+
* kept off the overview while that is on. `reduce` reads the same, for clicks. */
|
|
177
|
+
export function legendPlaceOf(state, scene, size) {
|
|
178
|
+
if (!pictureShown(state, scene, size) || scene.legend === undefined)
|
|
179
|
+
return undefined;
|
|
180
|
+
const overview = state.diagram?.overview === true ? overviewPlaceOf(state, scene, size) : undefined;
|
|
181
|
+
const framed = overview && {
|
|
182
|
+
x: overview.x - 1,
|
|
183
|
+
y: overview.y - 1,
|
|
184
|
+
width: overview.width + 2,
|
|
185
|
+
height: overview.height + 2,
|
|
186
|
+
};
|
|
187
|
+
return legendPlace(legendSize(scene.legend), scene.legend.position, canvasSize(state, size), framed);
|
|
188
|
+
}
|
|
189
|
+
/** Where the overview is drawn on this frame: on, with a place, over a picture. */
|
|
190
|
+
function overviewDrawn(state, input, size) {
|
|
191
|
+
if (state.diagram?.overview !== true || input.canvas === undefined)
|
|
192
|
+
return undefined;
|
|
193
|
+
return overviewPlaceOf(state, input.scene, size);
|
|
194
|
+
}
|
|
195
|
+
/** Whether the overview goes as an image: the terminal shows them, and the help is not
|
|
196
|
+
* up — it lies over the overview, and an image sits above the text. */
|
|
197
|
+
const asImage = (d, input) => input.graphics !== undefined && !d.help;
|
|
198
|
+
/** The overview as an image, where it is drawn as one on this frame. */
|
|
199
|
+
export function overviewImage(state, input, size) {
|
|
200
|
+
const [d, place, scene, graphics] = [state.diagram, overviewDrawn(state, input, size), input.scene, input.graphics];
|
|
201
|
+
if (d === undefined || place === undefined || scene === undefined || graphics === undefined || !asImage(d, input))
|
|
202
|
+
return undefined;
|
|
203
|
+
const { width, height } = canvasSize(state, size);
|
|
204
|
+
const window = { scroll: d.scroll, width, height };
|
|
205
|
+
const pixels = overviewPixels(scene.layout, { ...place, cell: graphics.cell }, window, d.selected);
|
|
206
|
+
return { place, width: pixels.width, height: pixels.height, rgba: pixels.rgba(chromeOf(input).ink ?? graphics.ink) };
|
|
207
|
+
}
|
|
208
|
+
function titleOf(d, model) {
|
|
209
|
+
if (model === undefined)
|
|
210
|
+
return d.name;
|
|
211
|
+
const name = (id) => printable(model.nodes.find((n) => n.id === id)?.name ?? id);
|
|
212
|
+
const parts = [[d.name, ...d.path.map(name)].join(' › ')];
|
|
213
|
+
const plane = model.planes.find((p) => p.id === d.plane) ?? model.planes[0];
|
|
214
|
+
if (plane !== undefined)
|
|
215
|
+
parts.push(printable(plane.name));
|
|
216
|
+
const layers = model.layers
|
|
217
|
+
.slice(0, 9)
|
|
218
|
+
.map((l, i) => `[${i + 1}] ${printable(l.name)}${d.layers.includes(l.id) ? ' ✓' : ''}`)
|
|
219
|
+
.join(' ');
|
|
220
|
+
if (layers !== '')
|
|
221
|
+
parts.push(`layers ${layers}`);
|
|
222
|
+
return parts.join(' · ');
|
|
223
|
+
}
|
|
224
|
+
function statusOf(d, input) {
|
|
225
|
+
const chrome = chromeOf(input);
|
|
226
|
+
if (d.search?.editing === true)
|
|
227
|
+
return [`/${d.search.query}_`, chrome.status];
|
|
228
|
+
if (d.message !== undefined)
|
|
229
|
+
return [d.message.text, d.message.error ? chrome.error : chrome.status];
|
|
230
|
+
const node = d.selected === undefined ? undefined : input.model?.nodes.find((n) => n.id === d.selected);
|
|
231
|
+
if (node === undefined)
|
|
232
|
+
return ['arrows move · Enter fold · > go in · / search · i details · ? help · q quit', chrome.hint];
|
|
233
|
+
const subtitle = printable(subtitleOf(node, input.scene?.notation));
|
|
234
|
+
const name = printable(node.name);
|
|
235
|
+
return [`${name}${subtitle === '' ? '' : ` ${subtitle}`} · Enter fold · i details · ? help`, chrome.status];
|
|
236
|
+
}
|
|
237
|
+
function drawPanel(rows, d, input, size) {
|
|
238
|
+
const { panel, rule } = chromeOf(input);
|
|
239
|
+
const beside = size.columns >= PANEL_MIN_COLUMNS;
|
|
240
|
+
const left = beside ? size.columns - PANEL_WIDTH : 0;
|
|
241
|
+
const width = beside ? PANEL_WIDTH - 3 : size.columns - 2;
|
|
242
|
+
const lines = d.selected !== undefined && input.model !== undefined
|
|
243
|
+
? detailLines(input.model, d.selected, width, input.scene?.notation)
|
|
244
|
+
: ['Select a box to see its details.'];
|
|
245
|
+
for (let r = 1; r < size.rows - 1; r++) {
|
|
246
|
+
const row = rows[r];
|
|
247
|
+
for (let c = left; c < size.columns; c++)
|
|
248
|
+
row[c] = { ch: ' ', style: panel };
|
|
249
|
+
if (beside)
|
|
250
|
+
row[left] = { ch: '│', style: rule };
|
|
251
|
+
const line = lines[r - 1];
|
|
252
|
+
if (line !== undefined)
|
|
253
|
+
write(row, left + (beside ? 2 : 1), line, { style: r === 1 ? { ...panel, bold: true } : panel });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** The overview, opaque over the canvas, in a border a cell wide around `place`; blank
|
|
257
|
+
* inside where an image goes over it. */
|
|
258
|
+
function drawOverview(rows, d, scene, places) {
|
|
259
|
+
const { canvas, overview: place, image } = places;
|
|
260
|
+
const { width, height } = canvas;
|
|
261
|
+
const cells = image
|
|
262
|
+
? Array.from({ length: place.height }, () => blankRow(place.width))
|
|
263
|
+
: overviewCells(scene.layout, place, { scroll: d.scroll, width, height }, d.selected);
|
|
264
|
+
const [left, right] = [place.x - 1, place.x + place.width];
|
|
265
|
+
for (let r = -1; r <= place.height; r++) {
|
|
266
|
+
const row = rows[place.y + r];
|
|
267
|
+
if (row === undefined)
|
|
268
|
+
continue;
|
|
269
|
+
if (r === -1 || r === place.height) {
|
|
270
|
+
const [l, rt] = r === -1 ? ['┌', '┐'] : ['└', '┘'];
|
|
271
|
+
write(row, left, `${l}${'─'.repeat(place.width)}${rt}`, { style: DIM });
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
write(row, left, '│', { style: DIM });
|
|
275
|
+
cells[r].forEach((cell, c) => write(row, place.x + c, cell.ch, { style: cell.style }));
|
|
276
|
+
write(row, right, '│', { style: DIM });
|
|
277
|
+
}
|
|
278
|
+
const title = ' overview ';
|
|
279
|
+
if (cellWidth(title) <= place.width - 2)
|
|
280
|
+
write(rows[place.y - 1], left + 2, title, { style: DIM });
|
|
281
|
+
}
|
|
282
|
+
/** The legend, opaque over the canvas, cut to `place`. */
|
|
283
|
+
function drawLegend(rows, scene, place) {
|
|
284
|
+
if (scene.legend === undefined)
|
|
285
|
+
return;
|
|
286
|
+
legendCells(scene.legend, UNICODE, place).forEach((cells, r) => {
|
|
287
|
+
const row = rows[place.y + r];
|
|
288
|
+
if (row !== undefined)
|
|
289
|
+
cells.forEach((cell, c) => write(row, place.x + c, cell.ch, { style: cell.style }));
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
/** A dialog's shadow, where the chrome casts one: two columns right of `box` and a row
|
|
293
|
+
* below, as the DOS style's boxes cast theirs. */
|
|
294
|
+
function shade(rows, box, shadow) {
|
|
295
|
+
if (shadow === undefined)
|
|
296
|
+
return;
|
|
297
|
+
const [right, bottom] = [box.x + box.width, box.y + box.height];
|
|
298
|
+
for (let y = box.y + SHADOW_REACH.y; y < bottom + SHADOW_REACH.y; y++) {
|
|
299
|
+
const row = rows[y];
|
|
300
|
+
for (let x = box.x + SHADOW_REACH.x; x < right + SHADOW_REACH.x; x++) {
|
|
301
|
+
const cell = row?.[x];
|
|
302
|
+
if (cell !== undefined && (x >= right || y >= bottom))
|
|
303
|
+
row[x] = { ch: cell.ch, style: { ...cell.style, ...shadow } };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function drawHelp(rows, area, chrome) {
|
|
308
|
+
const keyWidth = Math.max(...HELP.map(([k]) => cellWidth(k)));
|
|
309
|
+
const lines = HELP.map(([k, what]) => `${k}${' '.repeat(keyWidth - cellWidth(k))} ${what}`);
|
|
310
|
+
const width = Math.min(area.width, Math.max(...lines.map(cellWidth)) + 4);
|
|
311
|
+
const height = Math.min(area.height, lines.length + 2);
|
|
312
|
+
if (width < 3 || height < 2)
|
|
313
|
+
return;
|
|
314
|
+
const x = (area.width - width) >> 1;
|
|
315
|
+
const y = area.top + ((area.height - height) >> 1);
|
|
316
|
+
const [tl, tr, bl, br, across, down] = chrome.frame;
|
|
317
|
+
for (let r = 0; r < height; r++) {
|
|
318
|
+
const row = rows[y + r];
|
|
319
|
+
const edge = r === 0 || r === height - 1;
|
|
320
|
+
const [l, rt] = r === 0 ? [tl, tr] : r === height - 1 ? [bl, br] : [down, down];
|
|
321
|
+
write(row, x, l, { style: chrome.border });
|
|
322
|
+
write(row, x + 1, (edge ? across : ' ').repeat(width - 2), { style: edge ? chrome.border : chrome.dialog });
|
|
323
|
+
write(row, x + width - 1, rt, { style: chrome.border });
|
|
324
|
+
if (!edge)
|
|
325
|
+
write(row, x + 2, lines[r - 1] ?? '', { style: chrome.dialog, maxX: x + width - 1 });
|
|
326
|
+
}
|
|
327
|
+
write(rows[y], x + 2, ' Keys ', { style: { ...chrome.border, bold: true }, maxX: x + width - 1 });
|
|
328
|
+
shade(rows, { x, y, width, height }, chrome.shadow);
|
|
329
|
+
}
|
|
330
|
+
/** The canvas's cells that set no background of their own, laid on the chrome's ground:
|
|
331
|
+
* the picture's lies on it already, but not what lies over the picture. */
|
|
332
|
+
function onGround(rows, area, ground) {
|
|
333
|
+
if (ground.bg === undefined)
|
|
334
|
+
return;
|
|
335
|
+
for (const row of rows.slice(area.top, area.top + area.height)) {
|
|
336
|
+
row.forEach((cell, c) => {
|
|
337
|
+
if (cell.style.bg === undefined)
|
|
338
|
+
row[c] = { ch: cell.ch, style: { ...ground, ...cell.style } };
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function composeDiagram(d, input, size, places) {
|
|
343
|
+
const chrome = chromeOf(input);
|
|
344
|
+
const rows = Array.from({ length: size.rows }, () => blankRow(size.columns, chrome.ground));
|
|
345
|
+
const area = places.canvas;
|
|
346
|
+
rows[0] = bar(size.columns, titleOf(d, input.model), chrome.title);
|
|
347
|
+
if (input.scene === undefined || input.canvas === undefined)
|
|
348
|
+
centred(rows, area, input.notice ?? 'Loading…');
|
|
349
|
+
else if (input.scene.layout.boxes.size === 0)
|
|
350
|
+
centred(rows, area, 'Nothing on this plane');
|
|
351
|
+
else {
|
|
352
|
+
blit(rows, input.canvas, d.scroll, area);
|
|
353
|
+
const place = places.overview;
|
|
354
|
+
if (d.overview && place !== undefined) {
|
|
355
|
+
drawOverview(rows, d, input.scene, { canvas: area, overview: place, image: asImage(d, input) });
|
|
356
|
+
}
|
|
357
|
+
if (d.legend && places.legend !== undefined)
|
|
358
|
+
drawLegend(rows, input.scene, places.legend);
|
|
359
|
+
}
|
|
360
|
+
onGround(rows, area, chrome.ground);
|
|
361
|
+
if (d.panel && size.rows > 2)
|
|
362
|
+
drawPanel(rows, d, input, size);
|
|
363
|
+
if (d.help)
|
|
364
|
+
drawHelp(rows, area, chrome);
|
|
365
|
+
if (size.rows > 1) {
|
|
366
|
+
const [text, style] = statusOf(d, input);
|
|
367
|
+
rows[size.rows - 1] = bar(size.columns, fitCells(text, Math.max(0, size.columns - 2)), style);
|
|
368
|
+
}
|
|
369
|
+
return rows;
|
|
370
|
+
}
|
|
371
|
+
function composeList(state, input, size) {
|
|
372
|
+
const chrome = chromeOf(input);
|
|
373
|
+
const rows = Array.from({ length: size.rows }, () => blankRow(size.columns, chrome.ground));
|
|
374
|
+
const count = state.names.length;
|
|
375
|
+
rows[0] = bar(size.columns, `diagc · ${count} diagram${count === 1 ? '' : 's'}`, chrome.title);
|
|
376
|
+
const area = { top: 1, width: size.columns, height: Math.max(0, size.rows - 2) };
|
|
377
|
+
if (count === 0)
|
|
378
|
+
centred(rows, area, input.notice ?? 'No diagrams under .diagrams/src');
|
|
379
|
+
// keep the current name on screen: scroll the list once it passes the bottom
|
|
380
|
+
const first = Math.max(0, state.index - area.height + 1);
|
|
381
|
+
for (let r = 0; r < area.height && first + r < count; r++) {
|
|
382
|
+
const current = first + r === state.index;
|
|
383
|
+
// the marker as well as the inverse bar: a terminal may show no attributes at all
|
|
384
|
+
const name = state.names[first + r];
|
|
385
|
+
const row = current ? bar(size.columns, `› ${name}`, chrome.current) : blankRow(size.columns, chrome.ground);
|
|
386
|
+
if (!current)
|
|
387
|
+
write(row, 3, name, { style: chrome.ground });
|
|
388
|
+
rows[area.top + r] = row;
|
|
389
|
+
}
|
|
390
|
+
onGround(rows, area, chrome.ground);
|
|
391
|
+
// with names to show, a notice is what the last pick ran into, said in the status row
|
|
392
|
+
const [status, style] = count > 0 && input.notice !== undefined
|
|
393
|
+
? [input.notice, chrome.error]
|
|
394
|
+
: ['Enter open · ↑↓ move · s style · r reload · q quit', chrome.hint];
|
|
395
|
+
if (size.rows > 1)
|
|
396
|
+
rows[size.rows - 1] = bar(size.columns, fitCells(status, Math.max(0, size.columns - 2)), style);
|
|
397
|
+
return rows;
|
|
398
|
+
}
|
|
399
|
+
/** The frame for `state`: `size.rows` lines of `size.columns` cells each. */
|
|
400
|
+
export function composeFrame(state, input, size, mode) {
|
|
401
|
+
const safe = { columns: Math.max(0, size.columns), rows: Math.max(0, size.rows) };
|
|
402
|
+
const rows = state.screen === 'diagram' && state.diagram !== undefined
|
|
403
|
+
? composeDiagram(state.diagram, input, safe, {
|
|
404
|
+
canvas: canvasSize(state, safe),
|
|
405
|
+
overview: overviewPlaceOf(state, input.scene, safe),
|
|
406
|
+
legend: legendPlaceOf(state, input.scene, safe),
|
|
407
|
+
})
|
|
408
|
+
: composeList(state, input, safe);
|
|
409
|
+
return serialize(rows.slice(0, safe.rows), mode, false);
|
|
410
|
+
}
|
|
411
|
+
/** The escapes that put `lines` on the screen in one go: each line at its row, then
|
|
412
|
+
* `images`' commands, inside a synchronized update, so a terminal that has them never
|
|
413
|
+
* shows a frame half drawn. */
|
|
414
|
+
export function frameText(lines, images = '') {
|
|
415
|
+
return `\x1b[?2026h${lines.map((line, i) => `\x1b[${i + 1};1H${line}`).join('')}${images}\x1b[?2026l`;
|
|
416
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { type DiagramModel, type LayoutOverlay } from '@diagc/core/internal';
|
|
2
|
+
import type { CellPoint, CellRect } from '../text/canvas.js';
|
|
3
|
+
import type { TextStyle } from '../text/dos.js';
|
|
4
|
+
import { type Pins } from '../text/folds.js';
|
|
5
|
+
import type { SceneInput, Scene } from '../text/scene.js';
|
|
6
|
+
import type { UserInput } from './keys.js';
|
|
7
|
+
import { type View } from './nav.js';
|
|
8
|
+
import type { OverviewPlace } from './overview.js';
|
|
9
|
+
/** One open diagram, as the reader sees it. None of it is saved. */
|
|
10
|
+
export interface DiagramState {
|
|
11
|
+
name: string;
|
|
12
|
+
/** absent = the model's first plane */
|
|
13
|
+
plane?: string;
|
|
14
|
+
layers: string[];
|
|
15
|
+
pins: Pins;
|
|
16
|
+
/** the drill path; its last entry is the container whose inside is drawn */
|
|
17
|
+
path: string[];
|
|
18
|
+
selected?: string;
|
|
19
|
+
/** the picture's cell at the canvas's top-left corner */
|
|
20
|
+
scroll: CellPoint;
|
|
21
|
+
/** the details panel */
|
|
22
|
+
panel: boolean;
|
|
23
|
+
help: boolean;
|
|
24
|
+
/** the overview, which shows while the picture is bigger than the screen */
|
|
25
|
+
overview: boolean;
|
|
26
|
+
/** the legend, on as a diagram opens where it declares one */
|
|
27
|
+
legend: boolean;
|
|
28
|
+
search?: {
|
|
29
|
+
query: string;
|
|
30
|
+
editing: boolean;
|
|
31
|
+
};
|
|
32
|
+
/** one line for the status row, cleared by the next key */
|
|
33
|
+
message?: {
|
|
34
|
+
text: string;
|
|
35
|
+
error: boolean;
|
|
36
|
+
};
|
|
37
|
+
/** the last mouse press, for telling a double click */
|
|
38
|
+
lastPress?: {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
at: number;
|
|
42
|
+
};
|
|
43
|
+
/** where a drag last was, while it pans the canvas or, started on the overview, moves
|
|
44
|
+
* the screen about it */
|
|
45
|
+
drag?: CellPoint & {
|
|
46
|
+
overview?: true;
|
|
47
|
+
};
|
|
48
|
+
/** scroll the selection into view once the layout it waits for arrives (`settle`) */
|
|
49
|
+
follow?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface TuiState {
|
|
52
|
+
screen: 'list' | 'diagram';
|
|
53
|
+
/** the diagrams there are to open */
|
|
54
|
+
names: string[];
|
|
55
|
+
/** the list's current row */
|
|
56
|
+
index: number;
|
|
57
|
+
diagram?: DiagramState;
|
|
58
|
+
/** the style the reader picked, with `--style` or `s`, for every diagram; absent =
|
|
59
|
+
* each diagram's own */
|
|
60
|
+
style?: TextStyle;
|
|
61
|
+
}
|
|
62
|
+
/** What an event is read against: the open diagram's model and laid-out picture, the
|
|
63
|
+
* canvas's place on the screen, and when the event came (ms). */
|
|
64
|
+
export interface Ctx {
|
|
65
|
+
model?: DiagramModel;
|
|
66
|
+
overlay?: LayoutOverlay;
|
|
67
|
+
scene?: Scene;
|
|
68
|
+
/** the canvas: its first screen row, its size and the overview's corner (`viewOf`) */
|
|
69
|
+
view: View & {
|
|
70
|
+
top: number;
|
|
71
|
+
};
|
|
72
|
+
/** where the overview goes on this screen, whether or not it is on */
|
|
73
|
+
overview?: OverviewPlace;
|
|
74
|
+
/** where the legend goes on this screen, its border included, whether or not it is on */
|
|
75
|
+
legend?: CellRect;
|
|
76
|
+
/** the style the diagram is drawn in; absent = the terminal's own */
|
|
77
|
+
style?: TextStyle;
|
|
78
|
+
at: number;
|
|
79
|
+
}
|
|
80
|
+
export type Effect = {
|
|
81
|
+
kind: 'quit';
|
|
82
|
+
} | {
|
|
83
|
+
kind: 'open';
|
|
84
|
+
name: string;
|
|
85
|
+
} | {
|
|
86
|
+
kind: 'relayout';
|
|
87
|
+
} | {
|
|
88
|
+
kind: 'reload';
|
|
89
|
+
};
|
|
90
|
+
export interface Reduced {
|
|
91
|
+
state: TuiState;
|
|
92
|
+
effects: Effect[];
|
|
93
|
+
}
|
|
94
|
+
/** A diagram as it opens, and as a plane switch reopens it: the plane's own layers and
|
|
95
|
+
* the folds its layout saved, as the published page opens it. */
|
|
96
|
+
export declare function openDiagram(name: string, model: DiagramModel, overlay: LayoutOverlay | undefined, plane?: string): DiagramState;
|
|
97
|
+
/** What to lay out for `state`, in `style`. */
|
|
98
|
+
export declare function sceneInput(state: DiagramState, model: DiagramModel, overlay: LayoutOverlay | undefined, style?: TextStyle): SceneInput;
|
|
99
|
+
/** `state` fitted to a new picture: a selection the picture no longer draws is gone,
|
|
100
|
+
* one that asked to be followed is scrolled to, and the scroll stays on the picture. */
|
|
101
|
+
export declare function settle(state: DiagramState, scene: Scene, view: View): DiagramState;
|
|
102
|
+
export declare function reduce(state: TuiState, event: UserInput, ctx: Ctx): Reduced;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { presetLayers } from '@diagc/core/internal';
|
|
2
|
+
import { readerPins } from '../text/folds.js';
|
|
3
|
+
import { onKey, onMouse, otherStyle } 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`, in `style`. */
|
|
22
|
+
export function sceneInput(state, model, overlay, style) {
|
|
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
|
+
...(style !== undefined ? { style } : {}),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** `state` fitted to a new picture: a selection the picture no longer draws is gone,
|
|
35
|
+
* one that asked to be followed is scrolled to, and the scroll stays on the picture. */
|
|
36
|
+
export function settle(state, scene, view) {
|
|
37
|
+
const { follow: wanted, selected, ...rest } = state;
|
|
38
|
+
const box = selected === undefined ? undefined : scene.layout.boxes.get(selected);
|
|
39
|
+
const scroll = wanted === true && box !== undefined ? follow(state.scroll, box, view) : state.scroll;
|
|
40
|
+
return {
|
|
41
|
+
...rest,
|
|
42
|
+
...(box !== undefined ? { selected } : {}),
|
|
43
|
+
scroll: clampScroll(scroll, scene.layout, view),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function reduceList(state, event) {
|
|
47
|
+
const stay = (index = state.index) => ({ state: { ...state, index }, effects: [] });
|
|
48
|
+
if (event.kind !== 'key')
|
|
49
|
+
return stay();
|
|
50
|
+
const last = Math.max(0, state.names.length - 1);
|
|
51
|
+
if (event.name === 'q' || (event.ctrl === true && event.name === 'c'))
|
|
52
|
+
return { state, effects: [{ kind: 'quit' }] };
|
|
53
|
+
if (event.name === 'up' || event.name === 'k')
|
|
54
|
+
return stay(Math.max(0, state.index - 1));
|
|
55
|
+
if (event.name === 'down' || event.name === 'j')
|
|
56
|
+
return stay(Math.min(last, state.index + 1));
|
|
57
|
+
if (event.name === 'r')
|
|
58
|
+
return { state, effects: [{ kind: 'reload' }] };
|
|
59
|
+
if (event.name === 's')
|
|
60
|
+
return { state: { ...state, style: otherStyle(state.style) }, effects: [] };
|
|
61
|
+
const name = state.names[state.index];
|
|
62
|
+
if (event.name === 'enter' && name !== undefined)
|
|
63
|
+
return { state, effects: [{ kind: 'open', name }] };
|
|
64
|
+
return stay();
|
|
65
|
+
}
|
|
66
|
+
export function reduce(state, event, ctx) {
|
|
67
|
+
if (state.screen === 'list' || state.diagram === undefined)
|
|
68
|
+
return reduceList(state, event);
|
|
69
|
+
const { message: _cleared, ...diagram } = state.diagram;
|
|
70
|
+
const step = event.kind === 'mouse' ? onMouse(diagram, ctx, event) : onKey(diagram, ctx, event);
|
|
71
|
+
return {
|
|
72
|
+
state: {
|
|
73
|
+
...state,
|
|
74
|
+
diagram: step.diagram,
|
|
75
|
+
...(step.toList === true ? { screen: 'list' } : {}),
|
|
76
|
+
...(step.style !== undefined ? { style: step.style } : {}),
|
|
77
|
+
},
|
|
78
|
+
effects: step.effects,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -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
|
+
}
|