@diagc/cli 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +8 -3
  2. package/assets/guide/index.md +1 -1
  3. package/assets/skill/SKILL.md +1 -1
  4. package/dist/cli/io.d.ts +5 -0
  5. package/dist/cli/io.js +4 -0
  6. package/dist/cli/parse.d.ts +7 -0
  7. package/dist/cli/parse.js +25 -0
  8. package/dist/cli/run.js +4 -0
  9. package/dist/cli.js +24 -2
  10. package/dist/commands/shared.d.ts +4 -0
  11. package/dist/commands/shared.js +13 -0
  12. package/dist/commands/show.d.ts +5 -0
  13. package/dist/commands/show.js +115 -0
  14. package/dist/commands/tui.d.ts +3 -0
  15. package/dist/commands/tui.js +44 -0
  16. package/dist/init.d.ts +1 -1
  17. package/dist/init.js +1 -1
  18. package/dist/text/accents.d.ts +9 -0
  19. package/dist/text/accents.js +83 -0
  20. package/dist/text/ansi.d.ts +28 -0
  21. package/dist/text/ansi.js +117 -0
  22. package/dist/text/braille.d.ts +28 -0
  23. package/dist/text/braille.js +58 -0
  24. package/dist/text/canvas.d.ts +72 -0
  25. package/dist/text/canvas.js +173 -0
  26. package/dist/text/content.d.ts +32 -0
  27. package/dist/text/content.js +90 -0
  28. package/dist/text/dos.d.ts +69 -0
  29. package/dist/text/dos.js +93 -0
  30. package/dist/text/fish.d.ts +27 -0
  31. package/dist/text/fish.js +209 -0
  32. package/dist/text/folds.d.ts +14 -0
  33. package/dist/text/folds.js +45 -0
  34. package/dist/text/glyphs.d.ts +32 -0
  35. package/dist/text/glyphs.js +100 -0
  36. package/dist/text/layout.d.ts +70 -0
  37. package/dist/text/layout.js +337 -0
  38. package/dist/text/legend.d.ts +44 -0
  39. package/dist/text/legend.js +256 -0
  40. package/dist/text/paint.d.ts +27 -0
  41. package/dist/text/paint.js +260 -0
  42. package/dist/text/render.d.ts +11 -0
  43. package/dist/text/render.js +28 -0
  44. package/dist/text/scene.d.ts +44 -0
  45. package/dist/text/scene.js +58 -0
  46. package/dist/text/vocabulary.d.ts +37 -0
  47. package/dist/text/vocabulary.js +211 -0
  48. package/dist/text/width.d.ts +15 -0
  49. package/dist/text/width.js +181 -0
  50. package/dist/tui/actions.d.ts +15 -0
  51. package/dist/tui/actions.js +300 -0
  52. package/dist/tui/app.d.ts +34 -0
  53. package/dist/tui/app.js +402 -0
  54. package/dist/tui/details.d.ts +3 -0
  55. package/dist/tui/details.js +54 -0
  56. package/dist/tui/keys.d.ts +33 -0
  57. package/dist/tui/keys.js +113 -0
  58. package/dist/tui/kitty.d.ts +36 -0
  59. package/dist/tui/kitty.js +65 -0
  60. package/dist/tui/legend.d.ts +14 -0
  61. package/dist/tui/legend.js +18 -0
  62. package/dist/tui/nav.d.ts +31 -0
  63. package/dist/tui/nav.js +101 -0
  64. package/dist/tui/overview.d.ts +48 -0
  65. package/dist/tui/overview.js +145 -0
  66. package/dist/tui/pixels.d.ts +28 -0
  67. package/dist/tui/pixels.js +80 -0
  68. package/dist/tui/screen.d.ts +54 -0
  69. package/dist/tui/screen.js +416 -0
  70. package/dist/tui/state.d.ts +102 -0
  71. package/dist/tui/state.js +80 -0
  72. package/dist/tui/terminal.d.ts +21 -0
  73. package/dist/tui/terminal.js +53 -0
  74. package/dist/watch.d.ts +2 -0
  75. package/dist/watch.js +1 -0
  76. package/package.json +3 -2
@@ -0,0 +1,117 @@
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
+ /** The SGR parameters for `color` as the text's colour, or with `ground` as the cell's
63
+ * background: each code is the foreground's plus ten. */
64
+ function colorCode(color, mode, ground = false) {
65
+ const base = ground ? 10 : 0;
66
+ if ('ansi' in color)
67
+ return String(base + (color.ansi < 8 ? 30 + color.ansi : 90 + color.ansi - 8));
68
+ return mode === 'truecolor' ? `${38 + base};2;${color.rgb.join(';')}` : `${38 + base};5;${palette256(color.rgb)}`;
69
+ }
70
+ /** The escape that sets `style`, or `''` for the plain style. */
71
+ export function sgr(style, mode) {
72
+ if (mode === 'none')
73
+ return '';
74
+ const codes = [];
75
+ if (style.bold === true)
76
+ codes.push('1');
77
+ if (style.dim === true)
78
+ codes.push('2');
79
+ if (style.inverse === true)
80
+ codes.push('7');
81
+ if (style.fg !== undefined && mode !== 'mono')
82
+ codes.push(colorCode(style.fg, mode));
83
+ if (style.bg !== undefined && mode !== 'mono')
84
+ codes.push(colorCode(style.bg, mode, true));
85
+ return codes.length === 0 ? '' : `\x1b[${codes.join(';')}m`;
86
+ }
87
+ const RESET = '\x1b[0m';
88
+ /** A blank that shows nothing: trailing ones can go. An inverse blank is a bar, and a
89
+ * blank on a background is a patch of it. */
90
+ const invisible = (cell) => cell.ch === ' ' && cell.style.inverse !== true && cell.style.bg === undefined;
91
+ /**
92
+ * The picture as text, one string per row. Trailing blanks are trimmed unless `trim`
93
+ * is false (a full-screen frame keeps its width). A row that set a style ends with a
94
+ * reset, so no colour leaks into the next line or the shell.
95
+ */
96
+ export function serialize(rows, mode, trim = true) {
97
+ return rows.map((row) => {
98
+ let end = row.length;
99
+ if (trim)
100
+ while (end > 0 && (invisible(row[end - 1]) || row[end - 1].ch === ''))
101
+ end--;
102
+ let out = '';
103
+ let current = '';
104
+ for (let i = 0; i < end; i++) {
105
+ const cell = row[i];
106
+ if (cell.ch === '')
107
+ continue;
108
+ const code = sgr(cell.style, mode);
109
+ if (code !== current) {
110
+ out += current === '' ? code : `${RESET}${code}`;
111
+ current = code;
112
+ }
113
+ out += cell.ch;
114
+ }
115
+ return current === '' ? out : `${out}${RESET}`;
116
+ });
117
+ }
@@ -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,72 @@
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
+ /** the cell's background */
11
+ bg?: Color;
12
+ bold?: boolean;
13
+ dim?: boolean;
14
+ inverse?: boolean;
15
+ }
16
+ /** One cell of a finished picture. `ch` is `''` in the second cell of a wide
17
+ * character, which the character before it already covers. */
18
+ export interface Cell {
19
+ ch: string;
20
+ style: Style;
21
+ }
22
+ export interface CellPoint {
23
+ x: number;
24
+ y: number;
25
+ }
26
+ /** A rectangle of cells: from (x, y) to (x + width − 1, y + height − 1). */
27
+ export interface CellRect extends CellPoint {
28
+ width: number;
29
+ height: number;
30
+ }
31
+ /**
32
+ * A grid of character cells that lines, boxes and text are drawn into. Lines are
33
+ * kept as direction masks until `cells` resolves them, so whatever meets in a cell
34
+ * — a crossing, an edge leaving a box, a frame's corner — merges into one junction.
35
+ * Anything drawn outside the grid is dropped, which lets callers draw without
36
+ * clipping first.
37
+ */
38
+ export declare class Canvas {
39
+ readonly width: number;
40
+ readonly height: number;
41
+ private readonly slots;
42
+ constructor(width: number, height: number);
43
+ private slot;
44
+ /** Adds line directions to a cell. The heavier weight wins a shared cell, and the
45
+ * directions drawn lighter are kept, for a junction that can show both. */
46
+ stroke({ x, y }: CellPoint, mask: number, weight: Weight, style?: Style): void;
47
+ /** An axis-aligned line from `a` to `b`, both ends included. */
48
+ segment(a: CellPoint, b: CellPoint, weight: Weight, style?: Style): void;
49
+ /** A rectangle's border. */
50
+ rect({ x, y, width, height }: CellRect, weight: Weight, style?: Style, corner?: Corner): void;
51
+ /** Blanks a rectangle: what was drawn there, lines included, is gone. */
52
+ clear({ x, y, width, height }: CellRect): void;
53
+ /** Puts one character in a cell, over whatever was there but its ground. */
54
+ put({ x, y }: CellPoint, ch: string, style?: Style): void;
55
+ /** Lays a style under a rectangle: what is drawn there, before or after, takes its
56
+ * colours where it sets none of its own. A box's fill is its ground, which the text
57
+ * written in it keeps. `clear` takes the ground away with the rest. */
58
+ ground({ x, y, width, height }: CellRect, style: Style): void;
59
+ /**
60
+ * Writes `text` from (x, y) rightwards, stopping before `maxX` (exclusive) or the
61
+ * edge, and never splitting a wide character. A combining mark joins the cell
62
+ * before it. Returns the cells written.
63
+ */
64
+ text({ x, y }: CellPoint, text: string, style?: Style, maxX?: number): number;
65
+ /** Merges `patch` into the style of every cell in a rectangle. */
66
+ restyle({ x, y, width, height }: CellRect, patch: Style): void;
67
+ /** The finished picture, lines resolved with `glyphs`. */
68
+ cells(glyphs: Glyphs): Cell[][];
69
+ /** Before a cell is overwritten: if it is half of a wide character, blank the other
70
+ * half, so no orphaned half is left to shift the rest of the row. */
71
+ private unwide;
72
+ }
@@ -0,0 +1,173 @@
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, double: 3 };
4
+ const blank = () => ({ ch: ' ', style: {}, mask: 0, weight: 'dashed', top: 0 });
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, and the
25
+ * directions drawn lighter are kept, for a junction that can show both. */
26
+ stroke({ x, y }, mask, weight, style) {
27
+ const s = this.slot(x, y);
28
+ if (s === undefined)
29
+ return;
30
+ const was = s.mask;
31
+ if (was === 0)
32
+ this.unwide(x, y);
33
+ s.mask |= mask;
34
+ if (was === 0 || RANK[weight] > RANK[s.weight])
35
+ [s.weight, s.top] = [weight, mask];
36
+ else if (weight === s.weight)
37
+ s.top |= mask;
38
+ if (style !== undefined)
39
+ s.style = style;
40
+ }
41
+ /** An axis-aligned line from `a` to `b`, both ends included. */
42
+ segment(a, b, weight, style) {
43
+ if (a.x !== b.x && a.y !== b.y)
44
+ throw new Error(`not an axis-aligned segment: ${a.x},${a.y} → ${b.x},${b.y}`);
45
+ if (a.y === b.y) {
46
+ const [from, to] = a.x <= b.x ? [a.x, b.x] : [b.x, a.x];
47
+ for (let x = from; x <= to; x++) {
48
+ this.stroke({ x, y: a.y }, (x > from ? LEFT : 0) | (x < to ? RIGHT : 0) || LEFT | RIGHT, weight, style);
49
+ }
50
+ return;
51
+ }
52
+ const [from, to] = a.y <= b.y ? [a.y, b.y] : [b.y, a.y];
53
+ for (let y = from; y <= to; y++) {
54
+ this.stroke({ x: a.x, y }, (y > from ? UP : 0) | (y < to ? DOWN : 0) || UP | DOWN, weight, style);
55
+ }
56
+ }
57
+ /** A rectangle's border. */
58
+ rect({ x, y, width, height }, weight, style, corner) {
59
+ const right = x + width - 1;
60
+ const bottom = y + height - 1;
61
+ this.segment({ x, y }, { x: right, y }, weight, style);
62
+ this.segment({ x, y: bottom }, { x: right, y: bottom }, weight, style);
63
+ this.segment({ x, y }, { x, y: bottom }, weight, style);
64
+ this.segment({ x: right, y }, { x: right, y: bottom }, weight, style);
65
+ if (corner === undefined)
66
+ return;
67
+ for (const [cx, cy] of [
68
+ [x, y],
69
+ [right, y],
70
+ [x, bottom],
71
+ [right, bottom],
72
+ ]) {
73
+ const s = this.slot(cx, cy);
74
+ if (s !== undefined)
75
+ s.corner = corner;
76
+ }
77
+ }
78
+ /** Blanks a rectangle: what was drawn there, lines included, is gone. */
79
+ clear({ x, y, width, height }) {
80
+ for (let cy = y; cy < y + height; cy++) {
81
+ for (let cx = x; cx < x + width; cx++) {
82
+ if (this.slot(cx, cy) === undefined)
83
+ continue;
84
+ this.unwide(cx, cy);
85
+ this.slots[cy][cx] = blank();
86
+ }
87
+ }
88
+ }
89
+ /** Puts one character in a cell, over whatever was there but its ground. */
90
+ put({ x, y }, ch, style = {}) {
91
+ const s = this.slot(x, y);
92
+ if (s === undefined)
93
+ return;
94
+ this.unwide(x, y);
95
+ this.slots[y][x] = { ch, style, mask: 0, weight: 'dashed', top: 0, ...(s.ground ? { ground: s.ground } : {}) };
96
+ }
97
+ /** Lays a style under a rectangle: what is drawn there, before or after, takes its
98
+ * colours where it sets none of its own. A box's fill is its ground, which the text
99
+ * written in it keeps. `clear` takes the ground away with the rest. */
100
+ ground({ x, y, width, height }, style) {
101
+ for (let cy = y; cy < y + height; cy++) {
102
+ for (let cx = x; cx < x + width; cx++) {
103
+ const s = this.slot(cx, cy);
104
+ if (s !== undefined)
105
+ s.ground = style;
106
+ }
107
+ }
108
+ }
109
+ /**
110
+ * Writes `text` from (x, y) rightwards, stopping before `maxX` (exclusive) or the
111
+ * edge, and never splitting a wide character. A combining mark joins the cell
112
+ * before it. Returns the cells written.
113
+ */
114
+ text({ x, y }, text, style = {}, maxX = this.width) {
115
+ const limit = Math.min(maxX, this.width);
116
+ let cx = x;
117
+ for (const ch of text) {
118
+ const cp = ch.codePointAt(0);
119
+ // never sent to the terminal, where it would move the cursor instead of drawing
120
+ if (isControl(cp))
121
+ continue;
122
+ const w = charWidth(cp);
123
+ if (w === 0) {
124
+ const prev = this.slot(cx - 1, y);
125
+ if (prev !== undefined && cx > x)
126
+ prev.ch += ch;
127
+ continue;
128
+ }
129
+ if (cx + w > limit)
130
+ break;
131
+ if (cx >= 0) {
132
+ this.put({ x: cx, y }, ch, style);
133
+ if (w === 2)
134
+ this.put({ x: cx + 1, y }, '', style);
135
+ }
136
+ cx += w;
137
+ }
138
+ return cx - x;
139
+ }
140
+ /** Merges `patch` into the style of every cell in a rectangle. */
141
+ restyle({ x, y, width, height }, patch) {
142
+ for (let cy = y; cy < y + height; cy++) {
143
+ for (let cx = x; cx < x + width; cx++) {
144
+ const s = this.slot(cx, cy);
145
+ if (s !== undefined)
146
+ s.style = { ...s.style, ...patch };
147
+ }
148
+ }
149
+ }
150
+ /** The finished picture, lines resolved with `glyphs`. */
151
+ cells(glyphs) {
152
+ return this.slots.map((row) => row.map((s) => ({
153
+ ch: s.mask === 0 ? s.ch : glyphs.line(s.mask, s.weight, s.corner, s.mask & ~s.top),
154
+ style: s.ground === undefined ? s.style : { ...s.ground, ...s.style },
155
+ })));
156
+ }
157
+ /** Before a cell is overwritten: if it is half of a wide character, blank the other
158
+ * half, so no orphaned half is left to shift the rest of the row. */
159
+ unwide(x, y) {
160
+ const s = this.slot(x, y);
161
+ if (s === undefined || s.mask !== 0)
162
+ return;
163
+ if (s.ch === '') {
164
+ const left = this.slot(x - 1, y);
165
+ if (left !== undefined && left.mask === 0)
166
+ left.ch = ' ';
167
+ s.ch = ' ';
168
+ }
169
+ else if (this.slot(x + 1, y)?.ch === '') {
170
+ this.slots[y][x + 1].ch = ' ';
171
+ }
172
+ }
173
+ }
@@ -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,69 @@
1
+ import type { DiagramModel, NotationId } from '@diagc/core/internal';
2
+ import type { Canvas, CellRect, Color, Style } from './canvas.js';
3
+ import { type TextShape } from './vocabulary.js';
4
+ /** The styles a terminal draws: its own, and DOS. The rest are the browser's. */
5
+ export type TextStyle = 'clean' | 'dos';
6
+ /** The style a diagram asks to be drawn in, as far as a terminal has it. */
7
+ export declare const styleOf: (model: Pick<DiagramModel, "style">) => TextStyle;
8
+ /** CGA's light colours, the boxes' by type in slot order: every one takes black text. */
9
+ export declare const DOS_PALETTE: readonly string[];
10
+ /** A colour a palette or an author wrote down, as against one of the terminal's own. */
11
+ export type Rgb = Extract<Color, {
12
+ rgb: unknown;
13
+ }>;
14
+ export declare const BLUE: {
15
+ rgb: [number, number, number];
16
+ };
17
+ export declare const WHITE: {
18
+ rgb: [number, number, number];
19
+ };
20
+ export declare const BLACK: {
21
+ rgb: [number, number, number];
22
+ };
23
+ export declare const GREY: {
24
+ rgb: [number, number, number];
25
+ };
26
+ export declare const CYAN: {
27
+ rgb: [number, number, number];
28
+ };
29
+ export declare const YELLOW: {
30
+ rgb: [number, number, number];
31
+ };
32
+ export declare const RED: {
33
+ rgb: [number, number, number];
34
+ };
35
+ /** What the picture, and the screen round it, lie on. */
36
+ export declare const DESKTOP: Style;
37
+ /** A box's block shadow: what it falls on, dark grey on black, as Turbo Vision casts it. */
38
+ export declare const SHADOW: Style;
39
+ /** How far right and down the shadow falls: two columns to one row, a cell being about
40
+ * twice as tall as it is wide. */
41
+ export declare const SHADOW_REACH: {
42
+ x: number;
43
+ y: number;
44
+ };
45
+ /**
46
+ * Each node type's palette slot, as the renderer's `typeSlots` gives it: by first
47
+ * appearance, the types of the nodes that contain nothing first, and the types a
48
+ * notation fills alike (C4's containers) sharing one. Counted over the whole model, so
49
+ * a drill or a plane switch never recolours a box.
50
+ */
51
+ export declare function dosSlots(model: Pick<DiagramModel, 'nodes' | 'containment'>, notation?: NotationId): Map<string, number>;
52
+ /** A box's colour: its own (`css`, as the author or the notation set it), else its
53
+ * type's from the palette; nothing for an untyped box, which is grey. */
54
+ export declare function dosHue(css: string | undefined, type: string | undefined, slots: ReadonlyMap<string, number>): Rgb;
55
+ /** A box's fill, as its type's legend swatch shows it too: its colour (`dosHue`), but a
56
+ * table's body grey under its cyan head, as the page draws one. */
57
+ export declare const dosFill: (shape: TextShape, css: string | undefined, type: string | undefined, slots: ReadonlyMap<string, number>) => Rgb;
58
+ /** Black or white, whichever reads on `rgb`: the renderer's `hueInk`. */
59
+ export declare function inkOn([r, g, b]: readonly number[]): Rgb;
60
+ /** `share` of `a` over `b`: a box's small print, its ink 70% over its fill as the page
61
+ * mixes it. */
62
+ export declare const mixed: (a: readonly number[], b: readonly number[], share: number) => Rgb;
63
+ /**
64
+ * Each box's block shadow, laid as the ground beside it and below it before anything is
65
+ * drawn there: a line, which takes its colour from the ground, is shaded with it, while
66
+ * an arrowhead or a label, drawn in a colour of its own, stays bright on the black. A
67
+ * box drawn over another's shadow lays its own ground.
68
+ */
69
+ export declare function castShadows(canvas: Canvas, boxes: readonly CellRect[]): void;