fix(tui): expand cmd and command keybind modifier aliases to super

This commit is contained in:
Filip Hejmowski 2026-10-29 21:02:37 +01:00
parent 8609cc55d0
commit 93eeec56c8
2 changed files with 129 additions and 8 deletions

View file

@ -78,11 +78,7 @@ function Provider(props: ParentProps<{ config?: KeymapConfig }>) {
const dispose = [
registerCommaBindings(keymap),
keymap.appendBindingExpander((context) => {
const key = Object.entries({ enter: "return", esc: "escape", pgdown: "pagedown", pgup: "pageup" }).reduce(
(result, [alias, value]) =>
result.replace(new RegExp(`(^|[+,\\s>])${alias}(?=$|[+,\\s<])`, "gi"), `$1${value}`),
context.input,
)
const key = expandKeyAliases(context.input)
if (key === context.input) return
return [{ key, displays: context.displays }]
}),
@ -134,7 +130,7 @@ function Provider(props: ParentProps<{ config?: KeymapConfig }>) {
].flatMap((command) => config.keybinds.get(command)),
}),
]
const leader = config.keybinds.get("leader")?.[0]?.key
const leader = resolveLeaderKey(config)
if (leader) {
dispose.push(
registerTimedLeader(keymap, {
@ -186,7 +182,7 @@ export interface Keymap {
function use(): Keymap {
const value = useValue()
const enabled = useInteractivity()
const leader = value.config.keybinds.get("leader")?.[0]?.key
const leader = resolveLeaderKey(value.config)
const isLeader = leader ? value.keymap.createKeyMatcher(leader) : () => false
return {
dispatch(id, input) {
@ -452,8 +448,26 @@ function createMode(keymap: OpenTuiKeymap) {
}
}
function formatOptions(config: KeymapConfig) {
const ALIAS_PATTERNS = Object.entries({
cmd: "super",
command: "super",
enter: "return",
esc: "escape",
pgdown: "pagedown",
pgup: "pageup",
}).map(([alias, value]) => [new RegExp(`(^|[+,\\s>])${alias}(?=$|[+,\\s<])`, "gi"), `$1${value}`] as const)
function expandKeyAliases(input: string) {
return ALIAS_PATTERNS.reduce((result, [pattern, replacement]) => result.replace(pattern, replacement), input)
}
function resolveLeaderKey(config: KeymapConfig) {
const leader = config.keybinds.get("leader")?.[0]?.key
return typeof leader === "string" ? expandKeyAliases(leader) : leader
}
function formatOptions(config: KeymapConfig) {
const leader = resolveLeaderKey(config)
return {
tokenDisplay: {
leader: leader ? (typeof leader === "string" ? leader : stringifyKeyStroke(leader)) : TuiKeybind.LeaderDefault,

View file

@ -1,4 +1,5 @@
/** @jsxImportSource @opentui/solid */
import { TextareaRenderable } from "@opentui/core"
import { testRender } from "@opentui/solid"
import { expect, test } from "bun:test"
import { ConfigProvider } from "../src/config"
@ -139,3 +140,109 @@ test("global commands stay reachable when the mode changes", async () => {
app.renderer.destroy()
}
})
test("cmd and command modifier aliases bind to super without swallowing plain c in textarea", async () => {
const calls: string[] = []
let shortcuts = () => ({ new: [] as readonly string[], list: "" })
function Harness() {
const view = Keymap.useShortcuts()
Keymap.createLayer(() => ({
commands: [
{ id: "session.new", run: () => void calls.push("new") },
{ id: "session.list", run: () => void calls.push("list") },
],
}))
shortcuts = () => ({
new: view.list("session.new"),
list: view.get("session.list") ?? "",
})
return <textarea focused={true} />
}
const app = await testRender(
() => (
<ConfigProvider
config={createTuiResolvedConfig({
keybinds: {
"session.new": "cmd+n,cmd+t",
"session.list": "command+l",
},
})}
>
<Keymap.Provider>
<Harness />
</Keymap.Provider>
</ConfigProvider>
),
{ width: 80, height: 24, kittyKeyboard: true },
)
try {
await app.renderOnce()
const editor = app.renderer.currentFocusedEditor
if (!(editor instanceof TextareaRenderable)) throw new Error("expected focused textarea")
app.mockInput.pressKey("c")
expect(editor.plainText).toBe("c")
await app.mockInput.typeText("at")
expect(editor.plainText).toBe("cat")
expect(shortcuts()).toEqual({
new: ["super+n", "super+t"],
list: "super+l",
})
app.mockInput.pressKey("n", { super: true })
app.mockInput.pressKey("t", { super: true })
app.mockInput.pressKey("l", { super: true })
expect(calls).toEqual(["new", "new", "list"])
expect(editor.plainText).toBe("cat")
} finally {
app.renderer.destroy()
}
})
test("cmd modifier alias works as a leader trigger", async () => {
const calls: string[] = []
function Harness() {
Keymap.createLayer(() => ({
commands: [{ id: "session.compact", run: () => void calls.push("compact") }],
}))
return <textarea focused={true} />
}
const app = await testRender(
() => (
<ConfigProvider
config={createTuiResolvedConfig({
keybinds: {
leader: "cmd+x",
"session.compact": "<leader>c",
},
})}
>
<Keymap.Provider>
<Harness />
</Keymap.Provider>
</ConfigProvider>
),
{ width: 80, height: 24, kittyKeyboard: true },
)
try {
await app.renderOnce()
const editor = app.renderer.currentFocusedEditor
if (!(editor instanceof TextareaRenderable)) throw new Error("expected focused textarea")
app.mockInput.pressKey("c")
expect(editor.plainText).toBe("c")
app.mockInput.pressKey("x", { super: true })
app.mockInput.pressKey("c")
expect(calls).toEqual(["compact"])
expect(editor.plainText).toBe("c")
} finally {
app.renderer.destroy()
}
})