@carrieros/cli 0.1.0 → 0.2.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/dist/commands/auth.js +60 -0
- package/dist/commands/chat.js +27 -4
- package/dist/commands/doctor.js +7 -7
- package/dist/commands/help.js +2 -1
- package/dist/commands/login.js +19 -40
- package/dist/commands/logout.js +13 -4
- package/dist/commands/scope.js +152 -0
- package/dist/commands/whoami.js +2 -1
- package/dist/main.js +4 -0
- package/dist/services/api.js +33 -9
- package/dist/services/config.js +3 -1
- package/dist/services/context.js +59 -3
- package/dist/services/device-auth.js +249 -0
- package/dist/services/login-flow.js +113 -10
- package/dist/services/output.js +8 -3
- package/dist/services/spinner.js +23 -1
- package/dist/shell/shell-chat.js +102 -17
- package/dist/shell/shell-commands.js +21 -9
- package/dist/shell/shell-prompt.js +17 -38
- package/dist/shell/shell-renderer.js +14 -21
- package/dist/shell/shell.js +52 -32
- package/dist/ui/banner.js +16 -13
- package/dist/ui/chat-renderer.js +17 -16
- package/dist/ui/color-picker.js +21 -0
- package/dist/ui/colors.js +5 -1
- package/dist/ui/command-filter.js +15 -1
- package/dist/ui/command-palette.js +107 -60
- package/dist/ui/command-registry.js +4 -5
- package/dist/ui/live.js +15 -0
- package/dist/ui/screen.js +289 -0
- package/dist/ui/select.js +8 -0
- package/dist/ui/theme.js +17 -0
- package/package.json +5 -5
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import * as readline from "node:readline";
|
|
2
2
|
import { c } from "./colors.js";
|
|
3
3
|
import { COMMANDS, TAKES_ARG } from "./command-item.js";
|
|
4
|
-
import { filterCommands, ghostCompletion } from "./command-filter.js";
|
|
4
|
+
import { argHint, filterCommands, ghostCompletion } from "./command-filter.js";
|
|
5
5
|
import { paletteView } from "./command-renderer.js";
|
|
6
6
|
import { accentName, isThemeColor, setAccent } from "./theme.js";
|
|
7
7
|
import { MARGIN, contentWidth } from "./composer-layout.js";
|
|
8
|
+
import { screenClear, screenComposer, screenPrint, screenScrollable, screenScrollLines, screenScrollPage, screenScrollToBottom, screenScrollToTop } from "./screen.js";
|
|
9
|
+
const DEBUG_INPUT = Boolean(process.env.COS_DEBUG_INPUT);
|
|
8
10
|
/** Color name inside a `/color <name>` suggestion, or null. */
|
|
9
11
|
function colorOf(name) {
|
|
10
12
|
const m = name.match(/^\/(?:color|theme)\s+(\w+)$/i);
|
|
@@ -44,15 +46,18 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
44
46
|
// Full-width input bar (Claude-style): a horizontal rule above and below a
|
|
45
47
|
// `❯ ` prompt spanning the whole terminal. "❯ " is 2 visible cols; input
|
|
46
48
|
// starts at column 2. Long lines scroll horizontally to keep the caret in
|
|
47
|
-
// view.
|
|
48
|
-
//
|
|
49
|
+
// view. Rendering is delegated to the screen engine, which pins this bar to
|
|
50
|
+
// the bottom of the terminal and floats the palette directly above it.
|
|
49
51
|
const MARKER_COLS = 2;
|
|
50
|
-
let prevUp = 0;
|
|
51
52
|
// Live theme preview for `/color <name>` suggestions: previewing the accent
|
|
52
53
|
// as the highlighted color changes, restoring it if the user backs out.
|
|
53
54
|
let colorPreviewOrig = null;
|
|
54
55
|
const applyColorPreview = () => {
|
|
55
|
-
|
|
56
|
+
// Preview the accent for the color currently being typed (completing any
|
|
57
|
+
// ghost value), so the whole UI re-themes live; restored if the user backs
|
|
58
|
+
// out. `/color gr` previews green; a bare `/color ` previews nothing.
|
|
59
|
+
const g = paletteOpen && cursor === input.length ? ghostCompletion(input) : "";
|
|
60
|
+
const sel = paletteOpen ? colorOf((input + g).trim()) : null;
|
|
56
61
|
if (sel) {
|
|
57
62
|
if (colorPreviewOrig === null)
|
|
58
63
|
colorPreviewOrig = accentName();
|
|
@@ -63,60 +68,47 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
63
68
|
colorPreviewOrig = null;
|
|
64
69
|
}
|
|
65
70
|
};
|
|
71
|
+
// Build the composer (input bar + optional palette) and hand it to the
|
|
72
|
+
// screen engine, which pins the bar to the bottom of the terminal and floats
|
|
73
|
+
// the palette in the transcript rows directly above it — the bar never moves.
|
|
66
74
|
const render = () => {
|
|
67
75
|
applyColorPreview();
|
|
68
|
-
// Hide the cursor for the duration of the redraw so it doesn't visibly
|
|
69
|
-
// flicker as it's repositioned; it's shown again at its final resting spot.
|
|
70
|
-
stdout.write("\x1b[?25l");
|
|
71
|
-
// Rewind to the top of the previous block and wipe it clean.
|
|
72
|
-
if (prevUp > 0)
|
|
73
|
-
readline.moveCursor(stdout, 0, -prevUp);
|
|
74
|
-
readline.cursorTo(stdout, 0);
|
|
75
|
-
readline.clearScreenDown(stdout);
|
|
76
76
|
// The composer + its divider lines span the full content width (terminal
|
|
77
77
|
// minus a small margin/gutter), recomputed each render so a resize just
|
|
78
78
|
// redraws cleanly at the new width.
|
|
79
79
|
const total = contentWidth();
|
|
80
80
|
const field = Math.max(8, total - MARKER_COLS); // usable text width
|
|
81
|
-
//
|
|
82
|
-
|
|
81
|
+
// Inline trailer at the caret (only when it sits at the end of the input):
|
|
82
|
+
// a ghost completion (`/ver`→`sion`, `/color gr`→`een`), or — when a value
|
|
83
|
+
// is expected but not yet typed — a dim argument hint (`/color `→`[red|…]`).
|
|
84
|
+
const atEnd = paletteOpen && cursor === input.length;
|
|
85
|
+
const ghost = atEnd ? ghostCompletion(input) : "";
|
|
86
|
+
const hint = atEnd && !ghost ? argHint(input) : "";
|
|
87
|
+
const trailer = ghost || hint;
|
|
83
88
|
const start = cursor > field ? cursor - field : 0;
|
|
84
89
|
const visInput = input.slice(start, start + field);
|
|
85
|
-
const
|
|
90
|
+
const visTrailer = start + visInput.length >= input.length ? trailer.slice(0, field - visInput.length) : "";
|
|
86
91
|
// Empty + closed → show the dim italic placeholder (it never becomes part
|
|
87
92
|
// of the message; typing the first char replaces it, deleting brings it
|
|
88
93
|
// back). Otherwise render the typed text and any ghost completion.
|
|
89
94
|
const showPlaceholder = input === "" && !paletteOpen && placeholder !== "";
|
|
90
95
|
const bodyText = showPlaceholder ? placeholder.slice(0, field) : visInput;
|
|
91
96
|
const body = showPlaceholder ? c.placeholder(bodyText) : c.value(bodyText);
|
|
92
|
-
const promptCols = MARKER_COLS + bodyText.length + (showPlaceholder ? 0 :
|
|
97
|
+
const promptCols = MARKER_COLS + bodyText.length + (showPlaceholder ? 0 : visTrailer.length);
|
|
93
98
|
const pad = " ".repeat(Math.max(0, total - promptCols));
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (lastRow > midRow)
|
|
108
|
-
readline.moveCursor(stdout, 0, -(lastRow - midRow));
|
|
109
|
-
readline.cursorTo(stdout, MARGIN.length + MARKER_COLS + (cursor - start));
|
|
110
|
-
stdout.write("\x1b[?25h");
|
|
111
|
-
prevUp = midRow;
|
|
112
|
-
};
|
|
113
|
-
const clearBox = () => {
|
|
114
|
-
// Rewind to the top of the block and wipe it, so callers render clean.
|
|
115
|
-
if (prevUp > 0)
|
|
116
|
-
readline.moveCursor(stdout, 0, -prevUp);
|
|
117
|
-
readline.cursorTo(stdout, 0);
|
|
118
|
-
readline.clearScreenDown(stdout);
|
|
119
|
-
prevUp = 0;
|
|
99
|
+
// The two divider rules carry the runtime accent (/color); the prompt
|
|
100
|
+
// marker stays fixed brand green (branding, never recolored).
|
|
101
|
+
const rule = MARGIN + c.brand("─".repeat(total));
|
|
102
|
+
const promptLine = MARGIN + c.brandFixed("❯") + " " + body + (visTrailer ? c.dim(visTrailer) : "") + pad;
|
|
103
|
+
const core = [rule, promptLine, rule, footer]; // fixed 4-row composer core
|
|
104
|
+
// The command palette floats ABOVE the bar when open — except in the
|
|
105
|
+
// value-entry context (`/color <value>`), where the inline arg hint / ghost
|
|
106
|
+
// on the prompt line replaces the list. The caret sits on the prompt line
|
|
107
|
+
// (core index 1) at the text column (1-based).
|
|
108
|
+
const argMode = /^\/(?:color|theme)\s/i.test(input);
|
|
109
|
+
const paletteLines = paletteOpen && !argMode ? paletteView(filtered, selected, input) : [];
|
|
110
|
+
const caretCol = MARGIN.length + MARKER_COLS + (cursor - start) + 1;
|
|
111
|
+
screenComposer(paletteLines, core, 1, caretCol);
|
|
120
112
|
};
|
|
121
113
|
const finish = (res) => {
|
|
122
114
|
paletteOpen = false;
|
|
@@ -126,7 +118,12 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
126
118
|
setAccent(colorPreviewOrig);
|
|
127
119
|
colorPreviewOrig = null;
|
|
128
120
|
}
|
|
129
|
-
|
|
121
|
+
// Clear the composer to an empty prompt so the just-submitted text doesn't
|
|
122
|
+
// linger in the bar while the response streams into the transcript above.
|
|
123
|
+
const total = contentWidth();
|
|
124
|
+
const rule = MARGIN + c.brand("─".repeat(total));
|
|
125
|
+
const empty = MARGIN + c.brandFixed("❯") + " " + " ".repeat(Math.max(0, total - MARKER_COLS));
|
|
126
|
+
screenComposer([], [rule, empty, rule, footer], 1, MARGIN.length + MARKER_COLS + 1);
|
|
130
127
|
stdin.removeListener("keypress", onKey);
|
|
131
128
|
// Raw mode is intentionally left ON between prompts (see the setup note);
|
|
132
129
|
// the shell restores cooked mode when it exits.
|
|
@@ -142,6 +139,20 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
142
139
|
}
|
|
143
140
|
render();
|
|
144
141
|
};
|
|
142
|
+
// Input-history recall. On ↑/↓ when there's nothing to scroll, and always on
|
|
143
|
+
// Ctrl+P / Ctrl+N (so history stays reachable even in a long, scrollable chat).
|
|
144
|
+
const histPrev = () => {
|
|
145
|
+
if (!history.length)
|
|
146
|
+
return;
|
|
147
|
+
histIdx = Math.max(0, histIdx - 1);
|
|
148
|
+
setInput(history[histIdx] ?? "", false);
|
|
149
|
+
};
|
|
150
|
+
const histNext = () => {
|
|
151
|
+
if (!history.length)
|
|
152
|
+
return;
|
|
153
|
+
histIdx = Math.min(history.length, histIdx + 1);
|
|
154
|
+
setInput(histIdx === history.length ? "" : history[histIdx], false);
|
|
155
|
+
};
|
|
145
156
|
const acceptGhost = () => {
|
|
146
157
|
const ghost = ghostCompletion(input);
|
|
147
158
|
if (paletteOpen && cursor === input.length && ghost) {
|
|
@@ -163,6 +174,15 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
163
174
|
const handleKey = (str, key) => {
|
|
164
175
|
if (!key)
|
|
165
176
|
return;
|
|
177
|
+
if (DEBUG_INPUT) {
|
|
178
|
+
screenPrint([
|
|
179
|
+
` \x1b[36m[key] name=${key.name ?? "?"} ctrl=${!!key.ctrl} shift=${!!key.shift} meta=${!!key.meta} str=${JSON.stringify(str)}\x1b[0m`
|
|
180
|
+
]);
|
|
181
|
+
}
|
|
182
|
+
// Drop stray CSI fragments (e.g. mouse-report leftovers) so they can never
|
|
183
|
+
// land in the input buffer — wheel scrolling is handled by the screen engine.
|
|
184
|
+
if (key.name === undefined && !str)
|
|
185
|
+
return;
|
|
166
186
|
if (key.ctrl && key.name === "c") {
|
|
167
187
|
if (input.length)
|
|
168
188
|
return setInput("");
|
|
@@ -171,8 +191,7 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
171
191
|
if (key.ctrl && key.name === "d")
|
|
172
192
|
return finish({ type: "exit" });
|
|
173
193
|
if (key.ctrl && key.name === "l") {
|
|
174
|
-
|
|
175
|
-
prevUp = 0;
|
|
194
|
+
screenClear();
|
|
176
195
|
return render();
|
|
177
196
|
}
|
|
178
197
|
if (key.ctrl && (key.name === "u" || key.name === "k"))
|
|
@@ -181,9 +200,37 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
181
200
|
cursor = 0;
|
|
182
201
|
return render();
|
|
183
202
|
}
|
|
203
|
+
// History recall (always available; ↑/↓ scroll the transcript instead when
|
|
204
|
+
// there's history to scroll through).
|
|
205
|
+
if (key.ctrl && key.name === "p")
|
|
206
|
+
return histPrev();
|
|
207
|
+
if (key.ctrl && key.name === "n")
|
|
208
|
+
return histNext();
|
|
209
|
+
// ── Transcript scrolling (composer stays fixed) ──────────────────────
|
|
210
|
+
// Page keys always scroll history; Shift+↑/↓ nudge a line. Home/End scroll
|
|
211
|
+
// to the top/latest only when the input is empty (else they move the
|
|
212
|
+
// cursor). Mouse-wheel scrolling is handled in the screen engine.
|
|
213
|
+
if (key.name === "pageup")
|
|
214
|
+
return screenScrollPage(1);
|
|
215
|
+
if (key.name === "pagedown")
|
|
216
|
+
return screenScrollPage(-1);
|
|
217
|
+
if (key.shift && key.name === "up")
|
|
218
|
+
return screenScrollLines(1);
|
|
219
|
+
if (key.shift && key.name === "down")
|
|
220
|
+
return screenScrollLines(-1);
|
|
221
|
+
if (key.name === "home" && input.length === 0)
|
|
222
|
+
return screenScrollToTop();
|
|
223
|
+
if (key.name === "end" && input.length === 0)
|
|
224
|
+
return screenScrollToBottom();
|
|
184
225
|
if (key.name === "return" || key.name === "enter") {
|
|
185
|
-
//
|
|
186
|
-
//
|
|
226
|
+
// Value-entry (`/color <value>`): run what's typed, completing any ghost
|
|
227
|
+
// first (`/color gr` → `/color green`; a bare `/color ` → the picker).
|
|
228
|
+
if (paletteOpen && /^\/(?:color|theme)\s/i.test(input)) {
|
|
229
|
+
const g = cursor === input.length ? ghostCompletion(input) : "";
|
|
230
|
+
return finish({ type: "line", value: (input + g).trim() });
|
|
231
|
+
}
|
|
232
|
+
// Otherwise a single Enter RUNS the highlighted command (arg-taking ones
|
|
233
|
+
// open their own picker when given none). Tab inserts-and-adds-an-arg.
|
|
187
234
|
if (paletteOpen && filtered.length) {
|
|
188
235
|
return finish({ type: "line", value: filtered[selected].name });
|
|
189
236
|
}
|
|
@@ -196,27 +243,27 @@ export function readCommand(caret, history, footer = "", placeholder = "") {
|
|
|
196
243
|
}
|
|
197
244
|
return;
|
|
198
245
|
}
|
|
246
|
+
// ↑/↓ — palette navigation; else SCROLL the transcript when there's
|
|
247
|
+
// something to scroll (this is also where the mouse WHEEL lands, since the
|
|
248
|
+
// terminal's alternate-scroll mode turns the wheel into ↑/↓ keys); else
|
|
249
|
+
// recall input history. History is always available on Ctrl+P / Ctrl+N.
|
|
199
250
|
if (key.name === "up") {
|
|
200
251
|
if (paletteOpen) {
|
|
201
252
|
selected = Math.max(0, selected - 1);
|
|
202
|
-
render();
|
|
253
|
+
return render();
|
|
203
254
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
return;
|
|
255
|
+
if (screenScrollable())
|
|
256
|
+
return screenScrollLines(1); // wheel/↑ → older
|
|
257
|
+
return histPrev();
|
|
209
258
|
}
|
|
210
259
|
if (key.name === "down") {
|
|
211
260
|
if (paletteOpen) {
|
|
212
261
|
selected = Math.min(filtered.length - 1, selected + 1);
|
|
213
|
-
render();
|
|
214
|
-
}
|
|
215
|
-
else if (history.length) {
|
|
216
|
-
histIdx = Math.min(history.length, histIdx + 1);
|
|
217
|
-
setInput(histIdx === history.length ? "" : history[histIdx], false);
|
|
262
|
+
return render();
|
|
218
263
|
}
|
|
219
|
-
|
|
264
|
+
if (screenScrollable())
|
|
265
|
+
return screenScrollLines(-1); // wheel/↓ → newer
|
|
266
|
+
return histNext();
|
|
220
267
|
}
|
|
221
268
|
if (key.name === "tab") {
|
|
222
269
|
if (acceptGhost())
|
|
@@ -9,14 +9,13 @@ export const GROUP_ORDER = [
|
|
|
9
9
|
];
|
|
10
10
|
export const COMMANDS = [
|
|
11
11
|
{ name: "/chat", desc: "Start chatting with Carrier", group: "Chat", rank: 1 },
|
|
12
|
-
{ name: "/
|
|
12
|
+
{ name: "/switch", desc: "Switch chat: Carrier ⇄ Standard", group: "Chat", rank: 2 },
|
|
13
|
+
{ name: "/clear", desc: "Start a new conversation", group: "Chat", rank: 3 },
|
|
13
14
|
{ name: "/sessions", desc: "List conversations", group: "Sessions", rank: 1 },
|
|
14
15
|
{ name: "/session", desc: "Switch / manage a session", group: "Sessions", rank: 2 },
|
|
15
16
|
{ name: "/history", desc: "Conversation transcript", group: "Sessions", rank: 3 },
|
|
16
|
-
{ name: "/
|
|
17
|
-
{ name: "/
|
|
18
|
-
{ name: "/mode", desc: "Switch Carrier mode", group: "Models", rank: 3 },
|
|
19
|
-
{ name: "/modes", desc: "List Carrier modes", group: "Models", rank: 4 },
|
|
17
|
+
{ name: "/model", desc: "List & switch provider model", group: "Models", rank: 1 },
|
|
18
|
+
{ name: "/mode", desc: "List & switch Carrier mode", group: "Models", rank: 2 },
|
|
20
19
|
{ name: "/upload", desc: "Upload a file", group: "Files", rank: 1 },
|
|
21
20
|
{ name: "/files", desc: "List uploaded files", group: "Files", rank: 2 },
|
|
22
21
|
{ name: "/whoami", desc: "Current workspace & project", group: "Account", rank: 1 },
|
package/dist/ui/live.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as readline from "node:readline";
|
|
2
2
|
import { c } from "./colors.js";
|
|
3
3
|
import { MARGIN, contentWidth } from "./composer-layout.js";
|
|
4
|
+
import { screenActive, screenCommitTransient, screenPrint, screenTransient } from "./screen.js";
|
|
4
5
|
/** Non-TTY fallback: commits print normally, the bar/status are no-ops. */
|
|
5
6
|
const passthrough = {
|
|
6
7
|
setStatus: () => { },
|
|
@@ -13,6 +14,20 @@ const passthrough = {
|
|
|
13
14
|
};
|
|
14
15
|
export function startLiveComposer(footer = "") {
|
|
15
16
|
const stdout = process.stdout;
|
|
17
|
+
// In the persistent-composer shell the screen engine owns the input bar, so a
|
|
18
|
+
// live turn just streams into the transcript above it: transient lines (spinner
|
|
19
|
+
// / word-by-word reveal) go to the transient tail; finished lines are committed.
|
|
20
|
+
if (screenActive()) {
|
|
21
|
+
return {
|
|
22
|
+
setStatus: (lines) => screenTransient(lines),
|
|
23
|
+
commitStatus: () => screenCommitTransient(),
|
|
24
|
+
commit: (lines) => {
|
|
25
|
+
if (lines.length)
|
|
26
|
+
screenPrint(lines);
|
|
27
|
+
},
|
|
28
|
+
stop: () => screenTransient([])
|
|
29
|
+
};
|
|
30
|
+
}
|
|
16
31
|
if (!stdout.isTTY)
|
|
17
32
|
return passthrough;
|
|
18
33
|
let status = [];
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistent bottom-composer TUI engine (Claude Code–style).
|
|
3
|
+
*
|
|
4
|
+
* The terminal is a managed CANVAS, not a stream of `console.log()`s. We enter
|
|
5
|
+
* the ALTERNATE SCREEN BUFFER so the app owns the whole viewport and never
|
|
6
|
+
* pollutes the user's real scrollback — leaving TUI mode restores their terminal
|
|
7
|
+
* exactly as it was. The screen is split into two permanent regions:
|
|
8
|
+
*
|
|
9
|
+
* ┌───────────────────────────────┐
|
|
10
|
+
* │ transcript (our OWN scroll) │ history · activity · streaming · output
|
|
11
|
+
* │ … │
|
|
12
|
+
* ├───────────────────────────────┤
|
|
13
|
+
* │ ❯ Ask Carrier OS… │ the FIXED composer — never moves/scrolls
|
|
14
|
+
* └───────────────────────────────┘
|
|
15
|
+
*
|
|
16
|
+
* A virtual screen buffer is diffed against what's on-screen, so each frame only
|
|
17
|
+
* rewrites the rows that actually changed — no full repaints, no flicker. All
|
|
18
|
+
* positioning is ABSOLUTE (no DECSTBM scroll regions), so it behaves identically
|
|
19
|
+
* across Windows Terminal, PowerShell, CMD, Git Bash, macOS Terminal and Linux.
|
|
20
|
+
*
|
|
21
|
+
* Scrolling is OURS: the transcript is a virtualized window over an in-memory
|
|
22
|
+
* line buffer. `scrollOffset` counts lines up from the bottom; 0 = following the
|
|
23
|
+
* newest output ("pinned"). While pinned, new/streaming output stays in view;
|
|
24
|
+
* once the user scrolls up we HOLD their position as content grows below, so you
|
|
25
|
+
* can browse history while a reply is still generating.
|
|
26
|
+
*/
|
|
27
|
+
const out = process.stdout;
|
|
28
|
+
const ESC = "\x1b[";
|
|
29
|
+
const HIDE = `${ESC}?25l`;
|
|
30
|
+
const SHOW = `${ESC}?25h`;
|
|
31
|
+
const ALT_ON = `${ESC}?1049h`; // enter alternate screen buffer (own the canvas)
|
|
32
|
+
const ALT_OFF = `${ESC}?1049l`; // leave it → restores the user's real terminal
|
|
33
|
+
// Alternate scroll mode: in the alt-screen the terminal converts the mouse WHEEL
|
|
34
|
+
// into arrow-key presses sent to the app. This is what makes the wheel work in
|
|
35
|
+
// terminals (notably VS Code's) that DON'T forward SGR mouse reports to apps —
|
|
36
|
+
// the wheel arrives as ↑/↓ keys, which we map to scrolling (see command-palette).
|
|
37
|
+
const SCROLL_ON = `${ESC}?1007h`;
|
|
38
|
+
const SCROLL_OFF = `${ESC}?1007l`;
|
|
39
|
+
/** Fixed composer height: input rule / prompt / rule + one footer row. */
|
|
40
|
+
const CORE_H = 4;
|
|
41
|
+
/** Cap on retained transcript lines (we repaint, not native-scroll). */
|
|
42
|
+
const MAX_SCROLLBACK = 5000;
|
|
43
|
+
let active = false;
|
|
44
|
+
let committed = []; // permanent transcript lines
|
|
45
|
+
let transient = []; // in-progress tail (spinner / streaming), below `committed`
|
|
46
|
+
let palette = []; // slash-palette overlay (empty when closed)
|
|
47
|
+
let bar = []; // the fixed composer core (CORE_H lines)
|
|
48
|
+
let caretBarIndex = 1; // which composer row the caret sits on (1 = prompt line)
|
|
49
|
+
let caretCol = 1; // 1-based column of the caret
|
|
50
|
+
let showCaret = false;
|
|
51
|
+
// Scroll state — see file header. `pinned` ⇒ auto-follow the newest output.
|
|
52
|
+
let scrollOffset = 0;
|
|
53
|
+
let pinned = true;
|
|
54
|
+
let shown = []; // what is currently on each terminal row (0 = row 1)
|
|
55
|
+
const rows = () => out.rows ?? 24;
|
|
56
|
+
const write = (s) => {
|
|
57
|
+
out.write(s);
|
|
58
|
+
};
|
|
59
|
+
export function screenActive() {
|
|
60
|
+
return active;
|
|
61
|
+
}
|
|
62
|
+
/** Layout metrics for the current frame (region sizes + the full line buffer). */
|
|
63
|
+
function metrics() {
|
|
64
|
+
const h = rows();
|
|
65
|
+
const core = bar.length || CORE_H;
|
|
66
|
+
const tb = Math.max(1, h - core); // transcript occupies rows 1..tb
|
|
67
|
+
const pH = Math.min(palette.length, Math.max(0, tb - 1)); // overlay never covers the whole region
|
|
68
|
+
const contentH = Math.max(0, tb - pH); // transcript rows above the palette overlay
|
|
69
|
+
const all = transient.length ? committed.concat(transient) : committed;
|
|
70
|
+
const maxOffset = Math.max(0, all.length - contentH);
|
|
71
|
+
return { h, core, tb, pH, contentH, all, maxOffset };
|
|
72
|
+
}
|
|
73
|
+
/** Clamp the scroll offset into range and recompute the pinned flag. */
|
|
74
|
+
function clampScroll() {
|
|
75
|
+
const { maxOffset } = metrics();
|
|
76
|
+
if (scrollOffset < 0)
|
|
77
|
+
scrollOffset = 0;
|
|
78
|
+
if (scrollOffset > maxOffset)
|
|
79
|
+
scrollOffset = maxOffset;
|
|
80
|
+
pinned = scrollOffset === 0;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* When content is appended below while the user is scrolled up, grow the offset
|
|
84
|
+
* by the number of added lines so their view stays put (Claude-Code behaviour).
|
|
85
|
+
* When pinned, do nothing — flush() will render the newest tail.
|
|
86
|
+
*/
|
|
87
|
+
function onContentGrew(added) {
|
|
88
|
+
if (!pinned && added > 0) {
|
|
89
|
+
scrollOffset += added;
|
|
90
|
+
const { maxOffset } = metrics();
|
|
91
|
+
if (scrollOffset > maxOffset)
|
|
92
|
+
scrollOffset = maxOffset;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Build the full desired screen: [transcript window][palette][composer core]. */
|
|
96
|
+
function compose() {
|
|
97
|
+
const { h, tb, pH, contentH, all, maxOffset } = metrics();
|
|
98
|
+
if (scrollOffset > maxOffset)
|
|
99
|
+
scrollOffset = maxOffset; // stay valid across resizes
|
|
100
|
+
// The visible window into `all`. offset 0 = last `contentH` lines (the tail);
|
|
101
|
+
// offset maxOffset = the very top.
|
|
102
|
+
const end = all.length - scrollOffset;
|
|
103
|
+
const start = Math.max(0, end - contentH);
|
|
104
|
+
const win = all.slice(start, end);
|
|
105
|
+
const desired = new Array(h).fill("");
|
|
106
|
+
for (let i = 0; i < contentH; i++)
|
|
107
|
+
desired[i] = win[i] ?? "";
|
|
108
|
+
// A subtle "more below" hint on the last transcript row when scrolled up.
|
|
109
|
+
if (scrollOffset > 0 && contentH > 0) {
|
|
110
|
+
desired[contentH - 1] = `${ESC}2m ↓ ${scrollOffset} more line${scrollOffset === 1 ? "" : "s"} — End to jump to latest${ESC}0m`;
|
|
111
|
+
}
|
|
112
|
+
for (let i = 0; i < pH; i++)
|
|
113
|
+
desired[contentH + i] = palette[i] ?? "";
|
|
114
|
+
for (let i = 0; i < (bar.length || CORE_H); i++)
|
|
115
|
+
desired[tb + i] = bar[i] ?? "";
|
|
116
|
+
return desired;
|
|
117
|
+
}
|
|
118
|
+
/** Diff the desired screen against what's on-screen; rewrite only changed rows. */
|
|
119
|
+
function flush() {
|
|
120
|
+
if (!active)
|
|
121
|
+
return;
|
|
122
|
+
const desired = compose();
|
|
123
|
+
const n = Math.max(desired.length, shown.length);
|
|
124
|
+
let s = HIDE;
|
|
125
|
+
for (let r = 0; r < n; r++) {
|
|
126
|
+
const d = desired[r] ?? "";
|
|
127
|
+
if (d !== (shown[r] ?? ""))
|
|
128
|
+
s += `${ESC}${r + 1};1H${ESC}2K` + d;
|
|
129
|
+
}
|
|
130
|
+
shown = desired;
|
|
131
|
+
const core = bar.length || CORE_H;
|
|
132
|
+
const caretRow = rows() - core + 1 + caretBarIndex;
|
|
133
|
+
// The composer is a permanent region, so its caret shows whenever the composer
|
|
134
|
+
// is active — independent of where the transcript is scrolled.
|
|
135
|
+
s += showCaret ? `${ESC}${caretRow};${caretCol}H${SHOW}` : HIDE;
|
|
136
|
+
write(s);
|
|
137
|
+
}
|
|
138
|
+
/** Enter TUI mode: alt-screen, alt-scroll (wheel→arrows), clear, hide caret. */
|
|
139
|
+
export function startScreen() {
|
|
140
|
+
if (!out.isTTY)
|
|
141
|
+
return;
|
|
142
|
+
active = true;
|
|
143
|
+
committed = [];
|
|
144
|
+
transient = [];
|
|
145
|
+
palette = [];
|
|
146
|
+
bar = [];
|
|
147
|
+
shown = [];
|
|
148
|
+
scrollOffset = 0;
|
|
149
|
+
pinned = true;
|
|
150
|
+
write(`${ALT_ON}${SCROLL_ON}${ESC}2J${ESC}H${HIDE}`);
|
|
151
|
+
flush();
|
|
152
|
+
out.on("resize", screenResize);
|
|
153
|
+
}
|
|
154
|
+
/** Leave TUI mode: disable alt-scroll, leave alt-screen (restores real terminal). */
|
|
155
|
+
export function stopScreen() {
|
|
156
|
+
if (!active)
|
|
157
|
+
return;
|
|
158
|
+
active = false;
|
|
159
|
+
out.removeListener("resize", screenResize);
|
|
160
|
+
write(`${SCROLL_OFF}${ALT_OFF}${SHOW}`);
|
|
161
|
+
shown = [];
|
|
162
|
+
}
|
|
163
|
+
/** Trim the transcript scrollback to its cap (drop oldest lines). */
|
|
164
|
+
function trim() {
|
|
165
|
+
if (committed.length > MAX_SCROLLBACK)
|
|
166
|
+
committed.splice(0, committed.length - MAX_SCROLLBACK);
|
|
167
|
+
}
|
|
168
|
+
/** Append permanent transcript lines (multi-line strings are split). */
|
|
169
|
+
export function screenPrint(lines) {
|
|
170
|
+
let added = 0;
|
|
171
|
+
for (const l of lines) {
|
|
172
|
+
const parts = l.includes("\n") ? l.split("\n") : [l];
|
|
173
|
+
committed.push(...parts);
|
|
174
|
+
added += parts.length;
|
|
175
|
+
}
|
|
176
|
+
trim();
|
|
177
|
+
onContentGrew(added);
|
|
178
|
+
flush();
|
|
179
|
+
}
|
|
180
|
+
/** Set the in-progress tail shown just below the committed transcript. */
|
|
181
|
+
export function screenTransient(lines) {
|
|
182
|
+
const prev = transient.length;
|
|
183
|
+
transient = lines;
|
|
184
|
+
onContentGrew(transient.length - prev);
|
|
185
|
+
flush();
|
|
186
|
+
}
|
|
187
|
+
/** Promote the current transient tail to permanent transcript (no visual jump). */
|
|
188
|
+
export function screenCommitTransient() {
|
|
189
|
+
if (transient.length) {
|
|
190
|
+
committed.push(...transient);
|
|
191
|
+
transient = [];
|
|
192
|
+
trim();
|
|
193
|
+
}
|
|
194
|
+
flush();
|
|
195
|
+
}
|
|
196
|
+
/** Clear the whole transcript (Ctrl+L) and re-pin to the bottom. */
|
|
197
|
+
export function screenClear() {
|
|
198
|
+
committed = [];
|
|
199
|
+
transient = [];
|
|
200
|
+
shown = [];
|
|
201
|
+
scrollOffset = 0;
|
|
202
|
+
pinned = true;
|
|
203
|
+
write(`${ESC}2J`);
|
|
204
|
+
flush();
|
|
205
|
+
}
|
|
206
|
+
// ── Scroll API (keyboard + wheel) ─────────────────────────────────────────────
|
|
207
|
+
/** Scroll by `delta` lines: +up (older), −down (newer). Re-pins at the bottom. */
|
|
208
|
+
export function screenScrollLines(delta) {
|
|
209
|
+
if (!active)
|
|
210
|
+
return;
|
|
211
|
+
scrollOffset += delta;
|
|
212
|
+
clampScroll();
|
|
213
|
+
flush();
|
|
214
|
+
}
|
|
215
|
+
/** Scroll a (near-)page in the given direction: +1 up, −1 down. */
|
|
216
|
+
export function screenScrollPage(dir) {
|
|
217
|
+
const { contentH } = metrics();
|
|
218
|
+
screenScrollLines(dir * Math.max(1, contentH - 1));
|
|
219
|
+
}
|
|
220
|
+
/** Jump to the oldest content. */
|
|
221
|
+
export function screenScrollToTop() {
|
|
222
|
+
if (!active)
|
|
223
|
+
return;
|
|
224
|
+
const { maxOffset } = metrics();
|
|
225
|
+
scrollOffset = maxOffset;
|
|
226
|
+
pinned = maxOffset === 0;
|
|
227
|
+
flush();
|
|
228
|
+
}
|
|
229
|
+
/** Jump to the newest content and re-pin (auto-follow). */
|
|
230
|
+
export function screenScrollToBottom() {
|
|
231
|
+
if (!active)
|
|
232
|
+
return;
|
|
233
|
+
scrollOffset = 0;
|
|
234
|
+
pinned = true;
|
|
235
|
+
flush();
|
|
236
|
+
}
|
|
237
|
+
/** True when following the newest output (used to gate auto-follow decisions). */
|
|
238
|
+
export function screenIsPinned() {
|
|
239
|
+
return pinned;
|
|
240
|
+
}
|
|
241
|
+
/** True when the transcript is taller than the viewport (i.e. can be scrolled). */
|
|
242
|
+
export function screenScrollable() {
|
|
243
|
+
return metrics().maxOffset > 0;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Draw the composer: the fixed input `core` (CORE_H lines) at the bottom, with
|
|
247
|
+
* the `palette` overlaying the transcript rows directly above it. `caretIndex`
|
|
248
|
+
* is the core row the caret sits on (1 = prompt); `col` is its 1-based column.
|
|
249
|
+
*/
|
|
250
|
+
export function screenComposer(paletteLines, coreLines, caretIndex, col) {
|
|
251
|
+
palette = paletteLines;
|
|
252
|
+
bar = coreLines;
|
|
253
|
+
caretBarIndex = caretIndex;
|
|
254
|
+
caretCol = col;
|
|
255
|
+
showCaret = true;
|
|
256
|
+
flush();
|
|
257
|
+
}
|
|
258
|
+
/** Hide the caret (e.g. while a response streams — the ▋ is drawn in the text). */
|
|
259
|
+
export function screenHideCaret() {
|
|
260
|
+
showCaret = false;
|
|
261
|
+
flush();
|
|
262
|
+
}
|
|
263
|
+
/** Recompute + full-repaint after a terminal resize (composer stays pinned). */
|
|
264
|
+
export function screenResize() {
|
|
265
|
+
if (!active)
|
|
266
|
+
return;
|
|
267
|
+
shown = []; // force every row to be rewritten at the new size
|
|
268
|
+
clampScroll();
|
|
269
|
+
write(`${ESC}2J`);
|
|
270
|
+
flush();
|
|
271
|
+
}
|
|
272
|
+
/** Temporarily hand the terminal to an external UI (inquirer picker, etc.). */
|
|
273
|
+
export function suspendScreen() {
|
|
274
|
+
if (!active)
|
|
275
|
+
return;
|
|
276
|
+
active = false;
|
|
277
|
+
out.removeListener("resize", screenResize);
|
|
278
|
+
write(`${SCROLL_OFF}${ALT_OFF}${SHOW}`);
|
|
279
|
+
}
|
|
280
|
+
/** Resume TUI mode after suspendScreen(), repainting the preserved transcript. */
|
|
281
|
+
export function resumeScreen() {
|
|
282
|
+
if (!out.isTTY)
|
|
283
|
+
return;
|
|
284
|
+
active = true;
|
|
285
|
+
shown = [];
|
|
286
|
+
write(`${ALT_ON}${SCROLL_ON}${ESC}2J${ESC}H${HIDE}`);
|
|
287
|
+
flush();
|
|
288
|
+
out.on("resize", screenResize);
|
|
289
|
+
}
|
package/dist/ui/select.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as readline from "node:readline";
|
|
2
2
|
import { c } from "./colors.js";
|
|
3
|
+
import { resumeScreen, screenActive, suspendScreen } from "./screen.js";
|
|
3
4
|
import { termWidth } from "./ui.js";
|
|
4
5
|
export function pickFromList(opts) {
|
|
5
6
|
const { title, description = "", choices } = opts;
|
|
@@ -9,6 +10,11 @@ export function pickFromList(opts) {
|
|
|
9
10
|
return new Promise((resolve) => {
|
|
10
11
|
const stdin = process.stdin;
|
|
11
12
|
const stdout = process.stdout;
|
|
13
|
+
// Hand the terminal over from the persistent-composer engine for the picker,
|
|
14
|
+
// then repaint the transcript + composer when it closes.
|
|
15
|
+
const suspended = screenActive();
|
|
16
|
+
if (suspended)
|
|
17
|
+
suspendScreen();
|
|
12
18
|
stdin.removeAllListeners("keypress");
|
|
13
19
|
readline.emitKeypressEvents(stdin);
|
|
14
20
|
const prevRaw = stdin.isRaw ?? false;
|
|
@@ -57,6 +63,8 @@ export function pickFromList(opts) {
|
|
|
57
63
|
readline.clearScreenDown(stdout);
|
|
58
64
|
stdin.removeListener("keypress", onKey);
|
|
59
65
|
stdin.setRawMode(prevRaw);
|
|
66
|
+
if (suspended)
|
|
67
|
+
resumeScreen(); // repaint the pinned composer + transcript
|
|
60
68
|
resolve(val);
|
|
61
69
|
};
|
|
62
70
|
const onKey = (str, key) => {
|
package/dist/ui/theme.js
CHANGED
|
@@ -9,6 +9,12 @@ import chalk from "chalk";
|
|
|
9
9
|
* Future-ready: this is the seam a fuller theme (background, font, links) would
|
|
10
10
|
* plug into — add fields here, not scattered Chalk calls across the app.
|
|
11
11
|
*/
|
|
12
|
+
/**
|
|
13
|
+
* The Carrier OS brand green — FIXED. Brand elements (the "CARRIER OS" wordmark,
|
|
14
|
+
* the prompt marker) always render in this and never follow the runtime accent,
|
|
15
|
+
* so `/color` only re-themes the accent surfaces (rules, selection, etc.).
|
|
16
|
+
*/
|
|
17
|
+
export const BRAND_HEX = "#9ef56b";
|
|
12
18
|
/** Named accent colors and their hex. `default` is Carrier OS green. */
|
|
13
19
|
export const THEME_COLORS = {
|
|
14
20
|
green: "#9ef56b",
|
|
@@ -46,6 +52,17 @@ export function accentName() {
|
|
|
46
52
|
export function accentHex(name = current) {
|
|
47
53
|
return THEME_COLORS[name] ?? THEME_COLORS.green;
|
|
48
54
|
}
|
|
55
|
+
/** Mix a hex color toward white by `t` (0 = unchanged, 1 = white). */
|
|
56
|
+
function lighten(hex, t) {
|
|
57
|
+
const n = parseInt(hex.slice(1), 16);
|
|
58
|
+
const ch = [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
59
|
+
return "#" + ch.map((v) => Math.round(v + (255 - v) * t).toString(16).padStart(2, "0")).join("");
|
|
60
|
+
}
|
|
61
|
+
/** A light tint of the current accent — for soft accent marks (e.g. the
|
|
62
|
+
* assistant's lead dot), so they read as "theme-light" rather than pure white. */
|
|
63
|
+
export function accentLightHex(name = current) {
|
|
64
|
+
return lighten(accentHex(name), 0.5);
|
|
65
|
+
}
|
|
49
66
|
/** Switch the live accent. Takes effect immediately across the UI. */
|
|
50
67
|
export function setAccent(name) {
|
|
51
68
|
if (isThemeColor(name))
|