pi-odd-worktree 2.0.0 → 3.0.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/README.md +54 -311
- package/extensions/odd-worktree-tools.ts +4149 -2447
- package/lib/bash-activity.js +1164 -0
- package/lib/bash-design.js +684 -0
- package/lib/bash-destructive.js +559 -0
- package/lib/bash-frame.js +205 -0
- package/lib/card-content.js +23 -0
- package/lib/card-style.js +175 -0
- package/lib/checkout-facts.js +30 -70
- package/lib/codegraph-card-preview.js +628 -0
- package/lib/codegraph-card.js +515 -0
- package/lib/codegraph-compaction.js +76 -0
- package/lib/codegraph-daemon.js +246 -0
- package/lib/codegraph-edit-sync.js +182 -0
- package/lib/codegraph-guard-target.js +337 -0
- package/lib/codegraph-guard.js +211 -0
- package/lib/codegraph-index.js +1020 -0
- package/lib/codegraph-lock.js +607 -0
- package/lib/codegraph-mcp-config.js +134 -0
- package/lib/codegraph-mcp.js +123 -0
- package/lib/codegraph-paths.js +69 -0
- package/lib/codegraph-prompt/explore.md +11 -0
- package/lib/codegraph-prompt/fix.md +6 -0
- package/lib/codegraph-prompt/generic.md +8 -0
- package/lib/codegraph-prompt/judge.md +9 -0
- package/lib/codegraph-prompt/orchestrator.md +17 -0
- package/lib/codegraph-prompt/verify.md +10 -0
- package/lib/codegraph-prompt/worker.md +7 -0
- package/lib/codegraph-role.js +55 -0
- package/lib/codegraph-rules.js +146 -0
- package/lib/codegraph-session.js +303 -383
- package/lib/codegraph-warmup.js +139 -0
- package/lib/file-card-markdown.js +141 -0
- package/lib/file-card-resolver.js +245 -0
- package/lib/file-card.js +659 -0
- package/lib/files-index.js +387 -0
- package/lib/files-insert.js +81 -0
- package/lib/files-orders.js +326 -0
- package/lib/files-picker-view.js +1416 -0
- package/lib/files-preview.js +338 -0
- package/lib/files-scope-detail.js +171 -0
- package/lib/files-scope-view.js +507 -0
- package/lib/gh-issues.js +308 -14
- package/lib/issue-preview.js +26 -7
- package/lib/issues-bash-mutation.js +206 -0
- package/lib/issues-cache-store.js +261 -0
- package/lib/issues-section.js +710 -0
- package/lib/issues-view.js +649 -52
- package/lib/odd-prompt/odd-worktrees.md +21 -0
- package/lib/odd-worktrees-prompt.js +10 -28
- package/lib/related-issues-cache.js +210 -0
- package/lib/shell-card.js +825 -0
- package/lib/skill-odd-card.js +183 -0
- package/lib/tui-paint.js +4 -5
- package/lib/warp-create-panel.js +538 -250
- package/lib/warp-create-picker.js +56 -9
- package/lib/warp-delete-confirm.js +19 -28
- package/lib/warp-selector-view.js +160 -82
- package/lib/which-command.js +90 -0
- package/lib/worktree-card.js +267 -897
- package/lib/worktree-cards.js +383 -0
- package/lib/worktree-create.js +287 -0
- package/lib/worktree-git.js +3 -3
- package/lib/worktree-guard.js +548 -0
- package/lib/worktree-hooks.js +421 -711
- package/lib/worktree-list.js +120 -0
- package/lib/worktree-registry.js +162 -879
- package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
- package/lib/worktree-setup-runner.js +376 -0
- package/lib/worktree-setup.js +140 -0
- package/lib/worktree-warp-busy.js +462 -85
- package/lib/worktree-warp-sessions.js +1 -2
- package/package.json +2 -2
- package/tools/bash-activity.test.js +1419 -0
- package/tools/bash-design.test.js +1091 -0
- package/tools/bash-destructive.test.js +139 -0
- package/tools/card-content.test.js +44 -0
- package/tools/card-style.test.js +298 -0
- package/tools/codegraph-card-preview.test.js +254 -0
- package/tools/codegraph-card.test.js +1080 -0
- package/tools/codegraph-compaction.test.js +378 -0
- package/tools/codegraph-daemon.test.js +248 -0
- package/tools/codegraph-edit-sync.test.js +295 -0
- package/tools/codegraph-guard-target.test.js +174 -0
- package/tools/codegraph-guard.test.js +238 -0
- package/tools/codegraph-index.test.js +1948 -0
- package/tools/codegraph-lock.test.js +932 -0
- package/tools/codegraph-mcp-config.test.js +181 -0
- package/tools/codegraph-mcp.test.js +194 -0
- package/tools/codegraph-paths.test.js +44 -0
- package/tools/codegraph-role.test.js +30 -0
- package/tools/codegraph-rules.test.js +250 -0
- package/tools/codegraph-session.test.js +660 -391
- package/tools/codegraph-warmup.test.js +316 -0
- package/tools/commands.test.js +510 -31
- package/tools/file-card-markdown.test.js +188 -0
- package/tools/file-card.test.js +1289 -0
- package/tools/files-bridge.test.js +575 -0
- package/tools/files-command.test.js +610 -0
- package/tools/files-index.test.js +473 -0
- package/tools/files-insert.test.js +139 -0
- package/tools/files-orders.test.js +597 -0
- package/tools/files-picker-view.test.js +1871 -0
- package/tools/files-preview.test.js +452 -0
- package/tools/files-scope-detail.test.js +264 -0
- package/tools/files-scope-view.test.js +614 -0
- package/tools/fixtures/card-codegraph-outputs.js +110 -0
- package/tools/fixtures/codegraph-cli-stub.js +16 -0
- package/tools/fixtures/codegraph-init-process.js +28 -0
- package/tools/fixtures/codegraph-lock-worker.js +117 -0
- package/tools/fixtures/daemon-fake.js +98 -0
- package/tools/fixtures/daemon-mcp-server.js +39 -0
- package/tools/fixtures/daemon-warmup-worker.js +12 -0
- package/tools/fixtures/load-extension.js +136 -3
- package/tools/gh-issues-feedback.test.js +31 -7
- package/tools/gh-issues.test.js +391 -1
- package/tools/issue-preview.test.js +44 -7
- package/tools/issues-bash-mutation.test.js +101 -0
- package/tools/issues-cache-store.test.js +222 -0
- package/tools/issues-command.test.js +1512 -66
- package/tools/issues-open-guard.test.js +1096 -0
- package/tools/issues-section.test.js +4195 -0
- package/tools/issues-seed-query-regression.test.js +67 -0
- package/tools/issues-view.test.js +1489 -60
- package/tools/odd-worktrees-prompt.test.js +98 -65
- package/tools/package-surface.test.js +431 -36
- package/tools/related-issues-cache.test.js +324 -0
- package/tools/shell-card.test.js +834 -0
- package/tools/skill-odd-card.test.js +200 -0
- package/tools/t5-session-identity.test.js +44 -22
- package/tools/warp-busy-guard.test.js +2569 -308
- package/tools/warp-create-panel.test.js +499 -74
- package/tools/warp-create-picker.test.js +101 -5
- package/tools/warp-delete-confirm.test.js +49 -15
- package/tools/warp-empty-session.test.js +0 -1
- package/tools/warp-fallback-regression.test.js +533 -0
- package/tools/warp-selector-view.test.js +460 -196
- package/tools/which-command.test.js +126 -0
- package/tools/worktree-card.test.js +551 -0
- package/tools/worktree-cards.test.js +845 -0
- package/tools/worktree-changes.test.js +4 -5
- package/tools/worktree-create.test.js +489 -0
- package/tools/worktree-guard.test.js +823 -0
- package/tools/worktree-hooks.test.js +905 -0
- package/tools/worktree-list.test.js +170 -0
- package/tools/worktree-registry.test.js +89 -230
- package/tools/worktree-remove-async.test.js +318 -0
- package/tools/worktree-setup-runner.test.js +276 -0
- package/tools/worktree-setup.test.js +140 -0
- package/tools/worktree-tools.test.js +477 -93
- package/lib/codegraph-commands.js +0 -165
- package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
- package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
- package/lib/worktree-model-result.js +0 -291
- package/tools/codegraph-commands.test.js +0 -371
- package/tools/herdr-free-create.test.js +0 -161
- package/tools/worktree-close-herdr-async.test.js +0 -204
- package/tools/worktree-odd-migration.test.js +0 -224
- package/tools/worktree-registry-cache.test.js +0 -163
- package/tools/worktree-registry-odd.test.js +0 -231
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The bash activity DESIGN: the one way this package changes how a tool card is drawn.
|
|
3
|
+
//
|
|
4
|
+
// It is a `registerToolDesign` registration on gentle-shell-bridge's published API, not a
|
|
5
|
+
// renderer. The bridge owns Pi's one `registerToolRenderer` resolver, builds the HOST's own
|
|
6
|
+
// card (Gentle Shell's, in whatever style and content level is chosen) and hands this module
|
|
7
|
+
// the rows that card drew:
|
|
8
|
+
//
|
|
9
|
+
// apply(lines, { width, tool, theme, part, render }) -> lines
|
|
10
|
+
//
|
|
11
|
+
// So the frame is the host's by construction, the choice is the bridge's own `Card tools` line in
|
|
12
|
+
// `/gentle:customize` → `Cards` (`default` | `activity`), it is read on every paint so cards
|
|
13
|
+
// already on screen repaint when it changes, and extension load order does not matter.
|
|
14
|
+
//
|
|
15
|
+
// ONE line covers six tools. `bash`'s heading names what its command does; the three read-only
|
|
16
|
+
// shell tools the line covers (`grep`, `find`, `ls`) only ever read, so their heading is always
|
|
17
|
+
// `exploring` whatever their arguments are. `read` and `write` are declared for a different reason:
|
|
18
|
+
// the file card is painted only while the bridge's line resolves to this design for the tool being
|
|
19
|
+
// drawn, and the bridge resolves coverage from this list, so a read and a write have to be named here
|
|
20
|
+
// for the file card to be asked about them. The design itself paints nothing for either (see
|
|
21
|
+
// `READ_TOOL` and `WRITE_TOOL`): `exploring` names what a SHELL COMMAND does, and a read is not one,
|
|
22
|
+
// so a read card comes back exactly as the host drew it.
|
|
23
|
+
// A bridge older than the line (0.6.0) takes exactly the single `bash` design it always took, so
|
|
24
|
+
// nothing regresses and no registration is refused.
|
|
25
|
+
//
|
|
26
|
+
// What the design changes is exactly one thing, on the card's heading row: bash's `$` mark, or a
|
|
27
|
+
// read-only shell tool's `<glyph> <tool>`, becomes the activity — `⌕ exploring`, `✓ checking`, … —
|
|
28
|
+
// using the one classifier (`lib/bash-activity.js`). The columns the longer mark adds are taken back
|
|
29
|
+
// from the row's own fill (and, when there is none left, its tail), so the row keeps the width the
|
|
30
|
+
// host made it. Everything else comes back byte for byte.
|
|
31
|
+
//
|
|
32
|
+
// The rule is the classifier's: never guess. For bash the heading is the evidence when the bridge
|
|
33
|
+
// hands no arguments, so it names a call only when the heading provably shows the WHOLE command:
|
|
34
|
+
// no `…` abbreviation, no `… +N lines` fold, no continuation row, and nothing after the command but
|
|
35
|
+
// the host's own fill and hint. A read-only tool's word is the same whatever it reads, so its
|
|
36
|
+
// arguments are only the proof that the heading is not the whole evidence. Anything else, and every
|
|
37
|
+
// command the classifier cannot name, is drawn exactly as the host drew it. Pure: no Pi, no theme,
|
|
38
|
+
// no colour, no filesystem — the heading keeps the host's own paint.
|
|
39
|
+
|
|
40
|
+
const { SHELL_ACTIVITY_IDENTITY, inferBashActivity } = require('./bash-activity.js');
|
|
41
|
+
// S6: the file card's own identity rule, reused and never copied — a path it claims is
|
|
42
|
+
// the file card's row, so this design must leave it alone.
|
|
43
|
+
const { fileCardIdentity, FILE_CARD_TOOLS } = require('./skill-odd-card.js');
|
|
44
|
+
const { cardElapsed, charColumns, elapsedRow, paintFrame, rowColumns, sgrBefore, textColumns, visibleChars } = require('./bash-frame.js');
|
|
45
|
+
|
|
46
|
+
const TOOL_NAME = 'bash';
|
|
47
|
+
// The bridge line every covered tool is switched by. The bridge names the row `Card <line>`.
|
|
48
|
+
const TOOLS_LINE = 'tools';
|
|
49
|
+
// The bridge release that can take one line over several tools; earlier ones refuse a grouped
|
|
50
|
+
// registration and record the refusal, so the shape is chosen BEFORE anything is registered.
|
|
51
|
+
const TOOLS_LINE_VERSION = [0, 6, 0];
|
|
52
|
+
// The throwaway design a release whose version cannot be read is probed with: an id nothing selects
|
|
53
|
+
// and a line no plugin of the user's would ever use. It is registered and immediately disposed, so
|
|
54
|
+
// it can never take a value, appear on a menu, or paint a card.
|
|
55
|
+
const PROBE_DESIGN_ID = 'odd-worktree.capability-probe';
|
|
56
|
+
const PROBE_LINE = 'odd-worktree-probe';
|
|
57
|
+
const PROBE_TOOL = 'odd-worktree-probe';
|
|
58
|
+
// Stable across loads and unique per line: a reload replaces this design, never stacks one.
|
|
59
|
+
const BASH_DESIGN_ID = 'odd-worktree.bash-activity';
|
|
60
|
+
// The value the user reads on the bridge's line, next to the reserved `default`.
|
|
61
|
+
const BASH_DESIGN_LABEL = 'activity';
|
|
62
|
+
const BASH_DESIGN_PLUGIN_NAME = 'Odd worktree';
|
|
63
|
+
const MARK = '$';
|
|
64
|
+
|
|
65
|
+
// The read-only SHELL tools whose own `<glyph> <tool>` heading the design heads `exploring`, each with
|
|
66
|
+
// the glyph its card is drawn with. They are shell commands that only ever read, so the activity is
|
|
67
|
+
// the same for all of them and their arguments change nothing. `read` is not one of them: it runs no
|
|
68
|
+
// command, so the word does not describe it (see `READ_TOOL`).
|
|
69
|
+
const READ_TOOL_GLYPHS = new Map([
|
|
70
|
+
['grep', '⌕'],
|
|
71
|
+
['find', '⌖'],
|
|
72
|
+
['ls', '☷'],
|
|
73
|
+
]);
|
|
74
|
+
const EXPLORING = 'exploring';
|
|
75
|
+
// S1: a read is declared for coverage alone, exactly like `write` below and for the same reason — the
|
|
76
|
+
// file card draws a read only while this line answers that this design draws the tool, and the bridge
|
|
77
|
+
// derives that answer from the line's own `tools`. So `read` is DECLARED here and painted by nothing:
|
|
78
|
+
// `exploring` names what a shell command does and a read is not one, so `READ_TOOL_GLYPHS` has no mark
|
|
79
|
+
// for it, `toolHeadingSpec` finds no spec and `computeActivity` hands the host's own rows back
|
|
80
|
+
// untouched.
|
|
81
|
+
const READ_TOOL = 'read';
|
|
82
|
+
// S1: the file card draws `write` as a document too, and it is painted only while the bridge's
|
|
83
|
+
// `Card tools` line answers that this design draws the tool being drawn — an answer the bridge
|
|
84
|
+
// derives from the line's own `tools`. So `write` is DECLARED here, for that question alone: the
|
|
85
|
+
// design paints nothing for it, because `computeActivity` knows only bash and the read-only shell
|
|
86
|
+
// marks and answers `undefined` for any other tool, which hands the host's rows back untouched.
|
|
87
|
+
const WRITE_TOOL = 'write';
|
|
88
|
+
|
|
89
|
+
// The bridge keeps ONE persisted value per tool, in its own store (`bridge-tool-designs` →
|
|
90
|
+
// `selections` → `{ "bash": <design id> }`, documented in the bridge's contract). It has no notion
|
|
91
|
+
// of a default design: an absent value reads `native`. So a first run would open on `native`
|
|
92
|
+
// unless this package offers `activity` once, itself.
|
|
93
|
+
const BRIDGE_SELECTION_STORE = 'bridge-tool-designs';
|
|
94
|
+
const BRIDGE_SELECTION_KEY = 'selections';
|
|
95
|
+
// This package's own marker that `activity` was already offered on this machine. Without it a
|
|
96
|
+
// user who later picks `native` would get `activity` back on the next load.
|
|
97
|
+
const OWN_STORE = 'odd-worktree';
|
|
98
|
+
const SEEDED_KEY = 'bashActivitySeeded';
|
|
99
|
+
|
|
100
|
+
// The outline frame's side and closing rules: a `$` on such a row is output, never a heading.
|
|
101
|
+
const FRAME_BODY_STARTS = new Set(['│', '╰']);
|
|
102
|
+
|
|
103
|
+
const isWordCharacter = (char) => /[\p{L}\p{N}]/u.test(char);
|
|
104
|
+
|
|
105
|
+
/** `ctrl+o to expand`-style hints the host prints after the fill. */
|
|
106
|
+
const HOST_HINT = /^[\s─╮╯│]*(?:(?:ctrl\+\w+|click|enter) to \w+[\s─╮╯│]*)?$/iu;
|
|
107
|
+
// Where the text a heading carries ends and the host's fill begins: two spaces, or a space and a rule.
|
|
108
|
+
const FILL_START = / {2,}| ─+/u;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The text a heading row shows IN FULL — a bash command, or a read-only tool's arguments — or
|
|
112
|
+
* `undefined` when it cannot be proved: an abbreviation, a fold, text after it that is not the
|
|
113
|
+
* host's fill or hint, or none at all.
|
|
114
|
+
*/
|
|
115
|
+
function drawnText(chars, textStart) {
|
|
116
|
+
const body = chars
|
|
117
|
+
.slice(textStart)
|
|
118
|
+
.map((entry) => entry.char)
|
|
119
|
+
.join('');
|
|
120
|
+
const fill = FILL_START.exec(body);
|
|
121
|
+
const rest = fill === null ? '' : body.slice(fill.index);
|
|
122
|
+
if (!HOST_HINT.test(rest)) return undefined;
|
|
123
|
+
const text = (fill === null ? body : body.slice(0, fill.index)).replace(/ \(timeout \d+s\)$/u, '').trim();
|
|
124
|
+
if (text === '' || text.endsWith('…') || /…\s*\+\d+ lines?$/u.test(text)) return undefined;
|
|
125
|
+
return text;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Pi repaints every retained card on every frame, and a long session keeps hundreds of them, so
|
|
129
|
+
// the work of a frame must be a lookup: the classification of one command, and the whole answer
|
|
130
|
+
// for one (rows, call state, theme) pair. Both caches are bounded and drop the oldest entry.
|
|
131
|
+
const MEMO_LIMIT = 512;
|
|
132
|
+
const classifyMemo = new Map();
|
|
133
|
+
const applyMemo = new Map();
|
|
134
|
+
|
|
135
|
+
function remember(memo, key, value) {
|
|
136
|
+
if (memo.size >= MEMO_LIMIT) memo.delete(memo.keys().next().value);
|
|
137
|
+
memo.set(key, value);
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** `inferBashActivity` with a bounded memo: one command is classified once, not once per frame. */
|
|
142
|
+
function classify(command) {
|
|
143
|
+
if (classifyMemo.has(command)) return classifyMemo.get(command);
|
|
144
|
+
return remember(classifyMemo, command, inferBashActivity(command));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// The roles a drawing may paint with. A theme can change under the SAME object (Pi hands renderers
|
|
148
|
+
// a live theme), so the cache key carries what each role paints right now.
|
|
149
|
+
const THEME_ROLES = ['mdLink', 'mdHeading', 'syntaxFunction', 'success', 'warning', 'error', 'bashMode', 'dim'];
|
|
150
|
+
|
|
151
|
+
function themeFingerprint(theme) {
|
|
152
|
+
if (theme === null || typeof theme !== 'object' || typeof theme.fg !== 'function') return '';
|
|
153
|
+
return THEME_ROLES.map((role) => {
|
|
154
|
+
try {
|
|
155
|
+
return String(theme.fg(role, '.'));
|
|
156
|
+
} catch {
|
|
157
|
+
return '!';
|
|
158
|
+
}
|
|
159
|
+
}).join('');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// A heading that gives up the tail of its text for the label keeps at least this much of it.
|
|
163
|
+
const MIN_COMMAND_COLUMNS = 8;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Where the text drawn after the mark ends and the host's fill begins, as indexes into `chars`:
|
|
167
|
+
* the text is `[bodyStart, end)` and the fill run is `[fillStart, fillStart + fillLength)`. The fill
|
|
168
|
+
* is two spaces, or the rules after one space, exactly as `drawnText` splits it.
|
|
169
|
+
*/
|
|
170
|
+
function commandExtent(chars, bodyStart) {
|
|
171
|
+
let end = chars.length;
|
|
172
|
+
for (let index = bodyStart; index < chars.length; index += 1) {
|
|
173
|
+
if (chars[index].char !== ' ') continue;
|
|
174
|
+
const next = chars[index + 1]?.char;
|
|
175
|
+
if (next === ' ' || next === '─') {
|
|
176
|
+
end = index;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (end >= chars.length) return { bodyStart, end, fillStart: end, fillLength: 0 };
|
|
181
|
+
const fillChar = chars[end + 1].char === '─' ? '─' : ' ';
|
|
182
|
+
const fillStart = fillChar === '─' ? end + 1 : end;
|
|
183
|
+
let fillLength = 0;
|
|
184
|
+
while (
|
|
185
|
+
fillStart + fillLength < chars.length &&
|
|
186
|
+
chars[fillStart + fillLength].char === fillChar &&
|
|
187
|
+
(fillLength === 0 || chars[fillStart + fillLength].raw === chars[fillStart + fillLength - 1].raw + 1)
|
|
188
|
+
) {
|
|
189
|
+
fillLength += 1;
|
|
190
|
+
}
|
|
191
|
+
return { bodyStart, end, fillStart, fillLength };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Where the host's trailing timeout badge begins, as an index into `chars`: the badge's leading
|
|
196
|
+
* space when the text `[bodyStart, end)` ends with a ` (timeout 120s)`-shaped one, else `end`. The
|
|
197
|
+
* host draws that badge itself, after the command and in its own colour, as fixed metadata about the
|
|
198
|
+
* call: a cut that stops here frees columns from the command only, because shortening the badge
|
|
199
|
+
* loses information the command text cannot give the reader back. The shape is the one `drawnText`
|
|
200
|
+
* strips from a heading.
|
|
201
|
+
*/
|
|
202
|
+
function timeoutBadgeStart(chars, bodyStart, end) {
|
|
203
|
+
const text = chars
|
|
204
|
+
.slice(bodyStart, end)
|
|
205
|
+
.map((entry) => entry.char)
|
|
206
|
+
.join('');
|
|
207
|
+
const badge = / \(timeout \d+s\)$/u.exec(text);
|
|
208
|
+
if (badge === null) return end;
|
|
209
|
+
return bodyStart + Array.from(text.slice(0, badge.index)).length;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* How to free `need` columns from the tail of the text `[bodyStart, end)`: text the host already
|
|
214
|
+
* abbreviated (`…`, or `… +N lines`) loses characters BEFORE that mark; text shown in full gets an
|
|
215
|
+
* ellipsis of its own, because the heading is a preview and the expanded call shows the rest.
|
|
216
|
+
* `undefined` when too little of the text would be left to be worth showing.
|
|
217
|
+
*/
|
|
218
|
+
function planCommandCut(chars, bodyStart, end, need) {
|
|
219
|
+
let stop = end;
|
|
220
|
+
let ownEllipsis = true;
|
|
221
|
+
for (let index = end - 1; index >= bodyStart; index -= 1) {
|
|
222
|
+
if (chars[index].char !== '…') continue;
|
|
223
|
+
const after = chars.slice(index + 1, end).map((entry) => entry.char).join('');
|
|
224
|
+
if (after === '' || /^\s*\+\d+ lines?$/u.test(after)) {
|
|
225
|
+
stop = index;
|
|
226
|
+
ownEllipsis = false;
|
|
227
|
+
}
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
const goal = need + (ownEllipsis ? 1 : 0);
|
|
231
|
+
let from = stop;
|
|
232
|
+
let removed = 0;
|
|
233
|
+
while (from > bodyStart && removed < goal) {
|
|
234
|
+
from -= 1;
|
|
235
|
+
removed += charColumns(chars[from].char);
|
|
236
|
+
}
|
|
237
|
+
if (removed < goal) return undefined;
|
|
238
|
+
const kept = chars.slice(bodyStart, from).reduce((sum, entry) => sum + charColumns(entry.char), 0);
|
|
239
|
+
if (kept < MIN_COMMAND_COLUMNS) return undefined;
|
|
240
|
+
// A wide character removed past the goal is paid back in spaces, so the row keeps its width.
|
|
241
|
+
return { from, stop, insert: `${ownEllipsis ? '…' : ''}${' '.repeat(removed - goal)}` };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Bash's mark: the `$` and its one gap. `undefined` on a row whose `$` is not a heading mark, or
|
|
246
|
+
* that has none. `textStart` is where the command begins.
|
|
247
|
+
*/
|
|
248
|
+
function bashMark(chars) {
|
|
249
|
+
const start = chars.findIndex((entry) => entry.char === MARK);
|
|
250
|
+
if (start < 0 || chars[start + 1]?.char !== ' ') return undefined;
|
|
251
|
+
return { start, end: start + 1, textStart: start + 2 };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* A read-only tool's mark: its own glyph, one gap and its name. `undefined` for a row headed any
|
|
256
|
+
* other way. `textStart` is where the arguments begin.
|
|
257
|
+
*/
|
|
258
|
+
function toolMark(chars, tool) {
|
|
259
|
+
const glyph = READ_TOOL_GLYPHS.get(tool);
|
|
260
|
+
if (glyph === undefined) return undefined;
|
|
261
|
+
const start = chars.findIndex((entry) => entry.char === glyph);
|
|
262
|
+
if (start < 0) return undefined;
|
|
263
|
+
const end = start + 2 + tool.length;
|
|
264
|
+
const name = chars.slice(start + 1, end).map((entry) => entry.char).join('');
|
|
265
|
+
if (name !== ` ${tool}`) return undefined;
|
|
266
|
+
if (end < chars.length && chars[end].char !== ' ') return undefined;
|
|
267
|
+
return { start, end, textStart: end + 1 };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* The card's heading row: the first row carrying text, the design's own mark on it, and whether
|
|
272
|
+
* every other row carries no text at all (any other row with text is a continuation of the same
|
|
273
|
+
* heading, or output, so the heading alone is never the whole evidence).
|
|
274
|
+
*/
|
|
275
|
+
function headingSite(lines, leading, mark) {
|
|
276
|
+
let row = -1;
|
|
277
|
+
let chars = [];
|
|
278
|
+
for (const [index, line] of lines.entries()) {
|
|
279
|
+
const candidates = visibleChars(line);
|
|
280
|
+
if (!candidates.some((entry) => isWordCharacter(entry.char) || entry.char === leading)) continue;
|
|
281
|
+
row = index;
|
|
282
|
+
chars = candidates;
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
if (row < 0) return undefined;
|
|
286
|
+
const at = mark(chars);
|
|
287
|
+
if (at === undefined) return undefined;
|
|
288
|
+
const first = chars.find((entry) => entry.char.trim() !== '');
|
|
289
|
+
if (first !== undefined && FRAME_BODY_STARTS.has(first.char)) return undefined;
|
|
290
|
+
// A blank row first is Pi's native result component, never the card's top.
|
|
291
|
+
for (const previous of lines.slice(0, row)) {
|
|
292
|
+
if (visibleChars(previous).every((entry) => entry.char.trim() === '')) return undefined;
|
|
293
|
+
}
|
|
294
|
+
const before = chars.slice(0, at.start).map((entry) => entry.char).join('');
|
|
295
|
+
if (!/^[^\p{L}\p{N}]*$/u.test(before)) return undefined;
|
|
296
|
+
const sole = !lines.some(
|
|
297
|
+
(line, index) => index !== row && visibleChars(line).some((entry) => isWordCharacter(entry.char)),
|
|
298
|
+
);
|
|
299
|
+
return { row, chars, at, sole };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// What a call nothing can name is headed with: the host's `$` mark, the word `bash` and the
|
|
303
|
+
// role the host paints its own bash identity in.
|
|
304
|
+
const GENERIC_IDENTITY = { glyph: '$', titleRole: 'bashMode' };
|
|
305
|
+
const GENERIC_WORD = 'bash';
|
|
306
|
+
|
|
307
|
+
/** The heading text for an identity: its mark, one gap and its word. */
|
|
308
|
+
function headingText(identity, word) {
|
|
309
|
+
return `${identity.glyph}${identity.glyph.endsWith(' ') ? '' : ' '}${word}`;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The label painted in its theme ROLE, or the plain text when the theme cannot paint (absent,
|
|
314
|
+
* throws, or hands back something that no longer contains the text). Pure colour: the visible
|
|
315
|
+
* columns are the text's own either way.
|
|
316
|
+
*/
|
|
317
|
+
function paintLabel(theme, role, text) {
|
|
318
|
+
try {
|
|
319
|
+
if (theme === null || typeof theme !== 'object' || typeof theme.fg !== 'function') return text;
|
|
320
|
+
const painted = theme.fg(role, text);
|
|
321
|
+
return typeof painted === 'string' && painted.includes(text) ? painted : text;
|
|
322
|
+
} catch {
|
|
323
|
+
return text;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* What one covered tool's card is headed with, and how its evidence is read: `mark` finds the
|
|
329
|
+
* design's own mark on the heading row, `activity` turns the proven text into a family (`undefined`
|
|
330
|
+
* when nothing names it), and `generic` is the fallback word a tool keeps for a call it cannot
|
|
331
|
+
* prove — bash's `$ bash`. A read-only shell tool's word never depends on the text, so it passes a
|
|
332
|
+
* constant and has no fallback of its own. A tool the design only DECLARES (`read`, `write`) has no
|
|
333
|
+
* spec at all: its rows keep the heading the host drew.
|
|
334
|
+
*/
|
|
335
|
+
function toolHeadingSpec(tool, render) {
|
|
336
|
+
if (tool === TOOL_NAME) {
|
|
337
|
+
const command = renderedCommand(render);
|
|
338
|
+
return {
|
|
339
|
+
leading: MARK,
|
|
340
|
+
mark: bashMark,
|
|
341
|
+
evidenced: command !== undefined,
|
|
342
|
+
// The real arguments are the evidence when the bridge hands them over: nothing the host
|
|
343
|
+
// truncated or wrapped can hide the command. Without them the drawn heading must prove it.
|
|
344
|
+
command,
|
|
345
|
+
activity: classify,
|
|
346
|
+
generic: true,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
if (!READ_TOOL_GLYPHS.has(tool)) return undefined;
|
|
350
|
+
return {
|
|
351
|
+
leading: READ_TOOL_GLYPHS.get(tool),
|
|
352
|
+
mark: (chars) => toolMark(chars, tool),
|
|
353
|
+
evidenced: hasRenderedArguments(render),
|
|
354
|
+
command: undefined,
|
|
355
|
+
activity: () => EXPLORING,
|
|
356
|
+
generic: false,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* The heading step: the rows with the design's mark swapped for an identity label, or `undefined`
|
|
362
|
+
* when the label cannot be paid for or (for a read-only tool) the heading is not the whole
|
|
363
|
+
* evidence. bash keeps its generic `$ bash` for a call nothing names.
|
|
364
|
+
*/
|
|
365
|
+
function applyHeading(lines, ctx, spec) {
|
|
366
|
+
const site = headingSite(lines, spec.leading, spec.mark);
|
|
367
|
+
if (site === undefined) return undefined;
|
|
368
|
+
let activity;
|
|
369
|
+
if (spec.evidenced) {
|
|
370
|
+
activity = spec.activity(spec.command);
|
|
371
|
+
} else {
|
|
372
|
+
// Without the bridge's arguments the drawn heading is the only evidence, and it must show the
|
|
373
|
+
// whole text: no abbreviation, no fold, no continuation row.
|
|
374
|
+
const drawn = site.sole ? drawnText(site.chars, site.at.textStart) : undefined;
|
|
375
|
+
if (drawn === undefined && !spec.generic) return undefined;
|
|
376
|
+
activity = drawn === undefined ? undefined : spec.activity(drawn);
|
|
377
|
+
}
|
|
378
|
+
const identity = activity === undefined ? GENERIC_IDENTITY : SHELL_ACTIVITY_IDENTITY[activity];
|
|
379
|
+
if (identity === undefined) return undefined;
|
|
380
|
+
const heading = headingText(identity, activity ?? GENERIC_WORD);
|
|
381
|
+
const markText = site.chars.slice(site.at.start, site.at.end).map((entry) => entry.char).join('');
|
|
382
|
+
const extra = textColumns(heading) - textColumns(markText);
|
|
383
|
+
if (extra < 0) return undefined;
|
|
384
|
+
|
|
385
|
+
const row = lines[site.row];
|
|
386
|
+
const paid = payForLabel(row, site.chars, site.at, extra);
|
|
387
|
+
if (paid === undefined) return undefined;
|
|
388
|
+
const markEnd = site.chars[site.at.end - 1];
|
|
389
|
+
const markTo = markEnd.raw + markEnd.char.length;
|
|
390
|
+
// The role paints the label only; the escapes the host had in force where the text after the mark
|
|
391
|
+
// begins are issued again after it, so that text keeps the host's own paint.
|
|
392
|
+
const painted = paintLabel(ctx.theme, identity.titleRole, heading);
|
|
393
|
+
const label = painted === heading ? heading : `${painted}${sgrBefore(row, markTo)}`;
|
|
394
|
+
const markFrom = site.chars[site.at.start].raw;
|
|
395
|
+
const next = `${paid.slice(0, markFrom)}${label}${paid.slice(markTo)}`;
|
|
396
|
+
if (rowColumns(next) !== rowColumns(row)) return undefined;
|
|
397
|
+
return [...lines.slice(0, site.row), next, ...lines.slice(site.row + 1)];
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* `row` with its heading mark removed and `extra` columns freed: the host's fill pays first (one
|
|
402
|
+
* rule column always stays), and the tail of the command hidden after the mark only for what the
|
|
403
|
+
* fill cannot — never the timeout badge the host draws after it.
|
|
404
|
+
* Later offsets go first, so every raw offset still to be used stays valid. `undefined` when the
|
|
405
|
+
* label cannot be paid for while the row keeps its width.
|
|
406
|
+
*/
|
|
407
|
+
function payForLabel(row, chars, at, extra) {
|
|
408
|
+
const extent = commandExtent(chars, at.textStart);
|
|
409
|
+
const take = Math.min(extra, Math.max(0, extent.fillLength - 1));
|
|
410
|
+
const cut =
|
|
411
|
+
extra - take > 0
|
|
412
|
+
? planCommandCut(chars, at.textStart, timeoutBadgeStart(chars, at.textStart, extent.end), extra - take)
|
|
413
|
+
: null;
|
|
414
|
+
if (cut === undefined) return undefined;
|
|
415
|
+
let next = row;
|
|
416
|
+
const fillTail = chars.slice(extent.fillStart + extent.fillLength - take, extent.fillStart + extent.fillLength);
|
|
417
|
+
for (const entry of [...fillTail].reverse()) next = `${next.slice(0, entry.raw)}${next.slice(entry.raw + entry.char.length)}`;
|
|
418
|
+
if (cut !== null) {
|
|
419
|
+
for (const entry of chars.slice(cut.from, cut.stop).reverse()) {
|
|
420
|
+
next = `${next.slice(0, entry.raw)}${next.slice(entry.raw + entry.char.length)}`;
|
|
421
|
+
}
|
|
422
|
+
const at = chars[cut.from].raw;
|
|
423
|
+
next = `${next.slice(0, at)}${cut.insert}${next.slice(at)}`;
|
|
424
|
+
}
|
|
425
|
+
return next;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** The role the whole frame wears: the call's state speaks first, the identity once idle. */
|
|
429
|
+
function frameRole(render, identity) {
|
|
430
|
+
if (render.isPartial === true) return 'warning';
|
|
431
|
+
if (render.isError === true) return 'error';
|
|
432
|
+
return identity.titleRole;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** The command the bridge says this call runs, or `undefined`. */
|
|
436
|
+
function renderedCommand(render) {
|
|
437
|
+
const command = render?.args?.command;
|
|
438
|
+
return typeof command === 'string' && command.trim() !== '' ? command : undefined;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Whether a theme can paint at all: a role the theme cannot colour changes nothing. */
|
|
442
|
+
function canPaint(theme, role) {
|
|
443
|
+
return paintLabel(theme, role, '─') !== '─';
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Whether the bridge handed over this call's arguments. Every `grep`/`find`/`ls` wears the same word
|
|
448
|
+
* whatever it reads, so its arguments are not evidence of WHAT the call is — only of the fact that
|
|
449
|
+
* the design is not depending on a heading the host may have wrapped or abbreviated.
|
|
450
|
+
*/
|
|
451
|
+
function hasRenderedArguments(render) {
|
|
452
|
+
const args = render?.args;
|
|
453
|
+
if (args === null || typeof args !== 'object' || Array.isArray(args)) return false;
|
|
454
|
+
return Object.values(args).some(
|
|
455
|
+
(value) => (typeof value === 'string' && value !== '') || typeof value === 'number',
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* The design's whole behaviour. Returns the host's rows with the heading's mark replaced by an
|
|
461
|
+
* identity label — the activity the call names, or bash's `$ bash` — the frame painted in one role,
|
|
462
|
+
* and a finished call's duration on its closing rule; or a copy of them exactly as given when
|
|
463
|
+
* anything is unproven. Any other tool's rows are returned untouched.
|
|
464
|
+
*
|
|
465
|
+
* S6: a call whose `path` is a skill or an ODD artifact is the FILE card's row — that card draws its
|
|
466
|
+
* own heading and its own frame — so both parts come back exactly as the host drew them, whichever
|
|
467
|
+
* resolver runs first. The classification is `lib/skill-odd-card.js`'s, so the two surfaces can never
|
|
468
|
+
* disagree about which paths those are, and so is the list of tools that card claims (`read`, `edit`,
|
|
469
|
+
* `write`): a `grep` or an `ls` that names a skill's path is no file card's row, so this design keeps
|
|
470
|
+
* drawing it. A read is never painted here at all (it is declared for coverage alone, `READ_TOOL`), so
|
|
471
|
+
* every read row is the host's or that card's, never a status line of this design's.
|
|
472
|
+
*/
|
|
473
|
+
function computeActivity(lines, ctx) {
|
|
474
|
+
const unchanged = () => (Array.isArray(lines) ? [...lines] : []);
|
|
475
|
+
try {
|
|
476
|
+
if (!Array.isArray(lines) || ctx === null || typeof ctx !== 'object') return unchanged();
|
|
477
|
+
const render = ctx.render !== null && typeof ctx.render === 'object' ? ctx.render : undefined;
|
|
478
|
+
const path = render?.args?.path;
|
|
479
|
+
// Only the tools the file card claims can be its row; every other tool's path is this
|
|
480
|
+
// design's to head and frame as it always did.
|
|
481
|
+
if (FILE_CARD_TOOLS.includes(ctx.tool) && fileCardIdentity(path) !== undefined) return unchanged();
|
|
482
|
+
const spec = toolHeadingSpec(ctx.tool, render);
|
|
483
|
+
if (spec === undefined) return unchanged();
|
|
484
|
+
// The frame and the duration wear the identity the CALL's own evidence names, which is what the
|
|
485
|
+
// heading shows whenever the design could head it at all.
|
|
486
|
+
const known = spec.evidenced ? spec.activity(spec.command) : undefined;
|
|
487
|
+
const identity = known === undefined ? GENERIC_IDENTITY : SHELL_ACTIVITY_IDENTITY[known];
|
|
488
|
+
let out = [...lines];
|
|
489
|
+
if (ctx.part !== 'result') out = applyHeading(out, ctx, spec) ?? out;
|
|
490
|
+
if (render !== undefined && spec.evidenced) {
|
|
491
|
+
const role = frameRole(render, identity);
|
|
492
|
+
const elapsed = ctx.part === 'result' && render.isPartial !== true ? cardElapsed(render.durationMs) : undefined;
|
|
493
|
+
if (elapsed !== undefined && out.length > 0 && canPaint(ctx.theme, 'dim')) {
|
|
494
|
+
const last = elapsedRow(out[out.length - 1], elapsed, (text) => paintLabel(ctx.theme, 'dim', text));
|
|
495
|
+
if (last !== undefined) out = [...out.slice(0, -1), last];
|
|
496
|
+
}
|
|
497
|
+
if (canPaint(ctx.theme, role)) out = out.map((row) => paintFrame(row, (text) => paintLabel(ctx.theme, role, text)));
|
|
498
|
+
}
|
|
499
|
+
if (out.length !== lines.length || out.some((row, index) => rowColumns(row) !== rowColumns(lines[index]))) return unchanged();
|
|
500
|
+
return out;
|
|
501
|
+
} catch {
|
|
502
|
+
return unchanged();
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* The design's entry point: `computeActivity` behind a bounded memo. The key is everything the
|
|
508
|
+
* drawing reads: the tool and part, the host's rows, what each theme role paints now, and the
|
|
509
|
+
* call's arguments and state. A caller always gets a fresh array.
|
|
510
|
+
*/
|
|
511
|
+
function applyBashActivityHeading(lines, ctx) {
|
|
512
|
+
try {
|
|
513
|
+
if (!Array.isArray(lines) || ctx === null || typeof ctx !== 'object') return computeActivity(lines, ctx);
|
|
514
|
+
const render = ctx.render !== null && typeof ctx.render === 'object' ? ctx.render : undefined;
|
|
515
|
+
const call = render === undefined
|
|
516
|
+
? '-'
|
|
517
|
+
: [renderedCommand(render) ?? '', hasRenderedArguments(render) ? '1' : '', render.isPartial, render.isError, render.durationMs, render.args?.path ?? ''].join('\u0001');
|
|
518
|
+
const key = [ctx.tool, ctx.part, themeFingerprint(ctx.theme), call, lines.join('\n')].join('\u0000');
|
|
519
|
+
const hit = applyMemo.get(key);
|
|
520
|
+
if (hit !== undefined) return [...hit];
|
|
521
|
+
return [...remember(applyMemo, key, computeActivity(lines, ctx))];
|
|
522
|
+
} catch {
|
|
523
|
+
return Array.isArray(lines) ? [...lines] : [];
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// The read-only picture `/gentle:customize` draws while `activity` is highlighted.
|
|
528
|
+
const BASH_DESIGN_PREVIEW = {
|
|
529
|
+
title: `Cards · ${BASH_DESIGN_LABEL}`,
|
|
530
|
+
// One row, like every other Cards preview: the families a heading can wear.
|
|
531
|
+
sample: '⌕ exploring ✓ checking ⎇ versioning ↑ publishing ⚠ destructive',
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
/** The single `bash` design an older bridge can take: one `Card bash` line, bash only. */
|
|
535
|
+
function bashActivityDesign() {
|
|
536
|
+
return {
|
|
537
|
+
tool: TOOL_NAME,
|
|
538
|
+
id: BASH_DESIGN_ID,
|
|
539
|
+
name: BASH_DESIGN_PLUGIN_NAME,
|
|
540
|
+
label: BASH_DESIGN_LABEL,
|
|
541
|
+
description: 'heads a bash card with the activity its command names',
|
|
542
|
+
preview: () => ({ ...BASH_DESIGN_PREVIEW }),
|
|
543
|
+
apply: applyBashActivityHeading,
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* The grouped registration: ONE `Card tools` line over bash, the three read-only shell tools, `read`
|
|
549
|
+
* and `write`. The bridge names the row after the line (`Card tools`), keeps `default` first and
|
|
550
|
+
* stores the choice under its own reserved key, so the per-tool value stays where an older bridge put
|
|
551
|
+
* it.
|
|
552
|
+
*
|
|
553
|
+
* `read` and `write` are on the line so the file card can ask about them (S1): the card is the only
|
|
554
|
+
* surface that draws either, and it is painted only while this line answers for that tool. This design
|
|
555
|
+
* never heads a read or a write itself — `computeActivity` answers `undefined` for a tool it knows no
|
|
556
|
+
* mark for, so their rows come back exactly as the host drew them.
|
|
557
|
+
*/
|
|
558
|
+
function toolsActivityDesign() {
|
|
559
|
+
return {
|
|
560
|
+
line: TOOLS_LINE,
|
|
561
|
+
tools: [TOOL_NAME, ...READ_TOOL_GLYPHS.keys(), READ_TOOL, WRITE_TOOL],
|
|
562
|
+
id: BASH_DESIGN_ID,
|
|
563
|
+
name: BASH_DESIGN_PLUGIN_NAME,
|
|
564
|
+
label: BASH_DESIGN_LABEL,
|
|
565
|
+
description: 'heads a tools card with the activity its call names; the read-only shell tools are always exploring',
|
|
566
|
+
preview: () => ({ ...BASH_DESIGN_PREVIEW }),
|
|
567
|
+
apply: applyBashActivityHeading,
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Whether a published bridge can take one line over several tools. Read defensively: a bridge with
|
|
573
|
+
* no release string, or one this cannot read as a release, is treated as the older shape, because a
|
|
574
|
+
* grouped registration there is REFUSED and recorded, which is worse than the extra `Card bash` row.
|
|
575
|
+
*/
|
|
576
|
+
/**
|
|
577
|
+
* Ask the bridge itself, once, when its release string cannot be read: a grouped registration that
|
|
578
|
+
* is accepted and disposed is the capability, whatever the version says. An older bridge refuses it
|
|
579
|
+
* and records a rejection, which is why this runs only in that gap and never beside a readable
|
|
580
|
+
* release.
|
|
581
|
+
*/
|
|
582
|
+
function probeToolLines(api) {
|
|
583
|
+
try {
|
|
584
|
+
const dispose = api.registerToolDesign({
|
|
585
|
+
line: PROBE_LINE,
|
|
586
|
+
tools: [PROBE_TOOL],
|
|
587
|
+
id: PROBE_DESIGN_ID,
|
|
588
|
+
label: 'probe',
|
|
589
|
+
apply: (lines) => [...lines],
|
|
590
|
+
});
|
|
591
|
+
if (typeof dispose === 'function') dispose();
|
|
592
|
+
return true;
|
|
593
|
+
} catch {
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function bridgeSupportsToolLines(api) {
|
|
599
|
+
const version = api?.VERSION;
|
|
600
|
+
if (typeof version !== 'string') return probeToolLines(api);
|
|
601
|
+
const parts = version.split('.').slice(0, 3).map((part) => {
|
|
602
|
+
const digits = /^\d+/.exec(part);
|
|
603
|
+
return digits === null ? Number.NaN : Number(digits[0]);
|
|
604
|
+
});
|
|
605
|
+
if (parts.length < 2 || parts.some((part) => !Number.isFinite(part))) return probeToolLines(api);
|
|
606
|
+
const [major, minor, patch = 0] = parts;
|
|
607
|
+
if (major !== TOOLS_LINE_VERSION[0]) return major > TOOLS_LINE_VERSION[0];
|
|
608
|
+
if (minor !== TOOLS_LINE_VERSION[1]) return minor > TOOLS_LINE_VERSION[1];
|
|
609
|
+
return patch >= TOOLS_LINE_VERSION[2];
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Select `activity` ONCE, on the first load that finds no choice. Returns whether it wrote the
|
|
614
|
+
* selection. A choice already stored, `native` included, is never touched, and the marker is
|
|
615
|
+
* written only after the selection (or after finding one), so a failed write is tried again on the
|
|
616
|
+
* next load. Never throws: the design is cosmetic. It never writes the bridge's reserved line key:
|
|
617
|
+
* the bridge's own registration-time migration carries a `bash` choice over to the line.
|
|
618
|
+
*/
|
|
619
|
+
function seedBashDesignSelection(api) {
|
|
620
|
+
try {
|
|
621
|
+
if (api === null || typeof api !== 'object' || typeof api.createStore !== 'function') return false;
|
|
622
|
+
const own = api.createStore(OWN_STORE);
|
|
623
|
+
if (own.get(SEEDED_KEY, false) === true) return false;
|
|
624
|
+
const bridge = api.createStore(BRIDGE_SELECTION_STORE);
|
|
625
|
+
const stored = bridge.get(BRIDGE_SELECTION_KEY, {});
|
|
626
|
+
if (stored === null || typeof stored !== 'object' || Array.isArray(stored)) return false;
|
|
627
|
+
if (Object.hasOwn(stored, TOOL_NAME)) {
|
|
628
|
+
own.set(SEEDED_KEY, true);
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
bridge.set(BRIDGE_SELECTION_KEY, { ...stored, [TOOL_NAME]: BASH_DESIGN_ID });
|
|
632
|
+
own.set(SEEDED_KEY, true);
|
|
633
|
+
return true;
|
|
634
|
+
} catch {
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// The registration this process made: a second call is the same design, never two.
|
|
640
|
+
let designDisposer = null;
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Register the design on a bridge that can take one and hand back the idempotent disposer, or
|
|
644
|
+
* `null` when the bridge is absent or older than `registerToolDesign`. Never throws.
|
|
645
|
+
*
|
|
646
|
+
* The shape is decided BEFORE anything is registered: one `Card tools` line on a bridge that can
|
|
647
|
+
* take one, today's single `Card bash` design on anything older. The seed runs BEFORE the
|
|
648
|
+
* registration too, because a 0.6.0 bridge inherits a joining tool's legacy per-tool choice into
|
|
649
|
+
* the line's own value at REGISTRATION time — a choice written after would be missed.
|
|
650
|
+
*/
|
|
651
|
+
function registerBashDesign(api) {
|
|
652
|
+
if (api === null || typeof api !== 'object' || typeof api.registerToolDesign !== 'function') return null;
|
|
653
|
+
if (designDisposer !== null) return designDisposer;
|
|
654
|
+
seedBashDesignSelection(api);
|
|
655
|
+
let registered;
|
|
656
|
+
try {
|
|
657
|
+
registered = api.registerToolDesign(bridgeSupportsToolLines(api) ? toolsActivityDesign() : bashActivityDesign());
|
|
658
|
+
} catch {
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
const dispose = typeof registered === 'function' ? registered : () => {};
|
|
662
|
+
let disposed = false;
|
|
663
|
+
designDisposer = () => {
|
|
664
|
+
if (disposed) return;
|
|
665
|
+
disposed = true;
|
|
666
|
+
designDisposer = null;
|
|
667
|
+
try {
|
|
668
|
+
dispose();
|
|
669
|
+
} catch {
|
|
670
|
+
// A failed removal leaves the design registered; it never breaks the host.
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
return designDisposer;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
module.exports = {
|
|
677
|
+
BASH_DESIGN_ID,
|
|
678
|
+
BASH_DESIGN_LABEL,
|
|
679
|
+
applyBashActivityHeading,
|
|
680
|
+
bashActivityDesign,
|
|
681
|
+
registerBashDesign,
|
|
682
|
+
seedBashDesignSelection,
|
|
683
|
+
toolsActivityDesign,
|
|
684
|
+
};
|