@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
package/dist/publish/publish.js
CHANGED
|
@@ -60,6 +60,26 @@ export async function inlineAssets(model, opts, cache, label) {
|
|
|
60
60
|
}
|
|
61
61
|
return rewriteAssetRefs(model, (ref) => cache.get(ref));
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* A diagram's layout and drawings as its page carries them; a path left out is a
|
|
65
|
+
* sidecar the diagram does not have. A hand-edited drawings sidecar can be valid
|
|
66
|
+
* JSON and still not be a drawings overlay. Stamping it would put shapeless data
|
|
67
|
+
* into a page that has no validator of its own, so it is warned about and dropped
|
|
68
|
+
* — the page still ships, just without ink. Unparseable JSON throws.
|
|
69
|
+
*/
|
|
70
|
+
export async function readSidecars(d, warn) {
|
|
71
|
+
const out = {};
|
|
72
|
+
if (d.layoutPath !== undefined)
|
|
73
|
+
out.layout = JSON.parse(await readFile(d.layoutPath, 'utf8'));
|
|
74
|
+
if (d.drawingsPath !== undefined) {
|
|
75
|
+
const parsed = JSON.parse(await readFile(d.drawingsPath, 'utf8'));
|
|
76
|
+
if (isDrawings(parsed))
|
|
77
|
+
out.drawings = parsed;
|
|
78
|
+
else
|
|
79
|
+
warn(`publish: ignoring malformed drawings sidecar for diagram "${d.name}" (${d.drawingsPath}); the page ships without ink.`);
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
63
83
|
/**
|
|
64
84
|
* The pictures publish takes of a model, one per plane. The first plane's is
|
|
65
85
|
* `<name>.png`, the picture docs and READMEs already link; each further plane's
|
|
@@ -117,24 +137,11 @@ export async function publishDiagrams(opts) {
|
|
|
117
137
|
try {
|
|
118
138
|
const modelRaw = JSON.parse(await readFile(d.modelPath, 'utf8'));
|
|
119
139
|
titles.set(d.name, modelRaw.name);
|
|
120
|
-
const
|
|
121
|
-
// A hand-edited sidecar can be valid JSON and still not be a drawings
|
|
122
|
-
// overlay. Publishing it would stamp shapeless data into a page that has
|
|
123
|
-
// no validator of its own, so it is warned about and dropped — the page
|
|
124
|
-
// still ships, just without ink. (Unparseable JSON keeps its old
|
|
125
|
-
// behaviour: it throws, and the diagram is skipped below.)
|
|
126
|
-
let drawings;
|
|
127
|
-
if (d.drawingsPath !== undefined) {
|
|
128
|
-
const parsed = JSON.parse(await readFile(d.drawingsPath, 'utf8'));
|
|
129
|
-
if (isDrawings(parsed))
|
|
130
|
-
drawings = parsed;
|
|
131
|
-
else
|
|
132
|
-
warn(`publish: ignoring malformed drawings sidecar for diagram "${d.name}" (${d.drawingsPath}); the page ships without ink.`);
|
|
133
|
-
}
|
|
140
|
+
const sidecars = await readSidecars(d, warn);
|
|
134
141
|
const inlined = await inlineAssets(modelRaw, opts, cache, `diagram "${d.name}"`);
|
|
135
142
|
const pagePath = path.join(opts.htmlDir, `${d.name}.html`);
|
|
136
143
|
await mkdir(path.dirname(pagePath), { recursive: true });
|
|
137
|
-
await writeFile(pagePath, stampHtml(shell, { model: inlined,
|
|
144
|
+
await writeFile(pagePath, stampHtml(shell, { model: inlined, ...sidecars }));
|
|
138
145
|
pages.push(pagePath);
|
|
139
146
|
if (opts.images && opts.renderPng !== undefined) {
|
|
140
147
|
const run = {
|
package/dist/serve.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import type { PageSources } from './api/pages.js';
|
|
1
2
|
export declare const contentTypeFor: (file: string) => string;
|
|
2
3
|
export interface ServeOptions {
|
|
3
4
|
/** prebuilt studio bundle (index.html + assets + library) */
|
|
4
5
|
studioDir: string;
|
|
5
6
|
diagramsDir: string;
|
|
6
7
|
artifactsDir: string;
|
|
8
|
+
/** what the page route builds from (see `PageSources`) */
|
|
9
|
+
pages?: PageSources;
|
|
7
10
|
/** first port to try; the server walks upward when one is taken */
|
|
8
11
|
port?: number;
|
|
9
12
|
host?: string;
|
package/dist/serve.js
CHANGED
|
@@ -110,13 +110,13 @@ function listen(server, port, host, attempts = 20) {
|
|
|
110
110
|
* itself is an ordinary SPA that only ever talks to `/api/*`, which is what
|
|
111
111
|
* makes a static bundle behave identically here. */
|
|
112
112
|
export async function startStudioServer(opts) {
|
|
113
|
-
const { studioDir, diagramsDir, artifactsDir } = opts;
|
|
113
|
+
const { studioDir, diagramsDir, artifactsDir, pages } = opts;
|
|
114
114
|
const host = opts.host ?? '127.0.0.1';
|
|
115
115
|
const index = path.join(studioDir, 'index.html');
|
|
116
116
|
const server = createServer((req, res) => {
|
|
117
117
|
void (async () => {
|
|
118
118
|
try {
|
|
119
|
-
if (await handleApiRequest(req, res, { diagramsDir, artifactsDir }, handlers))
|
|
119
|
+
if (await handleApiRequest(req, res, { diagramsDir, artifactsDir, pages }, handlers))
|
|
120
120
|
return;
|
|
121
121
|
const file = resolveStatic(studioDir, req.url ?? '/');
|
|
122
122
|
if (file === undefined) {
|
package/dist/studio.js
CHANGED
|
@@ -84,6 +84,7 @@ async function servePackaged(home, env) {
|
|
|
84
84
|
studioDir: home.studioDir,
|
|
85
85
|
diagramsDir: env.DIAGRAMS_DIR,
|
|
86
86
|
artifactsDir: env.ARTIFACTS_DIR,
|
|
87
|
+
pages: { shellPath: home.viewerShell, libraryDir: home.libraryDir },
|
|
87
88
|
});
|
|
88
89
|
console.log(`Studio ready on ${server.url}`);
|
|
89
90
|
if (env.DIAGRAMS_OPEN === '1')
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type DiagramModel, type NotationId, type ViewEdge } from '@diagc/core/internal';
|
|
2
|
+
export interface Accents {
|
|
3
|
+
/** node id → its notation's colour: below the node's own, above the model's typeColors */
|
|
4
|
+
nodes: ReadonlyMap<string, string>;
|
|
5
|
+
/** edge id → its notation's colour: below the relation's own and its layer's tint */
|
|
6
|
+
edges: ReadonlyMap<string, string>;
|
|
7
|
+
}
|
|
8
|
+
/** What `notation` colours on the plane, of its nodes and of the drawn `edges`. */
|
|
9
|
+
export declare function notationAccents(model: DiagramModel, plane: string | undefined, notation: NotationId | undefined, edges: readonly ViewEdge[]): Accents;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { FB_CATEGORY_TYPE, FISHBONE_NOTATION, fishboneTree, GIT_NOTATION, gitGraph, isGitKind, TM_BOUNDARY_TYPE, TM_NOTATION, } from '@diagc/core/internal';
|
|
2
|
+
// The colours a notation gives its boxes and lines, as the browser gives them: a git
|
|
3
|
+
// graph's lanes and a fishbone's bones from one palette, a threat model's trust
|
|
4
|
+
// boundaries red. The CLI cannot import the renderer, so this is a copy of its notation
|
|
5
|
+
// colour hooks, which accents.test.ts holds to them. The renderer's other hooks give a
|
|
6
|
+
// theme's colours, one for a light page and one for a dark: a text picture cannot know
|
|
7
|
+
// the background it will be read on, so those notations draw without them.
|
|
8
|
+
/** A lane's or a bone's colour where it sets none, in turn. */
|
|
9
|
+
const PALETTE = ['#7ba7d9', '#d9534f', '#e0a030', '#7bbf7b', '#b08ad9', '#8a8f96'];
|
|
10
|
+
const BOUNDARY_COLOR = '#c62828';
|
|
11
|
+
const NONE = { nodes: new Map(), edges: new Map() };
|
|
12
|
+
const paletteAt = (i) => PALETTE[i % PALETTE.length];
|
|
13
|
+
/** Each category in its colour, and every cause and sub-cause hanging from it: its
|
|
14
|
+
* own colour, else the model's for a category, else the palette by column, a column
|
|
15
|
+
* being the two categories that meet the spine at one point, which read as one rib. */
|
|
16
|
+
function boneColors(model) {
|
|
17
|
+
const byType = model.typeColors?.[FB_CATEGORY_TYPE] ?? model.typeColors?.['*'];
|
|
18
|
+
const own = new Map(model.nodes.map((n) => [n.id, n.color]));
|
|
19
|
+
const colors = new Map();
|
|
20
|
+
fishboneTree(model).categories.forEach((category, i) => {
|
|
21
|
+
const color = own.get(category.id) ?? byType ?? paletteAt(Math.floor(i / 2));
|
|
22
|
+
colors.set(category.id, color);
|
|
23
|
+
for (const cause of category.causes) {
|
|
24
|
+
colors.set(cause.id, color);
|
|
25
|
+
for (const sub of cause.subs)
|
|
26
|
+
colors.set(sub, color);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return colors;
|
|
30
|
+
}
|
|
31
|
+
/** Each lane in its colour: its own, else the model's for a branch, else the palette
|
|
32
|
+
* by lane; and each commit in its own colour, else its lane's. */
|
|
33
|
+
function laneColors(model, graph) {
|
|
34
|
+
const byType = model.typeColors?.['branch'] ?? model.typeColors?.['*'];
|
|
35
|
+
const colors = new Map();
|
|
36
|
+
graph.lanes.forEach((lane, i) => {
|
|
37
|
+
const color = lane.node.color ?? byType ?? paletteAt(i);
|
|
38
|
+
colors.set(lane.id, color);
|
|
39
|
+
for (const commit of lane.commits)
|
|
40
|
+
colors.set(commit.id, commit.color ?? color);
|
|
41
|
+
});
|
|
42
|
+
return colors;
|
|
43
|
+
}
|
|
44
|
+
/** A git link in a lane's colour: a branch in the new lane's, a commit in its own
|
|
45
|
+
* lane's, a merge in the lower lane's, so a feature merging up keeps its colour and a
|
|
46
|
+
* trunk merging down takes the lane's. */
|
|
47
|
+
function linkColor(edge, graph, lanes) {
|
|
48
|
+
if (!isGitKind(edge.kind))
|
|
49
|
+
return undefined;
|
|
50
|
+
const [from, to] = [graph.laneOf.get(edge.from), graph.laneOf.get(edge.to)];
|
|
51
|
+
if (from === undefined || to === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
const index = (id) => graph.lanes.findIndex((l) => l.id === id);
|
|
54
|
+
const lane = edge.kind === 'branch' ? to : edge.kind === 'commit' ? from : index(from) >= index(to) ? from : to;
|
|
55
|
+
return lanes.get(lane);
|
|
56
|
+
}
|
|
57
|
+
function byEdge(edges, colorOf) {
|
|
58
|
+
const colors = new Map();
|
|
59
|
+
for (const e of edges) {
|
|
60
|
+
const color = colorOf(e);
|
|
61
|
+
if (color !== undefined)
|
|
62
|
+
colors.set(e.id, color);
|
|
63
|
+
}
|
|
64
|
+
return colors;
|
|
65
|
+
}
|
|
66
|
+
/** What `notation` colours on the plane, of its nodes and of the drawn `edges`. */
|
|
67
|
+
export function notationAccents(model, plane, notation, edges) {
|
|
68
|
+
if (notation === FISHBONE_NOTATION) {
|
|
69
|
+
const nodes = boneColors(model);
|
|
70
|
+
// a bone, a cause line or a tick takes the colour of the node it starts from
|
|
71
|
+
return { nodes, edges: byEdge(edges, (e) => nodes.get(e.from)) };
|
|
72
|
+
}
|
|
73
|
+
if (notation === GIT_NOTATION) {
|
|
74
|
+
const graph = gitGraph(model, plane);
|
|
75
|
+
const nodes = laneColors(model, graph);
|
|
76
|
+
return { nodes, edges: byEdge(edges, (e) => linkColor(e, graph, nodes)) };
|
|
77
|
+
}
|
|
78
|
+
if (notation === TM_NOTATION) {
|
|
79
|
+
const boundaries = model.nodes.filter((n) => n.type === TM_BOUNDARY_TYPE);
|
|
80
|
+
return { nodes: new Map(boundaries.map((n) => [n.id, BOUNDARY_COLOR])), edges: new Map() };
|
|
81
|
+
}
|
|
82
|
+
return NONE;
|
|
83
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Cell, Style } from './canvas.js';
|
|
2
|
+
/** How much colour the output may use: none (a pipe, NO_COLOR), the 256-colour
|
|
3
|
+
* palette every modern terminal has, or 24-bit where the terminal says it has it.
|
|
4
|
+
* `mono` is the TUI's no-colour: bold, dim and inverse but no colours, because a
|
|
5
|
+
* screen without inverse has no way to show where the reader is. */
|
|
6
|
+
export type ColorMode = 'none' | 'mono' | 'ansi256' | 'truecolor';
|
|
7
|
+
/** A CSS colour as RGB, for the forms a diagram writes literally (`#rgb`, `#rrggbb`,
|
|
8
|
+
* `rgb(r, g, b)`). Anything else — a theme variable, a colour name — has no value
|
|
9
|
+
* outside a browser, and the cell keeps the terminal's own colour. */
|
|
10
|
+
export declare function parseColor(css: string): [number, number, number] | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* The colour mode for an output, by the usual conventions in their usual order: a
|
|
13
|
+
* flag beats the environment, NO_COLOR (when non-empty) turns colour off, a dumb
|
|
14
|
+
* terminal gets none, FORCE_COLOR turns it on for a pipe, and otherwise a terminal
|
|
15
|
+
* is coloured and a pipe is not.
|
|
16
|
+
*/
|
|
17
|
+
export declare function colorModeFor(env: Readonly<Record<string, string | undefined>>, tty: boolean, flag?: 'color' | 'no-color'): ColorMode;
|
|
18
|
+
/** The TUI's mode: colour as for a terminal `show` prints to, but the attributes
|
|
19
|
+
* kept where colour is off. NO_COLOR is about colour, not about bold and inverse. */
|
|
20
|
+
export declare function screenColorMode(env: Readonly<Record<string, string | undefined>>): ColorMode;
|
|
21
|
+
/** The escape that sets `style`, or `''` for the plain style. */
|
|
22
|
+
export declare function sgr(style: Style, mode: ColorMode): string;
|
|
23
|
+
/**
|
|
24
|
+
* The picture as text, one string per row. Trailing blanks are trimmed unless `trim`
|
|
25
|
+
* is false (a full-screen frame keeps its width). A row that set a style ends with a
|
|
26
|
+
* reset, so no colour leaks into the next line or the shell.
|
|
27
|
+
*/
|
|
28
|
+
export declare function serialize(rows: readonly Cell[][], mode: ColorMode, trim?: boolean): string[];
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/** A CSS colour as RGB, for the forms a diagram writes literally (`#rgb`, `#rrggbb`,
|
|
2
|
+
* `rgb(r, g, b)`). Anything else — a theme variable, a colour name — has no value
|
|
3
|
+
* outside a browser, and the cell keeps the terminal's own colour. */
|
|
4
|
+
export function parseColor(css) {
|
|
5
|
+
const s = css.trim().toLowerCase();
|
|
6
|
+
const short = /^#([0-9a-f])([0-9a-f])([0-9a-f])$/.exec(s);
|
|
7
|
+
if (short !== null)
|
|
8
|
+
return [1, 2, 3].map((i) => parseInt(short[i].repeat(2), 16));
|
|
9
|
+
const long = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/.exec(s);
|
|
10
|
+
if (long !== null)
|
|
11
|
+
return [1, 2, 3].map((i) => parseInt(long[i], 16));
|
|
12
|
+
const rgb = /^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,[^)]*)?\)$/.exec(s);
|
|
13
|
+
if (rgb !== null)
|
|
14
|
+
return [1, 2, 3].map((i) => Math.min(255, Number(rgb[i])));
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The colour mode for an output, by the usual conventions in their usual order: a
|
|
19
|
+
* flag beats the environment, NO_COLOR (when non-empty) turns colour off, a dumb
|
|
20
|
+
* terminal gets none, FORCE_COLOR turns it on for a pipe, and otherwise a terminal
|
|
21
|
+
* is coloured and a pipe is not.
|
|
22
|
+
*/
|
|
23
|
+
export function colorModeFor(env, tty, flag) {
|
|
24
|
+
const depth = env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit' ? 'truecolor' : 'ansi256';
|
|
25
|
+
if (flag === 'no-color')
|
|
26
|
+
return 'none';
|
|
27
|
+
if (flag === 'color')
|
|
28
|
+
return depth;
|
|
29
|
+
if (env.NO_COLOR !== undefined && env.NO_COLOR !== '')
|
|
30
|
+
return 'none';
|
|
31
|
+
if (env.TERM === 'dumb')
|
|
32
|
+
return 'none';
|
|
33
|
+
if (env.FORCE_COLOR !== undefined)
|
|
34
|
+
return env.FORCE_COLOR === '0' ? 'none' : depth;
|
|
35
|
+
return tty ? depth : 'none';
|
|
36
|
+
}
|
|
37
|
+
/** The TUI's mode: colour as for a terminal `show` prints to, but the attributes
|
|
38
|
+
* kept where colour is off. NO_COLOR is about colour, not about bold and inverse. */
|
|
39
|
+
export function screenColorMode(env) {
|
|
40
|
+
const mode = colorModeFor(env, true);
|
|
41
|
+
return mode === 'none' ? 'mono' : mode;
|
|
42
|
+
}
|
|
43
|
+
const CUBE = [0, 95, 135, 175, 215, 255];
|
|
44
|
+
const nearestLevel = (v) => {
|
|
45
|
+
let best = 0;
|
|
46
|
+
for (let i = 1; i < CUBE.length; i++)
|
|
47
|
+
if (Math.abs(CUBE[i] - v) < Math.abs(CUBE[best] - v))
|
|
48
|
+
best = i;
|
|
49
|
+
return best;
|
|
50
|
+
};
|
|
51
|
+
const distance = (a, b) => a.reduce((sum, v, i) => sum + (v - b[i]) ** 2, 0);
|
|
52
|
+
/** The 256-colour palette entry nearest `rgb`: the 6×6×6 cube or the 24 greys. */
|
|
53
|
+
function palette256([r, g, b]) {
|
|
54
|
+
const [ri, gi, bi] = [r, g, b].map(nearestLevel);
|
|
55
|
+
const cube = [CUBE[ri], CUBE[gi], CUBE[bi]];
|
|
56
|
+
const grey = Math.max(0, Math.min(23, Math.round(((r + g + b) / 3 - 8) / 10)));
|
|
57
|
+
const greyValue = 8 + grey * 10;
|
|
58
|
+
return distance([r, g, b], [greyValue, greyValue, greyValue]) < distance([r, g, b], cube)
|
|
59
|
+
? 232 + grey
|
|
60
|
+
: 16 + 36 * ri + 6 * gi + bi;
|
|
61
|
+
}
|
|
62
|
+
function colorCode(color, mode) {
|
|
63
|
+
if ('ansi' in color)
|
|
64
|
+
return String(color.ansi < 8 ? 30 + color.ansi : 90 + color.ansi - 8);
|
|
65
|
+
return mode === 'truecolor' ? `38;2;${color.rgb.join(';')}` : `38;5;${palette256(color.rgb)}`;
|
|
66
|
+
}
|
|
67
|
+
/** The escape that sets `style`, or `''` for the plain style. */
|
|
68
|
+
export function sgr(style, mode) {
|
|
69
|
+
if (mode === 'none')
|
|
70
|
+
return '';
|
|
71
|
+
const codes = [];
|
|
72
|
+
if (style.bold === true)
|
|
73
|
+
codes.push('1');
|
|
74
|
+
if (style.dim === true)
|
|
75
|
+
codes.push('2');
|
|
76
|
+
if (style.inverse === true)
|
|
77
|
+
codes.push('7');
|
|
78
|
+
if (style.fg !== undefined && mode !== 'mono')
|
|
79
|
+
codes.push(colorCode(style.fg, mode));
|
|
80
|
+
return codes.length === 0 ? '' : `\x1b[${codes.join(';')}m`;
|
|
81
|
+
}
|
|
82
|
+
const RESET = '\x1b[0m';
|
|
83
|
+
/** A blank that shows nothing: trailing ones can go. An inverse blank is a bar. */
|
|
84
|
+
const invisible = (cell) => cell.ch === ' ' && cell.style.inverse !== true;
|
|
85
|
+
/**
|
|
86
|
+
* The picture as text, one string per row. Trailing blanks are trimmed unless `trim`
|
|
87
|
+
* is false (a full-screen frame keeps its width). A row that set a style ends with a
|
|
88
|
+
* reset, so no colour leaks into the next line or the shell.
|
|
89
|
+
*/
|
|
90
|
+
export function serialize(rows, mode, trim = true) {
|
|
91
|
+
return rows.map((row) => {
|
|
92
|
+
let end = row.length;
|
|
93
|
+
if (trim)
|
|
94
|
+
while (end > 0 && (invisible(row[end - 1]) || row[end - 1].ch === ''))
|
|
95
|
+
end--;
|
|
96
|
+
let out = '';
|
|
97
|
+
let current = '';
|
|
98
|
+
for (let i = 0; i < end; i++) {
|
|
99
|
+
const cell = row[i];
|
|
100
|
+
if (cell.ch === '')
|
|
101
|
+
continue;
|
|
102
|
+
const code = sgr(cell.style, mode);
|
|
103
|
+
if (code !== current) {
|
|
104
|
+
out += current === '' ? code : `${RESET}${code}`;
|
|
105
|
+
current = code;
|
|
106
|
+
}
|
|
107
|
+
out += cell.ch;
|
|
108
|
+
}
|
|
109
|
+
return current === '' ? out : `${out}${RESET}`;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** A rectangle of dots: from (x, y) to (x + width − 1, y + height − 1). */
|
|
2
|
+
export interface DotRect {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Cells as a grid of dots, two across and four down in each, drawn with braille
|
|
10
|
+
* characters: the finest a terminal draws in text, for a picture too small to
|
|
11
|
+
* letter. A cell is about twice as tall as it is wide, so a dot is about square.
|
|
12
|
+
* Anything drawn outside the grid is dropped, as on the canvas.
|
|
13
|
+
*/
|
|
14
|
+
export declare class Dots {
|
|
15
|
+
readonly columns: number;
|
|
16
|
+
readonly rows: number;
|
|
17
|
+
private readonly bits;
|
|
18
|
+
constructor(columns: number, rows: number);
|
|
19
|
+
get width(): number;
|
|
20
|
+
get height(): number;
|
|
21
|
+
set(x: number, y: number): void;
|
|
22
|
+
/** The rectangle's border, a dot thick. */
|
|
23
|
+
outline({ x, y, width, height }: DotRect): void;
|
|
24
|
+
/** The cell's character: its dots, or a space for none — a blank braille character
|
|
25
|
+
* shows as a box in fonts without braille, and copies as something that is not a
|
|
26
|
+
* space. */
|
|
27
|
+
char(column: number, row: number): string;
|
|
28
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** Each dot's bit in its braille character, by its row and column in the cell: the
|
|
2
|
+
* standard numbering, which runs down the left column, then the right, with the
|
|
3
|
+
* bottom row added last. */
|
|
4
|
+
const BIT = [
|
|
5
|
+
[0x01, 0x08],
|
|
6
|
+
[0x02, 0x10],
|
|
7
|
+
[0x04, 0x20],
|
|
8
|
+
[0x40, 0x80],
|
|
9
|
+
];
|
|
10
|
+
const BRAILLE_BLANK = 0x2800;
|
|
11
|
+
/**
|
|
12
|
+
* Cells as a grid of dots, two across and four down in each, drawn with braille
|
|
13
|
+
* characters: the finest a terminal draws in text, for a picture too small to
|
|
14
|
+
* letter. A cell is about twice as tall as it is wide, so a dot is about square.
|
|
15
|
+
* Anything drawn outside the grid is dropped, as on the canvas.
|
|
16
|
+
*/
|
|
17
|
+
export class Dots {
|
|
18
|
+
columns;
|
|
19
|
+
rows;
|
|
20
|
+
bits;
|
|
21
|
+
constructor(columns, rows) {
|
|
22
|
+
this.columns = columns;
|
|
23
|
+
this.rows = rows;
|
|
24
|
+
this.bits = new Uint8Array(Math.max(0, columns) * Math.max(0, rows));
|
|
25
|
+
}
|
|
26
|
+
get width() {
|
|
27
|
+
return this.columns * 2;
|
|
28
|
+
}
|
|
29
|
+
get height() {
|
|
30
|
+
return this.rows * 4;
|
|
31
|
+
}
|
|
32
|
+
set(x, y) {
|
|
33
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height)
|
|
34
|
+
return;
|
|
35
|
+
this.bits[(y >> 2) * this.columns + (x >> 1)] |= BIT[y & 3][x & 1];
|
|
36
|
+
}
|
|
37
|
+
/** The rectangle's border, a dot thick. */
|
|
38
|
+
outline({ x, y, width, height }) {
|
|
39
|
+
if (width <= 0 || height <= 0)
|
|
40
|
+
return;
|
|
41
|
+
const [right, bottom] = [x + width - 1, y + height - 1];
|
|
42
|
+
for (let i = x; i <= right; i++) {
|
|
43
|
+
this.set(i, y);
|
|
44
|
+
this.set(i, bottom);
|
|
45
|
+
}
|
|
46
|
+
for (let j = y; j <= bottom; j++) {
|
|
47
|
+
this.set(x, j);
|
|
48
|
+
this.set(right, j);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** The cell's character: its dots, or a space for none — a blank braille character
|
|
52
|
+
* shows as a box in fonts without braille, and copies as something that is not a
|
|
53
|
+
* space. */
|
|
54
|
+
char(column, row) {
|
|
55
|
+
const bits = this.bits[row * this.columns + column] ?? 0;
|
|
56
|
+
return bits === 0 ? ' ' : String.fromCodePoint(BRAILLE_BLANK + bits);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type Corner, type Glyphs, type Weight } from './glyphs.js';
|
|
2
|
+
/** A colour: a 24-bit one (a node's own), or one of the terminal's 16 (0–15). */
|
|
3
|
+
export type Color = {
|
|
4
|
+
rgb: [number, number, number];
|
|
5
|
+
} | {
|
|
6
|
+
ansi: number;
|
|
7
|
+
};
|
|
8
|
+
export interface Style {
|
|
9
|
+
fg?: Color;
|
|
10
|
+
bold?: boolean;
|
|
11
|
+
dim?: boolean;
|
|
12
|
+
inverse?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** One cell of a finished picture. `ch` is `''` in the second cell of a wide
|
|
15
|
+
* character, which the character before it already covers. */
|
|
16
|
+
export interface Cell {
|
|
17
|
+
ch: string;
|
|
18
|
+
style: Style;
|
|
19
|
+
}
|
|
20
|
+
export interface CellPoint {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
}
|
|
24
|
+
/** A rectangle of cells: from (x, y) to (x + width − 1, y + height − 1). */
|
|
25
|
+
export interface CellRect extends CellPoint {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A grid of character cells that lines, boxes and text are drawn into. Lines are
|
|
31
|
+
* kept as direction masks until `cells` resolves them, so whatever meets in a cell
|
|
32
|
+
* — a crossing, an edge leaving a box, a frame's corner — merges into one junction.
|
|
33
|
+
* Anything drawn outside the grid is dropped, which lets callers draw without
|
|
34
|
+
* clipping first.
|
|
35
|
+
*/
|
|
36
|
+
export declare class Canvas {
|
|
37
|
+
readonly width: number;
|
|
38
|
+
readonly height: number;
|
|
39
|
+
private readonly slots;
|
|
40
|
+
constructor(width: number, height: number);
|
|
41
|
+
private slot;
|
|
42
|
+
/** Adds line directions to a cell. The heavier weight wins a shared cell. */
|
|
43
|
+
stroke({ x, y }: CellPoint, mask: number, weight: Weight, style?: Style): void;
|
|
44
|
+
/** An axis-aligned line from `a` to `b`, both ends included. */
|
|
45
|
+
segment(a: CellPoint, b: CellPoint, weight: Weight, style?: Style): void;
|
|
46
|
+
/** A rectangle's border. */
|
|
47
|
+
rect({ x, y, width, height }: CellRect, weight: Weight, style?: Style, corner?: Corner): void;
|
|
48
|
+
/** Blanks a rectangle: what was drawn there, lines included, is gone. */
|
|
49
|
+
clear({ x, y, width, height }: CellRect): void;
|
|
50
|
+
/** Puts one character in a cell, over whatever was there. */
|
|
51
|
+
put({ x, y }: CellPoint, ch: string, style?: Style): void;
|
|
52
|
+
/**
|
|
53
|
+
* Writes `text` from (x, y) rightwards, stopping before `maxX` (exclusive) or the
|
|
54
|
+
* edge, and never splitting a wide character. A combining mark joins the cell
|
|
55
|
+
* before it. Returns the cells written.
|
|
56
|
+
*/
|
|
57
|
+
text({ x, y }: CellPoint, text: string, style?: Style, maxX?: number): number;
|
|
58
|
+
/** Merges `patch` into the style of every cell in a rectangle. */
|
|
59
|
+
restyle({ x, y, width, height }: CellRect, patch: Style): void;
|
|
60
|
+
/** The finished picture, lines resolved with `glyphs`. */
|
|
61
|
+
cells(glyphs: Glyphs): Cell[][];
|
|
62
|
+
/** Before a cell is overwritten: if it is half of a wide character, blank the other
|
|
63
|
+
* half, so no orphaned half is left to shift the rest of the row. */
|
|
64
|
+
private unwide;
|
|
65
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { DOWN, LEFT, RIGHT, UP } from './glyphs.js';
|
|
2
|
+
import { charWidth, isControl } from './width.js';
|
|
3
|
+
const RANK = { dashed: 0, light: 1, heavy: 2 };
|
|
4
|
+
const blank = () => ({ ch: ' ', style: {}, mask: 0, weight: 'dashed' });
|
|
5
|
+
/**
|
|
6
|
+
* A grid of character cells that lines, boxes and text are drawn into. Lines are
|
|
7
|
+
* kept as direction masks until `cells` resolves them, so whatever meets in a cell
|
|
8
|
+
* — a crossing, an edge leaving a box, a frame's corner — merges into one junction.
|
|
9
|
+
* Anything drawn outside the grid is dropped, which lets callers draw without
|
|
10
|
+
* clipping first.
|
|
11
|
+
*/
|
|
12
|
+
export class Canvas {
|
|
13
|
+
width;
|
|
14
|
+
height;
|
|
15
|
+
slots;
|
|
16
|
+
constructor(width, height) {
|
|
17
|
+
this.width = width;
|
|
18
|
+
this.height = height;
|
|
19
|
+
this.slots = Array.from({ length: Math.max(0, height) }, () => Array.from({ length: Math.max(0, width) }, blank));
|
|
20
|
+
}
|
|
21
|
+
slot(x, y) {
|
|
22
|
+
return this.slots[y]?.[x];
|
|
23
|
+
}
|
|
24
|
+
/** Adds line directions to a cell. The heavier weight wins a shared cell. */
|
|
25
|
+
stroke({ x, y }, mask, weight, style) {
|
|
26
|
+
const s = this.slot(x, y);
|
|
27
|
+
if (s === undefined)
|
|
28
|
+
return;
|
|
29
|
+
const was = s.mask;
|
|
30
|
+
if (was === 0)
|
|
31
|
+
this.unwide(x, y);
|
|
32
|
+
s.mask |= mask;
|
|
33
|
+
if (was === 0 || RANK[weight] > RANK[s.weight])
|
|
34
|
+
s.weight = weight;
|
|
35
|
+
if (style !== undefined)
|
|
36
|
+
s.style = style;
|
|
37
|
+
}
|
|
38
|
+
/** An axis-aligned line from `a` to `b`, both ends included. */
|
|
39
|
+
segment(a, b, weight, style) {
|
|
40
|
+
if (a.x !== b.x && a.y !== b.y)
|
|
41
|
+
throw new Error(`not an axis-aligned segment: ${a.x},${a.y} → ${b.x},${b.y}`);
|
|
42
|
+
if (a.y === b.y) {
|
|
43
|
+
const [from, to] = a.x <= b.x ? [a.x, b.x] : [b.x, a.x];
|
|
44
|
+
for (let x = from; x <= to; x++) {
|
|
45
|
+
this.stroke({ x, y: a.y }, (x > from ? LEFT : 0) | (x < to ? RIGHT : 0) || LEFT | RIGHT, weight, style);
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const [from, to] = a.y <= b.y ? [a.y, b.y] : [b.y, a.y];
|
|
50
|
+
for (let y = from; y <= to; y++) {
|
|
51
|
+
this.stroke({ x: a.x, y }, (y > from ? UP : 0) | (y < to ? DOWN : 0) || UP | DOWN, weight, style);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** A rectangle's border. */
|
|
55
|
+
rect({ x, y, width, height }, weight, style, corner) {
|
|
56
|
+
const right = x + width - 1;
|
|
57
|
+
const bottom = y + height - 1;
|
|
58
|
+
this.segment({ x, y }, { x: right, y }, weight, style);
|
|
59
|
+
this.segment({ x, y: bottom }, { x: right, y: bottom }, weight, style);
|
|
60
|
+
this.segment({ x, y }, { x, y: bottom }, weight, style);
|
|
61
|
+
this.segment({ x: right, y }, { x: right, y: bottom }, weight, style);
|
|
62
|
+
if (corner === undefined)
|
|
63
|
+
return;
|
|
64
|
+
for (const [cx, cy] of [
|
|
65
|
+
[x, y],
|
|
66
|
+
[right, y],
|
|
67
|
+
[x, bottom],
|
|
68
|
+
[right, bottom],
|
|
69
|
+
]) {
|
|
70
|
+
const s = this.slot(cx, cy);
|
|
71
|
+
if (s !== undefined)
|
|
72
|
+
s.corner = corner;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Blanks a rectangle: what was drawn there, lines included, is gone. */
|
|
76
|
+
clear({ x, y, width, height }) {
|
|
77
|
+
for (let cy = y; cy < y + height; cy++) {
|
|
78
|
+
for (let cx = x; cx < x + width; cx++) {
|
|
79
|
+
if (this.slot(cx, cy) === undefined)
|
|
80
|
+
continue;
|
|
81
|
+
this.unwide(cx, cy);
|
|
82
|
+
this.slots[cy][cx] = blank();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Puts one character in a cell, over whatever was there. */
|
|
87
|
+
put({ x, y }, ch, style = {}) {
|
|
88
|
+
const s = this.slot(x, y);
|
|
89
|
+
if (s === undefined)
|
|
90
|
+
return;
|
|
91
|
+
this.unwide(x, y);
|
|
92
|
+
this.slots[y][x] = { ch, style, mask: 0, weight: 'dashed' };
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Writes `text` from (x, y) rightwards, stopping before `maxX` (exclusive) or the
|
|
96
|
+
* edge, and never splitting a wide character. A combining mark joins the cell
|
|
97
|
+
* before it. Returns the cells written.
|
|
98
|
+
*/
|
|
99
|
+
text({ x, y }, text, style = {}, maxX = this.width) {
|
|
100
|
+
const limit = Math.min(maxX, this.width);
|
|
101
|
+
let cx = x;
|
|
102
|
+
for (const ch of text) {
|
|
103
|
+
const cp = ch.codePointAt(0);
|
|
104
|
+
// never sent to the terminal, where it would move the cursor instead of drawing
|
|
105
|
+
if (isControl(cp))
|
|
106
|
+
continue;
|
|
107
|
+
const w = charWidth(cp);
|
|
108
|
+
if (w === 0) {
|
|
109
|
+
const prev = this.slot(cx - 1, y);
|
|
110
|
+
if (prev !== undefined && cx > x)
|
|
111
|
+
prev.ch += ch;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (cx + w > limit)
|
|
115
|
+
break;
|
|
116
|
+
if (cx >= 0) {
|
|
117
|
+
this.put({ x: cx, y }, ch, style);
|
|
118
|
+
if (w === 2)
|
|
119
|
+
this.put({ x: cx + 1, y }, '', style);
|
|
120
|
+
}
|
|
121
|
+
cx += w;
|
|
122
|
+
}
|
|
123
|
+
return cx - x;
|
|
124
|
+
}
|
|
125
|
+
/** Merges `patch` into the style of every cell in a rectangle. */
|
|
126
|
+
restyle({ x, y, width, height }, patch) {
|
|
127
|
+
for (let cy = y; cy < y + height; cy++) {
|
|
128
|
+
for (let cx = x; cx < x + width; cx++) {
|
|
129
|
+
const s = this.slot(cx, cy);
|
|
130
|
+
if (s !== undefined)
|
|
131
|
+
s.style = { ...s.style, ...patch };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** The finished picture, lines resolved with `glyphs`. */
|
|
136
|
+
cells(glyphs) {
|
|
137
|
+
return this.slots.map((row) => row.map((s) => ({ ch: s.mask === 0 ? s.ch : glyphs.line(s.mask, s.weight, s.corner), style: s.style })));
|
|
138
|
+
}
|
|
139
|
+
/** Before a cell is overwritten: if it is half of a wide character, blank the other
|
|
140
|
+
* half, so no orphaned half is left to shift the rest of the row. */
|
|
141
|
+
unwide(x, y) {
|
|
142
|
+
const s = this.slot(x, y);
|
|
143
|
+
if (s === undefined || s.mask !== 0)
|
|
144
|
+
return;
|
|
145
|
+
if (s.ch === '') {
|
|
146
|
+
const left = this.slot(x - 1, y);
|
|
147
|
+
if (left !== undefined && left.mask === 0)
|
|
148
|
+
left.ch = ' ';
|
|
149
|
+
s.ch = ' ';
|
|
150
|
+
}
|
|
151
|
+
else if (this.slot(x + 1, y)?.ch === '') {
|
|
152
|
+
this.slots[y][x + 1].ch = ' ';
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type NotationId, type ViewEdge, type ViewNode } from '@diagc/core/internal';
|
|
2
|
+
import type { Glyphs } from './glyphs.js';
|
|
3
|
+
import { type TextShape } from './vocabulary.js';
|
|
4
|
+
/** The cells a name or subtitle wraps at: about what a browser box shows on a line. */
|
|
5
|
+
export declare const NAME_WRAP = 28;
|
|
6
|
+
/** The cells an edge label is cut to: the renderer's edge chip ellipsises near here,
|
|
7
|
+
* and a terminal has less room than a canvas, not more. */
|
|
8
|
+
export declare const LABEL_MAX = 32;
|
|
9
|
+
export interface BoxLine {
|
|
10
|
+
text: string;
|
|
11
|
+
role: 'title' | 'subtitle' | 'row' | 'note';
|
|
12
|
+
}
|
|
13
|
+
/** What a box holds and the cells it needs, border included. */
|
|
14
|
+
export interface BoxText {
|
|
15
|
+
lines: BoxLine[];
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
shape: TextShape;
|
|
19
|
+
dashed: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface BoxContext {
|
|
22
|
+
notation?: NotationId;
|
|
23
|
+
/** a container's direct children on the plane, for a folded one's note */
|
|
24
|
+
inside: number;
|
|
25
|
+
glyphs: Glyphs;
|
|
26
|
+
}
|
|
27
|
+
/** The mark a glyph type's name follows, which is what a legend keys it by. */
|
|
28
|
+
export declare function typeMark(type: string | undefined, shape: TextShape, glyphs: Glyphs): string | undefined;
|
|
29
|
+
export declare function boxText(vn: ViewNode, ctx: BoxContext): BoxText;
|
|
30
|
+
/** What an edge says beside its line: the relation's labels, or an aggregate's own
|
|
31
|
+
* label, then a causal link's polarity and delay. Cut to `LABEL_MAX`. */
|
|
32
|
+
export declare function edgeText(edge: ViewEdge, glyphs: Glyphs): string;
|