@carrieros/cli-staging 0.0.0-stage → 0.4.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/README.md +6 -2
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +182 -0
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +119 -0
- package/dist/commands/files.js +94 -0
- package/dist/commands/help.js +37 -0
- package/dist/commands/history.js +112 -0
- package/dist/commands/login.js +62 -0
- package/dist/commands/logout.js +26 -0
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +64 -0
- package/dist/commands/models.js +28 -0
- package/dist/commands/modes.js +30 -0
- package/dist/commands/permissions.js +78 -0
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +155 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +282 -0
- package/dist/commands/settings.js +160 -0
- package/dist/commands/update.js +285 -0
- package/dist/commands/upload.js +223 -0
- package/dist/commands/usage.js +229 -0
- package/dist/commands/version.js +26 -0
- package/dist/commands/whoami.js +44 -0
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +364 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +132 -0
- package/dist/services/api.js +441 -0
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/auto-update.js +221 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +356 -0
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +273 -0
- package/dist/services/command-policy.js +434 -0
- package/dist/services/config.js +384 -0
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +416 -0
- package/dist/services/device-auth.js +288 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +429 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +85 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +328 -0
- package/dist/services/local-files.js +639 -0
- package/dist/services/local-tools.js +937 -0
- package/dist/services/login-flow.js +156 -0
- package/dist/services/meta.js +42 -0
- package/dist/services/mutation-coordinator.js +327 -0
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +679 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +62 -0
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +78 -0
- package/dist/services/process-runner.js +412 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +217 -0
- package/dist/services/session-resolver.js +115 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +40 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +730 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1622 -0
- package/dist/shell/shell-commands.js +411 -0
- package/dist/shell/shell-prompt.js +110 -0
- package/dist/shell/shell-renderer.js +51 -0
- package/dist/shell/shell-router.js +37 -0
- package/dist/shell/shell.js +414 -0
- package/dist/shell/task-context-view.js +40 -0
- package/dist/staging.js +5 -0
- package/dist/types/index.js +6 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +111 -0
- package/dist/ui/banner.js +56 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +463 -0
- package/dist/ui/code-block.js +52 -0
- package/dist/ui/color-picker.js +100 -0
- package/dist/ui/colors.js +55 -0
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +92 -0
- package/dist/ui/command-item.js +6 -0
- package/dist/ui/command-palette.js +383 -0
- package/dist/ui/command-registry.js +128 -0
- package/dist/ui/command-renderer.js +55 -0
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +74 -0
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +141 -0
- package/dist/ui/logo.js +58 -0
- package/dist/ui/markdown.js +205 -0
- package/dist/ui/message.js +21 -0
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +297 -0
- package/dist/ui/select.js +284 -0
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/spinner.js +23 -0
- package/dist/ui/sticky-stream.js +64 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +80 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +101 -0
- package/package.json +33 -4
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { clampAnsi, wrapAnsi } from "./composer-layout.js";
|
|
2
|
+
/**
|
|
3
|
+
* Persistent bottom-composer TUI engine (Claude Code–style).
|
|
4
|
+
*
|
|
5
|
+
* The terminal is a managed CANVAS, not a stream of `console.log()`s. We enter
|
|
6
|
+
* the ALTERNATE SCREEN BUFFER so the app owns the whole viewport and never
|
|
7
|
+
* pollutes the user's real scrollback — leaving TUI mode restores their terminal
|
|
8
|
+
* exactly as it was. The screen is split into two permanent regions:
|
|
9
|
+
*
|
|
10
|
+
* ┌───────────────────────────────┐
|
|
11
|
+
* │ transcript (our OWN scroll) │ history · activity · streaming · output
|
|
12
|
+
* │ … │
|
|
13
|
+
* ├───────────────────────────────┤
|
|
14
|
+
* │ ❯ Ask Carrier OS… │ the FIXED composer — never moves/scrolls
|
|
15
|
+
* └───────────────────────────────┘
|
|
16
|
+
*
|
|
17
|
+
* A virtual screen buffer is diffed against what's on-screen, so each frame only
|
|
18
|
+
* rewrites the rows that actually changed — no full repaints, no flicker. All
|
|
19
|
+
* positioning is ABSOLUTE (no DECSTBM scroll regions), so it behaves identically
|
|
20
|
+
* across Windows Terminal, PowerShell, CMD, Git Bash, macOS Terminal and Linux.
|
|
21
|
+
*
|
|
22
|
+
* Scrolling is OURS: the transcript is a virtualized window over an in-memory
|
|
23
|
+
* line buffer. `scrollOffset` counts lines up from the bottom; 0 = following the
|
|
24
|
+
* newest output ("pinned"). While pinned, new/streaming output stays in view;
|
|
25
|
+
* once the user scrolls up we HOLD their position as content grows below, so you
|
|
26
|
+
* can browse history while a reply is still generating.
|
|
27
|
+
*/
|
|
28
|
+
const out = process.stdout;
|
|
29
|
+
const ESC = "\x1b[";
|
|
30
|
+
const HIDE = `${ESC}?25l`;
|
|
31
|
+
const SHOW = `${ESC}?25h`;
|
|
32
|
+
const ALT_ON = `${ESC}?1049h`; // enter alternate screen buffer (own the canvas)
|
|
33
|
+
const ALT_OFF = `${ESC}?1049l`; // leave it → restores the user's real terminal
|
|
34
|
+
// Alternate scroll mode: in the alt-screen the terminal converts the mouse WHEEL
|
|
35
|
+
// into arrow-key presses sent to the app. This is what makes the wheel work in
|
|
36
|
+
// terminals (notably VS Code's) that DON'T forward SGR mouse reports to apps —
|
|
37
|
+
// the wheel arrives as ↑/↓ keys, which we map to scrolling (see command-palette).
|
|
38
|
+
const SCROLL_ON = `${ESC}?1007h`;
|
|
39
|
+
const SCROLL_OFF = `${ESC}?1007l`;
|
|
40
|
+
/** Fixed composer height: input rule / prompt / rule + one footer row. */
|
|
41
|
+
const CORE_H = 4;
|
|
42
|
+
/** Cap on retained transcript lines (we repaint, not native-scroll). */
|
|
43
|
+
const MAX_SCROLLBACK = 5000;
|
|
44
|
+
let active = false;
|
|
45
|
+
let committed = []; // permanent transcript lines
|
|
46
|
+
let transient = []; // in-progress tail (spinner / streaming), below `committed`
|
|
47
|
+
let task = []; // existing task review projection, pinned while active
|
|
48
|
+
let palette = []; // slash-palette overlay (empty when closed)
|
|
49
|
+
let bar = []; // the fixed composer core (CORE_H lines)
|
|
50
|
+
let caretBarIndex = 1; // which composer row the caret sits on (1 = prompt line)
|
|
51
|
+
let caretCol = 1; // 1-based column of the caret
|
|
52
|
+
let showCaret = false;
|
|
53
|
+
// Scroll state — see file header. `pinned` ⇒ auto-follow the newest output.
|
|
54
|
+
let scrollOffset = 0;
|
|
55
|
+
let pinned = true;
|
|
56
|
+
let shown = []; // what is currently on each terminal row (0 = row 1)
|
|
57
|
+
const rows = () => out.rows ?? 24;
|
|
58
|
+
const write = (s) => {
|
|
59
|
+
out.write(s);
|
|
60
|
+
};
|
|
61
|
+
export function screenActive() {
|
|
62
|
+
return active;
|
|
63
|
+
}
|
|
64
|
+
/** Layout metrics for the current frame (region sizes + the full line buffer). */
|
|
65
|
+
function metrics() {
|
|
66
|
+
const h = rows();
|
|
67
|
+
const core = bar.length || CORE_H;
|
|
68
|
+
const tb = Math.max(1, h - core); // transcript occupies rows 1..tb
|
|
69
|
+
const pH = Math.min(palette.length, Math.max(0, tb - 1)); // overlay never covers the whole region
|
|
70
|
+
const taskH = Math.min(task.length, Math.max(0, Math.floor((tb - pH) / 2)));
|
|
71
|
+
const contentH = Math.max(0, tb - pH - taskH); // transcript rows above the palette overlay
|
|
72
|
+
const all = (transient.length ? committed.concat(transient) : committed).flatMap(line => wrapAnsi(line, Math.max(1, (out.columns ?? 80) - 1)));
|
|
73
|
+
const maxOffset = Math.max(0, all.length - contentH);
|
|
74
|
+
return { h, core, tb, pH, taskH, contentH, all, maxOffset };
|
|
75
|
+
}
|
|
76
|
+
/** Clamp the scroll offset into range and recompute the pinned flag. */
|
|
77
|
+
function clampScroll() {
|
|
78
|
+
const { maxOffset } = metrics();
|
|
79
|
+
if (scrollOffset < 0)
|
|
80
|
+
scrollOffset = 0;
|
|
81
|
+
if (scrollOffset > maxOffset)
|
|
82
|
+
scrollOffset = maxOffset;
|
|
83
|
+
pinned = scrollOffset === 0;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* When content is appended below while the user is scrolled up, grow the offset
|
|
87
|
+
* by the number of added lines so their view stays put (Claude-Code behaviour).
|
|
88
|
+
* When pinned, do nothing — flush() will render the newest tail.
|
|
89
|
+
*/
|
|
90
|
+
function onContentGrew(added) {
|
|
91
|
+
if (!pinned && added > 0) {
|
|
92
|
+
scrollOffset += added;
|
|
93
|
+
const { maxOffset } = metrics();
|
|
94
|
+
if (scrollOffset > maxOffset)
|
|
95
|
+
scrollOffset = maxOffset;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Build the full desired screen: [transcript window][palette][composer core]. */
|
|
99
|
+
function compose() {
|
|
100
|
+
const { h, tb, pH, taskH, contentH, all, maxOffset } = metrics();
|
|
101
|
+
if (scrollOffset > maxOffset)
|
|
102
|
+
scrollOffset = maxOffset; // stay valid across resizes
|
|
103
|
+
// The visible window into `all`. offset 0 = last `contentH` lines (the tail);
|
|
104
|
+
// offset maxOffset = the very top.
|
|
105
|
+
const end = all.length - scrollOffset;
|
|
106
|
+
const start = Math.max(0, end - contentH);
|
|
107
|
+
const win = all.slice(start, end);
|
|
108
|
+
const desired = new Array(h).fill("");
|
|
109
|
+
for (let i = 0; i < taskH; i++)
|
|
110
|
+
desired[i] = task[i] ?? "";
|
|
111
|
+
for (let i = 0; i < contentH; i++)
|
|
112
|
+
desired[taskH + i] = win[i] ?? "";
|
|
113
|
+
// A subtle "more below" hint on the last transcript row when scrolled up.
|
|
114
|
+
if (scrollOffset > 0 && contentH > 0) {
|
|
115
|
+
desired[taskH + contentH - 1] = `${ESC}2m ↓ ${scrollOffset} more line${scrollOffset === 1 ? "" : "s"} - End to jump to latest${ESC}0m`;
|
|
116
|
+
}
|
|
117
|
+
for (let i = 0; i < pH; i++)
|
|
118
|
+
desired[taskH + contentH + i] = palette[i] ?? "";
|
|
119
|
+
for (let i = 0; i < (bar.length || CORE_H); i++)
|
|
120
|
+
desired[tb + i] = bar[i] ?? "";
|
|
121
|
+
return desired.map(line => clampAnsi(line, Math.max(1, (out.columns ?? 80) - 1)));
|
|
122
|
+
}
|
|
123
|
+
/** Diff the desired screen against what's on-screen; rewrite only changed rows. */
|
|
124
|
+
function flush() {
|
|
125
|
+
if (!active)
|
|
126
|
+
return;
|
|
127
|
+
const desired = compose();
|
|
128
|
+
const n = Math.max(desired.length, shown.length);
|
|
129
|
+
let s = HIDE;
|
|
130
|
+
for (let r = 0; r < n; r++) {
|
|
131
|
+
const d = desired[r] ?? "";
|
|
132
|
+
if (d !== (shown[r] ?? ""))
|
|
133
|
+
s += `${ESC}${r + 1};1H${ESC}2K` + d;
|
|
134
|
+
}
|
|
135
|
+
shown = desired;
|
|
136
|
+
const core = bar.length || CORE_H;
|
|
137
|
+
const caretRow = rows() - core + 1 + caretBarIndex;
|
|
138
|
+
// The composer is a permanent region, so its caret shows whenever the composer
|
|
139
|
+
// is active — independent of where the transcript is scrolled.
|
|
140
|
+
s += showCaret ? `${ESC}${caretRow};${Math.min(caretCol, out.columns ?? 80)}H${SHOW}` : HIDE;
|
|
141
|
+
write(s);
|
|
142
|
+
}
|
|
143
|
+
/** Enter TUI mode: alt-screen, alt-scroll (wheel→arrows), clear, hide caret. */
|
|
144
|
+
export function startScreen() {
|
|
145
|
+
if (!out.isTTY || process.env.TERM === "dumb")
|
|
146
|
+
return;
|
|
147
|
+
active = true;
|
|
148
|
+
committed = [];
|
|
149
|
+
task = [];
|
|
150
|
+
transient = [];
|
|
151
|
+
palette = [];
|
|
152
|
+
bar = [];
|
|
153
|
+
shown = [];
|
|
154
|
+
scrollOffset = 0;
|
|
155
|
+
pinned = true;
|
|
156
|
+
write(`${ALT_ON}${SCROLL_ON}${ESC}2J${ESC}H${HIDE}`);
|
|
157
|
+
flush();
|
|
158
|
+
out.on("resize", screenResize);
|
|
159
|
+
}
|
|
160
|
+
/** Leave TUI mode: disable alt-scroll, leave alt-screen (restores real terminal). */
|
|
161
|
+
export function stopScreen() {
|
|
162
|
+
if (!active)
|
|
163
|
+
return;
|
|
164
|
+
active = false;
|
|
165
|
+
out.removeListener("resize", screenResize);
|
|
166
|
+
write(`${SCROLL_OFF}${ALT_OFF}${SHOW}`);
|
|
167
|
+
shown = [];
|
|
168
|
+
}
|
|
169
|
+
/** Trim the transcript scrollback to its cap (drop oldest lines). */
|
|
170
|
+
function trim() {
|
|
171
|
+
if (committed.length > MAX_SCROLLBACK)
|
|
172
|
+
committed.splice(0, committed.length - MAX_SCROLLBACK);
|
|
173
|
+
}
|
|
174
|
+
/** Append permanent transcript lines (multi-line strings are split). */
|
|
175
|
+
export function screenPrint(lines) {
|
|
176
|
+
let added = 0;
|
|
177
|
+
for (const l of lines) {
|
|
178
|
+
const parts = l.includes("\n") ? l.split("\n") : [l];
|
|
179
|
+
committed.push(...parts);
|
|
180
|
+
added += parts.length;
|
|
181
|
+
}
|
|
182
|
+
trim();
|
|
183
|
+
onContentGrew(added);
|
|
184
|
+
flush();
|
|
185
|
+
}
|
|
186
|
+
/** Set the in-progress tail shown just below the committed transcript. */
|
|
187
|
+
export function screenTransient(lines) {
|
|
188
|
+
const prev = transient.length;
|
|
189
|
+
transient = lines;
|
|
190
|
+
onContentGrew(transient.length - prev);
|
|
191
|
+
flush();
|
|
192
|
+
}
|
|
193
|
+
/** Promote the current transient tail to permanent transcript (no visual jump). */
|
|
194
|
+
export function screenCommitTransient() {
|
|
195
|
+
if (transient.length) {
|
|
196
|
+
committed.push(...transient);
|
|
197
|
+
transient = [];
|
|
198
|
+
trim();
|
|
199
|
+
}
|
|
200
|
+
flush();
|
|
201
|
+
}
|
|
202
|
+
/** Clear the whole transcript (Ctrl+L) and re-pin to the bottom. */
|
|
203
|
+
export function screenClear() {
|
|
204
|
+
committed = [];
|
|
205
|
+
transient = [];
|
|
206
|
+
shown = [];
|
|
207
|
+
scrollOffset = 0;
|
|
208
|
+
pinned = true;
|
|
209
|
+
write(`${ESC}2J`);
|
|
210
|
+
flush();
|
|
211
|
+
}
|
|
212
|
+
// ── Scroll API (keyboard + wheel) ─────────────────────────────────────────────
|
|
213
|
+
/** Scroll by `delta` lines: +up (older), −down (newer). Re-pins at the bottom. */
|
|
214
|
+
export function screenScrollLines(delta) {
|
|
215
|
+
if (!active)
|
|
216
|
+
return;
|
|
217
|
+
scrollOffset += delta;
|
|
218
|
+
clampScroll();
|
|
219
|
+
flush();
|
|
220
|
+
}
|
|
221
|
+
/** Scroll a (near-)page in the given direction: +1 up, −1 down. */
|
|
222
|
+
export function screenScrollPage(dir) {
|
|
223
|
+
const { contentH } = metrics();
|
|
224
|
+
screenScrollLines(dir * Math.max(1, contentH - 1));
|
|
225
|
+
}
|
|
226
|
+
/** Jump to the oldest content. */
|
|
227
|
+
export function screenScrollToTop() {
|
|
228
|
+
if (!active)
|
|
229
|
+
return;
|
|
230
|
+
const { maxOffset } = metrics();
|
|
231
|
+
scrollOffset = maxOffset;
|
|
232
|
+
pinned = maxOffset === 0;
|
|
233
|
+
flush();
|
|
234
|
+
}
|
|
235
|
+
/** Jump to the newest content and re-pin (auto-follow). */
|
|
236
|
+
export function screenScrollToBottom() {
|
|
237
|
+
if (!active)
|
|
238
|
+
return;
|
|
239
|
+
scrollOffset = 0;
|
|
240
|
+
pinned = true;
|
|
241
|
+
flush();
|
|
242
|
+
}
|
|
243
|
+
/** True when following the newest output (used to gate auto-follow decisions). */
|
|
244
|
+
export function screenIsPinned() {
|
|
245
|
+
return pinned;
|
|
246
|
+
}
|
|
247
|
+
/** True when the transcript is taller than the viewport (i.e. can be scrolled). */
|
|
248
|
+
export function screenScrollable() {
|
|
249
|
+
return metrics().maxOffset > 0;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Draw the composer: the fixed input `core` (CORE_H lines) at the bottom, with
|
|
253
|
+
* the `palette` overlaying the transcript rows directly above it. `caretIndex`
|
|
254
|
+
* is the core row the caret sits on (1 = prompt); `col` is its 1-based column.
|
|
255
|
+
*/
|
|
256
|
+
export function screenComposer(paletteLines, coreLines, caretIndex, col) {
|
|
257
|
+
palette = paletteLines;
|
|
258
|
+
bar = coreLines;
|
|
259
|
+
caretBarIndex = caretIndex;
|
|
260
|
+
caretCol = col;
|
|
261
|
+
showCaret = true;
|
|
262
|
+
flush();
|
|
263
|
+
}
|
|
264
|
+
/** Hide the caret (e.g. while a response streams — the ▋ is drawn in the text). */
|
|
265
|
+
export function screenHideCaret() {
|
|
266
|
+
showCaret = false;
|
|
267
|
+
flush();
|
|
268
|
+
}
|
|
269
|
+
/** Recompute + full-repaint after a terminal resize (composer stays pinned). */
|
|
270
|
+
export function screenResize() {
|
|
271
|
+
if (!active)
|
|
272
|
+
return;
|
|
273
|
+
shown = []; // force every row to be rewritten at the new size
|
|
274
|
+
clampScroll();
|
|
275
|
+
write(`${ESC}2J`);
|
|
276
|
+
flush();
|
|
277
|
+
}
|
|
278
|
+
/** Temporarily hand the terminal to an external UI (inquirer picker, etc.). */
|
|
279
|
+
export function suspendScreen() {
|
|
280
|
+
if (!active)
|
|
281
|
+
return;
|
|
282
|
+
active = false;
|
|
283
|
+
out.removeListener("resize", screenResize);
|
|
284
|
+
write(`${SCROLL_OFF}${ALT_OFF}${SHOW}`);
|
|
285
|
+
}
|
|
286
|
+
/** Resume TUI mode after suspendScreen(), repainting the preserved transcript. */
|
|
287
|
+
export function resumeScreen() {
|
|
288
|
+
if (!out.isTTY || process.env.TERM === "dumb")
|
|
289
|
+
return;
|
|
290
|
+
active = true;
|
|
291
|
+
shown = [];
|
|
292
|
+
write(`${ALT_ON}${SCROLL_ON}${ESC}2J${ESC}H${HIDE}`);
|
|
293
|
+
flush();
|
|
294
|
+
out.on("resize", screenResize);
|
|
295
|
+
}
|
|
296
|
+
/** Pin task evidence without changing transcript, approval or keyboard ownership. */
|
|
297
|
+
export function screenTask(lines) { task = lines.slice(0, 24); clampScroll(); flush(); }
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import * as readline from "node:readline";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
|
+
import { resumeScreen, screenActive, suspendScreen } from "./screen.js";
|
|
4
|
+
import { termWidth } from "./ui.js";
|
|
5
|
+
/** Resolved when the user pressed ← on a picker opened with `allowBack`. */
|
|
6
|
+
export const SELECT_BACK = "\u0000back";
|
|
7
|
+
/**
|
|
8
|
+
* One painted frame of the picker, clipped to the terminal.
|
|
9
|
+
*
|
|
10
|
+
* Split out because the same frame is drawn two ways: straight to the terminal
|
|
11
|
+
* (below), or into the shell's transcript via the screen engine — which is what
|
|
12
|
+
* keeps the banner and conversation on screen while a picker is open.
|
|
13
|
+
*
|
|
14
|
+
* Rows are hard-clipped rather than wrapped: callers count LOGICAL lines to
|
|
15
|
+
* rewind or size a region, so a row the terminal wraps would occupy two
|
|
16
|
+
* physical rows and leave half the previous frame behind.
|
|
17
|
+
*/
|
|
18
|
+
export function selectFrame(opts, selected) {
|
|
19
|
+
const width = Math.min(termWidth() - 4, 92);
|
|
20
|
+
const build = (descriptionsFor) => {
|
|
21
|
+
const lines = headerLines(opts, width);
|
|
22
|
+
lines.push("");
|
|
23
|
+
lines.push(...optionLines(opts, selected, width, descriptionsFor));
|
|
24
|
+
lines.push("");
|
|
25
|
+
lines.push(...hintLines(opts, width));
|
|
26
|
+
return lines;
|
|
27
|
+
};
|
|
28
|
+
// Full descriptions cost 2-3 rows each, which on a short terminal would push
|
|
29
|
+
// the conversation off screen entirely — so fall back to describing only the
|
|
30
|
+
// highlighted option, which is still readable in full as you arrow through.
|
|
31
|
+
let lines = build("all");
|
|
32
|
+
if (opts.stacked && lines.length > roomForPrompt())
|
|
33
|
+
lines = build("selected");
|
|
34
|
+
const cols = termWidth();
|
|
35
|
+
return lines.map((l) => (visibleWidth(l) > cols ? clipAnsi(l, cols) : l));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Transcript rows a prompt may occupy — terminal height minus the 4-row
|
|
39
|
+
* composer, minus one so the frame never sits flush against it.
|
|
40
|
+
*
|
|
41
|
+
* Deliberately not reserving space for the conversation: the descriptions are
|
|
42
|
+
* the reason the prompt exists, and PgUp/PgDn can reach anything they cover.
|
|
43
|
+
*/
|
|
44
|
+
function roomForPrompt() {
|
|
45
|
+
return Math.max(8, (process.stdout.rows ?? 24) - 5);
|
|
46
|
+
}
|
|
47
|
+
function headerLines(opts, width) {
|
|
48
|
+
if (opts.header)
|
|
49
|
+
return opts.header(width);
|
|
50
|
+
const lines = [" " + c.brand("─".repeat(width)), " " + c.brand.bold(opts.title)];
|
|
51
|
+
if (opts.description) {
|
|
52
|
+
for (const l of wrap(opts.description, width))
|
|
53
|
+
lines.push(" " + c.dim(l));
|
|
54
|
+
}
|
|
55
|
+
return lines;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The key legend, packed into as many lines as it needs.
|
|
59
|
+
*
|
|
60
|
+
* Wrapping rather than clipping: the last chip is what Esc does, and on this
|
|
61
|
+
* prompt that is "discard message" — the one hint you cannot afford to lose to
|
|
62
|
+
* a narrow terminal.
|
|
63
|
+
*
|
|
64
|
+
* ←→ rather than ⇄: it names both keys instead of half the pair, and it is the
|
|
65
|
+
* same arrow family as ↑↓ (U+2190/2192 beside U+2191/2193), so the two chips
|
|
66
|
+
* match in weight. ⇄ falls back to a different font in many terminals and
|
|
67
|
+
* rendered noticeably heavier than the arrows above it.
|
|
68
|
+
*/
|
|
69
|
+
function hintLines(opts, width) {
|
|
70
|
+
const chips = [c.dim("↑↓ navigate")];
|
|
71
|
+
if (opts.allowBack)
|
|
72
|
+
chips.push(c.key("←→") + c.dim(` ${opts.backHint ?? "back"}`));
|
|
73
|
+
if (opts.scrollable)
|
|
74
|
+
chips.push(c.key("PgUp/PgDn") + c.dim(" scroll"));
|
|
75
|
+
chips.push(c.key("Enter") + c.dim(" confirm"));
|
|
76
|
+
chips.push(c.key("Esc") + c.dim(opts.cancelHint ? ` ${opts.cancelHint}` : " cancel"));
|
|
77
|
+
const SEP = " · ";
|
|
78
|
+
const lines = [];
|
|
79
|
+
let cur = "";
|
|
80
|
+
let curW = 0;
|
|
81
|
+
for (const chip of chips) {
|
|
82
|
+
const w = visibleWidth(chip);
|
|
83
|
+
if (cur && curW + SEP.length + w > width) {
|
|
84
|
+
lines.push(" " + cur);
|
|
85
|
+
cur = chip;
|
|
86
|
+
curW = w;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
cur = cur ? cur + c.dim(SEP) + chip : chip;
|
|
90
|
+
curW = curW ? curW + SEP.length + w : w;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (cur)
|
|
94
|
+
lines.push(" " + cur);
|
|
95
|
+
return lines;
|
|
96
|
+
}
|
|
97
|
+
function optionLines(opts, selected, width, descriptionsFor) {
|
|
98
|
+
const { choices } = opts;
|
|
99
|
+
const numW = String(choices.length).length;
|
|
100
|
+
const lines = [];
|
|
101
|
+
if (!opts.stacked) {
|
|
102
|
+
// Column layout: label padded to a shared width, description clipped to fit.
|
|
103
|
+
const labelW = Math.min(30, Math.max(...choices.map((ch) => ch.label.length)));
|
|
104
|
+
const gutter = 2 + numW + 1 + 1 + labelW + 2;
|
|
105
|
+
choices.forEach((ch, i) => {
|
|
106
|
+
const on = i === selected;
|
|
107
|
+
const pointer = on ? c.brand("❯ ") : " ";
|
|
108
|
+
const num = c.dim(`${String(i + 1).padStart(numW)}.`);
|
|
109
|
+
const label = (on ? c.brand.bold : c.value)(ch.label.padEnd(labelW));
|
|
110
|
+
const mark = ch.current ? c.brand(" ✓") : "";
|
|
111
|
+
const room = width - gutter - (ch.current ? 2 : 0);
|
|
112
|
+
const desc = ch.description ? " " + c.dim(clip(ch.description, room)) : "";
|
|
113
|
+
lines.push(" " + pointer + num + " " + label + desc + mark);
|
|
114
|
+
});
|
|
115
|
+
return lines;
|
|
116
|
+
}
|
|
117
|
+
// Stacked layout: label on its own row, description wrapped beneath it and
|
|
118
|
+
// aligned under the label rather than the number.
|
|
119
|
+
const indent = " ".repeat(2 + 2 + numW + 1 + 1);
|
|
120
|
+
choices.forEach((ch, i) => {
|
|
121
|
+
const on = i === selected;
|
|
122
|
+
const pointer = on ? c.brand("❯ ") : " ";
|
|
123
|
+
const num = c.dim(`${String(i + 1).padStart(numW)}.`);
|
|
124
|
+
const label = (on ? c.brand.bold : c.value)(ch.label);
|
|
125
|
+
const mark = ch.current ? c.brand(" ✓") : "";
|
|
126
|
+
lines.push(" " + pointer + num + " " + label + mark);
|
|
127
|
+
const show = ch.description && (descriptionsFor === "all" || on);
|
|
128
|
+
if (show) {
|
|
129
|
+
// Two lines is the most a description needs; beyond that it is prose the
|
|
130
|
+
// list can't carry, and every extra row is one less of the conversation.
|
|
131
|
+
const wrapped = wrap(ch.description, width - indent.length + 2);
|
|
132
|
+
const shown = wrapped.slice(0, 2);
|
|
133
|
+
if (wrapped.length > 2)
|
|
134
|
+
shown[1] = shown[1].replace(/.{0,2}$/, "…");
|
|
135
|
+
for (const l of shown)
|
|
136
|
+
lines.push(indent + c.dim(l));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return lines;
|
|
140
|
+
}
|
|
141
|
+
export function pickFromList(opts) {
|
|
142
|
+
const { choices, confirmOnNumber = false } = opts;
|
|
143
|
+
if (!process.stdin.isTTY || choices.length === 0) {
|
|
144
|
+
return Promise.resolve(choices[opts.initial ?? 0]?.value ?? null);
|
|
145
|
+
}
|
|
146
|
+
return new Promise((resolve) => {
|
|
147
|
+
const stdin = process.stdin;
|
|
148
|
+
const stdout = process.stdout;
|
|
149
|
+
// Hand the terminal over from the persistent-composer engine for the picker,
|
|
150
|
+
// then repaint the transcript + composer when it closes.
|
|
151
|
+
const suspended = screenActive();
|
|
152
|
+
if (suspended)
|
|
153
|
+
suspendScreen();
|
|
154
|
+
stdin.removeAllListeners("keypress");
|
|
155
|
+
readline.emitKeypressEvents(stdin);
|
|
156
|
+
const prevRaw = stdin.isRaw ?? false;
|
|
157
|
+
// A one-shot command (e.g. the trust prompt before `cos files`) must not
|
|
158
|
+
// leave stdin flowing behind it, or the finished command never exits.
|
|
159
|
+
// (isPaused() is false for a stream never resumed, so test flowing state.)
|
|
160
|
+
const wasPaused = stdin.readableFlowing !== true;
|
|
161
|
+
if (stdin.isTTY)
|
|
162
|
+
stdin.setRawMode(true);
|
|
163
|
+
stdin.resume();
|
|
164
|
+
let selected = Math.min(Math.max(0, opts.initial ?? 0), choices.length - 1);
|
|
165
|
+
let prevUp = 0;
|
|
166
|
+
const render = () => {
|
|
167
|
+
if (prevUp > 0)
|
|
168
|
+
readline.moveCursor(stdout, 0, -prevUp);
|
|
169
|
+
readline.cursorTo(stdout, 0);
|
|
170
|
+
readline.clearScreenDown(stdout);
|
|
171
|
+
const painted = selectFrame(opts, selected);
|
|
172
|
+
stdout.write(painted.join("\n"));
|
|
173
|
+
prevUp = painted.length - 1; // cursor rests on the last line; rewind from there next time
|
|
174
|
+
};
|
|
175
|
+
const finish = (val) => {
|
|
176
|
+
if (prevUp > 0)
|
|
177
|
+
readline.moveCursor(stdout, 0, -prevUp);
|
|
178
|
+
readline.cursorTo(stdout, 0);
|
|
179
|
+
readline.clearScreenDown(stdout);
|
|
180
|
+
stdin.removeListener("keypress", onKey);
|
|
181
|
+
stdin.setRawMode(prevRaw);
|
|
182
|
+
if (wasPaused)
|
|
183
|
+
stdin.pause();
|
|
184
|
+
if (suspended)
|
|
185
|
+
resumeScreen(); // repaint the pinned composer + transcript
|
|
186
|
+
resolve(val);
|
|
187
|
+
};
|
|
188
|
+
const onKey = (str, key) => {
|
|
189
|
+
try {
|
|
190
|
+
if (!key)
|
|
191
|
+
return;
|
|
192
|
+
if (key.name === "up" || (key.ctrl && key.name === "p")) {
|
|
193
|
+
selected = (selected - 1 + choices.length) % choices.length;
|
|
194
|
+
return render();
|
|
195
|
+
}
|
|
196
|
+
if (key.name === "down" || (key.ctrl && key.name === "n")) {
|
|
197
|
+
selected = (selected + 1) % choices.length;
|
|
198
|
+
return render();
|
|
199
|
+
}
|
|
200
|
+
// ← steps back a question, → confirms and moves on (same as Enter), so
|
|
201
|
+
// a multi-step flow reads as one horizontal track.
|
|
202
|
+
if (opts.allowBack && key.name === "left")
|
|
203
|
+
return finish(SELECT_BACK);
|
|
204
|
+
if (key.name === "right")
|
|
205
|
+
return finish(choices[selected].value);
|
|
206
|
+
if (key.name === "return" || key.name === "enter")
|
|
207
|
+
return finish(choices[selected].value);
|
|
208
|
+
if (key.name === "escape" || (key.ctrl && (key.name === "c" || key.name === "d"))) {
|
|
209
|
+
return finish(null);
|
|
210
|
+
}
|
|
211
|
+
// Number keys jump to an option — and confirm it directly when the
|
|
212
|
+
// caller asked for that (Yes/No style prompts).
|
|
213
|
+
if (str && /^[1-9]$/.test(str)) {
|
|
214
|
+
const idx = Number(str) - 1;
|
|
215
|
+
if (idx < choices.length) {
|
|
216
|
+
selected = idx;
|
|
217
|
+
if (confirmOnNumber)
|
|
218
|
+
return finish(choices[idx].value);
|
|
219
|
+
render();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
stdout.write(`\n${c.err("Select error:")} ${err.message}\n`);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
stdin.on("keypress", onKey);
|
|
228
|
+
render();
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const ANSI_RE = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-9;]*m/g;
|
|
232
|
+
/** Visible columns, ignoring colour codes. */
|
|
233
|
+
function visibleWidth(s) {
|
|
234
|
+
return [...s.replace(ANSI_RE, "")].length;
|
|
235
|
+
}
|
|
236
|
+
/** Truncate to `max` visible columns, keeping colour codes intact and closing
|
|
237
|
+
* any open run so the cut can't bleed into the next line. */
|
|
238
|
+
function clipAnsi(s, max) {
|
|
239
|
+
let out = "";
|
|
240
|
+
let seen = 0;
|
|
241
|
+
let i = 0;
|
|
242
|
+
while (i < s.length && seen < max) {
|
|
243
|
+
if (s[i] === "\x1b") {
|
|
244
|
+
ANSI_RE.lastIndex = i;
|
|
245
|
+
const m = ANSI_RE.exec(s);
|
|
246
|
+
if (m && m.index === i) {
|
|
247
|
+
out += m[0];
|
|
248
|
+
i += m[0].length;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
out += s[i];
|
|
253
|
+
seen += 1;
|
|
254
|
+
i += 1;
|
|
255
|
+
}
|
|
256
|
+
return out + "\x1b[0m";
|
|
257
|
+
}
|
|
258
|
+
/** Trim to `max` columns, marking the cut with an ellipsis. Returns "" when
|
|
259
|
+
* there is no room worth using. */
|
|
260
|
+
function clip(text, max) {
|
|
261
|
+
if (max < 4)
|
|
262
|
+
return "";
|
|
263
|
+
const chars = [...text];
|
|
264
|
+
return chars.length <= max ? text : chars.slice(0, max - 1).join("").trimEnd() + "…";
|
|
265
|
+
}
|
|
266
|
+
/** Minimal word wrap for the description line. */
|
|
267
|
+
function wrap(text, width) {
|
|
268
|
+
const w = Math.max(20, width);
|
|
269
|
+
const words = text.trim().split(/\s+/).filter(Boolean);
|
|
270
|
+
const lines = [];
|
|
271
|
+
let cur = "";
|
|
272
|
+
for (const word of words) {
|
|
273
|
+
if (cur && [...cur].length + 1 + [...word].length > w) {
|
|
274
|
+
lines.push(cur);
|
|
275
|
+
cur = word;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
cur = cur ? `${cur} ${word}` : word;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (cur)
|
|
282
|
+
lines.push(cur);
|
|
283
|
+
return lines.length ? lines : [""];
|
|
284
|
+
}
|