MCPcopy Create free account
hub / github.com/anus-dev/ANUS / useFocus

Function useFocus

packages/cli/src/ui/hooks/useFocus.ts:19–49  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17export const FOCUS_OUT = '\x1b[O';
18
19export const useFocus = () => {
20 const { stdin } = useStdin();
21 const { stdout } = useStdout();
22 const [isFocused, setIsFocused] = useState(true);
23
24 useEffect(() => {
25 const handleData = (data: Buffer) => {
26 const sequence = data.toString();
27 const lastFocusIn = sequence.lastIndexOf(FOCUS_IN);
28 const lastFocusOut = sequence.lastIndexOf(FOCUS_OUT);
29
30 if (lastFocusIn > lastFocusOut) {
31 setIsFocused(true);
32 } else if (lastFocusOut > lastFocusIn) {
33 setIsFocused(false);
34 }
35 };
36
37 // Enable focus reporting
38 stdout?.write(ENABLE_FOCUS_REPORTING);
39 stdin?.on('data', handleData);
40
41 return () => {
42 // Disable focus reporting on cleanup
43 stdout?.write(DISABLE_FOCUS_REPORTING);
44 stdin?.removeListener('data', handleData);
45 };
46 }, [stdin, stdout]);
47
48 return isFocused;
49};

Callers 2

AppFunction · 0.85
useFocus.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected