feat: add context-aware ask keymaps

This commit is contained in:
Erik Kotsiuba
2026-05-06 12:44:37 +02:00
parent 3672ada13f
commit 808b62cc77
18 changed files with 1158 additions and 444 deletions
+80
View File
@@ -19,6 +19,86 @@ test("config migration framework accepts the current schema version", () => {
assert.deepEqual(result.config, DEFAULT_ASK_CONFIG);
});
test("config migration maps previous-version config files into v4 shape", () => {
const legacyKeymaps = {
cancel: "q",
confirm: "ctrl+k",
dismiss: "ctrl+d",
optionNote: "x",
questionNote: "shift+x",
toggle: "ctrl+t",
};
const cases = [
{
name: "v1",
raw: {
schemaVersion: 1,
behaviour: DEFAULT_ASK_CONFIG.behaviour,
keymaps: legacyKeymaps,
},
},
{
name: "v2",
raw: {
schemaVersion: 2,
answer: DEFAULT_ASK_CONFIG.answer,
behaviour: DEFAULT_ASK_CONFIG.behaviour,
keymaps: legacyKeymaps,
},
},
{
name: "v3",
raw: {
schemaVersion: 3,
answer: DEFAULT_ASK_CONFIG.answer,
behaviour: DEFAULT_ASK_CONFIG.behaviour,
keymaps: legacyKeymaps,
notifications: DEFAULT_ASK_CONFIG.notifications,
},
},
];
for (const { name, raw } of cases) {
const result = migrateAskConfig(raw);
assert.equal(result.migrated, true, name);
assert.deepEqual(result.config.keymaps.global.dismiss, ["ctrl+d"], name);
assert.deepEqual(result.config.keymaps.main.cancel, ["q"], name);
assert.deepEqual(result.config.keymaps.main.confirm, ["ctrl+k"], name);
assert.deepEqual(result.config.keymaps.main.toggle, ["ctrl+t"], name);
assert.deepEqual(result.config.keymaps.editor.submit, ["ctrl+k"], name);
assert.deepEqual(result.config.keymaps.editor.close, ["q"], name);
assert.deepEqual(result.config.keymaps.noteEditor.save, ["ctrl+k"], name);
assert.deepEqual(result.config.keymaps.noteEditor.close, ["q"], name);
assert.deepEqual(
result.config.keymaps.settingsModal,
DEFAULT_ASK_CONFIG.keymaps.settingsModal,
name
);
}
});
test("config migration maps legacy flat keymaps into context keymaps", () => {
const result = migrateAskConfig({
schemaVersion: 3,
behaviour: DEFAULT_ASK_CONFIG.behaviour,
keymaps: {
cancel: "q",
confirm: "ctrl+k",
dismiss: "ctrl+d",
optionNote: "x",
questionNote: "shift+x",
toggle: "ctrl+t",
},
notifications: DEFAULT_ASK_CONFIG.notifications,
});
assert.equal(result.migrated, true);
assert.deepEqual(result.config.keymaps.global.dismiss, ["ctrl+d"]);
assert.deepEqual(result.config.keymaps.main.cancel, ["q"]);
assert.deepEqual(result.config.keymaps.editor.submit, ["ctrl+k"]);
assert.deepEqual(result.config.keymaps.noteEditor.save, ["ctrl+k"]);
});
test("config migration framework rejects unsupported future schema versions", () => {
assert.throws(
() =>
+1 -1
View File
@@ -13,7 +13,7 @@ function expectedConfigFile(
overrides: { behaviour?: typeof DEFAULT_ASK_CONFIG.behaviour } = {}
) {
return {
schemaVersion: 3,
schemaVersion: 4,
answer: DEFAULT_ASK_CONFIG.answer,
behaviour: overrides.behaviour ?? DEFAULT_ASK_CONFIG.behaviour,
keymaps: DEFAULT_ASK_CONFIG.keymaps,
+100 -2
View File
@@ -1,11 +1,13 @@
import assert from "node:assert/strict";
import test from "node:test";
import { DEFAULT_ASK_CONFIG } from "../src/config/defaults.ts";
import { getAskConfigStore } from "../src/config/store.ts";
import { createInitialState } from "../src/state/create.ts";
import {
applyNumberShortcut,
enterQuestionNoteMode,
} from "../src/state/transitions.ts";
import { runAskFlow } from "../src/ui/controller.ts";
import { getInputCommand } from "../src/ui/input.ts";
function inputState() {
@@ -207,6 +209,99 @@ test("note shortcuts use n for option notes and Shift+N for question notes", ()
});
});
test("custom configured editor submit shortcut is used at runtime", () => {
const input = inputState();
const config = {
...DEFAULT_ASK_CONFIG,
keymaps: {
...DEFAULT_ASK_CONFIG.keymaps,
editor: {
...DEFAULT_ASK_CONFIG.keymaps.editor,
submit: ["ctrl+k"],
},
},
};
assert.deepEqual(getInputCommand(input, config, "\u000b", "answer"), {
kind: "editSubmit",
});
assert.deepEqual(getInputCommand(input, config, "\r", "answer"), {
kind: "delegateToEditor",
});
});
test("custom editor submit key controls actual editor submission", async () => {
const config = {
...DEFAULT_ASK_CONFIG,
notifications: {
...DEFAULT_ASK_CONFIG.notifications,
enabled: false,
},
keymaps: {
...DEFAULT_ASK_CONFIG.keymaps,
editor: {
...DEFAULT_ASK_CONFIG.keymaps.editor,
submit: ["ctrl+k"],
},
},
};
getAskConfigStore().setConfig(config);
let component: { handleInput(data: string): void } | undefined;
const resultPromise = runAskFlow(
{
cwd: process.cwd(),
ui: {
custom(callback: (...args: unknown[]) => unknown) {
return new Promise((resolve) => {
const tui = {
requestRender() {
// Rendering is not needed for this controller input test.
},
};
component = callback(
tui,
plainTheme(),
{},
resolve
) as typeof component;
});
},
},
} as never,
{
questions: [
{
id: "q1",
prompt: "Question?",
options: [{ value: "a", label: "A" }],
},
],
}
);
await new Promise((resolve) => setImmediate(resolve));
component?.handleInput("2");
component?.handleInput("x");
component?.handleInput("\r");
component?.handleInput("\u000b");
component?.handleInput("\r");
const result = await resultPromise;
assert.equal(result.answers.q1?.customText, "x");
getAskConfigStore().setConfig(DEFAULT_ASK_CONFIG);
});
function plainTheme() {
return {
bg(_color: string, text: string) {
return text;
},
fg(_color: string, text: string) {
return text;
},
};
}
test("custom configured note shortcuts are used at runtime", () => {
const navigation = createInitialState({
questions: [
@@ -221,8 +316,11 @@ test("custom configured note shortcuts are used at runtime", () => {
...DEFAULT_ASK_CONFIG,
keymaps: {
...DEFAULT_ASK_CONFIG.keymaps,
optionNote: "x",
questionNote: "shift+x",
main: {
...DEFAULT_ASK_CONFIG.keymaps.main,
optionNote: ["x"],
questionNote: ["shift+x"],
},
},
};
+5 -2
View File
@@ -90,8 +90,11 @@ test("editing footers use configured key labels", () => {
...DEFAULT_ASK_CONFIG,
keymaps: {
...DEFAULT_ASK_CONFIG.keymaps,
confirm: "ctrl+k",
cancel: "q",
noteEditor: {
...DEFAULT_ASK_CONFIG.keymaps.noteEditor,
save: ["ctrl+k"],
close: ["q"],
},
},
};
+35 -11
View File
@@ -15,14 +15,7 @@ const savedConfig: AskConfig = {
doublePressReviewShortcuts: true,
showFooterHints: true,
},
keymaps: {
cancel: "esc",
dismiss: "ctrl+c",
toggle: "space",
confirm: "enter",
optionNote: "n",
questionNote: "shift+n",
},
keymaps: DEFAULT_ASK_CONFIG.keymaps,
notifications: {
channels: ["bell"],
enabled: true,
@@ -44,6 +37,7 @@ function createList(
options: {
onClose?: () => void;
onSave?: (config: AskConfig) => Promise<AskConfig>;
savedConfig?: AskConfig;
} = {}
) {
const onClose =
@@ -56,7 +50,7 @@ function createList(
notice: undefined,
onClose,
onSave: options.onSave ?? ((config) => Promise.resolve(config)),
savedConfig,
savedConfig: options.savedConfig ?? savedConfig,
tui: {
requestRender() {
// no-op in tests
@@ -80,7 +74,7 @@ test("settings list renders behaviour settings and config path", () => {
assert(text.includes("notifications"));
assert(text.includes("extraction settings"));
assert(text.includes("/tmp/eko24ive-pi-ask.json"));
assert(text.includes("Esc to close"));
assert(text.includes("Esc / Ctrl+C / ? to close"));
assert.equal(text.includes("Esc to cancel"), false);
assert.equal(text.includes("Keymaps"), false);
assert.equal(text.includes("Ctrl+S"), false);
@@ -131,7 +125,37 @@ test("settings list shows save failures and reverts the toggle", async () => {
assert(text.includes("[off]"));
});
test("settings list closes with escape, ctrl-c, question mark, and dispose idempotently", () => {
test("settings list uses configured navigation and close keys", async () => {
let saved: AskConfig | undefined;
const customConfig: AskConfig = {
...savedConfig,
keymaps: {
...savedConfig.keymaps,
settingsModal: {
...savedConfig.keymaps.settingsModal,
nextOption: ["j"],
previousOption: ["k"],
toggle: ["x"],
close: ["q"],
},
},
};
const list = createList({
onSave: (config) => {
saved = config;
return Promise.resolve(config);
},
savedConfig: customConfig,
});
list.handleInput("j");
list.handleInput("x");
await new Promise((resolve) => setImmediate(resolve));
assert.equal(saved?.behaviour.confirmDismissWhenDirty, false);
});
test("settings list closes with configured keys and dispose idempotently", () => {
let closed = 0;
const list = createList({
onClose: () => {