@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,205 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
|
+
import { renderCodeBlock } from "./code-block.js";
|
|
4
|
+
import { clampAnsi, contentWidth, MARGIN, textWidth } from "./composer-layout.js";
|
|
5
|
+
/**
|
|
6
|
+
* Self-contained Markdown → ANSI renderer (no dependency). Supports headings,
|
|
7
|
+
* bold/italic, inline code, links, bullet/numbered/task lists, blockquotes,
|
|
8
|
+
* horizontal rules, tables and fenced code blocks — with soft word-wrap so long
|
|
9
|
+
* paragraphs never overflow or cut words. Returns col-0 lines; callers indent.
|
|
10
|
+
*/
|
|
11
|
+
export function renderMarkdown(md, maxWidth) {
|
|
12
|
+
// `maxWidth` lets a caller render into something narrower than the transcript
|
|
13
|
+
// — the /btw box, which spends 4 columns on its own borders. Without it the
|
|
14
|
+
// body would wrap to the full width and overflow the frame it sits in.
|
|
15
|
+
const wrap = Math.max(20, (maxWidth ?? textWidth()) - 4);
|
|
16
|
+
const out = [];
|
|
17
|
+
const lines = md.replace(/\r/g, "").split("\n");
|
|
18
|
+
let i = 0;
|
|
19
|
+
const flushTable = (rows) => {
|
|
20
|
+
out.push(...renderTable(rows));
|
|
21
|
+
};
|
|
22
|
+
while (i < lines.length) {
|
|
23
|
+
const raw = lines[i];
|
|
24
|
+
// Fenced code block.
|
|
25
|
+
const fence = raw.match(/^\s*```(.*)$/);
|
|
26
|
+
if (fence) {
|
|
27
|
+
const lang = fence[1].trim();
|
|
28
|
+
const body = [];
|
|
29
|
+
i++;
|
|
30
|
+
while (i < lines.length && !/^\s*```/.test(lines[i]))
|
|
31
|
+
body.push(lines[i++]);
|
|
32
|
+
i++; // closing fence
|
|
33
|
+
out.push(...renderCodeBlock(body.join("\n"), lang));
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
// Table block (consecutive pipe rows).
|
|
37
|
+
if (/^\s*\|.*\|\s*$/.test(raw)) {
|
|
38
|
+
const rows = [];
|
|
39
|
+
while (i < lines.length && /^\s*\|.*\|\s*$/.test(lines[i]))
|
|
40
|
+
rows.push(lines[i++]);
|
|
41
|
+
flushTable(rows);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// Horizontal rule.
|
|
45
|
+
if (/^\s*([-*_])\1{2,}\s*$/.test(raw)) {
|
|
46
|
+
out.push(c.rule("─".repeat(Math.min(wrap, 40))));
|
|
47
|
+
i++;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
// Heading — wrapped like body text; a long one would otherwise overflow.
|
|
51
|
+
const h = raw.match(/^(#{1,6})\s+(.*)$/);
|
|
52
|
+
if (h) {
|
|
53
|
+
const paint = h[1].length === 1 ? c.brand : c.title;
|
|
54
|
+
for (const w of wrapText(h[2], wrap))
|
|
55
|
+
out.push(paint(inline(w)));
|
|
56
|
+
i++;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
// Blockquote.
|
|
60
|
+
const quote = raw.match(/^\s*>\s?(.*)$/);
|
|
61
|
+
if (quote) {
|
|
62
|
+
for (const w of wrapText(quote[1], wrap - 2))
|
|
63
|
+
out.push(c.rule("│ ") + c.dim(inline(w)));
|
|
64
|
+
i++;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
// Task list.
|
|
68
|
+
const task = raw.match(/^(\s*)[-*+]\s+\[([ xX])\]\s+(.*)$/);
|
|
69
|
+
if (task) {
|
|
70
|
+
const box = task[2].trim() ? c.ok("☑") : c.dim("☐");
|
|
71
|
+
const wrapped = wrapText(task[3], wrap - task[1].length - 4);
|
|
72
|
+
out.push(`${task[1]} ${box} ${inline(wrapped[0] ?? "")}`);
|
|
73
|
+
for (const w of wrapped.slice(1))
|
|
74
|
+
out.push(`${task[1]} ${inline(w)}`);
|
|
75
|
+
i++;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
// Bullet list.
|
|
79
|
+
const bullet = raw.match(/^(\s*)[-*+]\s+(.*)$/);
|
|
80
|
+
if (bullet) {
|
|
81
|
+
const wrapped = wrapText(bullet[2], wrap - bullet[1].length - 4);
|
|
82
|
+
out.push(`${bullet[1]} ${c.accent("•")} ${inline(wrapped[0] ?? "")}`);
|
|
83
|
+
for (const w of wrapped.slice(1))
|
|
84
|
+
out.push(`${bullet[1]} ${inline(w)}`);
|
|
85
|
+
i++;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
// Numbered list.
|
|
89
|
+
const num = raw.match(/^(\s*)(\d+)\.\s+(.*)$/);
|
|
90
|
+
if (num) {
|
|
91
|
+
const marker = `${num[2]}.`;
|
|
92
|
+
const wrapped = wrapText(num[3], wrap - num[1].length - marker.length - 3);
|
|
93
|
+
out.push(`${num[1]} ${c.accent(marker)} ${inline(wrapped[0] ?? "")}`);
|
|
94
|
+
for (const w of wrapped.slice(1))
|
|
95
|
+
out.push(`${num[1]}${" ".repeat(marker.length + 3)}${inline(w)}`);
|
|
96
|
+
i++;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
// Blank line.
|
|
100
|
+
if (raw.trim() === "") {
|
|
101
|
+
out.push("");
|
|
102
|
+
i++;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
// Paragraph — soft wrap.
|
|
106
|
+
for (const w of wrapText(raw, wrap))
|
|
107
|
+
out.push(inline(w));
|
|
108
|
+
i++;
|
|
109
|
+
}
|
|
110
|
+
// Backstop. Code blocks and tables size themselves from their content, and a
|
|
111
|
+
// future block type could forget to wrap entirely — any of which would emit a
|
|
112
|
+
// line wider than the terminal and corrupt the pinned composer. Callers indent
|
|
113
|
+
// by MARGIN, so leave room for it.
|
|
114
|
+
const limit = Math.max(20, contentWidth() - MARGIN.length);
|
|
115
|
+
return out.map((l) => clampAnsi(l, limit));
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Word-wrap plain text to `width`, never cutting a word — EXCEPT a word that
|
|
119
|
+
* cannot fit on a line of its own.
|
|
120
|
+
*
|
|
121
|
+
* A 250-character URL, hash or base64 blob has no break point, so pure
|
|
122
|
+
* word-wrapping emits it as one over-wide line. The terminal then wraps it into
|
|
123
|
+
* several physical rows while the screen engine still counts one, and the
|
|
124
|
+
* pinned composer below gets overwritten. Splitting such a token across lines
|
|
125
|
+
* keeps every character (nothing is lost, unlike clipping) while holding the
|
|
126
|
+
* one-line-per-row invariant everything else depends on.
|
|
127
|
+
*/
|
|
128
|
+
function wrapText(text, width) {
|
|
129
|
+
const w = Math.max(20, width);
|
|
130
|
+
const words = text.trim().split(/\s+/).filter(Boolean);
|
|
131
|
+
if (!words.length)
|
|
132
|
+
return [""];
|
|
133
|
+
const lines = [];
|
|
134
|
+
let cur = "";
|
|
135
|
+
for (const word of words) {
|
|
136
|
+
const len = [...word].length;
|
|
137
|
+
if (len > w) {
|
|
138
|
+
if (cur) {
|
|
139
|
+
lines.push(cur);
|
|
140
|
+
cur = "";
|
|
141
|
+
}
|
|
142
|
+
const chars = [...word];
|
|
143
|
+
for (let i = 0; i < chars.length; i += w)
|
|
144
|
+
lines.push(chars.slice(i, i + w).join(""));
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (cur && [...cur].length + 1 + len > w) {
|
|
148
|
+
lines.push(cur);
|
|
149
|
+
cur = word;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
cur = cur ? `${cur} ${word}` : word;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (cur)
|
|
156
|
+
lines.push(cur);
|
|
157
|
+
return lines;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* A code-review severity, coloured by weight. A review's findings arrive as
|
|
161
|
+
* bold headers like `**High — \`src/x.js:12\`**`; recognising the leading
|
|
162
|
+
* severity word lets the terminal show it as a subtle marker (red for the
|
|
163
|
+
* serious ones, dim for the quiet ones) instead of undifferentiated bold. It is
|
|
164
|
+
* deliberately narrow — only a bold segment that STARTS with one of these five
|
|
165
|
+
* words is touched, so ordinary bold text is unaffected.
|
|
166
|
+
*/
|
|
167
|
+
const SEVERITY_COLOR = {
|
|
168
|
+
critical: c.err,
|
|
169
|
+
high: c.err,
|
|
170
|
+
medium: c.warn,
|
|
171
|
+
low: c.dim,
|
|
172
|
+
info: c.accent
|
|
173
|
+
};
|
|
174
|
+
function styleBold(b) {
|
|
175
|
+
const m = b.match(/^(critical|high|medium|low|info)\b(.*)$/i);
|
|
176
|
+
if (m) {
|
|
177
|
+
const paint = SEVERITY_COLOR[m[1].toLowerCase()];
|
|
178
|
+
return chalk.bold(paint(m[1].toUpperCase())) + chalk.bold(m[2]);
|
|
179
|
+
}
|
|
180
|
+
return chalk.bold(b);
|
|
181
|
+
}
|
|
182
|
+
/** Inline spans: links, `code`, **bold**, *italic*, ~~strike~~. */
|
|
183
|
+
function inline(text) {
|
|
184
|
+
return text
|
|
185
|
+
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, label) => c.accent(label))
|
|
186
|
+
.replace(/`([^`]+)`/g, (_m, code) => c.accent(code))
|
|
187
|
+
.replace(/\*\*([^*]+)\*\*/g, (_m, b) => styleBold(b))
|
|
188
|
+
.replace(/~~([^~]+)~~/g, (_m, s) => chalk.strikethrough(s))
|
|
189
|
+
.replace(/(^|[^*])\*([^*]+)\*/g, (_m, pre, it) => `${pre}${chalk.italic(it)}`);
|
|
190
|
+
}
|
|
191
|
+
/** Render a Markdown table with aligned columns. */
|
|
192
|
+
function renderTable(rows) {
|
|
193
|
+
const cells = rows.map((r) => r.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((x) => x.trim()));
|
|
194
|
+
const body = cells.filter((r) => !r.every((x) => /^:?-{2,}:?$/.test(x)));
|
|
195
|
+
if (body.length === 0)
|
|
196
|
+
return [];
|
|
197
|
+
const cols = Math.max(...body.map((r) => r.length));
|
|
198
|
+
const widths = Array.from({ length: cols }, (_, i) => Math.max(...body.map((r) => (r[i] ?? "").length)));
|
|
199
|
+
const row = (r, color) => r.map((cell, i) => color((cell ?? "").padEnd(widths[i]))).join(c.rule(" │ "));
|
|
200
|
+
const out = [row(body[0], c.title)];
|
|
201
|
+
out.push(widths.map((w) => c.rule("─".repeat(w))).join(c.rule("──┼──")));
|
|
202
|
+
for (const r of body.slice(1))
|
|
203
|
+
out.push(row(r, c.value));
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { line } from "../services/output.js";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
|
+
import { MARGIN, contentWidth } from "./composer-layout.js";
|
|
4
|
+
import { wrapText } from "./conversation.js";
|
|
5
|
+
/**
|
|
6
|
+
* A user turn: the accent caret the composer uses, then the message.
|
|
7
|
+
*
|
|
8
|
+
* Deliberately light. This used to be a full-width shaded band, which made
|
|
9
|
+
* every question look like a UI panel and gave the eye something heavier than
|
|
10
|
+
* the answer below it. The caret alone is enough to say "you said this", and it
|
|
11
|
+
* matches what the composer shows while typing, so a sent message reads as the
|
|
12
|
+
* same thing it was a moment ago.
|
|
13
|
+
*/
|
|
14
|
+
export function renderUserMessage(text) {
|
|
15
|
+
const lines = wrapText(text.trim(), contentWidth() - 3);
|
|
16
|
+
line();
|
|
17
|
+
for (const [i, l] of lines.entries()) {
|
|
18
|
+
line(MARGIN + (i === 0 ? c.brandFixed("›") : " ") + " " + c.title(l));
|
|
19
|
+
}
|
|
20
|
+
line();
|
|
21
|
+
}
|
|
@@ -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";
|