Skip to content

Commit

Permalink
Focus tracking support, fixes #222
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 29, 2018
1 parent 7008567 commit c259d8d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ public interface LineReader {
String YANK = "yank";
String YANK_POP = "yank-pop";
String MOUSE = "mouse";
String FOCUS_IN = "terminal-focus-in";
String FOCUS_OUT = "terminal-focus-out";

String BEGIN_PASTE = "begin-paste";

Expand Down
15 changes: 15 additions & 0 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public class LineReaderImpl implements LineReader, Flushable
public static final String BRACKETED_PASTE_BEGIN = "\033[200~";
public static final String BRACKETED_PASTE_END = "\033[201~";

public static final String FOCUS_IN_SEQ = "\033[I";
public static final String FOCUS_OUT_SEQ = "\033[O";

/**
* Possible states in which the current readline operation may be in.
*/
Expand Down Expand Up @@ -3338,6 +3341,8 @@ protected Map<String, Widget> builtinWidgets() {
widgets.put(YANK_POP, this::yankPop);
widgets.put(MOUSE, this::mouse);
widgets.put(BEGIN_PASTE, this::beginPaste);
widgets.put(FOCUS_IN, this::focusIn);
widgets.put(FOCUS_OUT, this::focusOut);
return widgets;
}

Expand Down Expand Up @@ -4995,6 +5000,14 @@ public boolean beginPaste() {
return true;
}

public boolean focusIn() {
return false;
}

public boolean focusOut() {
return false;
}

/**
* Clean the used display
*/
Expand Down Expand Up @@ -5426,6 +5439,8 @@ private void bindArrowKeys(KeyMap<Binding> map) {
bind(map, OVERWRITE_MODE, key(Capability.key_ic));
bind(map, MOUSE, key(Capability.key_mouse));
bind(map, BEGIN_PASTE, BRACKETED_PASTE_BEGIN);
bind(map, FOCUS_IN, FOCUS_IN_SEQ);
bind(map, FOCUS_OUT, FOCUS_OUT_SEQ);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions terminal/src/main/java/org/jline/terminal/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,21 @@ enum MouseTracking {
*/
MouseEvent readMouseEvent(IntSupplier reader);

/**
* Returns <code>true</code> if the terminal has support for focus tracking.
* @return whether focus tracking is supported by the terminal
* @see #trackFocus(boolean)
*/
boolean hasFocusSupport();

/**
* Enable or disable focus tracking mode.
* When focus tracking has been activated, each time the terminal grabs the focus,
* the string "\33[I" will be sent to the input stream and each time the focus is lost,
* the string "\33[O" will be sent to the input stream.
*
* @param tracking whether the focus tracking mode should be enabled or not
* @return <code>true</code> if focus tracking is supported
*/
boolean trackFocus(boolean tracking);
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ public MouseEvent readMouseEvent(IntSupplier reader) {
return lastMouseEvent = MouseSupport.readMouse(reader, lastMouseEvent);
}

@Override
public boolean hasFocusSupport() {
return type != null && type.startsWith("xterm");
}

@Override
public boolean trackFocus(boolean tracking) {
if (hasFocusSupport()) {
writer().write(tracking ? "\033[?1004h" : "\033[?1004l");
writer().flush();
return true;
} else {
return false;
}
}

protected void checkInterrupted() throws InterruptedIOException {
if (Thread.interrupted()) {
throw new InterruptedIOException();
Expand Down

0 comments on commit c259d8d

Please sign in to comment.