hermes-agent/ui-tui/src/lib/platform.ts
Dylan Socolobsky ea9ddecc72 fix(tui): route Ctrl+K and Ctrl+W through macOS readline fallback
Makes Ctrl+K and Ctrl+W work in hermes --tui mode in macOS
2026-04-22 14:38:17 -07:00

35 lines
1.7 KiB
TypeScript

/** Platform-aware keybinding helpers.
*
* On macOS the "action" modifier is Cmd. Modern terminals that support kitty
* keyboard protocol report Cmd as `key.super`; legacy terminals often surface it
* as `key.meta`. Some macOS terminals also translate Cmd+Left/Right/Backspace
* into readline-style Ctrl+A/Ctrl+E/Ctrl+U before the app sees them.
* On other platforms the action modifier is Ctrl.
* Ctrl+C is ALWAYS the interrupt key regardless of platform — it must never be
* remapped to copy.
*/
export const isMac = process.platform === 'darwin'
/** True when the platform action-modifier is pressed (Cmd on macOS, Ctrl elsewhere). */
export const isActionMod = (key: { ctrl: boolean; meta: boolean; super?: boolean }): boolean =>
isMac ? key.meta || key.super === true : key.ctrl
/**
* Accept raw Ctrl+<letter> as an action shortcut on macOS, where `isActionMod`
* otherwise means Cmd. Two motivations:
* - Some macOS terminals rewrite Cmd navigation/deletion into readline control
* keys (Cmd+Left → Ctrl+A, Cmd+Right → Ctrl+E, Cmd+Backspace → Ctrl+U).
* - Ctrl+K (kill-to-end) and Ctrl+W (delete-word-back) are standard readline
* bindings that users expect to work regardless of platform, even though
* no terminal rewrites Cmd into them.
*/
export const isMacActionFallback = (
key: { ctrl: boolean; meta: boolean; super?: boolean },
ch: string,
target: 'a' | 'e' | 'u' | 'k' | 'w'
): boolean => isMac && key.ctrl && !key.meta && key.super !== true && ch.toLowerCase() === target
/** Match action-modifier + a single character (case-insensitive). */
export const isAction = (key: { ctrl: boolean; meta: boolean; super?: boolean }, ch: string, target: string): boolean =>
isActionMod(key) && ch.toLowerCase() === target