MCPcopy Index your code
hub / github.com/codeaashu/claude-code / useExitOnCtrlCD

Function useExitOnCtrlCD

src/hooks/useExitOnCtrlCD.ts:45–95  ·  view source on GitHub ↗
(
  useKeybindingsHook: UseKeybindingsHook,
  onInterrupt?: () => boolean,
  onExit?: () => void,
  isActive = true,
)

Source from the content-addressed store, hash-verified

43 * before parent useKeybindings, so both see every keypress).
44 */
45export function useExitOnCtrlCD(
46 useKeybindingsHook: UseKeybindingsHook,
47 onInterrupt?: () => boolean,
48 onExit?: () => void,
49 isActive = true,
50): ExitState {
51 const { exit } = useApp()
52 const [exitState, setExitState] = useState<ExitState>({
53 pending: false,
54 keyName: null,
55 })
56
57 const exitFn = useMemo(() => onExit ?? exit, [onExit, exit])
58
59 // Double-press handler for ctrl+c
60 const handleCtrlCDoublePress = useDoublePress(
61 pending => setExitState({ pending, keyName: 'Ctrl-C' }),
62 exitFn,
63 )
64
65 // Double-press handler for ctrl+d
66 const handleCtrlDDoublePress = useDoublePress(
67 pending => setExitState({ pending, keyName: 'Ctrl-D' }),
68 exitFn,
69 )
70
71 // Handler for app:interrupt (ctrl+c by default)
72 // Let features handle interrupt first via callback
73 const handleInterrupt = useCallback(() => {
74 if (onInterrupt?.()) return // Feature handled it
75 handleCtrlCDoublePress()
76 }, [handleCtrlCDoublePress, onInterrupt])
77
78 // Handler for app:exit (ctrl+d by default)
79 // This also uses double-press to confirm exit
80 const handleExit = useCallback(() => {
81 handleCtrlDDoublePress()
82 }, [handleCtrlDDoublePress])
83
84 const handlers = useMemo(
85 () => ({
86 'app:interrupt': handleInterrupt,
87 'app:exit': handleExit,
88 }),
89 [handleInterrupt, handleExit],
90 )
91
92 useKeybindingsHook(handlers, { context: 'Global', isActive })
93
94 return exitState
95}
96

Callers 1

Calls 3

useAppFunction · 0.85
useDoublePressFunction · 0.85
onInterruptFunction · 0.70

Tested by

no test coverage detected