xonecas.io
64875
Mc

mini-coder / commit fcb9ca9

chore: cel tui 0.9.x upgrades

xonecas2026-07-13 fcb9ca9cba4482d7cd95b0bbad9fbb9ffab451e5 parent 07caae2

7 files changed

src/shared.ts+0−15
@@ -61,21 +61,6 @@ export function relativeTime(timestamp: number): string {
6161 return elapsedTime(seconds);
6262 }
6363
64-export function onceEvery<T extends unknown[]>(
65- n: number,
66- fn: (...args: T) => void,
67-) {
68- let calls = 0;
69-
70- return (...args: T) => {
71- calls++;
72-
73- if (calls % n === 0) {
74- fn(...args);
75- }
76- };
77-}
78-
7964 export function takeTail<T>(arr: T[], x: number): T[] {
8065 return x <= 0 ? [] : arr.slice(-x);
8166 }
src/themes.ts+54−67
@@ -19,25 +19,6 @@ export interface TUIThemeDefinition {
1919 userMessageBgColor?: Color;
2020 }
2121
22-const ANSI_SLOT_HEX: Record<Color, string> = {
23- color00: "#000000",
24- color01: "#cd3131",
25- color02: "#0dbc79",
26- color03: "#e5e510",
27- color04: "#2472c8",
28- color05: "#bc3fbc",
29- color06: "#11a8cd",
30- color07: "#e5e5e5",
31- color08: "#666666",
32- color09: "#f14c4c",
33- color10: "#23d18b",
34- color11: "#f5f543",
35- color12: "#3b8eea",
36- color13: "#d670d6",
37- color14: "#29b8db",
38- color15: "#ffffff",
39-};
40-
4122 export const theme = {
4223 black: "color00",
4324 red: "color01",
@@ -114,58 +95,64 @@ const molokaiLightPalette: Theme = {
11495 color15: "#fffdf5",
11596 };
11697
117-const slotColor = (slot: Color) => ANSI_SLOT_HEX[slot];
118-
119-function syntaxScope(
120- scope: string | readonly string[],
121- foreground: Color,
122- fontStyle?: string,
123-) {
124- return {
125- scope,
126- settings: {
127- foreground: slotColor(foreground),
128- ...(fontStyle ? { fontStyle } : {}),
129- },
130- };
131-}
132-
13398 const molokaiDarkSyntax = {
134- name: "mini-coder-molokai-dark",
135- type: "dark",
136- fg: slotColor("color07"),
137- tokenColors: [
138- syntaxScope(["comment", "markup.quote"], "color08", "italic"),
139- syntaxScope(["keyword", "operator"], "color05"),
140- syntaxScope(["string", "escape", "markup.list"], "color03"),
141- syntaxScope(["number", "regexp"], "color13"),
142- syntaxScope(["function", "command", "markup.code"], "color10"),
143- syntaxScope(["builtin", "property", "type", "meta"], "color06"),
144- syntaxScope("markup.heading", "color04", "bold"),
145- syntaxScope(["diff.deleted", "diff.file.old"], "color09"),
146- syntaxScope(["diff.inserted", "diff.file.new"], "color10"),
147- syntaxScope("diff.hunk", "color13"),
148- syntaxScope(["diff.header", "diff.no-newline"], "color08"),
149- ],
99+ baseStyle: { fgColor: theme.white },
100+ scopeStyles: {
101+ comment: { fgColor: theme.bblack, italic: true },
102+ "markup.quote": { fgColor: theme.bblack, italic: true },
103+ keyword: { fgColor: theme.magenta },
104+ operator: { fgColor: theme.magenta },
105+ string: { fgColor: theme.yellow },
106+ escape: { fgColor: theme.yellow },
107+ "markup.list": { fgColor: theme.yellow },
108+ number: { fgColor: theme.bmagenta },
109+ regexp: { fgColor: theme.bmagenta },
110+ function: { fgColor: theme.bgreen },
111+ command: { fgColor: theme.bgreen },
112+ "markup.code": { fgColor: theme.bgreen },
113+ builtin: { fgColor: theme.cyan },
114+ property: { fgColor: theme.cyan },
115+ type: { fgColor: theme.cyan },
116+ meta: { fgColor: theme.cyan },
117+ "markup.heading": { fgColor: theme.blue, bold: true },
118+ "diff.deleted": { fgColor: theme.bred },
119+ "diff.file.old": { fgColor: theme.bred },
120+ "diff.inserted": { fgColor: theme.bgreen },
121+ "diff.file.new": { fgColor: theme.bgreen },
122+ "diff.hunk": { fgColor: theme.bmagenta },
123+ "diff.header": { fgColor: theme.bblack },
124+ "diff.no-newline": { fgColor: theme.bblack },
125+ },
150126 } as const satisfies SyntaxHighlightTheme;
151127
152128 const molokaiLightSyntax = {
153- name: "mini-coder-molokai-light",
154- type: "light",
155- fg: slotColor("color00"),
156- tokenColors: [
157- syntaxScope(["comment", "markup.quote"], "color08", "italic"),
158- syntaxScope(["keyword", "operator"], "color13"),
159- syntaxScope(["string", "escape", "markup.list"], "color11"),
160- syntaxScope(["number", "regexp"], "color09"),
161- syntaxScope(["function", "command", "markup.code"], "color10"),
162- syntaxScope(["builtin", "property", "type", "meta"], "color14"),
163- syntaxScope("markup.heading", "color12", "bold"),
164- syntaxScope(["diff.deleted", "diff.file.old"], "color09"),
165- syntaxScope(["diff.inserted", "diff.file.new"], "color10"),
166- syntaxScope("diff.hunk", "color13"),
167- syntaxScope(["diff.header", "diff.no-newline"], "color08"),
168- ],
129+ baseStyle: { fgColor: theme.black },
130+ scopeStyles: {
131+ comment: { fgColor: theme.bblack, italic: true },
132+ "markup.quote": { fgColor: theme.bblack, italic: true },
133+ keyword: { fgColor: theme.bmagenta },
134+ operator: { fgColor: theme.bmagenta },
135+ string: { fgColor: theme.byellow },
136+ escape: { fgColor: theme.byellow },
137+ "markup.list": { fgColor: theme.byellow },
138+ number: { fgColor: theme.bred },
139+ regexp: { fgColor: theme.bred },
140+ function: { fgColor: theme.bgreen },
141+ command: { fgColor: theme.bgreen },
142+ "markup.code": { fgColor: theme.bgreen },
143+ builtin: { fgColor: theme.bcyan },
144+ property: { fgColor: theme.bcyan },
145+ type: { fgColor: theme.bcyan },
146+ meta: { fgColor: theme.bcyan },
147+ "markup.heading": { fgColor: theme.bblue, bold: true },
148+ "diff.deleted": { fgColor: theme.bred },
149+ "diff.file.old": { fgColor: theme.bred },
150+ "diff.inserted": { fgColor: theme.bgreen },
151+ "diff.file.new": { fgColor: theme.bgreen },
152+ "diff.hunk": { fgColor: theme.bmagenta },
153+ "diff.header": { fgColor: theme.bblack },
154+ "diff.no-newline": { fgColor: theme.bblack },
155+ },
169156 } as const satisfies SyntaxHighlightTheme;
170157
171158 export const DEFAULT_TUI_THEME_ID: TUIThemeId = "ansi16";
src/tui-components.ts+43−45
@@ -1,5 +1,5 @@
1+import { Spinner as CelSpinner } from "@cel-tui/components";
12 import { type Color, HStack, Text } from "@cel-tui/core";
2-import { onceEvery } from "./shared";
33 import { textColorForBackground, theme } from "./themes";
44 import type { TUIState } from "./types";
55
@@ -18,50 +18,48 @@ export function TextPill(
1818 ]);
1919 }
2020
21-export function Spinner() {
22- const spinnerFrames = [
23- "⠁",
24- "⠂",
25- "⠄",
26- "⡀",
27- "⡈",
28- "⡐",
29- "⡠",
30- "⣀",
31- "⣁",
32- "⣂",
33- "⣄",
34- "⣌",
35- "⣔",
36- "⣤",
37- "⣥",
38- "⣦",
39- "⣮",
40- "⣶",
41- "⣷",
42- "⣿",
43- "⡿",
44- "⠿",
45- "⢟",
46- "⠟",
47- "⡛",
48- "⠛",
49- "⠫",
50- "⢋",
51- "⠋",
52- "⠍",
53- "⡉",
54- "⠉",
55- "⠑",
56- "⠡",
57- "⢁",
58- ];
59- let spinnerTick = 0;
60- const spinnerEvery = onceEvery(4, () => spinnerTick++);
61- const currentSpinner = () =>
62- spinnerFrames[spinnerTick % spinnerFrames.length];
63-
64- return { spinnerEvery, currentSpinner };
21+export function Spinner(onFrame?: (frame: string) => void) {
22+ return CelSpinner({
23+ frames: [
24+ "⠁",
25+ "⠂",
26+ "⠄",
27+ "⡀",
28+ "⡈",
29+ "⡐",
30+ "⡠",
31+ "⣀",
32+ "⣁",
33+ "⣂",
34+ "⣄",
35+ "⣌",
36+ "⣔",
37+ "⣤",
38+ "⣥",
39+ "⣦",
40+ "⣮",
41+ "⣶",
42+ "⣷",
43+ "⣿",
44+ "⡿",
45+ "⠿",
46+ "⢟",
47+ "⠟",
48+ "⡛",
49+ "⠛",
50+ "⠫",
51+ "⢋",
52+ "⠋",
53+ "⠍",
54+ "⡉",
55+ "⠉",
56+ "⠑",
57+ "⠡",
58+ "⢁",
59+ ],
60+ maxFps: 15,
61+ onFrame,
62+ });
6563 }
6664
6765 export function ActivityPill(state: TUIState, spinnerFrame: string) {
src/tui-conversation.ts+32−2
@@ -1,4 +1,5 @@
11 import {
2+ createSyntaxHighlight,
23 SyntaxHighlight,
34 type SyntaxHighlightTheme,
45 } from "@cel-tui/components";
@@ -8,6 +9,31 @@ import { getTUITheme, textColorForBackground } from "./themes";
89 import { TextPill, theme } from "./tui-components";
910 import type { TUIMessage, TUIState, TUIToolCall } from "./types";
1011
12+const streamingHighlighter = createSyntaxHighlight();
13+let streamingMessage: TUIMessage | undefined;
14+
15+function highlightMessage(
16+ message: TUIMessage,
17+ syntaxTheme: SyntaxHighlightTheme,
18+ streaming: boolean,
19+) {
20+ if (!streaming) {
21+ if (streamingMessage === message) {
22+ streamingHighlighter.dispose();
23+ streamingMessage = undefined;
24+ }
25+ return SyntaxHighlight(message.text, "markdown", { theme: syntaxTheme });
26+ }
27+
28+ if (streamingMessage !== message) {
29+ streamingHighlighter.dispose();
30+ streamingMessage = message;
31+ }
32+ return streamingHighlighter(message.text, "markdown", {
33+ theme: syntaxTheme,
34+ });
35+}
36+
1137 export function emptyState(state: TUIState): Node {
1238 const randColor = theme.bgreen;
1339 const shortcutFgColor =
@@ -130,6 +156,7 @@ function ConversationMessage(
130156 message: TUIMessage,
131157 syntaxTheme: SyntaxHighlightTheme,
132158 userMessageBgColor: Color | undefined,
159+ streaming: boolean,
133160 ) {
134161 const blocks: Node[] = [];
135162
@@ -152,7 +179,7 @@ function ConversationMessage(
152179 bgColor: message.role === "user" ? userMessageBgColor : undefined,
153180 padding: { y: 1, x: message.role === "user" ? 1 : 0 },
154181 },
155- [SyntaxHighlight(message.text, "markdown", { theme: syntaxTheme })],
182+ [highlightMessage(message, syntaxTheme, streaming)],
156183 ),
157184 );
158185 }
@@ -208,11 +235,14 @@ export function Conversation(state: TUIState) {
208235 state.stickToBottom = offset >= maxOffset;
209236 },
210237 },
211- state.tuiMessages.map((message) =>
238+ state.tuiMessages.map((message, index) =>
212239 ConversationMessage(
213240 message,
214241 activeTheme.syntax,
215242 activeTheme.userMessageBgColor,
243+ state.streaming &&
244+ message.role === "assistant" &&
245+ index === state.tuiMessages.length - 1,
216246 ),
217247 ),
218248 );
src/tui-overlay.ts+95−213
@@ -1,4 +1,5 @@
1-import { cel, HStack, Text, TextInput, VStack } from "@cel-tui/core";
1+import { Select, type SelectInstance } from "@cel-tui/components";
2+import { cel, HStack, Text, VStack } from "@cel-tui/core";
23 import type { Message, ThinkingLevel } from "@earendil-works/pi-ai";
34 import { getOAuthProviders } from "@earendil-works/pi-ai/oauth";
45 import { saveSettings } from "./args";
@@ -8,171 +9,26 @@ import { listSessionsForCwd } from "./session";
89 import { estimateContextTokens, formatTimestamp } from "./shared";
910 import { getTUITheme, TUI_THEME_IDS } from "./themes";
1011 import { TextPill, theme } from "./tui-components";
11-import type { SelectOptions, SelectState, Session, TUIState } from "./types";
12-
13-export function SelectOverlay(
14- value: string,
15- selected: string,
16- list: { label: string; value: string }[],
17- label: string,
18- onOverlayKeyPress: (key: string) => boolean | undefined,
19- onChange: (newValue: string) => void,
20- onKeyPress: (key: string) => boolean | undefined,
21-) {
22- return VStack(
23- {
24- height: "100%",
25- justifyContent: "end",
26- onKeyPress: onOverlayKeyPress,
27- },
28- [
29- VStack(
30- {
31- bgColor: theme.white,
32- fgColor: theme.bblack,
33- gap: 1,
34- padding: { x: 1, y: 1 },
35- },
36- [
37- VStack(
38- { flex: 1, minHeight: 5, maxHeight: 20, padding: { x: 1 } },
39- list.map((i) =>
40- i.value === selected
41- ? Text(i.label, { fgColor: theme.black })
42- : Text(i.label, { fgColor: theme.bblack }),
43- ),
44- ),
45-
46- HStack({ width: "100%" }, [
47- TextPill(label, theme.bwhite, theme.bblack),
48- ]),
49-
50- TextInput({
51- stateKey: "menu-filter-input",
52- value,
53- minHeight: 3,
54- maxHeight: 10,
55- padding: { x: 1 },
56- placeholder: Text("Search...", {
57- fgColor: theme.bblack,
58- italic: true,
59- }),
60- fgColor: theme.bblack,
61- bgColor: theme.white,
62- onChange,
63- onKeyPress,
64- autoFocus: true,
65- }),
66- ],
67- ),
68- ],
69- );
70-}
71-
72-export function useSelectOverlay(initialOptions: SelectOptions) {
73- // Inner state
74- const s: SelectState = {
75- value: "",
76- selected: "",
77- label: "SELECT",
78- list: [],
79- };
80- let baseList: SelectOptions["list"] = [];
81-
82- const applyOptions = (
83- options: Pick<SelectOptions, "filter" | "label" | "list">,
84- ) => {
85- baseList = options.list;
86- s.value = options.filter ?? "";
87- s.label = options.label ? options.label.toUpperCase() : "SELECT";
88- s.list = s.value
89- ? baseList.filter((i) => i.label.includes(s.value))
90- : baseList;
91- s.selected = s.list.length ? s.list[0].value : "";
92- };
93-
94- applyOptions(initialOptions);
95-
96- const onChange = (newValue: string) => {
97- s.value = newValue;
98- s.list = s.value
99- ? baseList.filter((i) => i.label.includes(s.value))
100- : baseList;
101- s.selected = s.list.length ? s.list[0].value : "";
102- };
103-
104- const moveSelected = (direction: -1 | 1) => {
105- if (!s.list.length) return;
106-
107- const currentIndex = s.list.findIndex((i) => i.value === s.selected);
108- const nextIndex =
109- currentIndex === -1
110- ? direction === 1
111- ? 0
112- : s.list.length - 1
113- : (currentIndex + direction + s.list.length) % s.list.length;
114-
115- s.selected = s.list[nextIndex].value;
116- };
117-
118- const onMoveKeyPress = (key: string) => {
119- if (key !== "up" && key !== "down") return;
120-
121- moveSelected(key === "up" ? -1 : 1);
122- return false;
123- };
124-
125- const onEditorKeyPress = (key: string) => {
126- const didMove = onMoveKeyPress(key);
127- if (didMove === false) return false;
128-
129- if (key === "enter") {
130- const previousList = s.list;
131- const previousBaseList = baseList;
132- const applySelectionResult = () => {
133- if (s.list !== previousList) {
134- baseList = s.list;
135- } else {
136- s.list = previousBaseList;
137- s.selected = s.list.length ? s.list[0].value : "";
138- }
139- };
140-
141- s.value = "";
142- const result = initialOptions.onSelect(s);
143- if (result instanceof Promise) {
144- result.then(applySelectionResult).catch(() => {
145- s.list = previousBaseList;
146- s.selected = s.list.length ? s.list[0].value : "";
147- });
148- } else {
149- applySelectionResult();
150- }
151- return false;
152- }
153- };
154-
155- const onOverlayKeyPress = (key: string) => {
156- const didMove = onMoveKeyPress(key);
157- if (didMove === false) return false;
158-
159- if (key === "escape" || key === "ctrl+p") {
160- applyOptions(initialOptions);
161- initialOptions.onCancel();
162- }
163- return false;
164- };
165-
166- return () =>
167- SelectOverlay(
168- s.value,
169- s.selected,
170- s.list,
171- s.label,
172- onOverlayKeyPress,
173- onChange,
174- onEditorKeyPress,
175- );
12+import type { Session, TUIState } from "./types";
13+
14+function SelectOverlay(label: string, select: SelectInstance) {
15+ return VStack({ height: "100%", justifyContent: "end" }, [
16+ VStack(
17+ {
18+ width: "100%",
19+ bgColor: theme.white,
20+ fgColor: theme.bblack,
21+ gap: 1,
22+ padding: { x: 1, y: 1 },
23+ },
24+ [
25+ HStack({ width: "100%" }, [
26+ TextPill(label.toUpperCase(), theme.bwhite, theme.bblack),
27+ ]),
28+ select(),
29+ ],
30+ ),
31+ ]);
17632 }
17733
17834 function cleanSnippet(text: string): string {
@@ -268,7 +124,11 @@ function forkMessages(messages: Message[], selectedIndex: number): Message[] {
268124 }
269125
270126 export function mainMenu(state: TUIState, initialPane = "main") {
271- type MenuPane = Omit<SelectOptions, "onCancel" | "onSelect">;
127+ type MenuPane = {
128+ label: string;
129+ filter: string;
130+ list: { label: string; value: string }[];
131+ };
272132
273133 const oauthProviders = getOAuthProviders();
274134 const getProviderLabel = (provider: string) =>
@@ -361,28 +221,27 @@ export function mainMenu(state: TUIState, initialPane = "main") {
361221 const panes: MenuPane[] = [effortPane, themesPane()];
362222 let currentPane = initialPane === "fork" ? forkPane() : mainPane;
363223 let selectedProvider: string | undefined;
224+ let select: SelectInstance;
364225
365- const openPane = (s: SelectState, pane: MenuPane) => {
226+ const openPane = (pane: MenuPane) => {
366227 currentPane = pane;
367- s.value = pane.filter;
368- s.label = pane.label ? pane.label.toUpperCase() : "SELECT";
369- s.list = pane.list;
370- s.selected = s.list.length ? s.list[0].value : "";
228+ select.update({
229+ items: pane.list,
230+ query: pane.filter,
231+ cursor: pane.filter.length,
232+ highlightIndex: 0,
233+ });
371234 };
372235
373- const resetMenu = (s: SelectState) => {
236+ const resetMenu = () => {
374237 currentPane = mainPane;
375238 selectedProvider = undefined;
376239 currentProviders = [];
377240 currentSessions = [];
378- s.value = "";
379- s.label = mainPane.label ? mainPane.label.toUpperCase() : "SELECT";
380- s.list = mainPane.list;
381- s.selected = s.list.length ? s.list[0].value : "";
382241 };
383242
384- const closeMenu = (s: SelectState) => {
385- resetMenu(s);
243+ const closeMenu = () => {
244+ resetMenu();
386245 state.overlay = false;
387246 };
388247
@@ -441,36 +300,53 @@ export function mainMenu(state: TUIState, initialPane = "main") {
441300 }
442301 };
443302
444- const select = useSelectOverlay({
445- ...currentPane,
446- onSelect: (s) => {
447- if (!s.selected) return;
448-
303+ select = Select({
304+ items: currentPane.list,
305+ initialQuery: currentPane.filter,
306+ stateKey: "menu-filter-input",
307+ maxVisible: 20,
308+ searchLabel: "",
309+ placeholder: "Search...",
310+ highlightColor: theme.black,
311+ fgColor: theme.bblack,
312+ bgColor: theme.white,
313+ autoFocus: true,
314+ filter: (items, query) =>
315+ query ? items.filter((item) => item.filterText.includes(query)) : items,
316+ renderRow: (item, { highlighted }) =>
317+ Text(item.label, {
318+ fgColor: highlighted ? theme.black : theme.bblack,
319+ }),
320+ onSelect: (selected) => {
449321 if (currentPane.label === "main") {
450- if (s.selected === "providers") {
451- return providersPane().then((pane) => {
452- openPane(s, pane);
453- });
322+ if (selected === "providers") {
323+ select.update({ query: "", cursor: 0, highlightIndex: 0 });
324+ void providersPane()
325+ .then(openPane)
326+ .catch(() => openPane(mainPane));
327+ return;
454328 }
455329
456- if (s.selected === "sessions") {
457- return sessionsPane().then((pane) => {
458- openPane(s, pane);
459- });
330+ if (selected === "sessions") {
331+ select.update({ query: "", cursor: 0, highlightIndex: 0 });
332+ void sessionsPane()
333+ .then(openPane)
334+ .catch(() => openPane(mainPane));
335+ return;
460336 }
461337
462- if (s.selected === "fork") {
463- openPane(s, forkPane());
338+ if (selected === "fork") {
339+ openPane(forkPane());
464340 return;
465341 }
466342
467- const nextPane = panes.find((pane) => pane.label === s.selected);
468- if (nextPane) openPane(s, nextPane);
343+ const nextPane = panes.find((pane) => pane.label === selected);
344+ if (nextPane) openPane(nextPane);
469345 return;
470346 }
471347
472348 if (currentPane.label === "sessions") {
473- const session = currentSessions.find((v) => v.id === s.selected);
349+ const session = currentSessions.find((v) => v.id === selected);
474350 if (!session) return;
475351
476352 state.sessionId = session.id;
@@ -482,12 +358,12 @@ export function mainMenu(state: TUIState, initialPane = "main") {
482358 state.contextSize = estimateContextTokens(state.messages);
483359 state.scrollOffset = 0;
484360 state.stickToBottom = true;
485- closeMenu(s);
361+ closeMenu();
486362 return;
487363 }
488364
489365 if (currentPane.label === "fork") {
490- const selectedIndex = Number(s.selected);
366+ const selectedIndex = Number(selected);
491367 if (
492368 !Number.isInteger(selectedIndex) ||
493369 !state.messages[selectedIndex]
@@ -504,12 +380,12 @@ export function mainMenu(state: TUIState, initialPane = "main") {
504380 state.contextSize = estimateContextTokens(state.messages);
505381 state.scrollOffset = 0;
506382 state.stickToBottom = true;
507- closeMenu(s);
383+ closeMenu();
508384 return;
509385 }
510386
511387 if (currentPane.label === "themes") {
512- const nextTheme = TUI_THEME_IDS.find((id) => id === s.selected);
388+ const nextTheme = TUI_THEME_IDS.find((id) => id === selected);
513389 if (!nextTheme) return;
514390
515391 state.options.theme = nextTheme;
@@ -521,16 +397,16 @@ export function mainMenu(state: TUIState, initialPane = "main") {
521397 customProviders: state.options.customProviders,
522398 theme: nextTheme,
523399 });
524- closeMenu(s);
400+ closeMenu();
525401 return;
526402 }
527403
528404 if (currentPane.label === "providers") {
529- const provider = currentProviders.find((v) => v === s.selected);
405+ const provider = currentProviders.find((v) => v === selected);
530406 if (!provider) return;
531407
532408 selectedProvider = provider;
533- openPane(s, {
409+ openPane({
534410 label: "models",
535411 filter: "",
536412 list: getProviderModelOptions(selectedProvider),
@@ -542,7 +418,7 @@ export function mainMenu(state: TUIState, initialPane = "main") {
542418 if (!selectedProvider) return;
543419
544420 const model = findModelConfig(
545- s.selected,
421+ selected,
546422 selectedProvider,
547423 state.options.customProviders,
548424 );
@@ -557,12 +433,12 @@ export function mainMenu(state: TUIState, initialPane = "main") {
557433 customProviders: state.options.customProviders,
558434 theme: state.options.theme,
559435 });
560- closeMenu(s);
436+ closeMenu();
561437 return;
562438 }
563439
564440 if (currentPane.label === "effort") {
565- const effort = reasoningEfforts.find((v) => v === s.selected);
441+ const effort = reasoningEfforts.find((v) => v === selected);
566442 if (!effort) return;
567443
568444 state.options.effort = effort;
@@ -573,17 +449,23 @@ export function mainMenu(state: TUIState, initialPane = "main") {
573449 customProviders: state.options.customProviders,
574450 theme: state.options.theme,
575451 });
576- closeMenu(s);
452+ closeMenu();
577453 }
578454 },
579455 onCancel: () => {
580- currentPane = mainPane;
581- selectedProvider = undefined;
582- currentProviders = [];
583- currentSessions = [];
456+ resetMenu();
584457 state.overlay = false;
585458 },
459+ onKeyPress: (key) => {
460+ if (key === "ctrl+p") {
461+ resetMenu();
462+ state.overlay = false;
463+ return;
464+ }
465+
466+ return false;
467+ },
586468 });
587469
588- return select;
470+ return () => SelectOverlay(currentPane.label, select);
589471 }
src/tui.ts+32−28
@@ -54,30 +54,22 @@ function clearOrAbort(state: TUIState) {
5454
5555 export function initTUI(state: TUIState, leave: (s: string) => void) {
5656 // TODO: Cleanup accumulated sessions for this cwd.
57- const { spinnerEvery, currentSpinner } = Spinner();
58- void refreshAvailableUpdate(state);
59-
60- // Stable 60fps rendering.
61- // This ensure Xfps, and excessive calls get coalesced in cel-tui.
62- const fps = 60;
63- const baseFramerateIntervalId = setInterval(() => {
64- if (state.streaming) {
65- spinnerEvery();
66- }
67- cel.setTitle(
68- `mc ${state.streaming ? currentSpinner() : ">"} ../${state.cwd}`,
69- );
70- cel.render();
71- }, 1000 / fps);
57+ const spinner = Spinner((frame) => {
58+ if (state.streaming) cel.setTitle(`mc ${frame} ../${state.cwd}`);
59+ });
7260
7361 let menu = mainMenu(state);
7462
63+ const quit = (message: string) => {
64+ spinner.dispose();
65+ cel.stop();
66+ leave(message);
67+ };
68+
7569 const onWindowKeyPress = (key: string) => {
7670 if (key === "ctrl+q" || key === "ctrl+c" || key === "ctrl+d") {
7771 // Quit
78- clearInterval(baseFramerateIntervalId);
79- cel.stop();
80- leave("Done.");
72+ quit("Done.");
8173 } else if (key === "escape") {
8274 // Abort or clear prompt
8375 clearOrAbort(state);
@@ -96,14 +88,12 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
9688
9789 const onEditorKeyPress = (key: string) => {
9890 const submit = async () => {
99- await streamAgentTUI(state);
91+ await streamAgentTUI(state, spinner);
10092 };
10193 // onKeyPress
10294 if (key === "enter") {
10395 if (state.prompt === ":q") {
104- clearInterval(baseFramerateIntervalId);
105- cel.stop();
106- leave("Done. I like vim too.");
96+ quit("Done. I like vim too.");
10797 return false;
10898 }
10999 if (state.prompt === ":n" || state.prompt === "/new") {
@@ -123,6 +113,8 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
123113
124114 const initialTheme = getTUITheme(state.options.theme);
125115 cel.init(new ProcessTerminal(), { theme: initialTheme.palette });
116+ cel.setTitle(`mc > ../${state.cwd}`);
117+ void refreshAvailableUpdate(state).then(() => cel.render());
126118 cel.viewport(() => {
127119 const activeTheme = getTUITheme(state.options.theme);
128120 const layers = [
@@ -142,7 +134,7 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
142134 TextPill(`../${state.cwd}`, theme.bwhite, theme.bblack),
143135 GitPill(state),
144136 VStack({ flex: 1 }, []),
145- ActivityPill(state, currentSpinner()),
137+ ActivityPill(state, spinner.current),
146138 ContextPill(state),
147139 ]),
148140
@@ -158,10 +150,16 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
158150 });
159151 }
160152
161-async function streamAgentTUI(state: TUIState) {
153+async function streamAgentTUI(
154+ state: TUIState,
155+ spinner: ReturnType<typeof Spinner>,
156+) {
162157 // Just as a defensive thought, callers should set this ahead of time if doing other
163158 // ops before starting the stream.
164159 state.streaming = true;
160+ spinner.reset();
161+ spinner.start();
162+ cel.setTitle(`mc ${spinner.current} ../${state.cwd}`);
165163
166164 const abortController = new AbortController();
167165 state.abortController = abortController;
@@ -253,13 +251,15 @@ async function streamAgentTUI(state: TUIState) {
253251 state.tuiMessages.push(toTUIMessage(ev.partial));
254252 break;
255253 case "message_update":
256- state.tuiMessages[state.tuiMessages.length - 1] = toTUIMessage(
257- ev.partial,
254+ Object.assign(
255+ state.tuiMessages[state.tuiMessages.length - 1],
256+ toTUIMessage(ev.partial),
258257 );
259258 break;
260259 case "message_end": {
261- state.tuiMessages[state.tuiMessages.length - 1] = toTUIMessage(
262- ev.message,
260+ Object.assign(
261+ state.tuiMessages[state.tuiMessages.length - 1],
262+ toTUIMessage(ev.message),
263263 );
264264 const { systemPrompt, tools, messages } = ctx;
265265 state.contextSize = estimateContextTokens(
@@ -308,6 +308,9 @@ async function streamAgentTUI(state: TUIState) {
308308 });
309309 } finally {
310310 state.streaming = false;
311+ spinner.stop();
312+ cel.setTitle(`mc > ../${state.cwd}`);
313+ cel.render();
311314 if (!state.sessionId) {
312315 const id = secureRandomString(10);
313316 state.sessionId = id;
@@ -316,4 +319,5 @@ async function streamAgentTUI(state: TUIState) {
316319 }
317320
318321 state.gitBranch = await getBranchLabel();
322+ cel.render();
319323 }
src/types.ts+0−20
@@ -171,23 +171,3 @@ export type ToolAndRunner = {
171171 signal?: AbortSignal,
172172 ) => AsyncGenerator<ToolRunnerEvent>;
173173 };
174-
175-export type SelectListItem = {
176- label: string;
177- value: string;
178-};
179-
180-export type SelectState = {
181- value: string;
182- selected: string;
183- label: string;
184- list: SelectListItem[];
185-};
186-
187-export type SelectOptions = {
188- filter: string;
189- list: { label: string; value: string }[];
190- label?: string | undefined;
191- onSelect: (s: SelectState) => void | Promise<void>;
192- onCancel: () => void;
193-};