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

+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");
});
});