terminal-chat-ui
Version:
Shared UI components for terminal-based chat interfaces using Theater actors
54 lines (53 loc) • 1.5 kB
JavaScript
/**
* useKeyboardShortcuts - centralized keyboard shortcut handling
*/
import { useInput } from 'ink';
/**
* Hook for managing keyboard shortcuts in terminal chat interfaces
*/
export function useKeyboardShortcuts(config) {
const { shortcuts, disabled = false } = config;
useInput((input, key) => {
if (disabled)
return;
// Find matching shortcut
const matchingShortcut = shortcuts.find(shortcut => {
const keyMatches = shortcut.key.toLowerCase() === input.toLowerCase();
const ctrlMatches = shortcut.ctrl ? key.ctrl : !key.ctrl;
const metaMatches = shortcut.meta ? key.meta : !key.meta;
return keyMatches && ctrlMatches && metaMatches;
});
if (matchingShortcut) {
matchingShortcut.action();
}
});
}
/**
* Common keyboard shortcuts for Theater chat interfaces
*/
export const commonShortcuts = {
exit: (exitFn) => ({
key: 'c',
ctrl: true,
description: 'Exit application',
action: exitFn
}),
clear: (clearFn) => ({
key: 'l',
ctrl: true,
description: 'Clear message history',
action: clearFn
}),
toggleTools: (toggleFn) => ({
key: 't',
ctrl: true,
description: 'Toggle tool display',
action: toggleFn
}),
toggleHelp: (toggleFn) => ({
key: 'h',
ctrl: true,
description: 'Toggle help',
action: toggleFn
})
};