Skip to content

Commit 140acef

Browse files
committed
feat: added isFocusIn() and isFocusOut() methods
1 parent 2a82e91 commit 140acef

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

examples/PrintFocus.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//DEPS org.codejive.miniterm:ansiparser:0.1.3
2+
//DEPS org.codejive.miniterm:termcap:0.1.4-SNAPSHOT
3+
4+
package examples;
5+
6+
import java.io.IOException;
7+
import org.codejive.miniterm.Terminal;
8+
import org.codejive.miniterm.ansiparser.AnsiReader;
9+
import org.codejive.miniterm.termcap.TermCaps;
10+
11+
public class PrintFocus {
12+
public static void main(String[] args) {
13+
try (Terminal terminal = Terminal.create()) {
14+
terminal.enableRawMode();
15+
TermCaps.enableFocusTracking(terminal);
16+
try {
17+
System.out.println("Focus the terminal window (Ctrl+C to exit):");
18+
AnsiReader reader = new AnsiReader(() -> terminal.read(-1));
19+
String token;
20+
while ((token = reader.read()) != null) {
21+
if (token.isEmpty()) continue;
22+
if (!token.startsWith("\033") && token.charAt(0) == 3) break; // Ctrl+C
23+
if (TermCaps.isFocusIn(token)) {
24+
System.out.println("Focus IN");
25+
} else if (TermCaps.isFocusOut(token)) {
26+
System.out.println("Focus OUT");
27+
}
28+
}
29+
} finally {
30+
TermCaps.disableFocusTracking(terminal);
31+
}
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
}

termcap/src/main/java/org/codejive/miniterm/termcap/TermCaps.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,26 @@ public static void disableFocusTracking(Appendable out) throws java.io.IOExcepti
280280
out.append(CSI).append("?1004l");
281281
}
282282

283+
/**
284+
* Returns {@code true} if {@code sequence} is the focus-in event ({@code ESC[I}).
285+
*
286+
* @param sequence input sequence to test
287+
* @return {@code true} when the sequence signals the terminal window has gained focus
288+
*/
289+
public static boolean isFocusIn(String sequence) {
290+
return (CSI + "I").equals(sequence);
291+
}
292+
293+
/**
294+
* Returns {@code true} if {@code sequence} is the focus-out event ({@code ESC[O}).
295+
*
296+
* @param sequence input sequence to test
297+
* @return {@code true} when the sequence signals the terminal window has lost focus
298+
*/
299+
public static boolean isFocusOut(String sequence) {
300+
return (CSI + "O").equals(sequence);
301+
}
302+
283303
/**
284304
* Begins a synchronized-output frame (DEC private mode 2026).
285305
*

0 commit comments

Comments
 (0)