@diagc/cli 1.7.0 → 1.8.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.
@@ -15,6 +15,7 @@ export interface FlagValues {
15
15
  '--layers': string[];
16
16
  '--depth': number;
17
17
  '--root': string;
18
+ '--horizontal': true;
18
19
  '--ascii': true;
19
20
  '--color': true;
20
21
  '--no-color': true;
@@ -46,13 +47,19 @@ export declare class UsageError extends Error {
46
47
  readonly exitCode: number;
47
48
  constructor(message: string, exitCode: number);
48
49
  }
50
+ /** `--version`: the run prints the version, whatever else argv says after it, and ends
51
+ * with exit 0. */
52
+ export declare class VersionRequest extends Error {
53
+ constructor();
54
+ }
49
55
  /** The style presets `--style` takes: the renderer's, by id and in its order. The CLI
50
56
  * cannot import the renderer (it only couples to the viewer as a built artifact), so
51
57
  * the list is written out and styleIds.test.ts pins it to the renderer's. */
52
58
  export declare const STYLE_IDS: string[];
53
59
  /** Read argv against every flag there is, whichever command it names: which arguments
54
60
  * are values depends on the flags alone, and the command may come after its flags. The
55
- * first mistake in argv's order is thrown as a {@link UsageError}. An unknown flag is an
61
+ * first mistake in argv's order is thrown as a {@link UsageError}, and so is `--help`;
62
+ * `--version` is a {@link VersionRequest}. An unknown flag is an
56
63
  * error rather than a file path, so a typo surfaces loudly instead of quietly skewing
57
64
  * the file set. */
58
65
  export declare function parseArgv(argv: readonly string[]): ParsedArgv;
package/dist/cli/parse.js CHANGED
@@ -10,6 +10,14 @@ export class UsageError extends Error {
10
10
  this.name = 'UsageError';
11
11
  }
12
12
  }
13
+ /** `--version`: the run prints the version, whatever else argv says after it, and ends
14
+ * with exit 0. */
15
+ export class VersionRequest extends Error {
16
+ constructor() {
17
+ super('version requested');
18
+ this.name = 'VersionRequest';
19
+ }
20
+ }
13
21
  const badValue = (flag, why) => new UsageError(`${flag}: ${why}`, 1);
14
22
  /** The style presets `--style` takes: the renderer's, by id and in its order. The CLI
15
23
  * cannot import the renderer (it only couples to the viewer as a built artifact), so
@@ -109,6 +117,7 @@ const FLAGS = {
109
117
  return Number(next);
110
118
  },
111
119
  '--root': required('--root', 'needs a node id, e.g. --root checkout'),
120
+ '--horizontal': null,
112
121
  '--ascii': null,
113
122
  '--color': null,
114
123
  '--no-color': null,
@@ -116,7 +125,8 @@ const FLAGS = {
116
125
  const isFlag = (arg) => Object.hasOwn(FLAGS, arg);
117
126
  /** Read argv against every flag there is, whichever command it names: which arguments
118
127
  * are values depends on the flags alone, and the command may come after its flags. The
119
- * first mistake in argv's order is thrown as a {@link UsageError}. An unknown flag is an
128
+ * first mistake in argv's order is thrown as a {@link UsageError}, and so is `--help`;
129
+ * `--version` is a {@link VersionRequest}. An unknown flag is an
120
130
  * error rather than a file path, so a typo surfaces loudly instead of quietly skewing
121
131
  * the file set. */
122
132
  export function parseArgv(argv) {
@@ -132,6 +142,8 @@ export function parseArgv(argv) {
132
142
  }
133
143
  if (arg === '--help' || arg === '-h')
134
144
  throw new UsageError('help requested', 0);
145
+ if (arg === '--version')
146
+ throw new VersionRequest();
135
147
  if (arg.startsWith('--'))
136
148
  throw new UsageError(`Unknown flag '${arg}'`, 1);
137
149
  // The first argument that is not a flag names the command; anything after it is
package/dist/cli/run.js CHANGED
@@ -11,11 +11,11 @@ import { show } from '../commands/show.js';
11
11
  import { studio } from '../commands/studio.js';
12
12
  import { tui } from '../commands/tui.js';
13
13
  import { watch } from '../commands/watch.js';
14
- import { findHome, homePaths } from '../home.js';
14
+ import { cliVersion, findHome, homePaths } from '../home.js';
15
15
  import { resolveInclude } from '../includes.js';
16
16
  import { RELATIVE_PATHS } from '../paths.js';
17
17
  import { snapshotSession } from '../snapshots.js';
18
- import { checkTaken, parseArgv, UsageError } from './parse.js';
18
+ import { checkTaken, parseArgv, UsageError, VersionRequest } from './parse.js';
19
19
  /** Every command by name, in the order the refusal of a flag names the commands that
20
20
  * take it. */
21
21
  export const COMMANDS = {
@@ -56,6 +56,10 @@ export async function run(argv, io, usage) {
56
56
  return await dispatch(parsed, io, usage);
57
57
  }
58
58
  catch (e) {
59
+ if (e instanceof VersionRequest) {
60
+ io.out(cliVersion(homePaths(findHome(fileURLToPath(import.meta.url))).root));
61
+ return 0;
62
+ }
59
63
  if (!(e instanceof UsageError)) {
60
64
  io.err(errMessage(e));
61
65
  return 1;
package/dist/cli.js CHANGED
@@ -31,7 +31,8 @@ Commands:
31
31
  compile Compile *.diagram.{ts,json} sources into overlay artifacts once
32
32
  lint Report suspicious diagrams (typos, duplicates, unused or undrawn parts); exit 1 on any
33
33
  watch Recompile — and live-recompile — a directory of sources
34
- publish Compile and render an HTML/PNG site under .diagrams/
34
+ publish publish [names...]: compile, then write an HTML page and PNGs under
35
+ .diagrams/ for each diagram named, or for every diagram with none
35
36
  show show <name>: print the diagram as text — boxes, lines and labels in the
36
37
  terminal or a pipe, every container open (--depth folds deeper levels)
37
38
  studio Run the visual studio against the current directory
@@ -55,6 +56,8 @@ Options:
55
56
  --layers a,b show: draw these layers in place of the plane's own ('' for none)
56
57
  --depth n show: open only the top n levels of containers
57
58
  --root id show: draw this container's inside only
59
+ --horizontal show: a git graph across the screen, its lanes as rows, as the page
60
+ draws it (default: down the screen, its lanes as columns)
58
61
  --ascii show: plain ASCII in place of box-drawing characters
59
62
  --color, --no-color show: colour or not, whatever stdout is (default: a terminal is)
60
63
  --json lint: print the findings as a JSON array; diff: the changes as JSON
@@ -65,6 +68,7 @@ Options:
65
68
  --agents init: write the coding-agent block into AGENTS.md / CLAUDE.md, and
66
69
  the skill into .claude/skills/diagc/ where Claude Code is in use
67
70
  --help, -h Show this help and exit
71
+ --version Print diagc's version and exit
68
72
  `;
69
73
  /** End the process once stdout and stderr have taken everything written to them:
70
74
  * `process.exit` straight after a write drops what a pipe has not accepted yet. A
@@ -1,5 +1,5 @@
1
1
  import type { Command } from '../cli/command.js';
2
- type ShowFlag = '--plane' | '--layers' | '--depth' | '--root' | '--ascii' | '--color' | '--no-color';
2
+ type ShowFlag = '--plane' | '--layers' | '--depth' | '--root' | '--horizontal' | '--ascii' | '--color' | '--no-color';
3
3
  /** `diagc show <name>`: the diagram as text on stdout. */
4
4
  export declare const show: Command<ShowFlag>;
5
5
  export {};
@@ -64,7 +64,7 @@ function widthHint(io, lines, name) {
64
64
  }
65
65
  /** `diagc show <name>`: the diagram as text on stdout. */
66
66
  export const show = {
67
- flags: ['--plane', '--layers', '--depth', '--root', '--ascii', '--color', '--no-color'],
67
+ flags: ['--plane', '--layers', '--depth', '--root', '--horizontal', '--ascii', '--color', '--no-color'],
68
68
  positionals: { max: 1, what: 'one diagram name' },
69
69
  whyNot: LOCKED,
70
70
  async run({ positionals, flags }, { io, usage, home, snapshots }) {
@@ -103,7 +103,17 @@ export const show = {
103
103
  const plane = flags['--plane'];
104
104
  // A printed picture folds as the PNG does; --depth trades that for the top levels.
105
105
  const pins = flags['--depth'] !== undefined ? depthPins(model, plane, flags['--depth']) : exportPins(model, overlay);
106
- const lines = await renderToLines({ model, pins, ...defined({ overlay, plane, layers: flags['--layers'], root: flags['--root'] }) }, { ascii: flags['--ascii'] === true, color: colorModeFor(process.env, io.tty !== undefined, colorFlag(flags)) });
106
+ const lines = await renderToLines({
107
+ model,
108
+ pins,
109
+ ...defined({
110
+ overlay,
111
+ plane,
112
+ layers: flags['--layers'],
113
+ root: flags['--root'],
114
+ horizontal: flags['--horizontal'],
115
+ }),
116
+ }, { ascii: flags['--ascii'] === true, color: colorModeFor(process.env, io.tty !== undefined, colorFlag(flags)) });
107
117
  if (lines.length === 0) {
108
118
  io.err(`diagc: nothing to draw in ${name}`);
109
119
  return 0;
@@ -0,0 +1,52 @@
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 graph's spacing across the screen, in cells. */
5
+ export declare const GIT: {
6
+ /** neighbouring columns' distance when nothing needs more: a dot and three cells of line */
7
+ readonly PITCH: 4;
8
+ /** the least room between two tags on one lane: two cells, as one would read the
9
+ * two as one phrase */
10
+ readonly TAG_GAP: 2;
11
+ /** rows from one lane's line to the next: the next lane's tags and a blank row between */
12
+ readonly LANE: 3;
13
+ /** what a stage's frame needs besides its title: a corner and a cell of border
14
+ * before it, a space either side, a cell of border and a corner after */
15
+ readonly FRAME_ROOM: 6;
16
+ /** from the last column's reach to the lanes' names: the tail's run and a space */
17
+ readonly NAME_GAP: 2;
18
+ /** blank rows between the lanes and the boxes under them */
19
+ readonly STRAY_ROW: 1;
20
+ /** between two boxes in that row */
21
+ readonly STRAY_GAP: 4;
22
+ };
23
+ /** The graph's spacing down the screen, in cells. */
24
+ export declare const GIT_DOWN: {
25
+ /** rows from one column's dots to the next when nothing needs more: a dot and the row
26
+ * a branch turns off on, or a merge turns in */
27
+ readonly PITCH: 2;
28
+ /** rows a stage's frame reaches past its first and last dots: the row a line turns
29
+ * on, then its border, so no line turns along a border */
30
+ readonly FRAME: 2;
31
+ /** the least room between a lane's widest tag, or its name, and the next lane */
32
+ readonly LANE_GAP: 2;
33
+ /** columns left of the first lane where stages are framed: the frame's side and a space */
34
+ readonly MARGIN: 2;
35
+ /** what a stage's frame needs past the last lane besides its title: a cell of border
36
+ * before it, a space either side, a cell of border and a corner after */
37
+ readonly TITLE_ROOM: 5;
38
+ /** rows from the last dot's reach to the lanes' names: the tail's run */
39
+ readonly NAME_GAP: 2;
40
+ };
41
+ /** How a git graph is drawn: on which plane, and across the screen or down it. */
42
+ export interface GitWay {
43
+ plane?: string;
44
+ /** across the screen, as the page draws it; absent = down it */
45
+ across?: boolean;
46
+ }
47
+ /**
48
+ * The graph in cells, for a git-graph plane: every node the view shows gets a box — a
49
+ * commit its dot and tag, a lane its name, a stage its frame — and every drawn git
50
+ * link between two dots a route from one to the other.
51
+ */
52
+ export declare function gitCells(model: DiagramModel, view: CompiledView, boxes: ReadonlyMap<string, BoxText>, way: GitWay): TextLayout;
@@ -0,0 +1,274 @@
1
+ import { gitGraph, isGitKind, latestCommit, mergedAway, placeColumns, } from '@diagc/core/internal';
2
+ import { cellWidth, printable } from './width.js';
3
+ // A git graph in cells, one of two ways. Down the screen, the default, as a terminal
4
+ // reads a history: each lane a column in declaration order, each commit a dot on its
5
+ // column's row with its tag beside it, and the lanes' names along the foot. Across the
6
+ // screen, as the page draws it (the renderer's git-layout.ts): each lane a row, each
7
+ // commit a dot at its column with its tag on the row above, and the lane's name at the
8
+ // right end, where the page has its label box. Across, columns come from core's
9
+ // placeColumns, as the page's do, so a tag clears its neighbours and a stage frame is
10
+ // wide enough for its title; down, the rows do, so a frame has room for its border.
11
+ // Stages frame their columns across every lane. Commits on no lane get a track of their
12
+ // own after the lanes, and the rest a row of boxes under the picture.
13
+ /** The graph's spacing across the screen, in cells. */
14
+ export const GIT = {
15
+ /** neighbouring columns' distance when nothing needs more: a dot and three cells of line */
16
+ PITCH: 4,
17
+ /** the least room between two tags on one lane: two cells, as one would read the
18
+ * two as one phrase */
19
+ TAG_GAP: 2,
20
+ /** rows from one lane's line to the next: the next lane's tags and a blank row between */
21
+ LANE: 3,
22
+ /** what a stage's frame needs besides its title: a corner and a cell of border
23
+ * before it, a space either side, a cell of border and a corner after */
24
+ FRAME_ROOM: 6,
25
+ /** from the last column's reach to the lanes' names: the tail's run and a space */
26
+ NAME_GAP: 2,
27
+ /** blank rows between the lanes and the boxes under them */
28
+ STRAY_ROW: 1,
29
+ /** between two boxes in that row */
30
+ STRAY_GAP: 4,
31
+ };
32
+ /** The graph's spacing down the screen, in cells. */
33
+ export const GIT_DOWN = {
34
+ /** rows from one column's dots to the next when nothing needs more: a dot and the row
35
+ * a branch turns off on, or a merge turns in */
36
+ PITCH: 2,
37
+ /** rows a stage's frame reaches past its first and last dots: the row a line turns
38
+ * on, then its border, so no line turns along a border */
39
+ FRAME: 2,
40
+ /** the least room between a lane's widest tag, or its name, and the next lane */
41
+ LANE_GAP: 2,
42
+ /** columns left of the first lane where stages are framed: the frame's side and a space */
43
+ MARGIN: 2,
44
+ /** what a stage's frame needs past the last lane besides its title: a cell of border
45
+ * before it, a space either side, a cell of border and a corner after */
46
+ TITLE_ROOM: 5,
47
+ /** rows from the last dot's reach to the lanes' names: the tail's run */
48
+ NAME_GAP: 2,
49
+ };
50
+ /** A name on one line, as a tag, a lane's name and a stage's title are drawn. */
51
+ const oneLine = (node) => printable(node.name.split(/\r?\n/).join(' '));
52
+ /** Every node the view shows, parents before children. */
53
+ function shownNodes(view) {
54
+ const out = [];
55
+ const walk = (vn) => {
56
+ out.push(vn);
57
+ vn.children.forEach(walk);
58
+ };
59
+ view.roots.forEach(walk);
60
+ return out;
61
+ }
62
+ /** The tracks: each lane the view shows, then the strays' when there are any. */
63
+ function tracksOf(graph, shown) {
64
+ const tracks = graph.lanes
65
+ .filter((lane) => shown.has(lane.id))
66
+ .map((lane) => ({ id: lane.id, commits: lane.commits.filter((c) => shown.has(c.id)) }));
67
+ const strays = graph.strays.filter((c) => shown.has(c.id));
68
+ return strays.length > 0 ? [...tracks, { id: '', commits: strays }] : tracks;
69
+ }
70
+ const columnOf = (parts, commit) => parts.graph.columns.get(commit.id) ?? 0;
71
+ /** The dot a lane's tail runs on from: its last commit's, unless that merged away. */
72
+ function tailFrom(parts, lane, dots) {
73
+ const last = latestCommit(parts.graph, lane);
74
+ return last === undefined || mergedAway(parts.model, last.id) ? undefined : dots.get(last.id);
75
+ }
76
+ /** A git link's path across the screen: down from where a branch leaves and along its
77
+ * lane, along and up half a pitch before where a merge lands (the page's gitRoute). */
78
+ function acrossRoute(s, t) {
79
+ if (t.y === s.y)
80
+ return [s, t];
81
+ if (t.y > s.y || t.x <= s.x)
82
+ return [s, { x: s.x, y: t.y }, t];
83
+ const x = t.x - GIT.PITCH / 2;
84
+ return [s, { x, y: s.y }, { x, y: t.y }, t];
85
+ }
86
+ /** A git link's path down the screen, which turns the way acrossRoute does, by the
87
+ * lanes' order: across on the row under where it leaves, as its tag is beside it, and
88
+ * down its lane, or down its lane and across on the row over where it lands. */
89
+ function downRoute(s, t) {
90
+ if (t.x === s.x)
91
+ return [s, t];
92
+ const y = t.x > s.x || t.y <= s.y ? s.y + 1 : t.y - 1;
93
+ return [s, { x: s.x, y }, { x: t.x, y }, t];
94
+ }
95
+ /** What is not on a track, boxed in a row from the left at `y`. */
96
+ function placeLoose(nodes, boxes, y, placed) {
97
+ let x = 0;
98
+ for (const vn of nodes) {
99
+ if (placed.has(vn.id))
100
+ continue;
101
+ const text = boxes.get(vn.id);
102
+ const [width, height] = [text?.width ?? 5, text?.height ?? 3];
103
+ placed.set(vn.id, { x, y, width, height });
104
+ x += width + GIT.STRAY_GAP;
105
+ }
106
+ }
107
+ /** The picture's size: every box and every route. */
108
+ function measure(boxes, routes) {
109
+ let [width, height] = [0, 0];
110
+ for (const b of boxes.values())
111
+ [width, height] = [Math.max(width, b.x + b.width), Math.max(height, b.y + b.height)];
112
+ for (const r of routes.values()) {
113
+ for (const p of r.points)
114
+ [width, height] = [Math.max(width, p.x + 1), Math.max(height, p.y + 1)];
115
+ }
116
+ return [width, height];
117
+ }
118
+ /** The layout, its links routed from dot to dot and what is on no track boxed. */
119
+ function finished(parts, { git, placed, route, looseY }) {
120
+ const routes = new Map();
121
+ for (const e of parts.view.edges) {
122
+ const [s, t] = [git.dots.get(e.from), git.dots.get(e.to)];
123
+ if (isGitKind(e.kind) && s !== undefined && t !== undefined)
124
+ routes.set(e.id, { points: route(s, t) });
125
+ }
126
+ placeLoose(parts.nodes, parts.boxes, looseY, placed);
127
+ const [width, height] = measure(placed, routes);
128
+ return { width, height, boxes: placed, routes, git };
129
+ }
130
+ /** The graph across the screen: a row a track, the names at the right end. */
131
+ function across(parts) {
132
+ const { graph, tracks, stages, maxCol } = parts;
133
+ const tags = [];
134
+ for (const track of tracks) {
135
+ for (const c of track.commits) {
136
+ const width = cellWidth(oneLine(c));
137
+ if (width > 0)
138
+ tags.push({ col: columnOf(parts, c), row: track.id, left: Math.floor(width / 2), right: Math.ceil(width / 2) });
139
+ }
140
+ }
141
+ // a frame's width rounded up to even, so that it grows by whole cells either side
142
+ const frames = stages.map((s) => {
143
+ const width = cellWidth(oneLine(s.node)) + GIT.FRAME_ROOM;
144
+ return { id: s.id, fromCol: s.fromCol, toCol: s.toCol, width: width + (width % 2) };
145
+ });
146
+ const half = GIT.PITCH / 2;
147
+ const cols = placeColumns({ maxCol, first: half, pitch: GIT.PITCH, gap: GIT.TAG_GAP, tags, frames });
148
+ const placed = new Map();
149
+ const git = {
150
+ down: false,
151
+ dots: new Map(),
152
+ tags: new Map(),
153
+ names: new Map(),
154
+ tails: [],
155
+ stages: new Map(),
156
+ };
157
+ const top = stages.length > 0 ? 1 : 0;
158
+ const lineOf = (i) => top + i * GIT.LANE + 1;
159
+ const end = maxCol < 0 ? 0 : cols.x[maxCol] + half + cols.growRight[maxCol];
160
+ const nameX = end + GIT.NAME_GAP;
161
+ tracks.forEach((track, i) => {
162
+ const y = lineOf(i);
163
+ for (const c of track.commits) {
164
+ const dot = { x: cols.x[columnOf(parts, c)], y };
165
+ git.dots.set(c.id, dot);
166
+ const text = oneLine(c);
167
+ const width = cellWidth(text);
168
+ if (width === 0) {
169
+ placed.set(c.id, { ...dot, width: 1, height: 1 });
170
+ continue;
171
+ }
172
+ const tag = { x: dot.x - Math.floor(width / 2), y: y - 1, text };
173
+ git.tags.set(c.id, tag);
174
+ const x = Math.min(tag.x, dot.x);
175
+ placed.set(c.id, { x, y: tag.y, width: Math.max(tag.x + width, dot.x + 1) - x, height: 2 });
176
+ }
177
+ const lane = graph.lanes.find((l) => l.id === track.id);
178
+ if (lane === undefined)
179
+ return;
180
+ const name = { x: nameX, y, text: oneLine(lane.node) };
181
+ git.names.set(lane.id, name);
182
+ placed.set(lane.id, { x: name.x, y, width: Math.max(1, cellWidth(name.text)), height: 1 });
183
+ const from = tailFrom(parts, lane.id, git.dots);
184
+ if (from !== undefined && from.x + 1 <= nameX - 2) {
185
+ git.tails.push({ lane: lane.id, from: { x: from.x + 1, y }, to: { x: nameX - 2, y } });
186
+ }
187
+ });
188
+ const bottom = tracks.length === 0 ? top : lineOf(tracks.length - 1) + 1;
189
+ for (const s of stages) {
190
+ const grow = cols.growth.get(s.id);
191
+ const x = cols.x[s.fromCol] - half - grow;
192
+ placed.set(s.id, { x, y: 0, width: cols.x[s.toCol] + half + grow - x, height: bottom + 1 });
193
+ git.stages.set(s.id, { x: x + 2, y: 0, text: oneLine(s.node) });
194
+ }
195
+ return finished(parts, { git, placed, route: acrossRoute, looseY: bottom + 1 + GIT.STRAY_ROW });
196
+ }
197
+ /** The graph down the screen: a column a track, each as wide as its widest tag or its
198
+ * name, the names along the foot. */
199
+ function down(parts) {
200
+ const { graph, tracks, stages, maxCol } = parts;
201
+ const { PITCH, FRAME, LANE_GAP, NAME_GAP } = GIT_DOWN;
202
+ // A tag is on its dot's row, so it takes no room between rows; a frame takes its reach.
203
+ const frames = stages.map((s) => ({ ...s, width: (s.toCol - s.fromCol + 1) * PITCH + 2 * FRAME }));
204
+ const rows = placeColumns({ maxCol, first: 0, pitch: PITCH, gap: 0, tags: [], frames });
205
+ const nameY = (maxCol < 0 ? 0 : rows.x[maxCol] + rows.growRight[maxCol]) + NAME_GAP;
206
+ const placed = new Map();
207
+ const git = {
208
+ down: true,
209
+ dots: new Map(),
210
+ tags: new Map(),
211
+ names: new Map(),
212
+ tails: [],
213
+ stages: new Map(),
214
+ };
215
+ let x = stages.length > 0 ? GIT_DOWN.MARGIN : 0;
216
+ let [last, reachEnd] = [x, x];
217
+ for (const track of tracks) {
218
+ let reach = 1;
219
+ for (const c of track.commits) {
220
+ const dot = { x, y: rows.x[columnOf(parts, c)] };
221
+ git.dots.set(c.id, dot);
222
+ const text = oneLine(c);
223
+ const width = cellWidth(text);
224
+ if (width > 0)
225
+ git.tags.set(c.id, { x: x + 2, y: dot.y, text });
226
+ placed.set(c.id, { ...dot, width: width > 0 ? 2 + width : 1, height: 1 });
227
+ reach = Math.max(reach, width > 0 ? 2 + width : 1);
228
+ }
229
+ const lane = graph.lanes.find((l) => l.id === track.id);
230
+ if (lane !== undefined) {
231
+ const name = { x, y: nameY, text: oneLine(lane.node) };
232
+ git.names.set(lane.id, name);
233
+ const width = Math.max(1, cellWidth(name.text));
234
+ placed.set(lane.id, { x, y: nameY, width, height: 1 });
235
+ reach = Math.max(reach, width);
236
+ const from = tailFrom(parts, lane.id, git.dots);
237
+ if (from !== undefined)
238
+ git.tails.push({ lane: lane.id, from: { x, y: from.y + 1 }, to: { x, y: nameY - 1 } });
239
+ }
240
+ [last, reachEnd] = [x, x + reach];
241
+ x += reach + LANE_GAP;
242
+ }
243
+ // Every frame is as wide as the picture, and as the widest title written past the
244
+ // last track, where no lane's line crosses its border.
245
+ let right = reachEnd + 1;
246
+ for (const s of stages)
247
+ right = Math.max(right, last + cellWidth(oneLine(s.node)) + GIT_DOWN.TITLE_ROOM);
248
+ for (const s of stages) {
249
+ const y = rows.x[s.fromCol] - FRAME;
250
+ placed.set(s.id, { x: 0, y, width: right + 1, height: rows.x[s.toCol] + FRAME - y + 1 });
251
+ git.stages.set(s.id, { x: last + 2, y, text: oneLine(s.node) });
252
+ }
253
+ return finished(parts, { git, placed, route: downRoute, looseY: nameY + 1 + GIT.STRAY_ROW });
254
+ }
255
+ /**
256
+ * The graph in cells, for a git-graph plane: every node the view shows gets a box — a
257
+ * commit its dot and tag, a lane its name, a stage its frame — and every drawn git
258
+ * link between two dots a route from one to the other.
259
+ */
260
+ export function gitCells(model, view, boxes, way) {
261
+ const nodes = shownNodes(view);
262
+ const shown = new Set(nodes.map((n) => n.id));
263
+ const graph = gitGraph(model, way.plane);
264
+ const tracks = tracksOf(graph, shown);
265
+ const stages = graph.stages.filter((s) => shown.has(s.id));
266
+ let maxCol = -1;
267
+ for (const track of tracks)
268
+ for (const c of track.commits)
269
+ maxCol = Math.max(maxCol, graph.columns.get(c.id) ?? 0);
270
+ for (const s of stages)
271
+ maxCol = Math.max(maxCol, s.toCol);
272
+ const parts = { model, graph, view, boxes, nodes, tracks, stages, maxCol };
273
+ return way.across === true ? across(parts) : down(parts);
274
+ }
@@ -32,12 +32,41 @@ export interface FishDrawing {
32
32
  /** the categories among `words`: their bones' labels */
33
33
  labels: Set<string>;
34
34
  }
35
+ /** What a git graph is drawn with besides boxes and routes (git.ts). A commit's box
36
+ * is the cells of its dot and tag, a lane's its name's, a stage's its frame; a route
37
+ * runs from dot to dot, both included. */
38
+ export interface GitDrawing {
39
+ /** time runs down the screen and each lane is a column; false, across and a row */
40
+ down: boolean;
41
+ /** commit id → its dot */
42
+ dots: Map<string, CellPoint>;
43
+ /** commit id → its tag, where it starts; an untagged commit has none */
44
+ tags: Map<string, CellPoint & {
45
+ text: string;
46
+ }>;
47
+ /** lane id → its name, where it starts, at its line's end */
48
+ names: Map<string, CellPoint & {
49
+ text: string;
50
+ }>;
51
+ /** each lane's line from its last commit on to its name, dashed; none where that
52
+ * commit merged away */
53
+ tails: {
54
+ lane: string;
55
+ from: CellPoint;
56
+ to: CellPoint;
57
+ }[];
58
+ /** stage id → its title, where it starts in its frame's top border */
59
+ stages: Map<string, CellPoint & {
60
+ text: string;
61
+ }>;
62
+ }
35
63
  export interface TextLayout {
36
64
  width: number;
37
65
  height: number;
38
66
  boxes: Map<string, BoxGeometry>;
39
67
  routes: Map<string, Route>;
40
68
  fish?: FishDrawing;
69
+ git?: GitDrawing;
41
70
  }
42
71
  /**
43
72
  * elk `layered`, in character cells: a node is as big as its text, and the gaps
@@ -208,6 +208,97 @@ function paintFish(p, fish, edges) {
208
208
  for (const [id, g] of boxes)
209
209
  drawBox(p, id, g);
210
210
  }
211
+ /** Text or a line in a node's own colour (`ownCss`), where it has one. */
212
+ const ownInk = (p, id) => {
213
+ const fg = rgb(ownCss(p, id));
214
+ return fg !== undefined ? { fg } : {};
215
+ };
216
+ /** Every cell a link or a lane's tail runs through, as `x,y`. */
217
+ function linedCells(p, git, edges) {
218
+ const lined = new Set();
219
+ const run = (a, b) => {
220
+ const [dx, dy] = [Math.sign(b.x - a.x), Math.sign(b.y - a.y)];
221
+ for (let i = 0; i <= Math.abs(b.x - a.x) + Math.abs(b.y - a.y); i++)
222
+ lined.add(`${a.x + i * dx},${a.y + i * dy}`);
223
+ };
224
+ for (const edge of edges) {
225
+ const { points } = p.scene.layout.routes.get(edge.id);
226
+ for (let i = 1; i < points.length; i++)
227
+ run(points[i - 1], points[i]);
228
+ }
229
+ for (const tail of git.tails)
230
+ run(tail.from, tail.to);
231
+ return lined;
232
+ }
233
+ /** A stage's frame, dim and dashed, or heavy when it is selected. It is stroked cell by
234
+ * cell and gives way where a line runs through it, so a lane's line runs in front of
235
+ * the frame rather than meeting it in a junction. */
236
+ function drawStageFrame(p, id, lined) {
237
+ const g = p.scene.layout.boxes.get(id);
238
+ const [weight, ink] = id === p.selected ? ['heavy', ownInk(p, id)] : ['dashed', { ...ownInk(p, id), dim: true }];
239
+ const [right, bottom] = [g.x + g.width - 1, g.y + g.height - 1];
240
+ const stroke = (x, y, mask) => {
241
+ if (!lined.has(`${x},${y}`))
242
+ p.canvas.stroke({ x, y }, mask, weight, ink);
243
+ };
244
+ for (let x = g.x; x <= right; x++) {
245
+ const mask = (x > g.x ? LEFT : 0) | (x < right ? RIGHT : 0);
246
+ for (const y of [g.y, bottom])
247
+ stroke(x, y, mask);
248
+ }
249
+ for (let y = g.y; y <= bottom; y++) {
250
+ const mask = (y > g.y ? UP : 0) | (y < bottom ? DOWN : 0);
251
+ for (const x of [g.x, right])
252
+ stroke(x, y, mask);
253
+ }
254
+ }
255
+ /** A lane's tail, cell by cell down the screen, where a one-cell segment would read as
256
+ * one across it. */
257
+ function drawTail(p, git, tail) {
258
+ const ink = ownInk(p, tail.lane);
259
+ if (!git.down)
260
+ return p.canvas.segment(tail.from, tail.to, 'dashed', ink);
261
+ for (let y = tail.from.y; y <= tail.to.y; y++)
262
+ p.canvas.stroke({ x: tail.from.x, y }, UP | DOWN, 'dashed', ink);
263
+ }
264
+ /**
265
+ * A git graph as its lanes (git.ts): the stages' frames first, then the links
266
+ * and the lanes' tails, then each commit's dot over the lines that meet it, its tag
267
+ * bold beside it, the lanes' names, the stages' titles in their frames' top borders,
268
+ * and last the boxes of what is on no lane. A dot, tag, name or title is selected in
269
+ * reverse, a stage's frame heavy.
270
+ */
271
+ function paintGit(p, git, edges) {
272
+ const { layout } = p.scene;
273
+ const own = (id) => ownInk(p, id);
274
+ const chosen = (id, style) => (id === p.selected ? { ...style, inverse: true } : style);
275
+ const boxes = [...layout.boxes].filter(([id]) => !git.dots.has(id) && !git.names.has(id) && !git.stages.has(id));
276
+ if (p.dos !== undefined)
277
+ castShadows(p.canvas, boxes.map(([, g]) => g));
278
+ const lined = linedCells(p, git, edges);
279
+ for (const id of git.stages.keys())
280
+ drawStageFrame(p, id, lined);
281
+ for (const edge of edges) {
282
+ const { points } = layout.routes.get(edge.id);
283
+ const ink = edgeInk(p, edge);
284
+ for (let i = 1; i < points.length; i++)
285
+ p.canvas.segment(points[i - 1], points[i], ink.weight, ink.style);
286
+ }
287
+ for (const tail of git.tails)
288
+ drawTail(p, git, tail);
289
+ for (const [id, dot] of git.dots)
290
+ p.canvas.put(dot, p.glyphs.start, chosen(id, own(id)));
291
+ for (const [id, tag] of git.tags)
292
+ p.canvas.text(tag, tag.text, chosen(id, { ...own(id), bold: true }));
293
+ for (const [id, name] of git.names)
294
+ p.canvas.text(name, name.text, chosen(id, { ...own(id), bold: true }));
295
+ for (const [id, title] of git.stages) {
296
+ const g = layout.boxes.get(id);
297
+ p.canvas.text(title, ` ${title.text} `, chosen(id, { ...own(id), bold: true }), g.x + g.width - 2);
298
+ }
299
+ for (const [id, g] of boxes)
300
+ drawBox(p, id, g);
301
+ }
211
302
  /** An edge's label: in its line's ink, or in DOS yellow. */
212
303
  const labelStyle = (p, edge) => {
213
304
  const { style } = edgeInk(p, edge);
@@ -236,6 +327,10 @@ export function paint(scene, options) {
236
327
  paintFish(p, layout.fish, edges);
237
328
  return p.canvas;
238
329
  }
330
+ if (layout.git !== undefined) {
331
+ paintGit(p, layout.git, edges);
332
+ return p.canvas;
333
+ }
239
334
  if (p.dos !== undefined)
240
335
  castShadows(p.canvas, drawn.filter((d) => !d.frame).map((d) => d.g));
241
336
  for (const { id, g, frame } of drawn)
@@ -20,6 +20,9 @@ export interface SceneInput {
20
20
  /** the style it is drawn in; absent = the terminal's own. DOS's boxes cast shadows,
21
21
  * which the picture leaves room for at its right and its foot. */
22
22
  style?: TextStyle;
23
+ /** a git graph across the screen, its lanes rows, as the page draws it; absent = down
24
+ * the screen, its lanes columns, as a terminal reads a history */
25
+ horizontal?: boolean;
23
26
  }
24
27
  /** A laid-out view: everything painting and the TUI's navigation read. */
25
28
  export interface Scene {