One keymap for keys, hints, clickable hints and palette shortcuts

Each binding names its keys, its hint and its palette command once. The app
builds the bindings for the current state in hint order; the first that
takes a key runs it, the hint bar lists them and a click runs the same
action, and the palette reads its shortcuts from them. This replaces the
hand-ordered key handler, the hint strings and the parsing of hint text
back into key events.

The secret, queue and turn hooks return bindings in place of their key()
handlers and command lists.
This commit is contained in:
Callum Alpass committed 2026-11-02 22:40:50 +11:00
1 parent 87a3a79288
commit 8a0475ed63
11 files changed
+607 -502

No files matched your search

+27
View File
@@ -60,6 +60,26 @@ describe("modes and hints", () => {
});
});
describe("keys falling through", () => {
test("in the details panel, navigation keys still work", async () => {
const app = await open();
await app.press("l");
await app.press("c");
expect(app.frame()).toContain("What should the new thread do?");
});
test("the composer's / menu takes the arrows and esc; esc again leaves the composer", async () => {
const app = await open();
await app.press("i");
await app.type("/");
expect(app.hints()).toBe("↑/↓ move · tab or enter choose · esc close");
await app.press("escape");
expect(app.hints()).toStartWith("enter send");
await app.press("escape");
expect(app.hints()).toStartWith("j/k threads");
});
});
describe("palette and pickers", () => {
test("ctrl+k opens the palette, typing filters it, esc closes it", async () => {
const app = await open();
@@ -79,6 +99,13 @@ describe("palette and pickers", () => {
expect(app.frame()).toMatch(/New thread\s+c/);
});
test("palette entries for pending requests carry their keys", async () => {
const app = await open({ session: fakeSession({ projection: withApproval() }) });
await app.press("k", { ctrl: true });
await app.type("always allow");
expect(app.frame()).toMatch(/Always allow this session\s+Y/);
});
test("running a command from the palette", async () => {
const app = await open();
await app.press("k", { ctrl: true });
-25
View File
@@ -1,25 +0,0 @@
import { describe, expect, test } from "bun:test";
import { hintKey } from "../src/model/hints.ts";
describe("clickable key hints", () => {
test("single keys, shifted keys and ctrl chords", () => {
expect(hintKey("y approve", "nav")).toEqual({ name: "y", sequence: "y", shift: false, ctrl: false });
expect(hintKey("Y always", "nav")).toEqual({ name: "y", sequence: "Y", shift: true, ctrl: false });
expect(hintKey("ctrl+k commands", "nav")).toEqual({ name: "k", sequence: "\x0b", shift: false, ctrl: true });
expect(hintKey("esc close", "palette")?.name).toBe("escape");
expect(hintKey(". actions", "nav")?.name).toBe(".");
expect(hintKey("/ filter", "nav")?.name).toBe("/");
});
test("choices of keys, ranges and sending chords stay text", () => {
expect(hintKey("j/k threads", "nav")).toBeNull();
expect(hintKey("ctrl+n/p thread", "input")).toBeNull();
expect(hintKey("1-9 pick", "nav")).toBeNull();
expect(hintKey("alt+enter steer", "input")).toBeNull();
expect(hintKey("alt+↑ edit queued", "input")).toBeNull();
expect(hintKey("shift+enter or \\ newline", "input")).toBeNull();
expect(hintKey("enter send", "input")).toBeNull();
expect(hintKey("enter confirm", "confirm")?.name).toBe("return");
});
});
+72
View File
@@ -0,0 +1,72 @@
import { describe, expect, test } from "bun:test";
import { bindingFor, commandsFor, fromCommand, hintsFor, keyMatches, type Binding, type KeyLike } from "../src/model/keymap.ts";
const key = (name: string, extra: Partial<KeyLike> = {}): KeyLike => ({ name, ctrl: false, meta: false, shift: false, sequence: name.length === 1 ? name : "", ...extra });
describe("keys as people write them", () => {
test("letters, shifted letters and chords", () => {
expect(keyMatches("y", key("y"))).toBe(true);
expect(keyMatches("y", key("y", { shift: true }))).toBe(false);
expect(keyMatches("Y", key("y", { shift: true }))).toBe(true);
expect(keyMatches("Y", key("y"))).toBe(false);
expect(keyMatches("ctrl+k", key("k", { ctrl: true }))).toBe(true);
expect(keyMatches("k", key("k", { ctrl: true }))).toBe(false);
expect(keyMatches("alt+enter", key("return", { meta: true }))).toBe(true);
expect(keyMatches("alt+↑", key("up", { meta: true }))).toBe(true);
expect(keyMatches("shift+enter", key("return", { shift: true }))).toBe(true);
expect(keyMatches("enter", key("return", { shift: true }))).toBe(false);
});
test("named keys and punctuation", () => {
expect(keyMatches("esc", key("escape"))).toBe(true);
expect(keyMatches("enter", key("return"))).toBe(true);
expect(keyMatches("[", key("["))).toBe(true);
// ":" and "?" arrive shifted; that's how they're typed.
expect(keyMatches(":", key(":", { shift: true }))).toBe(true);
expect(keyMatches(".", key("."))).toBe(true);
expect(keyMatches("pagedown", key("pagedown"))).toBe(true);
});
test("ctrl+j also comes as a line feed", () => {
expect(keyMatches("ctrl+j", key("linefeed"))).toBe(true);
expect(keyMatches("j", key("linefeed"))).toBe(false);
});
});
describe("bindings", () => {
const ran: string[] = [];
const bindings: Binding[] = [
{ keys: ["y"], run: () => ran.push("approve"), hint: "approve", command: { id: "approve", title: "Approve request" } },
{ keys: ["j", "↓"], label: "j/k", run: () => ran.push("down"), hint: "threads" },
{ keys: ["k", "↑"], run: () => ran.push("up") },
{ keys: [], label: "1-9", hint: "pick" },
{ keys: ["enter"], run: () => ran.push("send"), hint: "send", passive: true },
{ keys: ["y"], run: () => ran.push("shadowed") },
];
test("the first binding with the key runs; passive ones leave it to the input", () => {
bindingFor(bindings, key("y"))?.run?.();
bindingFor(bindings, key("down"))?.run?.();
expect(ran).toEqual(["approve", "down"]);
expect(bindingFor(bindings, key("return"))).toBeUndefined();
expect(bindingFor(bindings, key("1"))).toBeUndefined();
});
test("hints in order, clickable when they run something", () => {
const hints = hintsFor(bindings);
expect(hints.map((hint) => `${hint.key} ${hint.label}`)).toEqual(["y approve", "j/k threads", "1-9 pick", "enter send"]);
expect(hints.map((hint) => hint.run !== undefined)).toEqual([true, true, false, true]);
});
test("palette commands carry their key", () => {
expect(commandsFor(bindings).map(({ id, title, shortcut }) => ({ id, title, shortcut }))).toEqual([{ id: "approve", title: "Approve request", shortcut: "y" }]);
});
test("a palette command's shortcut becomes its key", () => {
const binding = fromCommand({ id: "archived", title: "Show archived threads", shortcut: "A", run: () => ran.push("archived") });
bindingFor([binding], key("a", { shift: true }))?.run?.();
expect(ran.at(-1)).toBe("archived");
expect(commandsFor([binding])[0]?.shortcut).toBe("A");
});
});