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