MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / useTerminalFocus

Function useTerminalFocus

cli/src/hooks/use-terminal-focus.ts:69–115  ·  view source on GitHub ↗
({
  onFocusChange,
  onSupportDetected,
}: UseTerminalFocusOptions)

Source from the content-addressed store, hash-verified

67 * - \x1b[O on focus lost
68 */
69export function useTerminalFocus({
70 onFocusChange,
71 onSupportDetected,
72}: UseTerminalFocusOptions): void {
73 useEffect(() => {
74 const stdin = getStdin()
75 if (!stdin) {
76 return
77 }
78
79 let supportDetected = false
80
81 // Enable focus reporting
82 enableFocusReporting()
83
84 // Listen for data events on stdin to catch focus in/out sequences
85 const handleData = (chunk: Buffer | string) => {
86 const data = chunk.toString()
87
88 // Use includes() instead of strict equality to handle cases where
89 // terminal batches multiple sequences or keystrokes together
90 if (data.includes(FOCUS_IN_EVENT)) {
91 // First focus event confirms terminal support
92 if (!supportDetected) {
93 supportDetected = true
94 onSupportDetected?.()
95 }
96 onFocusChange(true)
97 } else if (data.includes(FOCUS_OUT_EVENT)) {
98 // First focus event confirms terminal support
99 if (!supportDetected) {
100 supportDetected = true
101 onSupportDetected?.()
102 }
103 onFocusChange(false)
104 }
105 }
106
107 stdin.on('data', handleData)
108
109 // Cleanup: disable focus reporting and remove listener
110 return () => {
111 stdin.off('data', handleData)
112 disableFocusReporting()
113 }
114 }, [onFocusChange, onSupportDetected])
115}

Callers 1

AppFunction · 0.90

Calls 4

getStdinFunction · 0.85
enableFocusReportingFunction · 0.85
disableFocusReportingFunction · 0.85
onMethod · 0.65

Tested by

no test coverage detected