@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
package/dist/tui/app.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { watch as chokidarWatch } from 'chokidar';
|
|
5
|
+
import { errMessage, isLayoutOverlay } from '@diagc/core/internal';
|
|
6
|
+
import { discoverDiagrams } from '../publish/discover.js';
|
|
7
|
+
import { lockedResolverFor } from '../snapshots.js';
|
|
8
|
+
import { styleOf } from '../text/dos.js';
|
|
9
|
+
import { UNICODE } from '../text/glyphs.js';
|
|
10
|
+
import { paint } from '../text/paint.js';
|
|
11
|
+
import { buildScene } from '../text/scene.js';
|
|
12
|
+
import { startWatch } from '../watch.js';
|
|
13
|
+
import { isReply, parseInput } from './keys.js';
|
|
14
|
+
import { CELL_SIZE_QUERY, graphicsOf, hear, imageChange, TERMINAL_QUERY } from './kitty.js';
|
|
15
|
+
import { composeFrame, frameText, legendPlaceOf, overviewImage, overviewPlaceOf, viewOf } from './screen.js';
|
|
16
|
+
import { openDiagram, reduce, sceneInput, settle } from './state.js';
|
|
17
|
+
/** What makes the process end while the TUI owns the terminal; each gives it back. */
|
|
18
|
+
const ENDINGS = ['SIGINT', 'SIGTERM', 'SIGHUP', 'uncaughtException', 'unhandledRejection'];
|
|
19
|
+
/** A name from a path under the sources: `team/app.diagram.ts` is `team/app`. */
|
|
20
|
+
const nameIn = (srcDir, file) => path
|
|
21
|
+
.relative(srcDir, file)
|
|
22
|
+
.replace(/\.(diagram\.(ts|json)|layout\.json)$/, '')
|
|
23
|
+
.split(path.sep)
|
|
24
|
+
.join('/');
|
|
25
|
+
/** The studio's compile-watch loop, for the TUI: sources compile into artifacts as they
|
|
26
|
+
* are saved, and a layout's change is a change of its diagram. Compile warnings are
|
|
27
|
+
* dropped rather than printed, which would scribble over the screen; `diagc lint`
|
|
28
|
+
* reports them. */
|
|
29
|
+
export function watchSources(srcDir, artifactsDir, coreEntry) {
|
|
30
|
+
return (onChange) => {
|
|
31
|
+
const sources = startWatch(srcDir, artifactsDir, {
|
|
32
|
+
coreEntry,
|
|
33
|
+
resolver: lockedResolverFor(srcDir),
|
|
34
|
+
warn: () => { },
|
|
35
|
+
onEvent: (e) => onChange(e.ok
|
|
36
|
+
? { name: nameIn(srcDir, e.file), ok: true }
|
|
37
|
+
: { name: nameIn(srcDir, e.file), ok: false, error: e.error }),
|
|
38
|
+
});
|
|
39
|
+
const layouts = chokidarWatch(srcDir, {
|
|
40
|
+
ignoreInitial: true,
|
|
41
|
+
awaitWriteFinish: { stabilityThreshold: 100, pollInterval: 20 },
|
|
42
|
+
});
|
|
43
|
+
const onLayout = (file) => {
|
|
44
|
+
if (file.endsWith('.layout.json'))
|
|
45
|
+
onChange({ name: nameIn(srcDir, file), ok: true });
|
|
46
|
+
};
|
|
47
|
+
layouts.on('add', onLayout).on('change', onLayout);
|
|
48
|
+
return {
|
|
49
|
+
async close() {
|
|
50
|
+
await Promise.all([sources.close(), layouts.close()]);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** A diagram's state carried over to a new version of its model: the plane if it is
|
|
56
|
+
* still there (else the diagram opens afresh), the layers and drill path that remain. */
|
|
57
|
+
function carried(d, model, overlay) {
|
|
58
|
+
if (d.plane !== undefined && !model.planes.some((p) => p.id === d.plane))
|
|
59
|
+
return openDiagram(d.name, model, overlay);
|
|
60
|
+
const nodes = new Set(model.nodes.map((n) => n.id));
|
|
61
|
+
const kept = d.path.findIndex((id) => !nodes.has(id));
|
|
62
|
+
return {
|
|
63
|
+
...d,
|
|
64
|
+
layers: d.layers.filter((id) => model.layers.some((l) => l.id === id)),
|
|
65
|
+
path: kept < 0 ? d.path : d.path.slice(0, kept),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
class App {
|
|
69
|
+
options;
|
|
70
|
+
state = { screen: 'list', names: [], index: 0 };
|
|
71
|
+
model;
|
|
72
|
+
overlay;
|
|
73
|
+
scene;
|
|
74
|
+
canvas;
|
|
75
|
+
notice;
|
|
76
|
+
/** a diagram asked for by name that has not compiled yet */
|
|
77
|
+
pending;
|
|
78
|
+
/** the newest layout asked for; an older one that finishes later is dropped */
|
|
79
|
+
layoutSeq = 0;
|
|
80
|
+
/** what the terminal has said about itself, in answer to `TERMINAL_QUERY` */
|
|
81
|
+
heard = {};
|
|
82
|
+
/** the image on the screen, which a frame changes only when it has to */
|
|
83
|
+
image;
|
|
84
|
+
/** the image may not be where it was: a resize can move or clear it */
|
|
85
|
+
imageStale = false;
|
|
86
|
+
watcher;
|
|
87
|
+
finished = false;
|
|
88
|
+
settleRun = { resolve: () => { }, reject: () => { } };
|
|
89
|
+
onEnding = (reason) => {
|
|
90
|
+
void this.finish(reason instanceof Error ? reason : undefined);
|
|
91
|
+
};
|
|
92
|
+
constructor(options) {
|
|
93
|
+
this.options = options;
|
|
94
|
+
if (options.style !== undefined)
|
|
95
|
+
this.state.style = options.style;
|
|
96
|
+
}
|
|
97
|
+
run() {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
this.settleRun = { resolve, reject };
|
|
100
|
+
for (const ending of ENDINGS)
|
|
101
|
+
process.on(ending, this.onEnding);
|
|
102
|
+
const { terminal } = this.options;
|
|
103
|
+
terminal.onInput((data) => this.guard(() => this.input(data)));
|
|
104
|
+
terminal.onResize(() => this.guard(() => this.resized()));
|
|
105
|
+
terminal.start();
|
|
106
|
+
terminal.write(TERMINAL_QUERY);
|
|
107
|
+
this.watcher = this.options.watch((change) => this.later(() => this.changed(change)));
|
|
108
|
+
this.later(() => this.boot());
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** Ends the run once, the terminal given back first: whatever happens after must not
|
|
112
|
+
* happen on a screen left in raw mode. */
|
|
113
|
+
async finish(error) {
|
|
114
|
+
if (this.finished)
|
|
115
|
+
return;
|
|
116
|
+
this.finished = true;
|
|
117
|
+
for (const ending of ENDINGS)
|
|
118
|
+
process.off(ending, this.onEnding);
|
|
119
|
+
try {
|
|
120
|
+
if (this.image !== undefined)
|
|
121
|
+
this.options.terminal.write(imageChange(this.image, undefined));
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// a terminal that cannot take the image's removal still has to be given back
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
this.options.terminal.stop();
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
await this.watcher?.close().catch(() => { });
|
|
131
|
+
if (error === undefined)
|
|
132
|
+
this.settleRun.resolve();
|
|
133
|
+
else
|
|
134
|
+
this.settleRun.reject(error);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
guard(step) {
|
|
138
|
+
if (this.finished)
|
|
139
|
+
return;
|
|
140
|
+
try {
|
|
141
|
+
step();
|
|
142
|
+
}
|
|
143
|
+
catch (e) {
|
|
144
|
+
void this.finish(e);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
later(step) {
|
|
148
|
+
if (!this.finished)
|
|
149
|
+
step().catch((e) => void this.finish(e));
|
|
150
|
+
}
|
|
151
|
+
size() {
|
|
152
|
+
return { columns: this.options.terminal.columns, rows: this.options.terminal.rows };
|
|
153
|
+
}
|
|
154
|
+
/** The style the screen is drawn in: the reader's pick, else the open diagram's own.
|
|
155
|
+
* Without colour it is the terminal's own, DOS being its colours more than its lines. */
|
|
156
|
+
style() {
|
|
157
|
+
if (this.options.color === 'none' || this.options.color === 'mono')
|
|
158
|
+
return 'clean';
|
|
159
|
+
const own = this.state.screen === 'diagram' && this.model !== undefined ? styleOf(this.model) : 'clean';
|
|
160
|
+
return this.state.style ?? own;
|
|
161
|
+
}
|
|
162
|
+
draw() {
|
|
163
|
+
if (this.finished)
|
|
164
|
+
return;
|
|
165
|
+
const graphics = graphicsOf(this.heard);
|
|
166
|
+
const input = {
|
|
167
|
+
...(this.model !== undefined ? { model: this.model } : {}),
|
|
168
|
+
...(this.scene !== undefined ? { scene: this.scene } : {}),
|
|
169
|
+
...(this.canvas !== undefined ? { canvas: this.canvas } : {}),
|
|
170
|
+
...(this.notice !== undefined ? { notice: this.notice } : {}),
|
|
171
|
+
...(graphics !== undefined ? { graphics } : {}),
|
|
172
|
+
style: this.style(),
|
|
173
|
+
};
|
|
174
|
+
const size = this.size();
|
|
175
|
+
const lines = composeFrame(this.state, input, size, this.options.color);
|
|
176
|
+
const image = overviewImage(this.state, input, size);
|
|
177
|
+
const change = this.imageStale
|
|
178
|
+
? imageChange(this.image, undefined) + imageChange(undefined, image)
|
|
179
|
+
: imageChange(this.image, image);
|
|
180
|
+
[this.image, this.imageStale] = [image, false];
|
|
181
|
+
this.options.terminal.write(frameText(lines, change));
|
|
182
|
+
}
|
|
183
|
+
/** A reply to the query: the overview turns into an image once the terminal says it
|
|
184
|
+
* shows them, and is redrawn at a new cell size. Off the diagram screen there is no
|
|
185
|
+
* overview to redraw, and nothing drawn yet that is worth drawing early. */
|
|
186
|
+
replied(reply) {
|
|
187
|
+
const before = JSON.stringify(graphicsOf(this.heard));
|
|
188
|
+
this.heard = hear(this.heard, reply);
|
|
189
|
+
if (this.state.screen === 'diagram' && JSON.stringify(graphicsOf(this.heard)) !== before)
|
|
190
|
+
this.draw();
|
|
191
|
+
}
|
|
192
|
+
repaint() {
|
|
193
|
+
const selected = this.state.diagram?.selected;
|
|
194
|
+
this.canvas =
|
|
195
|
+
this.scene === undefined
|
|
196
|
+
? undefined
|
|
197
|
+
: paint(this.scene, { glyphs: UNICODE, ...(selected !== undefined ? { selected } : {}) }).cells(UNICODE);
|
|
198
|
+
}
|
|
199
|
+
async names() {
|
|
200
|
+
return (await discoverDiagrams(this.options.artifactsDir, this.options.srcDir)).map((d) => d.name);
|
|
201
|
+
}
|
|
202
|
+
async boot() {
|
|
203
|
+
const names = await this.names();
|
|
204
|
+
this.state = { ...this.state, names };
|
|
205
|
+
const wanted = this.options.name;
|
|
206
|
+
if (wanted === undefined)
|
|
207
|
+
return this.draw();
|
|
208
|
+
this.state.index = Math.max(0, names.indexOf(wanted));
|
|
209
|
+
if (names.includes(wanted))
|
|
210
|
+
return this.open(wanted);
|
|
211
|
+
this.pending = wanted;
|
|
212
|
+
this.notice = `Compiling ${wanted}…`;
|
|
213
|
+
// a diagram screen without a picture yet, so the notice shows where it will be
|
|
214
|
+
this.state = {
|
|
215
|
+
...this.state,
|
|
216
|
+
screen: 'diagram',
|
|
217
|
+
diagram: {
|
|
218
|
+
name: wanted,
|
|
219
|
+
layers: [],
|
|
220
|
+
pins: {},
|
|
221
|
+
path: [],
|
|
222
|
+
scroll: { x: 0, y: 0 },
|
|
223
|
+
panel: false,
|
|
224
|
+
help: false,
|
|
225
|
+
overview: true,
|
|
226
|
+
legend: false,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
this.draw();
|
|
230
|
+
}
|
|
231
|
+
/** The diagram's model and layout. A layout that does not read is left out and
|
|
232
|
+
* named in `problem` — it only steers the picture, as `show` treats it. A model that
|
|
233
|
+
* does not read throws. */
|
|
234
|
+
async load(name) {
|
|
235
|
+
const model = JSON.parse(await readFile(path.join(this.options.artifactsDir, `${name}.diagram.json`), 'utf8'));
|
|
236
|
+
const layoutFile = path.join(this.options.srcDir, `${name}.layout.json`);
|
|
237
|
+
if (!existsSync(layoutFile))
|
|
238
|
+
return { model, overlay: undefined };
|
|
239
|
+
try {
|
|
240
|
+
const parsed = JSON.parse(await readFile(layoutFile, 'utf8'));
|
|
241
|
+
if (isLayoutOverlay(parsed))
|
|
242
|
+
return { model, overlay: parsed };
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
// said below, as a layout that does not read
|
|
246
|
+
}
|
|
247
|
+
return { model, overlay: undefined, problem: `ignoring ${name}.layout.json: not a layout file` };
|
|
248
|
+
}
|
|
249
|
+
/** `load`, with a failure said where the reader is looking instead of ending the run:
|
|
250
|
+
* the open diagram's status row, or the list's. */
|
|
251
|
+
async tryLoad(name) {
|
|
252
|
+
try {
|
|
253
|
+
return await this.load(name);
|
|
254
|
+
}
|
|
255
|
+
catch (e) {
|
|
256
|
+
this.say(`cannot read ${name}: ${errMessage(e)}`);
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
say(text) {
|
|
261
|
+
const d = this.state.diagram;
|
|
262
|
+
if (this.state.screen === 'diagram' && d !== undefined) {
|
|
263
|
+
this.state = { ...this.state, diagram: { ...d, message: { text, error: true } } };
|
|
264
|
+
}
|
|
265
|
+
else
|
|
266
|
+
this.notice = text;
|
|
267
|
+
this.draw();
|
|
268
|
+
}
|
|
269
|
+
withProblem(d, problem) {
|
|
270
|
+
return problem === undefined ? d : { ...d, message: { text: problem, error: true } };
|
|
271
|
+
}
|
|
272
|
+
async open(name) {
|
|
273
|
+
const loaded = await this.tryLoad(name);
|
|
274
|
+
if (loaded === undefined)
|
|
275
|
+
return;
|
|
276
|
+
const { model, overlay, problem } = loaded;
|
|
277
|
+
[this.model, this.overlay, this.scene, this.canvas, this.notice] = [
|
|
278
|
+
model,
|
|
279
|
+
overlay,
|
|
280
|
+
undefined,
|
|
281
|
+
undefined,
|
|
282
|
+
undefined,
|
|
283
|
+
];
|
|
284
|
+
const diagram = this.withProblem(openDiagram(name, model, overlay), problem);
|
|
285
|
+
this.state = { ...this.state, screen: 'diagram', diagram };
|
|
286
|
+
await this.relayout();
|
|
287
|
+
}
|
|
288
|
+
async reload() {
|
|
289
|
+
const d = this.state.diagram;
|
|
290
|
+
if (this.state.screen === 'list' || d === undefined || this.model === undefined) {
|
|
291
|
+
this.state = { ...this.state, names: await this.names() };
|
|
292
|
+
return this.draw();
|
|
293
|
+
}
|
|
294
|
+
const loaded = await this.tryLoad(d.name);
|
|
295
|
+
if (loaded === undefined)
|
|
296
|
+
return;
|
|
297
|
+
[this.model, this.overlay] = [loaded.model, loaded.overlay];
|
|
298
|
+
this.state = { ...this.state, diagram: this.withProblem(carried(d, loaded.model, loaded.overlay), loaded.problem) };
|
|
299
|
+
await this.relayout();
|
|
300
|
+
}
|
|
301
|
+
async relayout() {
|
|
302
|
+
const seq = ++this.layoutSeq;
|
|
303
|
+
const d = this.state.diagram;
|
|
304
|
+
if (d === undefined || this.model === undefined)
|
|
305
|
+
return this.draw();
|
|
306
|
+
let scene;
|
|
307
|
+
try {
|
|
308
|
+
scene = await buildScene(sceneInput(d, this.model, this.overlay, this.style()), UNICODE);
|
|
309
|
+
}
|
|
310
|
+
catch (e) {
|
|
311
|
+
this.state = { ...this.state, diagram: { ...d, message: { text: errMessage(e), error: true } } };
|
|
312
|
+
return this.draw();
|
|
313
|
+
}
|
|
314
|
+
if (seq !== this.layoutSeq || this.state.diagram === undefined)
|
|
315
|
+
return;
|
|
316
|
+
this.scene = scene;
|
|
317
|
+
// the state as it is now: keys pressed while elk worked have moved it on
|
|
318
|
+
this.state = { ...this.state, diagram: settle(this.state.diagram, scene, viewOf(this.state, scene, this.size())) };
|
|
319
|
+
this.repaint();
|
|
320
|
+
this.draw();
|
|
321
|
+
}
|
|
322
|
+
runEffect(effect) {
|
|
323
|
+
if (effect.kind === 'quit')
|
|
324
|
+
void this.finish();
|
|
325
|
+
else if (effect.kind === 'open')
|
|
326
|
+
this.later(() => this.open(effect.name));
|
|
327
|
+
else if (effect.kind === 'reload')
|
|
328
|
+
this.later(() => this.reload());
|
|
329
|
+
else
|
|
330
|
+
this.later(() => this.relayout());
|
|
331
|
+
}
|
|
332
|
+
input(data) {
|
|
333
|
+
// what the list said about the last pick is answered by the next key
|
|
334
|
+
if (this.state.screen === 'list' && this.pending === undefined)
|
|
335
|
+
this.notice = undefined;
|
|
336
|
+
for (const event of parseInput(data)) {
|
|
337
|
+
if (this.finished)
|
|
338
|
+
return;
|
|
339
|
+
if (isReply(event)) {
|
|
340
|
+
this.replied(event);
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const size = this.size();
|
|
344
|
+
const ctx = {
|
|
345
|
+
model: this.model,
|
|
346
|
+
overlay: this.overlay,
|
|
347
|
+
scene: this.scene,
|
|
348
|
+
view: viewOf(this.state, this.scene, size),
|
|
349
|
+
overview: overviewPlaceOf(this.state, this.scene, size),
|
|
350
|
+
legend: legendPlaceOf(this.state, this.scene, size),
|
|
351
|
+
style: this.style(),
|
|
352
|
+
at: (this.options.now ?? Date.now)(),
|
|
353
|
+
};
|
|
354
|
+
const { state, effects } = reduce(this.state, event, ctx);
|
|
355
|
+
this.state = state;
|
|
356
|
+
for (const effect of effects)
|
|
357
|
+
this.runEffect(effect);
|
|
358
|
+
// a new layout repaints when it lands; anything else repaints now, which is
|
|
359
|
+
// what shows a new selection
|
|
360
|
+
if (!this.finished && !effects.some((e) => e.kind === 'relayout')) {
|
|
361
|
+
this.repaint();
|
|
362
|
+
this.draw();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
resized() {
|
|
367
|
+
if (graphicsOf(this.heard) !== undefined)
|
|
368
|
+
this.options.terminal.write(CELL_SIZE_QUERY);
|
|
369
|
+
this.imageStale = true;
|
|
370
|
+
if (this.state.diagram !== undefined && this.scene !== undefined) {
|
|
371
|
+
this.state = {
|
|
372
|
+
...this.state,
|
|
373
|
+
diagram: settle(this.state.diagram, this.scene, viewOf(this.state, this.scene, this.size())),
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
this.draw();
|
|
377
|
+
}
|
|
378
|
+
async changed(change) {
|
|
379
|
+
const open = this.state.diagram?.name;
|
|
380
|
+
if (!change.ok) {
|
|
381
|
+
if (open === change.name && this.state.diagram !== undefined) {
|
|
382
|
+
const text = `${change.name} does not compile: ${(change.error ?? '').split('\n')[0]}`;
|
|
383
|
+
this.state = { ...this.state, diagram: { ...this.state.diagram, message: { text, error: true } } };
|
|
384
|
+
}
|
|
385
|
+
return this.draw();
|
|
386
|
+
}
|
|
387
|
+
const names = await this.names();
|
|
388
|
+
this.state = { ...this.state, names, index: Math.min(this.state.index, Math.max(0, names.length - 1)) };
|
|
389
|
+
if (this.pending === change.name) {
|
|
390
|
+
this.pending = undefined;
|
|
391
|
+
return this.open(change.name);
|
|
392
|
+
}
|
|
393
|
+
if (open === change.name && this.model !== undefined)
|
|
394
|
+
return this.reload();
|
|
395
|
+
this.draw();
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/** The TUI, until it is quit. The terminal is given back however the run ends; an
|
|
399
|
+
* error that ended it rejects the promise once it has been. */
|
|
400
|
+
export function runTui(options) {
|
|
401
|
+
return new App(options).run();
|
|
402
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type DiagramModel, type NotationId } from '@diagc/core/internal';
|
|
2
|
+
/** The panel's lines for node `id`, each at most `width` cells; none for an unknown id. */
|
|
3
|
+
export declare function detailLines(model: DiagramModel, id: string, width: number, notation?: NotationId): string[];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { relationLabels } from '@diagc/core/internal';
|
|
2
|
+
import { subtitleOf } from '../text/vocabulary.js';
|
|
3
|
+
import { cellWidth, printable, wrapCells } from '../text/width.js';
|
|
4
|
+
// The details panel: what the picture cannot say in a box — the description, every
|
|
5
|
+
// relation in and out (folded or not), threats, comments and links.
|
|
6
|
+
const nameOf = (model, id) => printable(model.nodes.find((n) => n.id === id)?.name ?? id);
|
|
7
|
+
/** Prose that may hold line breaks, as one panel line per line it has. */
|
|
8
|
+
const paragraphs = (text) => text.split(/\r?\n/).map(printable);
|
|
9
|
+
const labelOf = (labels) => labels
|
|
10
|
+
.map((l) => printable(l.text).trim())
|
|
11
|
+
.filter((t) => t !== '')
|
|
12
|
+
.join(' / ');
|
|
13
|
+
/** A titled section, or nothing when it has no lines. */
|
|
14
|
+
const section = (title, lines) => (lines.length === 0 ? [] : ['', title, ...lines]);
|
|
15
|
+
function relations(model, id) {
|
|
16
|
+
const line = (arrow, other, label) => `${arrow} ${nameOf(model, other)}${label === '' ? '' : ` ${label}`}`;
|
|
17
|
+
const out = model.relations.filter((r) => r.from === id).map((r) => line('→', r.to, labelOf(relationLabels(r))));
|
|
18
|
+
const into = model.relations.filter((r) => r.to === id).map((r) => line('←', r.from, labelOf(relationLabels(r))));
|
|
19
|
+
return [...section('Out', out), ...section('In', into)];
|
|
20
|
+
}
|
|
21
|
+
function notes(node) {
|
|
22
|
+
const threats = (node.threats ?? []).map((t) => `[${t.category}] ${printable(t.title)} (${t.severity ?? 'unrated'}, ${t.status ?? 'open'})`);
|
|
23
|
+
const comments = (node.comments ?? []).flatMap((c) => {
|
|
24
|
+
const who = printable([c.by, c.at].filter((s) => s !== undefined).join(' '));
|
|
25
|
+
const [first = '', ...rest] = paragraphs(c.text);
|
|
26
|
+
return [who === '' ? first : `${who}: ${first}`, ...rest];
|
|
27
|
+
});
|
|
28
|
+
const links = [
|
|
29
|
+
...(node.links ?? []).map((l) => printable(`${l.label} ${l.url}`)),
|
|
30
|
+
...(node.link !== undefined ? [printable(`link ${node.link}`)] : []),
|
|
31
|
+
];
|
|
32
|
+
return [...section('Threats', threats), ...section('Comments', comments), ...section('Links', links)];
|
|
33
|
+
}
|
|
34
|
+
/** The panel's lines for node `id`, each at most `width` cells; none for an unknown id. */
|
|
35
|
+
export function detailLines(model, id, width, notation) {
|
|
36
|
+
const node = model.nodes.find((n) => n.id === id);
|
|
37
|
+
if (node === undefined)
|
|
38
|
+
return [];
|
|
39
|
+
const subtitle = subtitleOf(node, notation);
|
|
40
|
+
const parents = [...new Set(model.containment.filter((c) => c.child === id).map((c) => c.parent))];
|
|
41
|
+
const lines = [
|
|
42
|
+
printable(node.name),
|
|
43
|
+
...(subtitle !== '' ? [printable(subtitle)] : node.technology !== undefined ? [printable(node.technology)] : []),
|
|
44
|
+
`id ${id}`,
|
|
45
|
+
...(node.description !== undefined ? ['', ...paragraphs(node.description)] : []),
|
|
46
|
+
...(parents.length > 0 ? ['', `Inside ${parents.map((p) => nameOf(model, p)).join(', ')}`] : []),
|
|
47
|
+
...relations(model, id),
|
|
48
|
+
...notes(node),
|
|
49
|
+
];
|
|
50
|
+
// A line that fits keeps its spacing (the gap before a relation's label); only one
|
|
51
|
+
// that does not is wrapped.
|
|
52
|
+
const max = Math.max(1, width);
|
|
53
|
+
return lines.flatMap((line) => (cellWidth(line) <= max ? [line] : wrapCells(line, max)));
|
|
54
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface Key {
|
|
2
|
+
kind: 'key';
|
|
3
|
+
/** `up down left right enter space tab backspace escape pageup pagedown home end`,
|
|
4
|
+
* or the printable character itself */
|
|
5
|
+
name: string;
|
|
6
|
+
shift?: boolean;
|
|
7
|
+
ctrl?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface Mouse {
|
|
10
|
+
kind: 'mouse';
|
|
11
|
+
action: 'press' | 'release' | 'drag' | 'wheel-up' | 'wheel-down' | 'wheel-left' | 'wheel-right';
|
|
12
|
+
/** the cell, counted from 0 */
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
shift: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** What the terminal says back to `TERMINAL_QUERY`, which nobody typed. */
|
|
18
|
+
export type Reply = {
|
|
19
|
+
kind: 'graphics';
|
|
20
|
+
ok: boolean;
|
|
21
|
+
} | {
|
|
22
|
+
kind: 'cell-size';
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
} | {
|
|
26
|
+
kind: 'text-color';
|
|
27
|
+
rgb: [number, number, number];
|
|
28
|
+
};
|
|
29
|
+
/** What the reader typed or did with the mouse. */
|
|
30
|
+
export type UserInput = Key | Mouse;
|
|
31
|
+
export type InputEvent = UserInput | Reply;
|
|
32
|
+
export declare const isReply: (event: InputEvent) => event is Reply;
|
|
33
|
+
export declare function parseInput(data: string): InputEvent[];
|
package/dist/tui/keys.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// What a terminal in raw mode sends, read into key presses, mouse reports and its
|
|
2
|
+
// replies to the TUI's query. One read can hold several (a paste, keys typed faster
|
|
3
|
+
// than they are read), so the input is consumed sequence by sequence.
|
|
4
|
+
export const isReply = (event) => event.kind !== 'key' && event.kind !== 'mouse';
|
|
5
|
+
const LETTER_KEYS = { A: 'up', B: 'down', C: 'right', D: 'left', H: 'home', F: 'end' };
|
|
6
|
+
const TILDE_KEYS = {
|
|
7
|
+
'1': 'home',
|
|
8
|
+
'7': 'home',
|
|
9
|
+
'4': 'end',
|
|
10
|
+
'8': 'end',
|
|
11
|
+
'5': 'pageup',
|
|
12
|
+
'6': 'pagedown',
|
|
13
|
+
};
|
|
14
|
+
const WHEEL = ['wheel-up', 'wheel-down', 'wheel-left', 'wheel-right'];
|
|
15
|
+
/** xterm's modifier parameter: 1 + shift(1) + alt(2) + ctrl(4). */
|
|
16
|
+
function modifiers(param) {
|
|
17
|
+
const bits = param === undefined ? 0 : Number(param) - 1;
|
|
18
|
+
return { ...((bits & 1) !== 0 ? { shift: true } : {}), ...((bits & 4) !== 0 ? { ctrl: true } : {}) };
|
|
19
|
+
}
|
|
20
|
+
/** An SGR (1006) mouse report: `ESC [ < button ; x ; y M` (press) or `m` (release). */
|
|
21
|
+
function mouseOf(button, x, y, release) {
|
|
22
|
+
const shift = (button & 4) !== 0;
|
|
23
|
+
const at = { kind: 'mouse', x: x - 1, y: y - 1, shift };
|
|
24
|
+
if ((button & 64) !== 0)
|
|
25
|
+
return { ...at, action: WHEEL[button & 3] };
|
|
26
|
+
if (release)
|
|
27
|
+
return { ...at, action: 'release' };
|
|
28
|
+
return { ...at, action: (button & 32) !== 0 ? 'drag' : 'press' };
|
|
29
|
+
}
|
|
30
|
+
/** A colour channel of an `rgb:` reply, one to four hex digits, as 0–255. */
|
|
31
|
+
const channel = (hex) => Math.round((parseInt(hex, 16) / (16 ** hex.length - 1)) * 255);
|
|
32
|
+
/** A string sequence, which the terminal ends with ST or, after `]`, BEL: its body and
|
|
33
|
+
* its length — or, cut short, no body and everything left, so none of it reads as keys. */
|
|
34
|
+
function stringSequence(body) {
|
|
35
|
+
// eslint-disable-next-line no-control-regex -- the terminators are control characters
|
|
36
|
+
const end = /\x1b\\|\x07/.exec(body);
|
|
37
|
+
return end === null ? [undefined, body.length + 1] : [body.slice(0, end.index), end.index + end[0].length + 1];
|
|
38
|
+
}
|
|
39
|
+
/** A reply to the query, or a string sequence (`ESC _`, `ESC ]`) of any other kind,
|
|
40
|
+
* read as nothing. Undefined for neither. */
|
|
41
|
+
function reply(body) {
|
|
42
|
+
if (body.startsWith('_') || body.startsWith(']')) {
|
|
43
|
+
const [text = '', length] = stringSequence(body);
|
|
44
|
+
const graphics = /^_G[^;]*;(.*)$/s.exec(text);
|
|
45
|
+
if (graphics !== null)
|
|
46
|
+
return [{ kind: 'graphics', ok: graphics[1] === 'OK' }, length];
|
|
47
|
+
const color = /^\]10;rgb:([0-9a-f]{1,4})\/([0-9a-f]{1,4})\/([0-9a-f]{1,4})$/i.exec(text);
|
|
48
|
+
const rgb = color === null ? undefined : [color[1], color[2], color[3]].map(channel);
|
|
49
|
+
return [rgb === undefined ? undefined : { kind: 'text-color', rgb }, length];
|
|
50
|
+
}
|
|
51
|
+
const m = /^\[6;(\d+);(\d+)t/.exec(body);
|
|
52
|
+
return m === null ? undefined : [{ kind: 'cell-size', width: Number(m[2]), height: Number(m[1]) }, m[0].length + 1];
|
|
53
|
+
}
|
|
54
|
+
/** The escape sequence at the start of `s`, which starts with ESC, read. The patterns
|
|
55
|
+
* match what follows the ESC. */
|
|
56
|
+
function escape(s) {
|
|
57
|
+
const body = s.slice(1);
|
|
58
|
+
const answer = reply(body);
|
|
59
|
+
if (answer !== undefined)
|
|
60
|
+
return answer;
|
|
61
|
+
let m = /^\[<(\d+);(\d+);(\d+)([Mm])/.exec(body);
|
|
62
|
+
if (m !== null)
|
|
63
|
+
return [mouseOf(Number(m[1]), Number(m[2]), Number(m[3]), m[4] === 'm'), m[0].length + 1];
|
|
64
|
+
if (body.startsWith('[Z'))
|
|
65
|
+
return [{ kind: 'key', name: 'tab', shift: true }, 3];
|
|
66
|
+
m = /^(?:\[(?:1;(\d+))?|O)([ABCDHF])/.exec(body);
|
|
67
|
+
if (m !== null)
|
|
68
|
+
return [{ kind: 'key', name: LETTER_KEYS[m[2]], ...modifiers(m[1]) }, m[0].length + 1];
|
|
69
|
+
m = /^\[(\d+)(?:;(\d+))?~/.exec(body);
|
|
70
|
+
if (m !== null) {
|
|
71
|
+
const name = TILDE_KEYS[m[1]];
|
|
72
|
+
return [name === undefined ? undefined : { kind: 'key', name, ...modifiers(m[2]) }, m[0].length + 1];
|
|
73
|
+
}
|
|
74
|
+
// Anything else that looks like a control sequence is consumed whole, so its
|
|
75
|
+
// letters do not arrive as keys; a lone ESC is the escape key.
|
|
76
|
+
m = /^\[[0-9;?<>=]*[@-~]/.exec(body);
|
|
77
|
+
if (m !== null)
|
|
78
|
+
return [undefined, m[0].length + 1];
|
|
79
|
+
return [{ kind: 'key', name: 'escape' }, 1];
|
|
80
|
+
}
|
|
81
|
+
const SINGLE = {
|
|
82
|
+
'\r': 'enter',
|
|
83
|
+
'\n': 'enter',
|
|
84
|
+
'\t': 'tab',
|
|
85
|
+
'\x7f': 'backspace',
|
|
86
|
+
'\b': 'backspace',
|
|
87
|
+
' ': 'space',
|
|
88
|
+
};
|
|
89
|
+
/** The one key at the start of `s`, which is no escape sequence. */
|
|
90
|
+
function plain(s) {
|
|
91
|
+
const ch = String.fromCodePoint(s.codePointAt(0));
|
|
92
|
+
const named = SINGLE[ch];
|
|
93
|
+
if (named !== undefined)
|
|
94
|
+
return [{ kind: 'key', name: named }, ch.length];
|
|
95
|
+
const code = ch.codePointAt(0);
|
|
96
|
+
// Ctrl+A … Ctrl+Z arrive as 1 … 26
|
|
97
|
+
if (code >= 1 && code <= 26)
|
|
98
|
+
return [{ kind: 'key', name: String.fromCharCode(96 + code), ctrl: true }, 1];
|
|
99
|
+
if (code < 0x20)
|
|
100
|
+
return [undefined, 1];
|
|
101
|
+
return [{ kind: 'key', name: ch }, ch.length];
|
|
102
|
+
}
|
|
103
|
+
export function parseInput(data) {
|
|
104
|
+
const events = [];
|
|
105
|
+
for (let i = 0; i < data.length;) {
|
|
106
|
+
const rest = data.slice(i);
|
|
107
|
+
const [event, length] = rest.startsWith('\x1b') ? escape(rest) : plain(rest);
|
|
108
|
+
if (event !== undefined)
|
|
109
|
+
events.push(event);
|
|
110
|
+
i += length;
|
|
111
|
+
}
|
|
112
|
+
return events;
|
|
113
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CellRect } from '../text/canvas.js';
|
|
2
|
+
import type { Reply } from './keys.js';
|
|
3
|
+
import type { CellPixels } from './overview.js';
|
|
4
|
+
/** What the TUI asks the terminal as it starts: its text colour (OSC 10), its cell's
|
|
5
|
+
* size in pixels (CSI 16 t), and whether it shows images, by the query the protocol
|
|
6
|
+
* keeps for that, which a terminal without it ignores. Terminals answer in order, so
|
|
7
|
+
* by the time the last answer comes the others have. */
|
|
8
|
+
export declare const TERMINAL_QUERY = "\u001B]10;?\u001B\\\u001B[16t\u001B_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\u001B\\";
|
|
9
|
+
/** The cell's size again, after a resize: a font size change is one. */
|
|
10
|
+
export declare const CELL_SIZE_QUERY = "\u001B[16t";
|
|
11
|
+
/** The overview's image id: any number, since the TUI's screen is its own. */
|
|
12
|
+
export declare const OVERVIEW_IMAGE = 1684627815;
|
|
13
|
+
/** What the terminal has said about itself. */
|
|
14
|
+
export interface Heard {
|
|
15
|
+
images?: boolean;
|
|
16
|
+
cell?: CellPixels;
|
|
17
|
+
ink?: [number, number, number];
|
|
18
|
+
}
|
|
19
|
+
/** A terminal that shows images: its cell's size in pixels, and its text colour, which
|
|
20
|
+
* the overview is drawn in, so it looks like the rest of the screen's text. */
|
|
21
|
+
export interface Graphics {
|
|
22
|
+
cell: CellPixels;
|
|
23
|
+
ink: [number, number, number];
|
|
24
|
+
}
|
|
25
|
+
/** An image for a frame, `width` × `height` pixels of RGBA, over the cells of `place`. */
|
|
26
|
+
export interface FrameImage {
|
|
27
|
+
place: CellRect;
|
|
28
|
+
width: number;
|
|
29
|
+
height: number;
|
|
30
|
+
rgba: Uint8Array;
|
|
31
|
+
}
|
|
32
|
+
export declare function hear(heard: Heard, reply: Reply): Heard;
|
|
33
|
+
export declare function graphicsOf(heard: Heard): Graphics | undefined;
|
|
34
|
+
/** What turns the image `shown` into `next` on the screen: nothing when they are the same,
|
|
35
|
+
* which is most frames, since every frame rewrites the text but an image stays put. */
|
|
36
|
+
export declare function imageChange(shown: FrameImage | undefined, next: FrameImage | undefined): string;
|