@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.
- package/assets/guide/index.md +1 -1
- package/assets/skill/SKILL.md +1 -1
- package/assets/studio/assets/{index-B4EbTBLO.js → index-DRsUukzt.js} +48 -46
- package/assets/studio/index.html +1 -1
- package/dist/api/diagrams.d.ts +5 -0
- package/dist/api/diagrams.js +37 -24
- package/dist/api/dispatch.d.ts +3 -0
- package/dist/api/handlers.d.ts +1 -0
- package/dist/api/handlers.js +1 -0
- package/dist/api/pages.d.ts +21 -0
- package/dist/api/pages.js +50 -0
- package/dist/api/routes.d.ts +2 -0
- package/dist/api/routes.js +6 -0
- 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 +23 -1
- 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 +36 -0
- package/dist/init.d.ts +1 -1
- package/dist/init.js +1 -1
- package/dist/publish/publish.d.ts +15 -1
- package/dist/publish/publish.js +22 -15
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +2 -2
- package/dist/studio.js +1 -0
- 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 +111 -0
- package/dist/text/braille.d.ts +28 -0
- package/dist/text/braille.js +58 -0
- package/dist/text/canvas.d.ts +65 -0
- package/dist/text/canvas.js +155 -0
- package/dist/text/content.d.ts +32 -0
- package/dist/text/content.js +90 -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 +31 -0
- package/dist/text/glyphs.js +84 -0
- package/dist/text/layout.d.ts +61 -0
- package/dist/text/layout.js +334 -0
- package/dist/text/legend.d.ts +44 -0
- package/dist/text/legend.js +252 -0
- package/dist/text/paint.d.ts +25 -0
- package/dist/text/paint.js +208 -0
- package/dist/text/render.d.ts +11 -0
- package/dist/text/render.js +28 -0
- package/dist/text/scene.d.ts +40 -0
- package/dist/text/scene.js +50 -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 +10 -0
- package/dist/tui/actions.js +297 -0
- package/dist/tui/app.d.ts +31 -0
- package/dist/tui/app.js +389 -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 +25 -0
- package/dist/tui/nav.js +79 -0
- package/dist/tui/overview.d.ts +46 -0
- package/dist/tui/overview.js +115 -0
- package/dist/tui/pixels.d.ts +28 -0
- package/dist/tui/pixels.js +80 -0
- package/dist/tui/screen.d.ts +44 -0
- package/dist/tui/screen.js +335 -0
- package/dist/tui/state.d.ts +100 -0
- package/dist/tui/state.js +72 -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,181 @@
|
|
|
1
|
+
// How many terminal cells text takes. A box is drawn to the width of its name, so a
|
|
2
|
+
// name measured in characters instead of cells pushes the right border of every row
|
|
3
|
+
// that holds a wide character out of line with the rows that do not.
|
|
4
|
+
//
|
|
5
|
+
// Measured per code point, which is what a terminal does short of joining emoji
|
|
6
|
+
// sequences (a ZWJ sequence draws as one glyph on some terminals and as its parts on
|
|
7
|
+
// others; counting the parts keeps the frame wide enough on both).
|
|
8
|
+
// [first, last] code points drawn two cells wide: East Asian Wide and Fullwidth, and
|
|
9
|
+
// the emoji drawn in emoji presentation by default.
|
|
10
|
+
const WIDE = [
|
|
11
|
+
[0x1100, 0x115f],
|
|
12
|
+
[0x231a, 0x231b],
|
|
13
|
+
[0x2329, 0x232a],
|
|
14
|
+
[0x23e9, 0x23ec],
|
|
15
|
+
[0x23f0, 0x23f0],
|
|
16
|
+
[0x23f3, 0x23f3],
|
|
17
|
+
[0x25fd, 0x25fe],
|
|
18
|
+
[0x2614, 0x2615],
|
|
19
|
+
[0x2648, 0x2653],
|
|
20
|
+
[0x267f, 0x267f],
|
|
21
|
+
[0x2693, 0x2693],
|
|
22
|
+
[0x26a1, 0x26a1],
|
|
23
|
+
[0x26aa, 0x26ab],
|
|
24
|
+
[0x26bd, 0x26be],
|
|
25
|
+
[0x26c4, 0x26c5],
|
|
26
|
+
[0x26ce, 0x26ce],
|
|
27
|
+
[0x26d4, 0x26d4],
|
|
28
|
+
[0x26ea, 0x26ea],
|
|
29
|
+
[0x26f2, 0x26f3],
|
|
30
|
+
[0x26f5, 0x26f5],
|
|
31
|
+
[0x26fa, 0x26fa],
|
|
32
|
+
[0x26fd, 0x26fd],
|
|
33
|
+
[0x2705, 0x2705],
|
|
34
|
+
[0x270a, 0x270b],
|
|
35
|
+
[0x2728, 0x2728],
|
|
36
|
+
[0x274c, 0x274c],
|
|
37
|
+
[0x274e, 0x274e],
|
|
38
|
+
[0x2753, 0x2755],
|
|
39
|
+
[0x2757, 0x2757],
|
|
40
|
+
[0x2795, 0x2797],
|
|
41
|
+
[0x27b0, 0x27b0],
|
|
42
|
+
[0x27bf, 0x27bf],
|
|
43
|
+
[0x2b1b, 0x2b1c],
|
|
44
|
+
[0x2b50, 0x2b50],
|
|
45
|
+
[0x2b55, 0x2b55],
|
|
46
|
+
[0x2e80, 0x303e],
|
|
47
|
+
[0x3041, 0x33ff],
|
|
48
|
+
[0x3400, 0x4dbf],
|
|
49
|
+
[0x4e00, 0x9fff],
|
|
50
|
+
[0xa000, 0xa4cf],
|
|
51
|
+
[0xa960, 0xa97f],
|
|
52
|
+
[0xac00, 0xd7a3],
|
|
53
|
+
[0xf900, 0xfaff],
|
|
54
|
+
[0xfe10, 0xfe19],
|
|
55
|
+
[0xfe30, 0xfe6f],
|
|
56
|
+
[0xff00, 0xff60],
|
|
57
|
+
[0xffe0, 0xffe6],
|
|
58
|
+
[0x1f004, 0x1f004],
|
|
59
|
+
[0x1f0cf, 0x1f0cf],
|
|
60
|
+
[0x1f18e, 0x1f18e],
|
|
61
|
+
[0x1f191, 0x1f19a],
|
|
62
|
+
[0x1f200, 0x1f251],
|
|
63
|
+
[0x1f300, 0x1f64f],
|
|
64
|
+
[0x1f680, 0x1f6ff],
|
|
65
|
+
[0x1f7e0, 0x1f7eb],
|
|
66
|
+
[0x1f900, 0x1f9ff],
|
|
67
|
+
[0x1fa70, 0x1faff],
|
|
68
|
+
[0x20000, 0x3fffd],
|
|
69
|
+
];
|
|
70
|
+
// [first, last] code points that take no cell of their own: combining marks, which
|
|
71
|
+
// draw over the character before them, and the invisible joiners and selectors.
|
|
72
|
+
const ZERO = [
|
|
73
|
+
[0x0300, 0x036f],
|
|
74
|
+
[0x0483, 0x0489],
|
|
75
|
+
[0x0591, 0x05bd],
|
|
76
|
+
[0x1ab0, 0x1aff],
|
|
77
|
+
[0x1dc0, 0x1dff],
|
|
78
|
+
[0x200b, 0x200f],
|
|
79
|
+
[0x2060, 0x2064],
|
|
80
|
+
[0x20d0, 0x20ff],
|
|
81
|
+
[0xfe00, 0xfe0f],
|
|
82
|
+
[0xfe20, 0xfe2f],
|
|
83
|
+
[0xfeff, 0xfeff],
|
|
84
|
+
[0xe0100, 0xe01ef],
|
|
85
|
+
];
|
|
86
|
+
function inRanges(cp, ranges) {
|
|
87
|
+
// Binary search: both tables are sorted and disjoint.
|
|
88
|
+
let lo = 0;
|
|
89
|
+
let hi = ranges.length - 1;
|
|
90
|
+
while (lo <= hi) {
|
|
91
|
+
const mid = (lo + hi) >> 1;
|
|
92
|
+
const [first, last] = ranges[mid];
|
|
93
|
+
if (cp < first)
|
|
94
|
+
hi = mid - 1;
|
|
95
|
+
else if (cp > last)
|
|
96
|
+
lo = mid + 1;
|
|
97
|
+
else
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
/** A C0 or C1 control character, or DEL: a terminal acts on one (a line break, an
|
|
103
|
+
* escape) instead of drawing it. */
|
|
104
|
+
export const isControl = (cp) => cp < 0x20 || (cp >= 0x7f && cp < 0xa0);
|
|
105
|
+
/** `text` with every control character a space: what a name, a label or a description
|
|
106
|
+
* may hold (a line break typed in the studio, a tab) and a terminal must not be sent. */
|
|
107
|
+
export function printable(text) {
|
|
108
|
+
let out = '';
|
|
109
|
+
for (const ch of text)
|
|
110
|
+
out += isControl(ch.codePointAt(0)) ? ' ' : ch;
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
/** The cells one code point takes: 0 for a mark, a joiner or a control character. */
|
|
114
|
+
export function charWidth(cp) {
|
|
115
|
+
if (cp < 0x20 || (cp >= 0x7f && cp < 0xa0))
|
|
116
|
+
return 0;
|
|
117
|
+
if (inRanges(cp, ZERO))
|
|
118
|
+
return 0;
|
|
119
|
+
return inRanges(cp, WIDE) ? 2 : 1;
|
|
120
|
+
}
|
|
121
|
+
/** The cells `text` takes in a terminal. */
|
|
122
|
+
export function cellWidth(text) {
|
|
123
|
+
let width = 0;
|
|
124
|
+
for (const ch of text)
|
|
125
|
+
width += charWidth(ch.codePointAt(0));
|
|
126
|
+
return width;
|
|
127
|
+
}
|
|
128
|
+
/** `text` cut to `max` cells, ending in `ellipsis` (one cell) when anything was cut. */
|
|
129
|
+
export function fitCells(text, max, ellipsis = '…') {
|
|
130
|
+
if (cellWidth(text) <= max)
|
|
131
|
+
return text;
|
|
132
|
+
if (max <= 0)
|
|
133
|
+
return '';
|
|
134
|
+
let out = '';
|
|
135
|
+
let width = 0;
|
|
136
|
+
for (const ch of text) {
|
|
137
|
+
const w = charWidth(ch.codePointAt(0));
|
|
138
|
+
if (width + w > max - 1)
|
|
139
|
+
break;
|
|
140
|
+
out += ch;
|
|
141
|
+
width += w;
|
|
142
|
+
}
|
|
143
|
+
return `${out}${ellipsis}`;
|
|
144
|
+
}
|
|
145
|
+
/** A word wider than `max`, in pieces of at most `max` cells. */
|
|
146
|
+
function splitWord(word, max) {
|
|
147
|
+
const pieces = [];
|
|
148
|
+
let piece = '';
|
|
149
|
+
let width = 0;
|
|
150
|
+
for (const ch of word) {
|
|
151
|
+
const w = charWidth(ch.codePointAt(0));
|
|
152
|
+
if (width + w > max && piece !== '') {
|
|
153
|
+
pieces.push(piece);
|
|
154
|
+
piece = '';
|
|
155
|
+
width = 0;
|
|
156
|
+
}
|
|
157
|
+
piece += ch;
|
|
158
|
+
width += w;
|
|
159
|
+
}
|
|
160
|
+
pieces.push(piece);
|
|
161
|
+
return pieces;
|
|
162
|
+
}
|
|
163
|
+
/** `text` wrapped at spaces into lines of at most `max` cells; a word wider than a
|
|
164
|
+
* line is split. Empty text is one empty line, so a box always has a title row. */
|
|
165
|
+
export function wrapCells(text, max) {
|
|
166
|
+
const lines = [];
|
|
167
|
+
let line = '';
|
|
168
|
+
for (const word of text.split(' ').filter((w) => w !== '')) {
|
|
169
|
+
for (const piece of cellWidth(word) > max ? splitWord(word, max) : [word]) {
|
|
170
|
+
const joined = line === '' ? piece : `${line} ${piece}`;
|
|
171
|
+
if (cellWidth(joined) <= max)
|
|
172
|
+
line = joined;
|
|
173
|
+
else {
|
|
174
|
+
lines.push(line);
|
|
175
|
+
line = piece;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
lines.push(line);
|
|
180
|
+
return lines;
|
|
181
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Key, Mouse } from './keys.js';
|
|
2
|
+
import { type Ctx, type DiagramState, type Effect } from './state.js';
|
|
3
|
+
export interface Step {
|
|
4
|
+
diagram: DiagramState;
|
|
5
|
+
effects: Effect[];
|
|
6
|
+
/** back to the list of diagrams */
|
|
7
|
+
toList?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function onKey(d: DiagramState, ctx: Ctx, key: Key): Step;
|
|
10
|
+
export declare function onMouse(d: DiagramState, ctx: Ctx, mouse: Mouse): Step;
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { neverFolds } from '../text/vocabulary.js';
|
|
2
|
+
import { onLegend } from './legend.js';
|
|
3
|
+
import { clampScroll, follow, hitTest, nearest, readingOrder } from './nav.js';
|
|
4
|
+
import { onOverview, overviewJump } from './overview.js';
|
|
5
|
+
import { openDiagram } from './state.js';
|
|
6
|
+
/** A second press on the same cell within this many ms is a double click. */
|
|
7
|
+
const DOUBLE_CLICK_MS = 400;
|
|
8
|
+
/** Rows a wheel notch scrolls; a sideways notch scrolls twice as many columns. */
|
|
9
|
+
const WHEEL_ROWS = 3;
|
|
10
|
+
const stay = (diagram) => ({ diagram, effects: [] });
|
|
11
|
+
const relayout = (diagram) => ({ diagram, effects: [{ kind: 'relayout' }] });
|
|
12
|
+
const say = (diagram, text) => stay({ ...diagram, message: { text, error: true } });
|
|
13
|
+
const nameOf = (ctx, id) => ctx.model?.nodes.find((n) => n.id === id)?.name ?? id;
|
|
14
|
+
const isContainer = (ctx, id) => (ctx.scene?.hierarchy.childrenOf.get(id)?.length ?? 0) > 0;
|
|
15
|
+
/** The scroll moved by (dx, dy), kept on the picture. */
|
|
16
|
+
function scrolled(d, ctx, by) {
|
|
17
|
+
const at = { x: d.scroll.x + by.x, y: d.scroll.y + by.y };
|
|
18
|
+
return { ...d, scroll: ctx.scene === undefined ? at : clampScroll(at, ctx.scene.layout, ctx.view) };
|
|
19
|
+
}
|
|
20
|
+
/** `id` selected, and scrolled to when it is drawn. */
|
|
21
|
+
function select(d, ctx, id) {
|
|
22
|
+
const box = ctx.scene?.layout.boxes.get(id);
|
|
23
|
+
if (box === undefined || ctx.scene === undefined)
|
|
24
|
+
return { ...d, selected: id };
|
|
25
|
+
return { ...d, selected: id, scroll: clampScroll(follow(d.scroll, box, ctx.view), ctx.scene.layout, ctx.view) };
|
|
26
|
+
}
|
|
27
|
+
function move(d, ctx, dir) {
|
|
28
|
+
const boxes = ctx.scene?.layout.boxes;
|
|
29
|
+
if (boxes === undefined || boxes.size === 0)
|
|
30
|
+
return stay(d);
|
|
31
|
+
const to = d.selected !== undefined && boxes.has(d.selected) ? nearest(boxes, d.selected, dir) : readingOrder(boxes)[0];
|
|
32
|
+
return stay(to === undefined ? d : select(d, ctx, to));
|
|
33
|
+
}
|
|
34
|
+
function tab(d, ctx, back) {
|
|
35
|
+
const order = ctx.scene === undefined ? [] : readingOrder(ctx.scene.layout.boxes);
|
|
36
|
+
if (order.length === 0)
|
|
37
|
+
return stay(d);
|
|
38
|
+
const at = d.selected === undefined ? -1 : order.indexOf(d.selected);
|
|
39
|
+
const next = at < 0 ? (back ? order.length - 1 : 0) : (at + (back ? -1 : 1) + order.length) % order.length;
|
|
40
|
+
return stay(select(d, ctx, order[next]));
|
|
41
|
+
}
|
|
42
|
+
/** Why the container `id` cannot be folded, unfolded or drilled into, if it cannot. */
|
|
43
|
+
function fixedOpen(ctx, id) {
|
|
44
|
+
const node = ctx.model?.nodes.find((n) => n.id === id);
|
|
45
|
+
if (node === undefined || !neverFolds(node, ctx.scene?.notation))
|
|
46
|
+
return undefined;
|
|
47
|
+
return `${node.name} is always open`;
|
|
48
|
+
}
|
|
49
|
+
function toggleFold(d, ctx, id = d.selected) {
|
|
50
|
+
if (id === undefined || !isContainer(ctx, id))
|
|
51
|
+
return stay(d);
|
|
52
|
+
const fixed = fixedOpen(ctx, id);
|
|
53
|
+
if (fixed !== undefined)
|
|
54
|
+
return say(d, fixed);
|
|
55
|
+
const open = ctx.scene?.nodes.get(id)?.state === 'expanded';
|
|
56
|
+
return relayout({ ...d, pins: { ...d.pins, [id]: open ? 'collapsed' : 'expanded' } });
|
|
57
|
+
}
|
|
58
|
+
function foldAll(d, ctx, state) {
|
|
59
|
+
const pins = {};
|
|
60
|
+
for (const [id, kids] of ctx.scene?.hierarchy.childrenOf ?? [])
|
|
61
|
+
if (kids.length > 0)
|
|
62
|
+
pins[id] = state;
|
|
63
|
+
return relayout({ ...d, pins });
|
|
64
|
+
}
|
|
65
|
+
function drillIn(d, ctx) {
|
|
66
|
+
const id = d.selected;
|
|
67
|
+
if (id === undefined)
|
|
68
|
+
return stay(d);
|
|
69
|
+
if (!isContainer(ctx, id))
|
|
70
|
+
return say(d, `${nameOf(ctx, id)} holds nothing to go into`);
|
|
71
|
+
const fixed = fixedOpen(ctx, id);
|
|
72
|
+
if (fixed !== undefined)
|
|
73
|
+
return say(d, fixed);
|
|
74
|
+
const { selected: _left, ...rest } = d;
|
|
75
|
+
return relayout({ ...rest, path: [...d.path, id], scroll: { x: 0, y: 0 } });
|
|
76
|
+
}
|
|
77
|
+
function drillOut(d) {
|
|
78
|
+
const left = d.path.at(-1);
|
|
79
|
+
if (left === undefined)
|
|
80
|
+
return stay(d);
|
|
81
|
+
return relayout({ ...d, path: d.path.slice(0, -1), selected: left, follow: true });
|
|
82
|
+
}
|
|
83
|
+
function switchPlane(d, ctx, step) {
|
|
84
|
+
const planes = ctx.model?.planes ?? [];
|
|
85
|
+
if (ctx.model === undefined || planes.length < 2)
|
|
86
|
+
return say(d, 'this diagram has one plane');
|
|
87
|
+
const at = Math.max(0, planes.findIndex((p) => p.id === (d.plane ?? planes[0].id)));
|
|
88
|
+
const plane = planes[(at + step + planes.length) % planes.length];
|
|
89
|
+
// what the reader has open or shut stays as it was; the view is the new plane's
|
|
90
|
+
const { panel, overview, legend } = d;
|
|
91
|
+
return relayout({ ...openDiagram(d.name, ctx.model, ctx.overlay, plane.id), panel, overview, legend });
|
|
92
|
+
}
|
|
93
|
+
function toggleLayer(d, ctx, n) {
|
|
94
|
+
const layer = ctx.model?.layers[n - 1];
|
|
95
|
+
if (layer === undefined)
|
|
96
|
+
return say(d, `no layer ${n}`);
|
|
97
|
+
const on = d.layers.includes(layer.id);
|
|
98
|
+
return relayout({ ...d, layers: on ? d.layers.filter((l) => l !== layer.id) : [...d.layers, layer.id] });
|
|
99
|
+
}
|
|
100
|
+
/** The nodes on the plane whose names hold the search, in the model's order. */
|
|
101
|
+
function matches(d, ctx) {
|
|
102
|
+
const query = d.search?.query.toLowerCase() ?? '';
|
|
103
|
+
const onPlane = ctx.scene?.hierarchy.childrenOf;
|
|
104
|
+
if (query === '' || onPlane === undefined || ctx.model === undefined)
|
|
105
|
+
return [];
|
|
106
|
+
return ctx.model.nodes.filter((n) => onPlane.has(n.id) && n.name.toLowerCase().includes(query)).map((n) => n.id);
|
|
107
|
+
}
|
|
108
|
+
/** `id` selected, unfolding the containers it is folded inside (and leaving a drill
|
|
109
|
+
* that does not hold it), so a search can reach what the picture hides. */
|
|
110
|
+
function reveal(d, ctx, id) {
|
|
111
|
+
if (ctx.scene?.layout.boxes.has(id) === true)
|
|
112
|
+
return stay(select(d, ctx, id));
|
|
113
|
+
const pins = { ...d.pins };
|
|
114
|
+
const ancestors = [];
|
|
115
|
+
const seen = new Set();
|
|
116
|
+
for (let at = ctx.scene?.hierarchy.parentsOf.get(id)?.[0]; at !== undefined && !seen.has(at);) {
|
|
117
|
+
seen.add(at);
|
|
118
|
+
ancestors.push(at);
|
|
119
|
+
pins[at] = 'expanded';
|
|
120
|
+
at = ctx.scene?.hierarchy.parentsOf.get(at)?.[0];
|
|
121
|
+
}
|
|
122
|
+
const root = d.path.at(-1);
|
|
123
|
+
const path = root === undefined || ancestors.includes(root) ? d.path : [];
|
|
124
|
+
return relayout({ ...d, pins, path, selected: id, follow: true });
|
|
125
|
+
}
|
|
126
|
+
function nextMatch(d, ctx, step) {
|
|
127
|
+
const found = matches(d, ctx);
|
|
128
|
+
if (found.length === 0)
|
|
129
|
+
return d.search === undefined ? stay(d) : say(d, `nothing matches '${d.search.query}'`);
|
|
130
|
+
const at = d.selected === undefined ? -1 : found.indexOf(d.selected);
|
|
131
|
+
const next = at < 0 ? (step === 1 ? 0 : found.length - 1) : (at + step + found.length) % found.length;
|
|
132
|
+
return reveal(d, ctx, found[next]);
|
|
133
|
+
}
|
|
134
|
+
/** A key while the search line is being typed into. */
|
|
135
|
+
function editSearch(d, ctx, key) {
|
|
136
|
+
const query = d.search?.query ?? '';
|
|
137
|
+
if (key.name === 'escape') {
|
|
138
|
+
const { search: _gone, ...rest } = d;
|
|
139
|
+
return stay(rest);
|
|
140
|
+
}
|
|
141
|
+
if (key.name === 'enter') {
|
|
142
|
+
// the first match, wherever the selection is
|
|
143
|
+
const { selected: _from, ...rest } = d;
|
|
144
|
+
const committed = { ...rest, search: { query, editing: false } };
|
|
145
|
+
return query === '' ? stay(committed) : nextMatch(committed, ctx, 1);
|
|
146
|
+
}
|
|
147
|
+
if (key.name === 'backspace')
|
|
148
|
+
return stay({ ...d, search: { query: [...query].slice(0, -1).join(''), editing: true } });
|
|
149
|
+
const ch = key.name === 'space' ? ' ' : [...key.name].length === 1 && key.ctrl !== true ? key.name : '';
|
|
150
|
+
return stay({ ...d, search: { query: query + ch, editing: true } });
|
|
151
|
+
}
|
|
152
|
+
/** Escape closes the innermost thing open: the search, the help, the panel; with
|
|
153
|
+
* nothing open it goes back to the list. */
|
|
154
|
+
function escape(d) {
|
|
155
|
+
if (d.search !== undefined) {
|
|
156
|
+
const { search: _gone, ...rest } = d;
|
|
157
|
+
return stay(rest);
|
|
158
|
+
}
|
|
159
|
+
if (d.help)
|
|
160
|
+
return stay({ ...d, help: false });
|
|
161
|
+
if (d.panel)
|
|
162
|
+
return stay({ ...d, panel: false });
|
|
163
|
+
return { diagram: d, effects: [], toList: true };
|
|
164
|
+
}
|
|
165
|
+
/** The overview on or off; turned on where it cannot show, it says why, or the key
|
|
166
|
+
* would seem to do nothing. */
|
|
167
|
+
function toggleOverview(d, ctx) {
|
|
168
|
+
if (d.overview || ctx.overview !== undefined || ctx.scene === undefined)
|
|
169
|
+
return stay({ ...d, overview: !d.overview });
|
|
170
|
+
const { layout } = ctx.scene;
|
|
171
|
+
const fits = layout.width <= ctx.view.width && layout.height <= ctx.view.height;
|
|
172
|
+
const text = fits ? 'the whole diagram fits on the screen' : 'the screen is too small for the overview';
|
|
173
|
+
return stay({ ...d, overview: true, message: { text, error: false } });
|
|
174
|
+
}
|
|
175
|
+
/** The legend on or off; turned on where it cannot show, it says why, as the overview does. */
|
|
176
|
+
function toggleLegend(d, ctx) {
|
|
177
|
+
if (d.legend || ctx.legend !== undefined || ctx.scene === undefined)
|
|
178
|
+
return stay({ ...d, legend: !d.legend });
|
|
179
|
+
const text = ctx.scene.legend === undefined ? 'this diagram has no legend' : 'the screen is too small for the legend';
|
|
180
|
+
return stay({ ...d, legend: true, message: { text, error: false } });
|
|
181
|
+
}
|
|
182
|
+
const page = (ctx) => Math.max(1, ctx.view.height - 2);
|
|
183
|
+
const pan = (ctx) => ({ x: Math.max(4, ctx.view.width >> 2), y: Math.max(2, ctx.view.height >> 2) });
|
|
184
|
+
/** The keys of an open diagram, by name; `shift+` before an arrow's name for the
|
|
185
|
+
* shifted one. */
|
|
186
|
+
const KEYS = {
|
|
187
|
+
up: (d, ctx) => move(d, ctx, 'up'),
|
|
188
|
+
down: (d, ctx) => move(d, ctx, 'down'),
|
|
189
|
+
left: (d, ctx) => move(d, ctx, 'left'),
|
|
190
|
+
right: (d, ctx) => move(d, ctx, 'right'),
|
|
191
|
+
k: (d, ctx) => move(d, ctx, 'up'),
|
|
192
|
+
j: (d, ctx) => move(d, ctx, 'down'),
|
|
193
|
+
h: (d, ctx) => move(d, ctx, 'left'),
|
|
194
|
+
l: (d, ctx) => move(d, ctx, 'right'),
|
|
195
|
+
'shift+up': (d, ctx) => stay(scrolled(d, ctx, { x: 0, y: -pan(ctx).y })),
|
|
196
|
+
'shift+down': (d, ctx) => stay(scrolled(d, ctx, { x: 0, y: pan(ctx).y })),
|
|
197
|
+
'shift+left': (d, ctx) => stay(scrolled(d, ctx, { x: -pan(ctx).x, y: 0 })),
|
|
198
|
+
'shift+right': (d, ctx) => stay(scrolled(d, ctx, { x: pan(ctx).x, y: 0 })),
|
|
199
|
+
pageup: (d, ctx) => stay(scrolled(d, ctx, { x: 0, y: -page(ctx) })),
|
|
200
|
+
pagedown: (d, ctx) => stay(scrolled(d, ctx, { x: 0, y: page(ctx) })),
|
|
201
|
+
home: (d) => stay({ ...d, scroll: { x: 0, y: 0 } }),
|
|
202
|
+
end: (d, ctx) => stay(scrolled(d, ctx, { x: 0, y: ctx.scene?.layout.height ?? 0 })),
|
|
203
|
+
tab: (d, ctx) => tab(d, ctx, false),
|
|
204
|
+
'shift+tab': (d, ctx) => tab(d, ctx, true),
|
|
205
|
+
enter: (d, ctx) => toggleFold(d, ctx),
|
|
206
|
+
space: (d, ctx) => toggleFold(d, ctx),
|
|
207
|
+
e: (d, ctx) => foldAll(d, ctx, 'expanded'),
|
|
208
|
+
c: (d, ctx) => foldAll(d, ctx, 'collapsed'),
|
|
209
|
+
'>': (d, ctx) => drillIn(d, ctx),
|
|
210
|
+
'<': (d) => drillOut(d),
|
|
211
|
+
p: (d, ctx) => switchPlane(d, ctx, 1),
|
|
212
|
+
P: (d, ctx) => switchPlane(d, ctx, -1),
|
|
213
|
+
'/': (d) => stay({ ...d, search: { query: '', editing: true } }),
|
|
214
|
+
n: (d, ctx) => nextMatch(d, ctx, 1),
|
|
215
|
+
N: (d, ctx) => nextMatch(d, ctx, -1),
|
|
216
|
+
i: (d) => stay({ ...d, panel: !d.panel }),
|
|
217
|
+
o: (d, ctx) => toggleOverview(d, ctx),
|
|
218
|
+
L: (d, ctx) => toggleLegend(d, ctx),
|
|
219
|
+
'?': (d) => stay({ ...d, help: !d.help }),
|
|
220
|
+
r: (d) => ({ diagram: d, effects: [{ kind: 'reload' }] }),
|
|
221
|
+
q: (d) => ({ diagram: d, effects: [{ kind: 'quit' }] }),
|
|
222
|
+
escape: (d) => escape(d),
|
|
223
|
+
};
|
|
224
|
+
export function onKey(d, ctx, key) {
|
|
225
|
+
if (key.ctrl === true && key.name === 'c')
|
|
226
|
+
return { diagram: d, effects: [{ kind: 'quit' }] };
|
|
227
|
+
if (d.search?.editing === true)
|
|
228
|
+
return editSearch(d, ctx, key);
|
|
229
|
+
if (d.help && key.name !== 'q')
|
|
230
|
+
return stay({ ...d, help: false });
|
|
231
|
+
if (/^[1-9]$/.test(key.name))
|
|
232
|
+
return toggleLayer(d, ctx, Number(key.name));
|
|
233
|
+
const action = KEYS[key.shift === true ? `shift+${key.name}` : key.name];
|
|
234
|
+
return action === undefined ? stay(d) : action(d, ctx, key);
|
|
235
|
+
}
|
|
236
|
+
/** A press: select the box under it, or toggle a container's fold on a double click.
|
|
237
|
+
* Every press also starts a pan, which a drag then moves: once a group is open, most
|
|
238
|
+
* of the canvas is inside some frame, so panning only from bare canvas would hardly
|
|
239
|
+
* ever be possible. */
|
|
240
|
+
function onPress(d, ctx, mouse) {
|
|
241
|
+
const cell = { x: mouse.x + d.scroll.x, y: mouse.y - ctx.view.top + d.scroll.y };
|
|
242
|
+
const hit = ctx.scene === undefined ? undefined : hitTest(ctx.scene.layout.boxes, cell.x, cell.y);
|
|
243
|
+
const last = d.lastPress;
|
|
244
|
+
const double = last !== undefined && last.x === mouse.x && last.y === mouse.y && ctx.at - last.at <= DOUBLE_CLICK_MS;
|
|
245
|
+
const { lastPress: _last, ...rest } = d;
|
|
246
|
+
const drag = { x: mouse.x, y: mouse.y };
|
|
247
|
+
const pressed = { ...rest, drag, lastPress: { x: mouse.x, y: mouse.y, at: ctx.at } };
|
|
248
|
+
if (hit === undefined)
|
|
249
|
+
return stay(pressed);
|
|
250
|
+
// a third press is a new first one, not a second double click
|
|
251
|
+
if (double)
|
|
252
|
+
return toggleFold({ ...rest, drag, selected: hit }, ctx, hit);
|
|
253
|
+
return stay({ ...pressed, selected: hit });
|
|
254
|
+
}
|
|
255
|
+
/** The screen moved to the part of the picture under `mouse` on the overview, and a
|
|
256
|
+
* drag started there, which goes on moving it however far it strays off the overview. */
|
|
257
|
+
function jump(d, ctx, mouse) {
|
|
258
|
+
if (ctx.overview === undefined || ctx.scene === undefined)
|
|
259
|
+
return stay(d);
|
|
260
|
+
const { lastPress: _last, ...rest } = d;
|
|
261
|
+
const scroll = overviewJump(ctx.overview, mouse, ctx.view, ctx.scene.layout);
|
|
262
|
+
return stay({ ...rest, scroll, drag: { x: mouse.x, y: mouse.y, overview: true } });
|
|
263
|
+
}
|
|
264
|
+
function onDrag(d, ctx, mouse) {
|
|
265
|
+
if (d.drag === undefined)
|
|
266
|
+
return stay(d);
|
|
267
|
+
if (d.drag.overview === true)
|
|
268
|
+
return jump(d, ctx, mouse);
|
|
269
|
+
const moved = scrolled(d, ctx, { x: d.drag.x - mouse.x, y: d.drag.y - mouse.y });
|
|
270
|
+
return stay({ ...moved, drag: { x: mouse.x, y: mouse.y } });
|
|
271
|
+
}
|
|
272
|
+
export function onMouse(d, ctx, mouse) {
|
|
273
|
+
const inCanvas = mouse.y >= ctx.view.top && mouse.y < ctx.view.top + ctx.view.height && mouse.x < ctx.view.width;
|
|
274
|
+
switch (mouse.action) {
|
|
275
|
+
case 'press':
|
|
276
|
+
// the overview and the legend lie over the canvas, so they are asked first; a
|
|
277
|
+
// legend's rows switch nothing, so a press there does nothing
|
|
278
|
+
if (d.overview && ctx.overview !== undefined && onOverview(ctx.overview, mouse))
|
|
279
|
+
return jump(d, ctx, mouse);
|
|
280
|
+
if (d.legend && ctx.legend !== undefined && onLegend(ctx.legend, mouse))
|
|
281
|
+
return stay(d);
|
|
282
|
+
return inCanvas ? onPress(d, ctx, mouse) : stay(d);
|
|
283
|
+
case 'drag':
|
|
284
|
+
return onDrag(d, ctx, mouse);
|
|
285
|
+
case 'release': {
|
|
286
|
+
const { drag: _done, ...rest } = d;
|
|
287
|
+
return stay(rest);
|
|
288
|
+
}
|
|
289
|
+
case 'wheel-up':
|
|
290
|
+
case 'wheel-down': {
|
|
291
|
+
const by = mouse.action === 'wheel-up' ? -WHEEL_ROWS : WHEEL_ROWS;
|
|
292
|
+
return stay(scrolled(d, ctx, mouse.shift ? { x: by * 2, y: 0 } : { x: 0, y: by }));
|
|
293
|
+
}
|
|
294
|
+
default:
|
|
295
|
+
return stay(scrolled(d, ctx, { x: mouse.action === 'wheel-left' ? -WHEEL_ROWS * 2 : WHEEL_ROWS * 2, y: 0 }));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ColorMode } from '../text/ansi.js';
|
|
2
|
+
import type { Terminal } from './terminal.js';
|
|
3
|
+
/** A diagram's source compiled (or failed to), or its layout changed. */
|
|
4
|
+
export interface Change {
|
|
5
|
+
name: string;
|
|
6
|
+
ok: boolean;
|
|
7
|
+
error?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface DiagramWatcher {
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/** Starts watching, reporting each change; the TUI's own loop is the only caller. */
|
|
13
|
+
export type WatchStarter = (onChange: (change: Change) => void) => DiagramWatcher;
|
|
14
|
+
export interface TuiOptions {
|
|
15
|
+
terminal: Terminal;
|
|
16
|
+
srcDir: string;
|
|
17
|
+
artifactsDir: string;
|
|
18
|
+
/** the diagram to open; the list of diagrams without one */
|
|
19
|
+
name?: string;
|
|
20
|
+
watch: WatchStarter;
|
|
21
|
+
color: ColorMode;
|
|
22
|
+
now?: () => number;
|
|
23
|
+
}
|
|
24
|
+
/** The studio's compile-watch loop, for the TUI: sources compile into artifacts as they
|
|
25
|
+
* are saved, and a layout's change is a change of its diagram. Compile warnings are
|
|
26
|
+
* dropped rather than printed, which would scribble over the screen; `diagc lint`
|
|
27
|
+
* reports them. */
|
|
28
|
+
export declare function watchSources(srcDir: string, artifactsDir: string, coreEntry: string): WatchStarter;
|
|
29
|
+
/** The TUI, until it is quit. The terminal is given back however the run ends; an
|
|
30
|
+
* error that ended it rejects the promise once it has been. */
|
|
31
|
+
export declare function runTui(options: TuiOptions): Promise<void>;
|