crysk 0.0.0 → 0.3.1
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/LICENSE +176 -0
- package/README.md +611 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/SKILL-y8rv8msh.md +134 -0
- package/dist/SKILL-ygka1bs0.md +515 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/ci-admission-qk9r9taq.md +124 -0
- package/dist/cli.js +42501 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +124 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +515 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +945 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +910 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +984 -0
- package/src/pr/answered-findings.ts +169 -0
- package/src/pr/ci-admission-gate.ts +743 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +845 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
package/src/ui/select.ts
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
// The terminal surface's INTERACTIVE half: the keyboard-driven menu that
|
|
2
|
+
// replaced the cost band's `[y/N]` read. ui/primitives.ts stays pure by construction;
|
|
3
|
+
// this module is where the one unavoidable side effect — putting the user's
|
|
4
|
+
// terminal into raw mode — is quarantined behind an injected reader, so every
|
|
5
|
+
// decision above it (which options exist, where the cursor lands, what a key
|
|
6
|
+
// means, what "Show details" does) is a total function testable with no TTY.
|
|
7
|
+
//
|
|
8
|
+
// The threat model that shapes the whole file: the watcher spawns reviews with
|
|
9
|
+
// `--yes` AND `stdin: "ignore"` (watch/watch.ts). The old confirm() degraded there by
|
|
10
|
+
// accident — its stdin read resolved `{ value: undefined }` immediately — but
|
|
11
|
+
// `setRawMode` in that same position THROWS under launchd. So raw mode is
|
|
12
|
+
// entered only behind an isTTY + setRawMode-exists guard, the non-TTY path
|
|
13
|
+
// keeps the old plain-text behaviour byte for byte, and the restore lives in a
|
|
14
|
+
// `finally` so a throw mid-menu cannot strand a human in raw mode.
|
|
15
|
+
|
|
16
|
+
import type { FreeRouteBilling } from "#model/free-discovery";
|
|
17
|
+
import { bold, dim, log } from "./primitives";
|
|
18
|
+
|
|
19
|
+
export type ConfirmResult =
|
|
20
|
+
| { kind: "review"; post: boolean }
|
|
21
|
+
| { kind: "cancel" };
|
|
22
|
+
|
|
23
|
+
// What a keypress MEANS, decided once. "char" carries the lowercased
|
|
24
|
+
// character so shortcut matching never re-derives it.
|
|
25
|
+
export type Key =
|
|
26
|
+
| { type: "up" }
|
|
27
|
+
| { type: "down" }
|
|
28
|
+
| { type: "left" }
|
|
29
|
+
| { type: "right" }
|
|
30
|
+
| { type: "enter" }
|
|
31
|
+
| { type: "escape" }
|
|
32
|
+
| { type: "ctrl-c" }
|
|
33
|
+
| { type: "cancel" }
|
|
34
|
+
| { type: "char"; char: string }
|
|
35
|
+
| { type: "ignore" };
|
|
36
|
+
|
|
37
|
+
const ESC = "\x1b";
|
|
38
|
+
const CTRL_C = "\x03";
|
|
39
|
+
|
|
40
|
+
// One chunk off a raw stdin can carry several keys: a held arrow, a paste, or
|
|
41
|
+
// simply two presses inside one tick. Splitting is pure and separate from
|
|
42
|
+
// interpretation so both halves stay testable.
|
|
43
|
+
export function splitKeys(chunk: string): string[] {
|
|
44
|
+
const keys: string[] = [];
|
|
45
|
+
let i = 0;
|
|
46
|
+
while (i < chunk.length) {
|
|
47
|
+
if (chunk[i] === ESC && chunk[i + 1] === "[") {
|
|
48
|
+
// CSI: ESC [ ...params... final-byte in 0x40–0x7E.
|
|
49
|
+
let j = i + 2;
|
|
50
|
+
while (j < chunk.length && !/[@-~]/.test(chunk[j] as string)) j++;
|
|
51
|
+
keys.push(chunk.slice(i, Math.min(j + 1, chunk.length)));
|
|
52
|
+
i = j + 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
keys.push(chunk[i] as string);
|
|
56
|
+
i++;
|
|
57
|
+
}
|
|
58
|
+
return keys;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function parseKey(key: string): Key {
|
|
62
|
+
if (key === `${ESC}[A`) return { type: "up" };
|
|
63
|
+
if (key === `${ESC}[B`) return { type: "down" };
|
|
64
|
+
if (key === `${ESC}[C`) return { type: "right" };
|
|
65
|
+
if (key === `${ESC}[D`) return { type: "left" };
|
|
66
|
+
// \r is what a raw-mode terminal sends for Enter; \n only ever arrives from
|
|
67
|
+
// a cooked stream. Both count — a menu that ignored one would look dead.
|
|
68
|
+
if (key === "\r" || key === "\n") return { type: "enter" };
|
|
69
|
+
if (key === CTRL_C) return { type: "ctrl-c" };
|
|
70
|
+
if (key === ESC) return { type: "escape" };
|
|
71
|
+
// Any other control byte (backspace, tab, Ctrl-anything) is noise, not a
|
|
72
|
+
// shortcut: matching it against option letters would fire a $6 review on a
|
|
73
|
+
// stray keystroke.
|
|
74
|
+
if (key.length !== 1) return { type: "ignore" };
|
|
75
|
+
const code = key.charCodeAt(0);
|
|
76
|
+
if (code < 0x20 || code === 0x7f) return { type: "ignore" };
|
|
77
|
+
return { type: "char", char: key.toLowerCase() };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface MenuOption {
|
|
81
|
+
// "review" and "cancel" terminate; "details" is a VIEW action that
|
|
82
|
+
// re-presents the menu instead of resolving it.
|
|
83
|
+
action: "review" | "details" | "cancel";
|
|
84
|
+
label: string;
|
|
85
|
+
hint?: string;
|
|
86
|
+
// Lowercase single characters. `y`/`n` are kept on the review/cancel rows on
|
|
87
|
+
// purpose: they are what a decade of `[y/N]` prompts trained into fingers.
|
|
88
|
+
shortcuts: string[];
|
|
89
|
+
post: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface ConfirmSpec {
|
|
93
|
+
low: number;
|
|
94
|
+
high: number;
|
|
95
|
+
// Set only for a route the plan card already priced as free or mixed. Absent
|
|
96
|
+
// means today's wording on both the menu hint and the plain prompt.
|
|
97
|
+
billing?: FreeBillingMode;
|
|
98
|
+
// Only true when the run was actually going to post: offering "don't post"
|
|
99
|
+
// to a run that never would is an option with no meaning behind it.
|
|
100
|
+
canSkipPost: boolean;
|
|
101
|
+
// A thunk, not an array: building the details view costs filesystem probes
|
|
102
|
+
// (worktree/codegraph existence), and most runs never open it.
|
|
103
|
+
details: () => string[];
|
|
104
|
+
styles: boolean;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The option list, composed and nothing more — the single place that decides
|
|
108
|
+
// what a human is offered, so a test can assert it without a terminal.
|
|
109
|
+
export function menuOptions(canSkipPost: boolean): MenuOption[] {
|
|
110
|
+
const options: MenuOption[] = [
|
|
111
|
+
{
|
|
112
|
+
action: "review",
|
|
113
|
+
label: "Review it",
|
|
114
|
+
shortcuts: ["r", "y"],
|
|
115
|
+
post: canSkipPost,
|
|
116
|
+
},
|
|
117
|
+
];
|
|
118
|
+
if (canSkipPost) {
|
|
119
|
+
options.push({
|
|
120
|
+
action: "review",
|
|
121
|
+
label: "Review, but don't post",
|
|
122
|
+
shortcuts: ["p"],
|
|
123
|
+
post: false,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
options.push(
|
|
127
|
+
{ action: "details", label: "Show details", shortcuts: ["d"], post: false },
|
|
128
|
+
{
|
|
129
|
+
action: "cancel",
|
|
130
|
+
label: "Cancel",
|
|
131
|
+
shortcuts: ["c", "n", "q"],
|
|
132
|
+
post: false,
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
return options;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type SizeGateConfirmResult = { kind: "proceed" } | { kind: "cancel" };
|
|
139
|
+
|
|
140
|
+
// Continue / Cancel only. No cost hint (the gate exists because we will
|
|
141
|
+
// not guess at this bill) and no details view (the SKIP line above the
|
|
142
|
+
// menu already named the reason and the two flag hatches).
|
|
143
|
+
export function sizeGateMenuOptions(): MenuOption[] {
|
|
144
|
+
return [
|
|
145
|
+
{
|
|
146
|
+
action: "review",
|
|
147
|
+
label: "Review anyway",
|
|
148
|
+
shortcuts: ["r", "y"],
|
|
149
|
+
post: false,
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
action: "cancel",
|
|
153
|
+
label: "Cancel",
|
|
154
|
+
shortcuts: ["c", "n", "q"],
|
|
155
|
+
post: false,
|
|
156
|
+
},
|
|
157
|
+
];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const LABEL_WIDTH = 32;
|
|
161
|
+
|
|
162
|
+
// The billing mode of an admitted route plan, derived ONCE from the same
|
|
163
|
+
// free-step count the plan card prints (planEstimateLine in ui/plan.ts reads
|
|
164
|
+
// this too). All steps free: nothing is billed. Some free: the band stays and
|
|
165
|
+
// names the free share. Otherwise there is no mode, and today's wording stands.
|
|
166
|
+
export type FreeBillingMode = "free" | "mixed";
|
|
167
|
+
|
|
168
|
+
export const FREE_STEPS_NOTE = "(free steps not billed)";
|
|
169
|
+
|
|
170
|
+
export function freeBillingMode(
|
|
171
|
+
free: FreeRouteBilling | undefined,
|
|
172
|
+
): FreeBillingMode | undefined {
|
|
173
|
+
if (free === undefined || free.total === 0) return undefined;
|
|
174
|
+
if (free.free === free.total) return "free";
|
|
175
|
+
return free.free > 0 ? "mixed" : undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function band(low: number, high: number): string {
|
|
179
|
+
return `$${low.toFixed(2)} – $${high.toFixed(2)}`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// The menu's first-row hint. A free route shows no dollar figure at all: the
|
|
183
|
+
// band would read as spend, which is the claim the plan card just retracted.
|
|
184
|
+
export function costHint(
|
|
185
|
+
low: number,
|
|
186
|
+
high: number,
|
|
187
|
+
billing?: FreeBillingMode,
|
|
188
|
+
): string {
|
|
189
|
+
if (billing === "free") return "free route (not billed)";
|
|
190
|
+
if (billing === "mixed") return `${band(low, high)} ${FREE_STEPS_NOTE}`;
|
|
191
|
+
return band(low, high);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// The plain [y/N] prompt's sentence, without its question mark and answer
|
|
195
|
+
// domain. Metered is the sentence this module always printed, byte for byte.
|
|
196
|
+
export function spendSentence(
|
|
197
|
+
low: number,
|
|
198
|
+
high: number,
|
|
199
|
+
billing?: FreeBillingMode,
|
|
200
|
+
): string {
|
|
201
|
+
const range = `$${low.toFixed(2)}–$${high.toFixed(2)}`;
|
|
202
|
+
if (billing === "free") {
|
|
203
|
+
return `Run this review on a free route (token-derived ${range}, not billed)`;
|
|
204
|
+
}
|
|
205
|
+
const note = billing === "mixed" ? ` ${FREE_STEPS_NOTE}` : "";
|
|
206
|
+
return `Spend an estimated ${range} on this review${note}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// The frame, as lines. Built unstyled and painted per line, same rule as the
|
|
210
|
+
// plan card: the pointer is plain text either way, so a piped run reads
|
|
211
|
+
// correctly with no escapes at all.
|
|
212
|
+
export function renderMenu(
|
|
213
|
+
options: MenuOption[],
|
|
214
|
+
cursor: number,
|
|
215
|
+
styles: boolean,
|
|
216
|
+
): string[] {
|
|
217
|
+
return options.map((option, i) => {
|
|
218
|
+
const active = i === cursor;
|
|
219
|
+
const head = active ? "❯ " : " ";
|
|
220
|
+
const body = option.hint
|
|
221
|
+
? `${option.label.padEnd(LABEL_WIDTH)}${option.hint}`
|
|
222
|
+
: option.label;
|
|
223
|
+
const line = ` ${head}${body}`;
|
|
224
|
+
if (!styles) return line;
|
|
225
|
+
return active ? bold(line, true) : dim(line, true);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Cursor movement WRAPS. A four-item menu is short enough that walking off
|
|
230
|
+
// either end and landing on the other end is what a human expects, and it
|
|
231
|
+
// removes the dead keypress at the boundaries.
|
|
232
|
+
export function moveCursor(cursor: number, delta: number, len: number): number {
|
|
233
|
+
return (cursor + delta + len) % len;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// The injected input. `raw` decides which of the two prompts runs at all:
|
|
237
|
+
// true is the menu, false is the legacy one-chunk `[y/N]` read that the
|
|
238
|
+
// watcher's `stdin: "ignore"` depends on.
|
|
239
|
+
export interface KeyReader {
|
|
240
|
+
raw: boolean;
|
|
241
|
+
// A raw reader yields keypress chunks; a line reader yields the single
|
|
242
|
+
// chunk it read. `undefined` means the stream ended without an answer —
|
|
243
|
+
// exactly what a closed/ignored stdin produces, and it must read as "no".
|
|
244
|
+
read(): Promise<string | undefined>;
|
|
245
|
+
close(): void;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface ConfirmIo {
|
|
249
|
+
write: (text: string) => void;
|
|
250
|
+
line: (text?: string) => void;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const defaultIo: ConfirmIo = {
|
|
254
|
+
write: (text) => process.stderr.write(text),
|
|
255
|
+
line: (text) => log(text),
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// The pure-ish core: given a reader and an io sink, run the menu to a
|
|
259
|
+
// decision. Every branch here is reachable from a test with a scripted reader.
|
|
260
|
+
export async function runConfirm(
|
|
261
|
+
spec: ConfirmSpec,
|
|
262
|
+
reader: KeyReader,
|
|
263
|
+
io: ConfirmIo = defaultIo,
|
|
264
|
+
): Promise<ConfirmResult> {
|
|
265
|
+
if (!reader.raw) return runPlainConfirm(spec, reader, io);
|
|
266
|
+
|
|
267
|
+
const options = menuOptions(spec.canSkipPost);
|
|
268
|
+
const withHint = options.map((option, i) =>
|
|
269
|
+
i === 0
|
|
270
|
+
? { ...option, hint: costHint(spec.low, spec.high, spec.billing) }
|
|
271
|
+
: option,
|
|
272
|
+
);
|
|
273
|
+
let cursor = 0;
|
|
274
|
+
// Repainting in place needs cursor-movement escapes, which are exactly what
|
|
275
|
+
// must never reach a pipe. When styling is off the frames are simply
|
|
276
|
+
// appended — uglier, but honest bytes.
|
|
277
|
+
const repaint = spec.styles;
|
|
278
|
+
let drawn = 0;
|
|
279
|
+
|
|
280
|
+
const draw = (): void => {
|
|
281
|
+
if (repaint && drawn > 0) io.write(`${ESC}[${drawn}A`);
|
|
282
|
+
const lines = renderMenu(withHint, cursor, spec.styles);
|
|
283
|
+
for (const line of lines) {
|
|
284
|
+
if (repaint) io.write(`${ESC}[2K`);
|
|
285
|
+
io.line(line);
|
|
286
|
+
}
|
|
287
|
+
drawn = lines.length;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
io.line();
|
|
291
|
+
draw();
|
|
292
|
+
|
|
293
|
+
for (;;) {
|
|
294
|
+
const chunk = await reader.read();
|
|
295
|
+
// Stream ended mid-menu (stdin closed under us). Never a licence to
|
|
296
|
+
// spend: an unanswered prompt is a "no".
|
|
297
|
+
if (chunk === undefined) {
|
|
298
|
+
io.line();
|
|
299
|
+
return { kind: "cancel" };
|
|
300
|
+
}
|
|
301
|
+
for (const rawKey of splitKeys(chunk)) {
|
|
302
|
+
const key = parseKey(rawKey);
|
|
303
|
+
if (key.type === "ignore") continue;
|
|
304
|
+
if (
|
|
305
|
+
key.type === "cancel" ||
|
|
306
|
+
key.type === "escape" ||
|
|
307
|
+
key.type === "ctrl-c"
|
|
308
|
+
) {
|
|
309
|
+
// Loud, never swallowed: a Ctrl-C that produced silence looks like a
|
|
310
|
+
// hang, and the caller's "aborted" line lands under this.
|
|
311
|
+
io.line();
|
|
312
|
+
io.line("^C");
|
|
313
|
+
return { kind: "cancel" };
|
|
314
|
+
}
|
|
315
|
+
if (key.type === "up" || key.type === "down") {
|
|
316
|
+
cursor = moveCursor(
|
|
317
|
+
cursor,
|
|
318
|
+
key.type === "up" ? -1 : 1,
|
|
319
|
+
withHint.length,
|
|
320
|
+
);
|
|
321
|
+
draw();
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
const chosen =
|
|
325
|
+
key.type === "enter"
|
|
326
|
+
? withHint[cursor]
|
|
327
|
+
: key.type === "char"
|
|
328
|
+
? withHint.find((option) => option.shortcuts.includes(key.char))
|
|
329
|
+
: undefined;
|
|
330
|
+
if (chosen === undefined) continue;
|
|
331
|
+
if (chosen.action === "details") {
|
|
332
|
+
// A VIEW, not a decision: print and fall back into the same loop. The
|
|
333
|
+
// frame counter resets because the details pushed the old frame up.
|
|
334
|
+
io.line();
|
|
335
|
+
for (const line of spec.details()) io.line(line);
|
|
336
|
+
drawn = 0;
|
|
337
|
+
io.line();
|
|
338
|
+
draw();
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
io.line();
|
|
342
|
+
return chosen.action === "cancel"
|
|
343
|
+
? { kind: "cancel" }
|
|
344
|
+
: { kind: "review", post: chosen.post };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// The non-TTY path, unchanged in behaviour from the confirm() this replaced:
|
|
350
|
+
// same sentence, same `[y/N]`, one chunk read, `y`/`yes` proceeds and
|
|
351
|
+
// EVERYTHING else — including a stdin that yields nothing at all — cancels.
|
|
352
|
+
// The watcher runs here (with --yes short-circuiting above it) and a shell
|
|
353
|
+
// pipeline runs here; neither may be handed a menu.
|
|
354
|
+
async function runPlainConfirm(
|
|
355
|
+
spec: ConfirmSpec,
|
|
356
|
+
reader: KeyReader,
|
|
357
|
+
io: ConfirmIo,
|
|
358
|
+
): Promise<ConfirmResult> {
|
|
359
|
+
io.line();
|
|
360
|
+
io.write(`${spendSentence(spec.low, spec.high, spec.billing)}? [y/N] `);
|
|
361
|
+
const answer = (await reader.read())?.trim().toLowerCase() ?? "";
|
|
362
|
+
io.line();
|
|
363
|
+
return answer === "y" || answer === "yes"
|
|
364
|
+
? { kind: "review", post: spec.canSkipPost }
|
|
365
|
+
: { kind: "cancel" };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// One chunk off Bun.stdin, then release it. Reading the whole stream would
|
|
369
|
+
// block until EOF, which never comes on an interactive terminal.
|
|
370
|
+
function createLineReader(): KeyReader {
|
|
371
|
+
return {
|
|
372
|
+
raw: false,
|
|
373
|
+
read: async () => {
|
|
374
|
+
const stream = Bun.stdin.stream().getReader();
|
|
375
|
+
const { value } = await stream.read();
|
|
376
|
+
await stream.cancel();
|
|
377
|
+
return value === undefined ? undefined : new TextDecoder().decode(value);
|
|
378
|
+
},
|
|
379
|
+
close: () => {
|
|
380
|
+
// Nothing held open: the reader above cancels its own stream.
|
|
381
|
+
},
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// The one place raw mode is entered. BOTH guards matter: `isTTY` is false
|
|
386
|
+
// under the watcher's `stdin: "ignore"`, and `setRawMode` is absent entirely
|
|
387
|
+
// on some non-tty stdin shapes — calling it there throws, which is precisely
|
|
388
|
+
// the failure this whole module is arranged to avoid.
|
|
389
|
+
function createStdinReader(stdin = process.stdin): KeyReader {
|
|
390
|
+
if (!stdin.isTTY || typeof stdin.setRawMode !== "function") {
|
|
391
|
+
return createLineReader();
|
|
392
|
+
}
|
|
393
|
+
stdin.setRawMode(true);
|
|
394
|
+
stdin.resume();
|
|
395
|
+
stdin.setEncoding("utf8");
|
|
396
|
+
const pending: string[] = [];
|
|
397
|
+
let deliver: ((chunk: string) => void) | undefined;
|
|
398
|
+
const onData = (chunk: string): void => {
|
|
399
|
+
if (deliver === undefined) {
|
|
400
|
+
pending.push(chunk);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const resolve = deliver;
|
|
404
|
+
deliver = undefined;
|
|
405
|
+
resolve(chunk);
|
|
406
|
+
};
|
|
407
|
+
stdin.on("data", onData);
|
|
408
|
+
return {
|
|
409
|
+
raw: true,
|
|
410
|
+
read: () => {
|
|
411
|
+
const queued = pending.shift();
|
|
412
|
+
if (queued !== undefined) return Promise.resolve(queued);
|
|
413
|
+
return new Promise<string>((resolve) => {
|
|
414
|
+
deliver = resolve;
|
|
415
|
+
});
|
|
416
|
+
},
|
|
417
|
+
close: () => {
|
|
418
|
+
stdin.off("data", onData);
|
|
419
|
+
stdin.setRawMode(false);
|
|
420
|
+
stdin.pause();
|
|
421
|
+
},
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// The shell. The `finally` is the load-bearing line in this file: a throw
|
|
426
|
+
// anywhere inside the menu must still hand the terminal back cooked.
|
|
427
|
+
export async function confirmReview(
|
|
428
|
+
spec: ConfirmSpec,
|
|
429
|
+
reader: KeyReader = createStdinReader(),
|
|
430
|
+
io: ConfirmIo = defaultIo,
|
|
431
|
+
): Promise<ConfirmResult> {
|
|
432
|
+
try {
|
|
433
|
+
return await runConfirm(spec, reader, io);
|
|
434
|
+
} finally {
|
|
435
|
+
reader.close();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Sibling of runConfirm for the size-gate override. Same reader/io contract,
|
|
440
|
+
// same unanswered-is-no rule, no details view and no dollar hint — guessing
|
|
441
|
+
// at this bill is the thing the gate refuses to do.
|
|
442
|
+
export async function runSizeGateConfirm(
|
|
443
|
+
styles: boolean,
|
|
444
|
+
reader: KeyReader,
|
|
445
|
+
io: ConfirmIo = defaultIo,
|
|
446
|
+
): Promise<SizeGateConfirmResult> {
|
|
447
|
+
if (!reader.raw) return runPlainSizeGateConfirm(reader, io);
|
|
448
|
+
|
|
449
|
+
const options = sizeGateMenuOptions();
|
|
450
|
+
let cursor = 0;
|
|
451
|
+
const repaint = styles;
|
|
452
|
+
let drawn = 0;
|
|
453
|
+
|
|
454
|
+
const draw = (): void => {
|
|
455
|
+
if (repaint && drawn > 0) io.write(`${ESC}[${drawn}A`);
|
|
456
|
+
const lines = renderMenu(options, cursor, styles);
|
|
457
|
+
for (const line of lines) {
|
|
458
|
+
if (repaint) io.write(`${ESC}[2K`);
|
|
459
|
+
io.line(line);
|
|
460
|
+
}
|
|
461
|
+
drawn = lines.length;
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
io.line();
|
|
465
|
+
draw();
|
|
466
|
+
|
|
467
|
+
for (;;) {
|
|
468
|
+
const chunk = await reader.read();
|
|
469
|
+
if (chunk === undefined) {
|
|
470
|
+
io.line();
|
|
471
|
+
return { kind: "cancel" };
|
|
472
|
+
}
|
|
473
|
+
for (const rawKey of splitKeys(chunk)) {
|
|
474
|
+
const key = parseKey(rawKey);
|
|
475
|
+
if (key.type === "ignore") continue;
|
|
476
|
+
if (
|
|
477
|
+
key.type === "cancel" ||
|
|
478
|
+
key.type === "escape" ||
|
|
479
|
+
key.type === "ctrl-c"
|
|
480
|
+
) {
|
|
481
|
+
io.line();
|
|
482
|
+
io.line("^C");
|
|
483
|
+
return { kind: "cancel" };
|
|
484
|
+
}
|
|
485
|
+
if (key.type === "up" || key.type === "down") {
|
|
486
|
+
cursor = moveCursor(cursor, key.type === "up" ? -1 : 1, options.length);
|
|
487
|
+
draw();
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
const chosen =
|
|
491
|
+
key.type === "enter"
|
|
492
|
+
? options[cursor]
|
|
493
|
+
: key.type === "char"
|
|
494
|
+
? options.find((option) => option.shortcuts.includes(key.char))
|
|
495
|
+
: undefined;
|
|
496
|
+
if (chosen === undefined) continue;
|
|
497
|
+
io.line();
|
|
498
|
+
return chosen.action === "cancel"
|
|
499
|
+
? { kind: "cancel" }
|
|
500
|
+
: { kind: "proceed" };
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
async function runPlainSizeGateConfirm(
|
|
506
|
+
reader: KeyReader,
|
|
507
|
+
io: ConfirmIo,
|
|
508
|
+
): Promise<SizeGateConfirmResult> {
|
|
509
|
+
io.line();
|
|
510
|
+
io.write("This diff exceeds the size gate. Review anyway? [y/N] ");
|
|
511
|
+
const answer = (await reader.read())?.trim().toLowerCase() ?? "";
|
|
512
|
+
io.line();
|
|
513
|
+
return answer === "y" || answer === "yes"
|
|
514
|
+
? { kind: "proceed" }
|
|
515
|
+
: { kind: "cancel" };
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export async function confirmSizeGate(
|
|
519
|
+
styles: boolean,
|
|
520
|
+
reader: KeyReader = createStdinReader(),
|
|
521
|
+
io: ConfirmIo = defaultIo,
|
|
522
|
+
): Promise<SizeGateConfirmResult> {
|
|
523
|
+
try {
|
|
524
|
+
return await runSizeGateConfirm(styles, reader, io);
|
|
525
|
+
} finally {
|
|
526
|
+
reader.close();
|
|
527
|
+
}
|
|
528
|
+
}
|
package/src/ui/tree.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// A pure tree renderer over box-drawing characters (U+2500–U+257F). Fourth
|
|
2
|
+
// member of the terminal surface's pure half (ui/primitives.ts formats, ui/select.ts
|
|
3
|
+
// asks, ui/result.ts reports, this one nests): same contract as all three —
|
|
4
|
+
// a total function of its inputs, with the style flag AND the spinner frame
|
|
5
|
+
// arriving as PARAMETERS so nothing here needs a TTY or a timer to be tested.
|
|
6
|
+
//
|
|
7
|
+
// WHY a component rather than glyphs sprinkled into one panel: three surfaces
|
|
8
|
+
// in this CLI show a parent with children — the live progress panel's
|
|
9
|
+
// refuter fan-out (its first consumer), the plan card's agent list, and the
|
|
10
|
+
// result block's root-cause clusters (`debug.root_causes.clusters` is already
|
|
11
|
+
// `RC001 -> [F001, F002]`). Only the first is wired today; the shape below
|
|
12
|
+
// deliberately assumes nothing about it.
|
|
13
|
+
//
|
|
14
|
+
// Deliberately self-contained, matching the rule ui/primitives.ts's own header states:
|
|
15
|
+
// the ANSI convention is COPIED here, not imported, so that progress.ts —
|
|
16
|
+
// which must stay independent of ui/primitives.ts — can consume this without acquiring
|
|
17
|
+
// that dependency transitively.
|
|
18
|
+
|
|
19
|
+
export type TreeStatus = "pending" | "running" | "done" | "failed";
|
|
20
|
+
|
|
21
|
+
export interface TreeNode {
|
|
22
|
+
label: string;
|
|
23
|
+
// Absent → no glyph column at all (a plain nested label).
|
|
24
|
+
status?: TreeStatus;
|
|
25
|
+
// Dim, right of the label. Caller-formatted: a caller aligning sub-columns
|
|
26
|
+
// pads them itself, and the string is emitted VERBATIM inside one dim wrap
|
|
27
|
+
// so those runs of spaces survive (the same rule ui/primitives.ts's row() documents).
|
|
28
|
+
detail?: string;
|
|
29
|
+
children?: TreeNode[];
|
|
30
|
+
// Caller's own decision to show a branch as a summary line. Honoured
|
|
31
|
+
// ALWAYS, and never silently: the hidden descendants are counted on the
|
|
32
|
+
// node's own line.
|
|
33
|
+
collapsed?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TreeOptions {
|
|
37
|
+
styles: boolean;
|
|
38
|
+
// The spinner character for every "running" node — data, not a timer, so a
|
|
39
|
+
// frame is reproducible in a test (renderPanelLines takes it the same way).
|
|
40
|
+
frame: string;
|
|
41
|
+
// Hard ceiling on the returned line count. The panel's redraw does cursor
|
|
42
|
+
// arithmetic against the number of lines it last drew, so a tree that grows
|
|
43
|
+
// past the terminal's height walks the cursor off the top and corrupts the
|
|
44
|
+
// screen. Absent → unbounded (a caller that owns its own space).
|
|
45
|
+
maxLines?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const GREEN = "\x1b[32m";
|
|
49
|
+
const RED = "\x1b[31m";
|
|
50
|
+
const DIM = "\x1b[2m";
|
|
51
|
+
const RESET = "\x1b[0m";
|
|
52
|
+
|
|
53
|
+
function paint(text: string, code: string, styles: boolean): string {
|
|
54
|
+
return styles ? `${code}${text}${RESET}` : text;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function glyph(status: TreeStatus, opts: TreeOptions): string {
|
|
58
|
+
switch (status) {
|
|
59
|
+
case "pending":
|
|
60
|
+
return paint("·", DIM, opts.styles);
|
|
61
|
+
case "running":
|
|
62
|
+
return opts.frame;
|
|
63
|
+
case "done":
|
|
64
|
+
return paint("✓", GREEN, opts.styles);
|
|
65
|
+
case "failed":
|
|
66
|
+
return paint("✗", RED, opts.styles);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// How many LINES a collapsed node stands in for — not how many nodes it
|
|
71
|
+
// holds. A child that is itself collapsed contributes its own line only, so
|
|
72
|
+
// the count matches what expanding this branch would actually put on screen.
|
|
73
|
+
function countDescendants(node: TreeNode): number {
|
|
74
|
+
let total = 0;
|
|
75
|
+
for (const child of node.children ?? []) {
|
|
76
|
+
total += 1 + (child.collapsed === true ? 0 : countDescendants(child));
|
|
77
|
+
}
|
|
78
|
+
return total;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// One node's own line. `hidden` > 0 appends the honest count of what this
|
|
82
|
+
// line stands in for — a collapsed branch that does not say so is a silent
|
|
83
|
+
// truncation of a run's history.
|
|
84
|
+
function nodeLine(
|
|
85
|
+
node: TreeNode,
|
|
86
|
+
prefix: string,
|
|
87
|
+
isLast: boolean,
|
|
88
|
+
hidden: number,
|
|
89
|
+
opts: TreeOptions,
|
|
90
|
+
): string {
|
|
91
|
+
const connector = paint(isLast ? "└─ " : "├─ ", DIM, opts.styles);
|
|
92
|
+
const head = node.status === undefined ? "" : `${glyph(node.status, opts)} `;
|
|
93
|
+
const detail =
|
|
94
|
+
node.detail === undefined || node.detail.length === 0
|
|
95
|
+
? ""
|
|
96
|
+
: ` ${paint(node.detail, DIM, opts.styles)}`;
|
|
97
|
+
const folded =
|
|
98
|
+
hidden === 0 ? "" : paint(` (+${hidden} hidden)`, DIM, opts.styles);
|
|
99
|
+
return `${prefix}${connector}${head}${node.label}${detail}${folded}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function walk(
|
|
103
|
+
nodes: TreeNode[],
|
|
104
|
+
prefix: string,
|
|
105
|
+
opts: TreeOptions,
|
|
106
|
+
folded: ReadonlySet<TreeNode>,
|
|
107
|
+
out: string[],
|
|
108
|
+
): void {
|
|
109
|
+
nodes.forEach((node, i) => {
|
|
110
|
+
const isLast = i === nodes.length - 1;
|
|
111
|
+
const collapsed = node.collapsed === true || folded.has(node);
|
|
112
|
+
const hiddenCount = collapsed ? countDescendants(node) : 0;
|
|
113
|
+
out.push(nodeLine(node, prefix, isLast, hiddenCount, opts));
|
|
114
|
+
if (collapsed) return;
|
|
115
|
+
walk(
|
|
116
|
+
node.children ?? [],
|
|
117
|
+
prefix + (isLast ? " " : paint("│ ", DIM, opts.styles)),
|
|
118
|
+
opts,
|
|
119
|
+
folded,
|
|
120
|
+
out,
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Completed branches, parents first and in document order — which is oldest
|
|
126
|
+
// first, so the height budget is paid by the part of the run that already
|
|
127
|
+
// settled while the part still moving stays expanded.
|
|
128
|
+
function collapseCandidates(nodes: TreeNode[], into: TreeNode[]): TreeNode[] {
|
|
129
|
+
for (const node of nodes) {
|
|
130
|
+
const done = node.status === "done" || node.status === "failed";
|
|
131
|
+
if (done && (node.children?.length ?? 0) > 0 && node.collapsed !== true) {
|
|
132
|
+
into.push(node);
|
|
133
|
+
}
|
|
134
|
+
collapseCandidates(node.children ?? [], into);
|
|
135
|
+
}
|
|
136
|
+
return into;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Renders the tree, then makes it fit — in one place, by ONE mechanism, so
|
|
140
|
+
// "collapse a finished branch" and "the terminal is too short" are the same
|
|
141
|
+
// operation and cannot drift apart:
|
|
142
|
+
// 1. the full tree, when it fits (and always when maxLines is absent);
|
|
143
|
+
// 2. completed branches collapsed to their summary line, oldest first,
|
|
144
|
+
// each stating how many lines it stands for;
|
|
145
|
+
// 3. still too tall → the MIDDLE elided, keeping the head (which rows
|
|
146
|
+
// exist) and biased toward the tail (what just happened), with the
|
|
147
|
+
// hidden count on its own line.
|
|
148
|
+
// What it never does is truncate quietly: every dropped line is counted
|
|
149
|
+
// somewhere in the output.
|
|
150
|
+
export function renderTree(nodes: TreeNode[], opts: TreeOptions): string[] {
|
|
151
|
+
const render = (folded: ReadonlySet<TreeNode>): string[] => {
|
|
152
|
+
const out: string[] = [];
|
|
153
|
+
walk(nodes, "", opts, folded, out);
|
|
154
|
+
return out;
|
|
155
|
+
};
|
|
156
|
+
const max = opts.maxLines;
|
|
157
|
+
let lines = render(new Set());
|
|
158
|
+
if (max === undefined || lines.length <= max) return lines;
|
|
159
|
+
if (max <= 0) return [];
|
|
160
|
+
const folded = new Set<TreeNode>();
|
|
161
|
+
for (const candidate of collapseCandidates(nodes, [])) {
|
|
162
|
+
folded.add(candidate);
|
|
163
|
+
lines = render(folded);
|
|
164
|
+
if (lines.length <= max) return lines;
|
|
165
|
+
}
|
|
166
|
+
// The elision line costs one of the budget, so head + tail share max - 1.
|
|
167
|
+
const keep = max - 1;
|
|
168
|
+
const hidden = lines.length - keep;
|
|
169
|
+
const note = paint(`⋯ ${hidden} lines hidden`, DIM, opts.styles);
|
|
170
|
+
if (keep <= 0) return [note];
|
|
171
|
+
const headCount = Math.max(Math.floor(keep / 3), 1);
|
|
172
|
+
const tailCount = keep - headCount;
|
|
173
|
+
return [
|
|
174
|
+
...lines.slice(0, headCount),
|
|
175
|
+
note,
|
|
176
|
+
...(tailCount > 0 ? lines.slice(lines.length - tailCount) : []),
|
|
177
|
+
];
|
|
178
|
+
}
|