xonecas.io
64875
Mc

mini-coder / commit c08d32f

feat: slash commands in the editor

xonecas2026-09-24 c08d32fa3b707c8b034334328fabb4884ea94d39 parent 1ae754c

4 files changed

src/tui/commands.ts+63−0
@@ -0,0 +1,63 @@
1+import { dim } from "./styles.ts";
2+
3+export interface CommandContext {
4+ /** Appends already-styled lines to scrollback. */
5+ write(lines: string[]): void;
6+}
7+
8+export interface Command {
9+ name: string; // no leading slash, lowercase
10+ description: string; // one line, shown by /help
11+ run(ctx: CommandContext, args: string): void;
12+}
13+
14+/** The keybindings the TUI accepts, in the order `/help` prints them. */
15+const KEYBINDINGS: [string, string][] = [
16+ ["Enter", "submit"],
17+ ["Shift+Enter", "newline (Ctrl+J also works)"],
18+ ["Esc", "pause the turn at the next step boundary"],
19+ ["Ctrl+C", "cancel the turn"],
20+ ["Ctrl+D", "exit on an empty draft"],
21+];
22+
23+/** One aligned `key description` block; the key column is dimmed. */
24+function keyed(rows: [string, string][], width: number): string[] {
25+ return rows.map(([key, description]) => ` ${dim(key.padEnd(width))} ${description}`);
26+}
27+
28+const help: Command = {
29+ name: "help",
30+ description: "list commands and keybindings",
31+ run(ctx): void {
32+ const names = COMMANDS.map((command): [string, string] => [`/${command.name}`, command.description]);
33+ const width = Math.max(...names.map(([key]) => key.length), ...KEYBINDINGS.map(([key]) => key.length));
34+ ctx.write(["commands", ...keyed(names, width), "", "keybindings", ...keyed(KEYBINDINGS, width)]);
35+ },
36+};
37+
38+export const COMMANDS: Command[] = [help];
39+
40+/** `/name args` for a known `name`, else null; unknown slash text stays a message. */
41+export function findCommand(text: string): { command: Command; args: string } | null {
42+ const match = /^\/(\S+)(?:\s+([\s\S]*))?$/.exec(text);
43+ if (match === null) return null;
44+ const command = COMMANDS.find((candidate) => candidate.name === match[1]);
45+ return command === undefined ? null : { command, args: match[2] ?? "" };
46+}
47+
48+/** Tab completion for a half-typed command name; null leaves the draft alone. */
49+export function completeCommand(draft: string): string | null {
50+ if (!draft.startsWith("/") || /\s/.test(draft)) return null;
51+ const typed = draft.slice(1);
52+ const matches = COMMANDS.filter((command) => command.name.startsWith(typed));
53+ if (matches.length === 0) return null;
54+ if (matches.length === 1) return `/${matches[0].name}`;
55+ const shared = matches.map((command) => command.name).reduce(commonPrefix);
56+ return shared === typed ? null : `/${shared}`;
57+}
58+
59+function commonPrefix(a: string, b: string): string {
60+ let i = 0;
61+ while (i < a.length && i < b.length && a[i] === b[i]) i++;
62+ return a.slice(0, i);
63+}
src/tui/editor.ts+7−3
@@ -51,6 +51,13 @@ export class Editor {
5151 this.scroll = 0;
5252 }
5353
54+ setText(text: string): void {
55+ this.lines = text.split("\n");
56+ this.row = this.lines.length - 1;
57+ this.col = codePoints(this.lines[this.row]).length;
58+ this.scroll = 0;
59+ }
60+
5461 handle(key: Key): "submit" | "changed" | "none" {
5562 switch (key.type) {
5663 case "submit":
@@ -61,9 +68,6 @@ export class Editor {
6168 case "text":
6269 this.insert(key.text);
6370 return "changed";
64- case "tab":
65- this.insert("\t");
66- return "changed";
6771 case "backspace":
6872 this.backspace();
6973 return "changed";
src/tui/tui.ts+25−5
@@ -3,6 +3,7 @@ import type { AssistantMessage, JsonObject, Message, UserMessage } from "@earend
33 import { runAgentTurn, type AgentEvent, type AgentOptions, type Phase } from "../agent.ts";
44 import { Terminal, expandTabs, wrapLine, type Key } from "./term.ts";
55 import { Editor } from "./editor.ts";
6+import { completeCommand, findCommand, type CommandContext } from "./commands.ts";
67 import { MarkdownStream, TailStream, type BodyLine, type StreamRenderer } from "./stream.ts";
78 import { cyan, dim, green, red } from "./styles.ts";
89 import { contextUsageLine, estimateContextTokens } from "./usage.ts";
@@ -143,6 +144,15 @@ class Tui {
143144 private readonly messages: Message[] = [];
144145 readonly done: Promise<void>;
145146
147+ /** The only capability a command gets: styled lines into scrollback. */
148+ private readonly commandContext: CommandContext = {
149+ write: (lines) => {
150+ this.separator = true;
151+ this.commitLines(lines.map((text) => ({ text })));
152+ this.separator = true;
153+ },
154+ };
155+
146156 private resolveExit: () => void = () => {};
147157 private phase: Phase = "idle";
148158 private detail: string | undefined;
@@ -187,11 +197,6 @@ class Tui {
187197 process.on("SIGTERM", this.onSignal);
188198 this.separator = true;
189199 this.push(`mini-coder · ${this.opts.model.provider}/${this.opts.model.id}`);
190- this.push(
191- dim(
192- "Enter submit · Shift+Enter (or Ctrl+J) newline · Esc pause · Ctrl+C cancel · Ctrl+D exit",
193- ),
194- );
195200 this.separator = true;
196201 this.render();
197202 }
@@ -228,6 +233,14 @@ class Tui {
228233 }
229234 return;
230235 }
236+ if (key.type === "tab") {
237+ const completed = completeCommand(this.editor.text());
238+ if (completed !== null) {
239+ this.editor.setText(completed);
240+ this.render();
241+ }
242+ return;
243+ }
231244 const result = this.editor.handle(key);
232245 if (result === "submit") this.submit();
233246 else if (result === "changed") this.render();
@@ -244,6 +257,13 @@ class Tui {
244257 return;
245258 }
246259 if (text.trim() === "") return;
260+ const invocation = findCommand(text);
261+ if (invocation !== null) {
262+ this.editor.clear();
263+ invocation.command.run(this.commandContext, invocation.args);
264+ this.render();
265+ return;
266+ }
247267 this.editor.clear();
248268 const message: UserMessage = { role: "user", content: text, timestamp: Date.now() };
249269 this.messages.push(message);
syntax-highlighting.md+0−26
@@ -1,26 +0,0 @@
1-# We want to support coloring, or highlighting the syntaxes envolved in mini-coder
2-
3-This is a problem that `tree-sitter` was made for, and it's used in `nvim` which I use and can
4-see it's quite an efficient and fast implementation. This should be enough to tokenize the syntaxes.
5-
6-## How
7-
8-The intent is to use tree-sitter in some way to provide the tokenized tree using one of their `Language`s. Then we implement a module that wraps each token in ansi codes for rendering.
9-Some changes to how we render already landed, so we can wait for closing blocks and control when to flush.
10-
11-The live preview stays unstyled. We only highlight once on commit to scrollback. No incremental logic.
12-
13-If this works well, we should also replace the diff coloring we hand rolled and use tree sitter based highlighting as well. No need for two way to highlight.
14-
15-We might add more themes in the future. For now TokyoNight dark only.
16-
17-## Guardrails
18-
19-- Do not format or render the markdown into special chars. Syntax highlighting only
20-
21-## References:
22-
23-- https://github.com/tree-sitter/node-tree-sitter old it seems and likely unmaintained. But seems to be the api we want, string goes in with syntax, tree comes out. No typescript types afaik.
24-- https://github.com/tree-sitter/tree-sitter/tree/master/lib the actual C11 library. We might need it, it has `binding_web` might be what we need, the WASM (last activity 2 weeks ago, seems more alive).
25-- https://github.com/wixdaq is the author of the Popular TokyoNight theme, I want to use this pallete for our default theme.
26-