@diagc/cli 1.5.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/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/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,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;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { ACTIVITY_END_TYPE, FB_EFFECT_TYPE, } from '@diagc/core/internal';
|
|
2
|
+
import { subtitleOf, typeLook } from './vocabulary.js';
|
|
3
|
+
import { cellWidth, fitCells, printable, wrapCells } from './width.js';
|
|
4
|
+
/** The cells a name or subtitle wraps at: about what a browser box shows on a line. */
|
|
5
|
+
export 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 const LABEL_MAX = 32;
|
|
9
|
+
/** An open container's title is one line in its top border; past this it is cut. */
|
|
10
|
+
const TITLE_MAX = 60;
|
|
11
|
+
/** The mark a glyph type's name follows, which is what a legend keys it by. */
|
|
12
|
+
export function typeMark(type, shape, glyphs) {
|
|
13
|
+
if (shape === 'diamond')
|
|
14
|
+
return glyphs.decision;
|
|
15
|
+
if (type === ACTIVITY_END_TYPE)
|
|
16
|
+
return glyphs.end;
|
|
17
|
+
// an activity's start, a git commit: the dot is the element, the name a caption
|
|
18
|
+
if (shape === 'dot')
|
|
19
|
+
return glyphs.start;
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const markOf = (vn, shape, glyphs) => vn.external !== undefined ? glyphs.external : typeMark(vn.node.type, shape, glyphs);
|
|
23
|
+
/** A table's rows: a key column when any row is a key, names padded so the types
|
|
24
|
+
* line up in one column. */
|
|
25
|
+
function columnRows(columns) {
|
|
26
|
+
const keyed = columns.some((c) => c.pk === true || c.fk === true);
|
|
27
|
+
const nameWidth = Math.max(0, ...columns.map((c) => cellWidth(printable(c.name))));
|
|
28
|
+
return columns.map((c) => {
|
|
29
|
+
const key = !keyed ? '' : c.pk === true ? 'PK ' : c.fk === true ? 'FK ' : ' ';
|
|
30
|
+
const own = printable(c.name);
|
|
31
|
+
const name = c.type === undefined ? own : `${own}${' '.repeat(nameWidth - cellWidth(own))} ${printable(c.type)}`;
|
|
32
|
+
return { text: `${key}${name}`, role: 'row' };
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** The cells a box's lines need with its border and a cell of padding each side. */
|
|
36
|
+
const widthOf = (lines) => Math.max(...lines.map((l) => cellWidth(l.text))) + 4;
|
|
37
|
+
export function boxText(vn, ctx) {
|
|
38
|
+
const look = typeLook(vn.node.type, ctx.notation);
|
|
39
|
+
const dashed = look.dashed === true || vn.external !== undefined;
|
|
40
|
+
// A table's header is its name alone, as the renderer draws it: the rows say what it is.
|
|
41
|
+
const subtitle = printable(look.shape === 'table' ? '' : subtitleOf(vn.node, ctx.notation));
|
|
42
|
+
if (vn.state === 'expanded') {
|
|
43
|
+
// one line in the border: a name's own line breaks become spaces there
|
|
44
|
+
const own = printable(vn.node.name);
|
|
45
|
+
const title = fitCells(subtitle === '' ? own : `${own} ${subtitle}`, TITLE_MAX, ctx.glyphs.ellipsis);
|
|
46
|
+
return {
|
|
47
|
+
lines: [{ text: title, role: 'title' }],
|
|
48
|
+
width: cellWidth(title) + 6,
|
|
49
|
+
height: 3,
|
|
50
|
+
shape: look.shape,
|
|
51
|
+
dashed,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const mark = markOf(vn, look.shape, ctx.glyphs);
|
|
55
|
+
const name = mark === undefined ? vn.node.name : vn.node.name === '' ? mark : `${mark} ${vn.node.name}`;
|
|
56
|
+
// A name keeps its own line breaks (the studio's name field takes them), each line
|
|
57
|
+
// wrapped on its own. A fishbone's head is one line, as on the page: it names the
|
|
58
|
+
// whole fish, where wrapping would leave a word on a line of its own.
|
|
59
|
+
const lines = vn.node.type === FB_EFFECT_TYPE
|
|
60
|
+
? [{ text: fitCells(printable(name), TITLE_MAX, ctx.glyphs.ellipsis), role: 'title' }]
|
|
61
|
+
: name
|
|
62
|
+
.split(/\r?\n/)
|
|
63
|
+
.flatMap((line) => wrapCells(printable(line), NAME_WRAP))
|
|
64
|
+
.map((text) => ({ text, role: 'title' }));
|
|
65
|
+
for (const text of subtitle === '' ? [] : wrapCells(subtitle, NAME_WRAP))
|
|
66
|
+
lines.push({ text, role: 'subtitle' });
|
|
67
|
+
const columns = look.shape === 'table' ? (vn.columns ?? vn.node.columns ?? []) : [];
|
|
68
|
+
lines.push(...columnRows(columns));
|
|
69
|
+
if (vn.state === 'collapsed')
|
|
70
|
+
lines.push({ text: `${ctx.glyphs.folded} ${ctx.inside} inside`, role: 'note' });
|
|
71
|
+
// a cylinder's lid and a table's rule under its title each take a row
|
|
72
|
+
const extra = look.shape === 'cylinder' || columns.length > 0 ? 1 : 0;
|
|
73
|
+
return { lines, width: widthOf(lines), height: lines.length + 2 + extra, shape: look.shape, dashed };
|
|
74
|
+
}
|
|
75
|
+
/** What an edge says beside its line: the relation's labels, or an aggregate's own
|
|
76
|
+
* label, then a causal link's polarity and delay. Cut to `LABEL_MAX`. */
|
|
77
|
+
export function edgeText(edge, glyphs) {
|
|
78
|
+
const said = edge.constituents.length === 1
|
|
79
|
+
? (edge.labels ?? [])
|
|
80
|
+
.map((l) => printable(l.text).trim())
|
|
81
|
+
.filter((t) => t !== '')
|
|
82
|
+
.join(' / ')
|
|
83
|
+
: printable(edge.label ?? '');
|
|
84
|
+
const parts = [said];
|
|
85
|
+
if (edge.polarity !== undefined)
|
|
86
|
+
parts.push(`(${edge.polarity})`);
|
|
87
|
+
if (edge.delay === true)
|
|
88
|
+
parts.push(glyphs.delay);
|
|
89
|
+
return fitCells(parts.filter((p) => p !== '').join(' '), LABEL_MAX, glyphs.ellipsis);
|
|
90
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type CompiledView, type DiagramModel } from '@diagc/core/internal';
|
|
2
|
+
import type { BoxText } from './content.js';
|
|
3
|
+
import type { TextLayout } from './layout.js';
|
|
4
|
+
/** The fish's spacing, in cells. */
|
|
5
|
+
export declare const FISH: {
|
|
6
|
+
/** the shortest bone, in rows */
|
|
7
|
+
readonly BONE_MIN: 2;
|
|
8
|
+
/** the shortest line from a cause's words to its bone */
|
|
9
|
+
readonly LINE_MIN: 2;
|
|
10
|
+
/** from where a cause's line meets its bone to where its first sub-cause's tick
|
|
11
|
+
* does: far enough that the tick does not read as a second bone */
|
|
12
|
+
readonly SUB_INSET: 4;
|
|
13
|
+
/** between a sub-cause's words and the tick of the next one along */
|
|
14
|
+
readonly SUB_GAP: 2;
|
|
15
|
+
/** between a column's bones and the next column's words, and before the head */
|
|
16
|
+
readonly COLUMN_GAP: 3;
|
|
17
|
+
/** between the fish and the row under it of what is not on it */
|
|
18
|
+
readonly STRAY_ROW: 2;
|
|
19
|
+
/** between two boxes in that row */
|
|
20
|
+
readonly STRAY_GAP: 4;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The fish in cells, for a fishbone plane: every node the view shows gets a box, the
|
|
24
|
+
* words' cells for one on the fish, and every drawn relation on the fish a route. A
|
|
25
|
+
* view without its effect has no fish, only the row of boxes.
|
|
26
|
+
*/
|
|
27
|
+
export declare function fishCells(model: DiagramModel, view: CompiledView, boxes: ReadonlyMap<string, BoxText>): TextLayout;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { fishboneTree, } from '@diagc/core/internal';
|
|
2
|
+
import { cellWidth, printable } from './width.js';
|
|
3
|
+
// A fishbone in cells, as the page draws it (the renderer's fishbone-layout.ts): the
|
|
4
|
+
// spine across with the head boxed at its right end, the categories alternately above
|
|
5
|
+
// and below it in relation order, two to a column from left to right, each on a bone
|
|
6
|
+
// slanting in to the spine. A slant is one cell a row, and a cell is about twice as
|
|
7
|
+
// tall as it is wide, so it comes out near the page's sixty degrees. A bone's causes
|
|
8
|
+
// hang on lines that end at it, the first nearest its label, and their sub-causes sit
|
|
9
|
+
// past those lines on ticks. What is not on the fish goes in a row of boxes under it.
|
|
10
|
+
/** The fish's spacing, in cells. */
|
|
11
|
+
export const FISH = {
|
|
12
|
+
/** the shortest bone, in rows */
|
|
13
|
+
BONE_MIN: 2,
|
|
14
|
+
/** the shortest line from a cause's words to its bone */
|
|
15
|
+
LINE_MIN: 2,
|
|
16
|
+
/** from where a cause's line meets its bone to where its first sub-cause's tick
|
|
17
|
+
* does: far enough that the tick does not read as a second bone */
|
|
18
|
+
SUB_INSET: 4,
|
|
19
|
+
/** between a sub-cause's words and the tick of the next one along */
|
|
20
|
+
SUB_GAP: 2,
|
|
21
|
+
/** between a column's bones and the next column's words, and before the head */
|
|
22
|
+
COLUMN_GAP: 3,
|
|
23
|
+
/** between the fish and the row under it of what is not on it */
|
|
24
|
+
STRAY_ROW: 2,
|
|
25
|
+
/** between two boxes in that row */
|
|
26
|
+
STRAY_GAP: 4,
|
|
27
|
+
};
|
|
28
|
+
/** A name on one line: what a category's label and a sub-cause's words are. */
|
|
29
|
+
const oneLine = (fish, id) => printable(fish.model.nodes
|
|
30
|
+
.find((n) => n.id === id)
|
|
31
|
+
.name.split(/\r?\n/)
|
|
32
|
+
.join(' '));
|
|
33
|
+
/** A cause's words, wrapped as its box would wrap them. */
|
|
34
|
+
const linesOf = (fish, id) => fish.boxes.get(id)?.lines.map((l) => l.text) ?? [oneLine(fish, id)];
|
|
35
|
+
/**
|
|
36
|
+
* One category's side of the spine: the causes from the spine outwards, the last one
|
|
37
|
+
* nearest it, a blank row between, then the bone up past the outermost words and its
|
|
38
|
+
* label. A cause's words end a space before its line, right-aligned when they wrap,
|
|
39
|
+
* and hang from the line towards the spine. Its sub-causes go leftwards along the row
|
|
40
|
+
* past the line, from near the bone, each on a tick that meets the line.
|
|
41
|
+
*/
|
|
42
|
+
function sideOf(fish, category, above) {
|
|
43
|
+
const words = new Map();
|
|
44
|
+
const links = [];
|
|
45
|
+
const causes = category.causes.filter((c) => fish.shown.has(c.id));
|
|
46
|
+
let start = FISH.BONE_MIN;
|
|
47
|
+
let top = 0;
|
|
48
|
+
for (const cause of [...causes].reverse()) {
|
|
49
|
+
// Hung towards the spine, the words leave the row past the line to the sub-causes,
|
|
50
|
+
// each on a tick of one cell, crossing nothing. There they may reach out over the
|
|
51
|
+
// cause's words, which keeps the line short: it need only reach the ticks.
|
|
52
|
+
const lines = linesOf(fish, cause.id);
|
|
53
|
+
const h = start + lines.length - 1;
|
|
54
|
+
const bone = -h;
|
|
55
|
+
let land = bone - FISH.SUB_INSET;
|
|
56
|
+
let line = bone - FISH.LINE_MIN;
|
|
57
|
+
const subs = cause.subs.filter((s) => fish.shown.has(s));
|
|
58
|
+
for (const sub of subs) {
|
|
59
|
+
const text = oneLine(fish, sub);
|
|
60
|
+
const tick = land - 1;
|
|
61
|
+
const begin = tick - 1 - cellWidth(text);
|
|
62
|
+
words.set(sub, [{ x: begin, h: h + 1, text }]);
|
|
63
|
+
links.push({
|
|
64
|
+
from: sub,
|
|
65
|
+
to: cause.id,
|
|
66
|
+
points: [
|
|
67
|
+
{ x: tick, h: h + 1 },
|
|
68
|
+
{ x: land, h },
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
line = Math.min(line, land);
|
|
72
|
+
land = begin - FISH.SUB_GAP;
|
|
73
|
+
}
|
|
74
|
+
links.push({
|
|
75
|
+
from: cause.id,
|
|
76
|
+
to: category.id,
|
|
77
|
+
points: [
|
|
78
|
+
{ x: line, h },
|
|
79
|
+
{ x: bone - 1, h },
|
|
80
|
+
],
|
|
81
|
+
});
|
|
82
|
+
const k = lines.length;
|
|
83
|
+
words.set(cause.id, lines.map((text, i) => ({ x: line - 1 - cellWidth(text), h: above ? h - i : h - (k - 1 - i), text })));
|
|
84
|
+
top = h + (subs.length > 0 ? 1 : 0);
|
|
85
|
+
start = top + 2;
|
|
86
|
+
}
|
|
87
|
+
const height = Math.max(FISH.BONE_MIN, top);
|
|
88
|
+
const label = oneLine(fish, category.id);
|
|
89
|
+
const labelX = -(height + 1) - Math.floor(cellWidth(label) / 2);
|
|
90
|
+
words.set(category.id, [{ x: labelX, h: height + 1, text: label }]);
|
|
91
|
+
const starts = [...words.values()].flat().map((w) => w.x);
|
|
92
|
+
return {
|
|
93
|
+
category: category.id,
|
|
94
|
+
above,
|
|
95
|
+
height,
|
|
96
|
+
words,
|
|
97
|
+
links,
|
|
98
|
+
left: -Math.min(...starts),
|
|
99
|
+
right: Math.max(0, labelX + cellWidth(label) - 1),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const extent = (lines) => {
|
|
103
|
+
const x = Math.min(...lines.map((l) => l.x));
|
|
104
|
+
const y = Math.min(...lines.map((l) => l.y));
|
|
105
|
+
return {
|
|
106
|
+
x,
|
|
107
|
+
y,
|
|
108
|
+
width: Math.max(...lines.map((l) => l.x + cellWidth(l.text))) - x,
|
|
109
|
+
height: Math.max(...lines.map((l) => l.y)) - y + 1,
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
/** Every node the view shows, parents before children. */
|
|
113
|
+
function shownNodes(view) {
|
|
114
|
+
const out = [];
|
|
115
|
+
const walk = (vn) => {
|
|
116
|
+
out.push(vn);
|
|
117
|
+
vn.children.forEach(walk);
|
|
118
|
+
};
|
|
119
|
+
view.roots.forEach(walk);
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
/** What is not on the fish, boxed in a row from the left at `y`. */
|
|
123
|
+
function placeStrays(nodes, fish, y, boxes) {
|
|
124
|
+
let x = 0;
|
|
125
|
+
for (const vn of nodes) {
|
|
126
|
+
if (boxes.has(vn.id))
|
|
127
|
+
continue;
|
|
128
|
+
const text = fish.boxes.get(vn.id);
|
|
129
|
+
const [width, height] = [text?.width ?? 5, text?.height ?? 3];
|
|
130
|
+
boxes.set(vn.id, { x, y, width, height });
|
|
131
|
+
x += width + FISH.STRAY_GAP;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** The picture's size: every box and every route; the spine runs inside them. */
|
|
135
|
+
function measure(boxes, routes) {
|
|
136
|
+
let [width, height] = [0, 0];
|
|
137
|
+
for (const b of boxes.values())
|
|
138
|
+
[width, height] = [Math.max(width, b.x + b.width), Math.max(height, b.y + b.height)];
|
|
139
|
+
for (const r of routes.values()) {
|
|
140
|
+
for (const p of r.points)
|
|
141
|
+
[width, height] = [Math.max(width, p.x + 1), Math.max(height, p.y + 1)];
|
|
142
|
+
}
|
|
143
|
+
return [width, height];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The fish in cells, for a fishbone plane: every node the view shows gets a box, the
|
|
147
|
+
* words' cells for one on the fish, and every drawn relation on the fish a route. A
|
|
148
|
+
* view without its effect has no fish, only the row of boxes.
|
|
149
|
+
*/
|
|
150
|
+
export function fishCells(model, view, boxes) {
|
|
151
|
+
const nodes = shownNodes(view);
|
|
152
|
+
const fish = { model, boxes, shown: new Set(nodes.map((n) => n.id)) };
|
|
153
|
+
const tree = fishboneTree(model);
|
|
154
|
+
const effect = tree.effect !== undefined && fish.shown.has(tree.effect) ? tree.effect : undefined;
|
|
155
|
+
const placed = new Map();
|
|
156
|
+
const routes = new Map();
|
|
157
|
+
if (effect === undefined) {
|
|
158
|
+
placeStrays(nodes, fish, 0, placed);
|
|
159
|
+
const [width, height] = measure(placed, routes);
|
|
160
|
+
return { width, height, boxes: placed, routes };
|
|
161
|
+
}
|
|
162
|
+
const sides = tree.categories.filter((c) => fish.shown.has(c.id)).map((c, i) => sideOf(fish, c, i % 2 === 0));
|
|
163
|
+
const head = boxes.get(effect);
|
|
164
|
+
const reach = (above) => Math.max(0, ...sides.filter((s) => s.above === above).map((s) => s.height + 1));
|
|
165
|
+
const spine = Math.max(reach(true), Math.floor(head.height / 2));
|
|
166
|
+
const bottom = spine + Math.max(reach(false), head.height - 1 - Math.floor(head.height / 2));
|
|
167
|
+
const edgeOf = new Map(view.edges.map((e) => [`${e.from} ${e.to}`, e.id]));
|
|
168
|
+
const words = new Map();
|
|
169
|
+
let left = 0;
|
|
170
|
+
for (let c = 0; c < sides.length; c += 2) {
|
|
171
|
+
const column = sides.slice(c, c + 2);
|
|
172
|
+
const join = left + Math.max(...column.map((s) => s.left));
|
|
173
|
+
for (const side of column) {
|
|
174
|
+
const at = (p) => ({ x: join + p.x, y: side.above ? spine - p.h : spine + p.h });
|
|
175
|
+
for (const [id, lines] of side.words) {
|
|
176
|
+
const placedLines = lines.map((l) => ({ ...at(l), text: l.text }));
|
|
177
|
+
words.set(id, placedLines);
|
|
178
|
+
placed.set(id, extent(placedLines));
|
|
179
|
+
}
|
|
180
|
+
const bone = {
|
|
181
|
+
from: side.category,
|
|
182
|
+
to: effect,
|
|
183
|
+
points: [
|
|
184
|
+
{ x: -side.height, h: side.height },
|
|
185
|
+
{ x: 0, h: 0 },
|
|
186
|
+
],
|
|
187
|
+
};
|
|
188
|
+
for (const link of [...side.links, bone]) {
|
|
189
|
+
const id = edgeOf.get(`${link.from} ${link.to}`);
|
|
190
|
+
if (id !== undefined)
|
|
191
|
+
routes.set(id, { points: link.points.map(at) });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
left = join + Math.max(...column.map((s) => s.right)) + 1 + FISH.COLUMN_GAP;
|
|
195
|
+
}
|
|
196
|
+
// with no bones yet the head still ends a spine, a stub of one
|
|
197
|
+
const headX = sides.length === 0 ? FISH.COLUMN_GAP + 1 : left;
|
|
198
|
+
placed.set(effect, { x: headX, y: spine - Math.floor(head.height / 2), width: head.width, height: head.height });
|
|
199
|
+
placeStrays(nodes, fish, bottom + 1 + FISH.STRAY_ROW, placed);
|
|
200
|
+
const [width, height] = measure(placed, routes);
|
|
201
|
+
const labels = new Set(sides.map((s) => s.category));
|
|
202
|
+
return {
|
|
203
|
+
width,
|
|
204
|
+
height,
|
|
205
|
+
boxes: placed,
|
|
206
|
+
routes,
|
|
207
|
+
fish: { spine: { y: spine, from: 0, to: headX }, head: effect, words, labels },
|
|
208
|
+
};
|
|
209
|
+
}
|