@carrieros/cli 0.2.0 → 0.4.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 +170 -64
- package/dist/commands/attach.js +45 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +81 -15
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +63 -3
- package/dist/commands/files.js +84 -23
- package/dist/commands/help.js +1 -0
- package/dist/commands/history.js +88 -23
- package/dist/commands/login.js +31 -7
- package/dist/commands/logout.js +1 -0
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +39 -3
- package/dist/commands/modes.js +4 -1
- package/dist/commands/permissions.js +77 -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 +9 -6
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +224 -65
- package/dist/commands/settings.js +94 -0
- package/dist/commands/update.js +229 -0
- package/dist/commands/upload.js +204 -31
- package/dist/commands/usage.js +213 -17
- package/dist/commands/whoami.js +12 -9
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +355 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +26 -1
- package/dist/services/api.js +315 -15
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +352 -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 +311 -24
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +296 -23
- package/dist/services/device-auth.js +72 -35
- 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 +55 -17
- package/dist/services/meta.js +3 -1
- 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 +5 -2
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +60 -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 +113 -0
- package/dist/services/sia.js +51 -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 +311 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1297 -59
- package/dist/shell/shell-commands.js +243 -12
- package/dist/shell/shell-prompt.js +94 -11
- package/dist/shell/shell-renderer.js +8 -20
- package/dist/shell/shell-router.js +19 -3
- package/dist/shell/shell.js +252 -22
- package/dist/shell/task-context-view.js +40 -0
- package/dist/types/index.js +3 -1
- 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 +31 -48
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +207 -14
- package/dist/ui/colors.js +12 -8
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +26 -5
- package/dist/ui/command-palette.js +44 -14
- package/dist/ui/command-registry.js +92 -10
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +48 -1
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +4 -2
- package/dist/ui/markdown.js +63 -9
- package/dist/ui/message.js +14 -31
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +19 -11
- package/dist/ui/select.js +191 -29
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +27 -1
- package/package.json +18 -4
- package/dist/ui/activity.js +0 -77
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import * as readline from "node:readline";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
|
+
import { screenActive, screenScrollPage, screenScrollToBottom, screenTransient } from "./screen.js";
|
|
4
|
+
import { selectFrame, SELECT_BACK } from "./select.js";
|
|
5
|
+
import { termWidth } from "./ui.js";
|
|
6
|
+
/**
|
|
7
|
+
* Prompts that render INSIDE the shell's transcript instead of taking the
|
|
8
|
+
* terminal over.
|
|
9
|
+
*
|
|
10
|
+
* The pickers in select.ts suspend the screen engine and paint on the raw
|
|
11
|
+
* terminal. That is correct for a one-off `cos model`, but inside the shell it
|
|
12
|
+
* swaps away the buffer holding the banner and the whole conversation — so
|
|
13
|
+
* while a picker is open there is nothing above it and nothing to scroll back
|
|
14
|
+
* to. These variants push their frame through `screenTransient`, so the
|
|
15
|
+
* transcript stays exactly where it was and the prompt appears beneath it, the
|
|
16
|
+
* way the thinking spinner already does.
|
|
17
|
+
*
|
|
18
|
+
* They only work while the screen engine is running; callers fall back to the
|
|
19
|
+
* select.ts versions when it isn't (`cos chat`, piped input).
|
|
20
|
+
*/
|
|
21
|
+
/** True when these should be preferred over the terminal-owning pickers. */
|
|
22
|
+
export function inScreen() {
|
|
23
|
+
return screenActive() && process.stdin.isTTY === true;
|
|
24
|
+
}
|
|
25
|
+
/** Take over key handling for the duration of a prompt, then hand it back. */
|
|
26
|
+
export function captureKeys(onKey) {
|
|
27
|
+
const stdin = process.stdin;
|
|
28
|
+
// The composer is not reading between turns, but a stale listener from an
|
|
29
|
+
// earlier prompt would double-handle every keystroke.
|
|
30
|
+
const previous = stdin.listeners("keypress");
|
|
31
|
+
stdin.removeAllListeners("keypress");
|
|
32
|
+
readline.emitKeypressEvents(stdin);
|
|
33
|
+
const prevRaw = stdin.isRaw ?? false;
|
|
34
|
+
if (stdin.isTTY)
|
|
35
|
+
stdin.setRawMode(true);
|
|
36
|
+
stdin.resume();
|
|
37
|
+
stdin.on("keypress", onKey);
|
|
38
|
+
return () => {
|
|
39
|
+
stdin.removeListener("keypress", onKey);
|
|
40
|
+
if (stdin.isTTY)
|
|
41
|
+
stdin.setRawMode(prevRaw);
|
|
42
|
+
for (const l of previous)
|
|
43
|
+
stdin.on("keypress", l);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** select.ts's picker, drawn into the transcript. Same keys, same result. */
|
|
47
|
+
export function pickInScreen(opts) {
|
|
48
|
+
const { choices } = opts;
|
|
49
|
+
// Only this variant can page the transcript, so only it advertises the keys.
|
|
50
|
+
const withScroll = { ...opts, scrollable: true };
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
let selected = Math.min(Math.max(0, opts.initial ?? 0), choices.length - 1);
|
|
53
|
+
const paint = () => screenTransient(selectFrame(withScroll, selected));
|
|
54
|
+
const finish = (value) => {
|
|
55
|
+
release();
|
|
56
|
+
screenTransient([]); // the answer is echoed by the caller, not this frame
|
|
57
|
+
resolve(value);
|
|
58
|
+
};
|
|
59
|
+
const onKey = (str, key) => {
|
|
60
|
+
if (!key)
|
|
61
|
+
return;
|
|
62
|
+
if (key.name === "up" || (key.ctrl && key.name === "p")) {
|
|
63
|
+
selected = (selected - 1 + choices.length) % choices.length;
|
|
64
|
+
return paint();
|
|
65
|
+
}
|
|
66
|
+
if (key.name === "down" || (key.ctrl && key.name === "n")) {
|
|
67
|
+
selected = (selected + 1) % choices.length;
|
|
68
|
+
return paint();
|
|
69
|
+
}
|
|
70
|
+
// Look back over the conversation without losing your place. ↑↓ belong to
|
|
71
|
+
// the list, so paging gets its own keys.
|
|
72
|
+
//
|
|
73
|
+
// A mouse wheel can't be routed here: with the alternate buffer active,
|
|
74
|
+
// terminals translate wheel ticks into ↑↓ before the app sees them, so a
|
|
75
|
+
// scroll and a keypress are the same bytes. Separating them would mean
|
|
76
|
+
// enabling mouse tracking, which costs drag-to-select.
|
|
77
|
+
if (key.name === "pageup") {
|
|
78
|
+
screenScrollPage(-1);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (key.name === "pagedown") {
|
|
82
|
+
screenScrollPage(1);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (key.name === "end") {
|
|
86
|
+
screenScrollToBottom();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (opts.allowBack && key.name === "left")
|
|
90
|
+
return finish(SELECT_BACK);
|
|
91
|
+
if (key.name === "right" || key.name === "return" || key.name === "enter") {
|
|
92
|
+
return finish(choices[selected].value);
|
|
93
|
+
}
|
|
94
|
+
if (key.name === "escape" || (key.ctrl && (key.name === "c" || key.name === "d"))) {
|
|
95
|
+
return finish(null);
|
|
96
|
+
}
|
|
97
|
+
if (str && /^[1-9]$/.test(str)) {
|
|
98
|
+
const idx = Number(str) - 1;
|
|
99
|
+
if (idx < choices.length) {
|
|
100
|
+
selected = idx;
|
|
101
|
+
if (opts.confirmOnNumber)
|
|
102
|
+
return finish(choices[idx].value);
|
|
103
|
+
paint();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const release = captureKeys(onKey);
|
|
108
|
+
paint();
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** Word-wrap to `width` columns. */
|
|
112
|
+
function wrap(text, width) {
|
|
113
|
+
const w = Math.max(20, width);
|
|
114
|
+
const out = [];
|
|
115
|
+
let cur = "";
|
|
116
|
+
for (const word of text.trim().split(/\s+/).filter(Boolean)) {
|
|
117
|
+
if (cur && [...cur].length + 1 + [...word].length > w) {
|
|
118
|
+
out.push(cur);
|
|
119
|
+
cur = word;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
cur = cur ? `${cur} ${word}` : word;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (cur)
|
|
126
|
+
out.push(cur);
|
|
127
|
+
return out.length ? out : [""];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A single-line text prompt drawn into the transcript.
|
|
131
|
+
*
|
|
132
|
+
* Line editing is deliberately minimal — type, backspace, Enter, Esc. readline
|
|
133
|
+
* can't be used here: it owns the terminal cursor, which is precisely what the
|
|
134
|
+
* screen engine is managing. Resolves the trimmed text, or null if empty or
|
|
135
|
+
* cancelled.
|
|
136
|
+
*/
|
|
137
|
+
export function askTextInScreen(headerLines, placeholder) {
|
|
138
|
+
return new Promise((resolve) => {
|
|
139
|
+
let buffer = "";
|
|
140
|
+
const paint = () => {
|
|
141
|
+
const width = Math.min(termWidth() - 4, 92);
|
|
142
|
+
const shown = buffer
|
|
143
|
+
? c.value(buffer)
|
|
144
|
+
: c.placeholder(placeholder ?? "Type your answer");
|
|
145
|
+
screenTransient([
|
|
146
|
+
...headerLines(width),
|
|
147
|
+
"",
|
|
148
|
+
" " + c.brandFixed("❯ ") + shown + c.dim("▋"),
|
|
149
|
+
"",
|
|
150
|
+
" " + c.key("Enter") + c.dim(" confirm · ") + c.key("Esc") + c.dim(" cancel")
|
|
151
|
+
]);
|
|
152
|
+
};
|
|
153
|
+
const finish = (value) => {
|
|
154
|
+
release();
|
|
155
|
+
screenTransient([]);
|
|
156
|
+
resolve(value);
|
|
157
|
+
};
|
|
158
|
+
const onKey = (str, key) => {
|
|
159
|
+
if (!key)
|
|
160
|
+
return;
|
|
161
|
+
if (key.name === "return" || key.name === "enter")
|
|
162
|
+
return finish(buffer.trim() || null);
|
|
163
|
+
if (key.name === "escape" || (key.ctrl && (key.name === "c" || key.name === "d"))) {
|
|
164
|
+
return finish(null);
|
|
165
|
+
}
|
|
166
|
+
if (key.name === "backspace") {
|
|
167
|
+
buffer = [...buffer].slice(0, -1).join("");
|
|
168
|
+
return paint();
|
|
169
|
+
}
|
|
170
|
+
// Printable input only — ignore control chords and navigation keys.
|
|
171
|
+
if (str && !key.ctrl && !key.meta && str >= " " && str !== "\x7f") {
|
|
172
|
+
buffer += str;
|
|
173
|
+
paint();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
const release = captureKeys(onKey);
|
|
177
|
+
paint();
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
export { wrap as wrapForPrompt };
|
|
181
|
+
export { askApproval as approveInScreen } from "./approval.js";
|
package/dist/ui/screen.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { clampAnsi, wrapAnsi } from "./composer-layout.js";
|
|
1
2
|
/**
|
|
2
3
|
* Persistent bottom-composer TUI engine (Claude Code–style).
|
|
3
4
|
*
|
|
@@ -43,6 +44,7 @@ const MAX_SCROLLBACK = 5000;
|
|
|
43
44
|
let active = false;
|
|
44
45
|
let committed = []; // permanent transcript lines
|
|
45
46
|
let transient = []; // in-progress tail (spinner / streaming), below `committed`
|
|
47
|
+
let task = []; // existing task review projection, pinned while active
|
|
46
48
|
let palette = []; // slash-palette overlay (empty when closed)
|
|
47
49
|
let bar = []; // the fixed composer core (CORE_H lines)
|
|
48
50
|
let caretBarIndex = 1; // which composer row the caret sits on (1 = prompt line)
|
|
@@ -65,10 +67,11 @@ function metrics() {
|
|
|
65
67
|
const core = bar.length || CORE_H;
|
|
66
68
|
const tb = Math.max(1, h - core); // transcript occupies rows 1..tb
|
|
67
69
|
const pH = Math.min(palette.length, Math.max(0, tb - 1)); // overlay never covers the whole region
|
|
68
|
-
const
|
|
69
|
-
const
|
|
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)));
|
|
70
73
|
const maxOffset = Math.max(0, all.length - contentH);
|
|
71
|
-
return { h, core, tb, pH, contentH, all, maxOffset };
|
|
74
|
+
return { h, core, tb, pH, taskH, contentH, all, maxOffset };
|
|
72
75
|
}
|
|
73
76
|
/** Clamp the scroll offset into range and recompute the pinned flag. */
|
|
74
77
|
function clampScroll() {
|
|
@@ -94,7 +97,7 @@ function onContentGrew(added) {
|
|
|
94
97
|
}
|
|
95
98
|
/** Build the full desired screen: [transcript window][palette][composer core]. */
|
|
96
99
|
function compose() {
|
|
97
|
-
const { h, tb, pH, contentH, all, maxOffset } = metrics();
|
|
100
|
+
const { h, tb, pH, taskH, contentH, all, maxOffset } = metrics();
|
|
98
101
|
if (scrollOffset > maxOffset)
|
|
99
102
|
scrollOffset = maxOffset; // stay valid across resizes
|
|
100
103
|
// The visible window into `all`. offset 0 = last `contentH` lines (the tail);
|
|
@@ -103,17 +106,19 @@ function compose() {
|
|
|
103
106
|
const start = Math.max(0, end - contentH);
|
|
104
107
|
const win = all.slice(start, end);
|
|
105
108
|
const desired = new Array(h).fill("");
|
|
109
|
+
for (let i = 0; i < taskH; i++)
|
|
110
|
+
desired[i] = task[i] ?? "";
|
|
106
111
|
for (let i = 0; i < contentH; i++)
|
|
107
|
-
desired[i] = win[i] ?? "";
|
|
112
|
+
desired[taskH + i] = win[i] ?? "";
|
|
108
113
|
// A subtle "more below" hint on the last transcript row when scrolled up.
|
|
109
114
|
if (scrollOffset > 0 && contentH > 0) {
|
|
110
|
-
desired[contentH - 1] = `${ESC}2m ↓ ${scrollOffset} more line${scrollOffset === 1 ? "" : "s"}
|
|
115
|
+
desired[taskH + contentH - 1] = `${ESC}2m ↓ ${scrollOffset} more line${scrollOffset === 1 ? "" : "s"} - End to jump to latest${ESC}0m`;
|
|
111
116
|
}
|
|
112
117
|
for (let i = 0; i < pH; i++)
|
|
113
|
-
desired[contentH + i] = palette[i] ?? "";
|
|
118
|
+
desired[taskH + contentH + i] = palette[i] ?? "";
|
|
114
119
|
for (let i = 0; i < (bar.length || CORE_H); i++)
|
|
115
120
|
desired[tb + i] = bar[i] ?? "";
|
|
116
|
-
return desired;
|
|
121
|
+
return desired.map(line => clampAnsi(line, Math.max(1, (out.columns ?? 80) - 1)));
|
|
117
122
|
}
|
|
118
123
|
/** Diff the desired screen against what's on-screen; rewrite only changed rows. */
|
|
119
124
|
function flush() {
|
|
@@ -132,15 +137,16 @@ function flush() {
|
|
|
132
137
|
const caretRow = rows() - core + 1 + caretBarIndex;
|
|
133
138
|
// The composer is a permanent region, so its caret shows whenever the composer
|
|
134
139
|
// is active — independent of where the transcript is scrolled.
|
|
135
|
-
s += showCaret ? `${ESC}${caretRow};${caretCol}H${SHOW}` : HIDE;
|
|
140
|
+
s += showCaret ? `${ESC}${caretRow};${Math.min(caretCol, out.columns ?? 80)}H${SHOW}` : HIDE;
|
|
136
141
|
write(s);
|
|
137
142
|
}
|
|
138
143
|
/** Enter TUI mode: alt-screen, alt-scroll (wheel→arrows), clear, hide caret. */
|
|
139
144
|
export function startScreen() {
|
|
140
|
-
if (!out.isTTY)
|
|
145
|
+
if (!out.isTTY || process.env.TERM === "dumb")
|
|
141
146
|
return;
|
|
142
147
|
active = true;
|
|
143
148
|
committed = [];
|
|
149
|
+
task = [];
|
|
144
150
|
transient = [];
|
|
145
151
|
palette = [];
|
|
146
152
|
bar = [];
|
|
@@ -279,7 +285,7 @@ export function suspendScreen() {
|
|
|
279
285
|
}
|
|
280
286
|
/** Resume TUI mode after suspendScreen(), repainting the preserved transcript. */
|
|
281
287
|
export function resumeScreen() {
|
|
282
|
-
if (!out.isTTY)
|
|
288
|
+
if (!out.isTTY || process.env.TERM === "dumb")
|
|
283
289
|
return;
|
|
284
290
|
active = true;
|
|
285
291
|
shown = [];
|
|
@@ -287,3 +293,5 @@ export function resumeScreen() {
|
|
|
287
293
|
flush();
|
|
288
294
|
out.on("resize", screenResize);
|
|
289
295
|
}
|
|
296
|
+
/** Pin task evidence without changing transcript, approval or keyboard ownership. */
|
|
297
|
+
export function screenTask(lines) { task = lines.slice(0, 24); clampScroll(); flush(); }
|
package/dist/ui/select.js
CHANGED
|
@@ -2,8 +2,144 @@ import * as readline from "node:readline";
|
|
|
2
2
|
import { c } from "./colors.js";
|
|
3
3
|
import { resumeScreen, screenActive, suspendScreen } from "./screen.js";
|
|
4
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
|
+
}
|
|
5
141
|
export function pickFromList(opts) {
|
|
6
|
-
const {
|
|
142
|
+
const { choices, confirmOnNumber = false } = opts;
|
|
7
143
|
if (!process.stdin.isTTY || choices.length === 0) {
|
|
8
144
|
return Promise.resolve(choices[opts.initial ?? 0]?.value ?? null);
|
|
9
145
|
}
|
|
@@ -18,43 +154,23 @@ export function pickFromList(opts) {
|
|
|
18
154
|
stdin.removeAllListeners("keypress");
|
|
19
155
|
readline.emitKeypressEvents(stdin);
|
|
20
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;
|
|
21
161
|
if (stdin.isTTY)
|
|
22
162
|
stdin.setRawMode(true);
|
|
23
163
|
stdin.resume();
|
|
24
164
|
let selected = Math.min(Math.max(0, opts.initial ?? 0), choices.length - 1);
|
|
25
165
|
let prevUp = 0;
|
|
26
|
-
const labelW = Math.min(30, Math.max(...choices.map((ch) => ch.label.length)));
|
|
27
|
-
const numW = String(choices.length).length;
|
|
28
166
|
const render = () => {
|
|
29
167
|
if (prevUp > 0)
|
|
30
168
|
readline.moveCursor(stdout, 0, -prevUp);
|
|
31
169
|
readline.cursorTo(stdout, 0);
|
|
32
170
|
readline.clearScreenDown(stdout);
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
for (const l of wrap(description, width))
|
|
37
|
-
lines.push(" " + c.dim(l));
|
|
38
|
-
}
|
|
39
|
-
lines.push("");
|
|
40
|
-
choices.forEach((ch, i) => {
|
|
41
|
-
const on = i === selected;
|
|
42
|
-
const pointer = on ? c.brand("❯ ") : " ";
|
|
43
|
-
const num = c.dim(`${String(i + 1).padStart(numW)}.`);
|
|
44
|
-
const label = (on ? c.brand.bold : c.value)(ch.label.padEnd(labelW));
|
|
45
|
-
const desc = ch.description ? " " + c.dim(ch.description) : "";
|
|
46
|
-
const mark = ch.current ? c.brand(" ✓") : "";
|
|
47
|
-
lines.push(" " + pointer + num + " " + label + desc + mark);
|
|
48
|
-
});
|
|
49
|
-
lines.push("");
|
|
50
|
-
lines.push(" " +
|
|
51
|
-
c.dim("↑↓ navigate · ") +
|
|
52
|
-
c.key("Enter") +
|
|
53
|
-
c.dim(" confirm · ") +
|
|
54
|
-
c.key("Esc") +
|
|
55
|
-
c.dim(" cancel"));
|
|
56
|
-
stdout.write(lines.join("\n"));
|
|
57
|
-
prevUp = lines.length - 1; // cursor rests on the last line; rewind from there next time
|
|
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
|
|
58
174
|
};
|
|
59
175
|
const finish = (val) => {
|
|
60
176
|
if (prevUp > 0)
|
|
@@ -63,6 +179,8 @@ export function pickFromList(opts) {
|
|
|
63
179
|
readline.clearScreenDown(stdout);
|
|
64
180
|
stdin.removeListener("keypress", onKey);
|
|
65
181
|
stdin.setRawMode(prevRaw);
|
|
182
|
+
if (wasPaused)
|
|
183
|
+
stdin.pause();
|
|
66
184
|
if (suspended)
|
|
67
185
|
resumeScreen(); // repaint the pinned composer + transcript
|
|
68
186
|
resolve(val);
|
|
@@ -79,16 +197,25 @@ export function pickFromList(opts) {
|
|
|
79
197
|
selected = (selected + 1) % choices.length;
|
|
80
198
|
return render();
|
|
81
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);
|
|
82
206
|
if (key.name === "return" || key.name === "enter")
|
|
83
207
|
return finish(choices[selected].value);
|
|
84
208
|
if (key.name === "escape" || (key.ctrl && (key.name === "c" || key.name === "d"))) {
|
|
85
209
|
return finish(null);
|
|
86
210
|
}
|
|
87
|
-
// Number keys jump to an option
|
|
211
|
+
// Number keys jump to an option — and confirm it directly when the
|
|
212
|
+
// caller asked for that (Yes/No style prompts).
|
|
88
213
|
if (str && /^[1-9]$/.test(str)) {
|
|
89
214
|
const idx = Number(str) - 1;
|
|
90
215
|
if (idx < choices.length) {
|
|
91
216
|
selected = idx;
|
|
217
|
+
if (confirmOnNumber)
|
|
218
|
+
return finish(choices[idx].value);
|
|
92
219
|
render();
|
|
93
220
|
}
|
|
94
221
|
}
|
|
@@ -101,6 +228,41 @@ export function pickFromList(opts) {
|
|
|
101
228
|
render();
|
|
102
229
|
});
|
|
103
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
|
+
}
|
|
104
266
|
/** Minimal word wrap for the description line. */
|
|
105
267
|
function wrap(text, width) {
|
|
106
268
|
const w = Math.max(20, width);
|