Skip to content

Commit

Permalink
Improve support for dumb terminals (see #42, FELIX-5388)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 12, 2016
1 parent d6ac0de commit 456c131
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
56 changes: 29 additions & 27 deletions src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,21 +430,13 @@ public String readLine(String prompt, String rightPrompt, Character mask, String
SignalHandler previousWinchHandler = null;
SignalHandler previousContHandler = null;
Attributes originalAttributes = null;
boolean dumb = Terminal.TYPE_DUMB.equals(terminal.getType());
try {
if (reading) {
throw new IllegalStateException();
}
reading = true;

if (history != null) {
history.attach(this);
}

previousIntrHandler = terminal.handle(Signal.INT, signal -> readLineThread.interrupt());
previousWinchHandler = terminal.handle(Signal.WINCH, this::handleSignal);
previousContHandler = terminal.handle(Signal.CONT, this::handleSignal);
originalAttributes = terminal.enterRawMode();

this.mask = mask;

/*
Expand All @@ -461,12 +453,28 @@ public String readLine(String prompt, String rightPrompt, Character mask, String

modifiedHistory.clear();

setPrompt(prompt);
setRightPrompt(rightPrompt);
buf.clear();
if (buffer != null) {
buf.write(buffer);
}
undo.clear();
parsedLine = null;
keyMap = MAIN;

if (history != null) {
history.attach(this);
}

previousIntrHandler = terminal.handle(Signal.INT, signal -> readLineThread.interrupt());
previousWinchHandler = terminal.handle(Signal.WINCH, this::handleSignal);
previousContHandler = terminal.handle(Signal.CONT, this::handleSignal);
originalAttributes = terminal.enterRawMode();

// Cache terminal size for the duration of the call to readLine()
// It will eventually be updated with WINCH signals
size.copy(terminal.getSize());
// if (size.getColumns() == 0 || size.getRows() == 0) {
// throw new IllegalStateException("Invalid terminal size: " + size);
// }

display = new Display(terminal, false);
if (size.getRows() == 0 || size.getColumns() == 0) {
Expand All @@ -478,21 +486,13 @@ public String readLine(String prompt, String rightPrompt, Character mask, String
display.setDelayLineWrap(true);

// Move into application mode
terminal.puts(Capability.keypad_xmit);
if (isSet(Option.AUTO_FRESH_LINE))
freshLine();
if (isSet(Option.MOUSE))
terminal.trackMouse(Terminal.MouseTracking.Normal);

setPrompt(prompt);
setRightPrompt(rightPrompt);
buf.clear();
if (buffer != null) {
buf.write(buffer);
if (!dumb) {
terminal.puts(Capability.keypad_xmit);
if (isSet(Option.AUTO_FRESH_LINE))
freshLine();
if (isSet(Option.MOUSE))
terminal.trackMouse(Terminal.MouseTracking.Normal);
}
undo.clear();
parsedLine = null;
keyMap = MAIN;

callWidget(CALLBACK_INIT);

Expand Down Expand Up @@ -555,7 +555,9 @@ public String readLine(String prompt, String rightPrompt, Character mask, String
mult = 1;
}

redisplay();
if (!dumb) {
redisplay();
}
}
} catch (IOError e) {
if (e.getCause() instanceof InterruptedIOException) {
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/jline/terminal/TerminalBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ private Terminal doBuild() throws IOException {
if (type == null) {
type = System.getenv("TERM");
}
Boolean dumb = this.dumb;
if (dumb == null) {
String str = System.getProperty("org.jline.terminal.dumb");
if (str != null) {
dumb = Boolean.parseBoolean(str);
}
}
if ((system != null && system) || (system == null && in == null && out == null)) {
if (attributes != null || size != null) {
Log.warn("Attributes and size fields are ignored when creating a system terminal");
Expand Down Expand Up @@ -229,7 +236,11 @@ else if (OSUtils.IS_WINDOWS) {
}
if (dumb == null || dumb) {
if (dumb == null) {
Log.warn("Creating a dumb terminal", exception);
if (Log.isDebugEnabled()) {
Log.warn("Creating a dumb terminal", exception);
} else {
Log.warn("Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)");
}
}
return new DumbTerminal(name, type != null ? type : Terminal.TYPE_DUMB,
new FileInputStream(FileDescriptor.in),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ public void close() throws IOException {
Signals.unregister(entry.getKey().name(), entry.getValue());
}
super.close();
reader.shutdown();
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/jline/utils/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public void update(List<AttributedString> newLines, int targetCursorPos) {
reset = false;
}

// If dumb display, get rid of ansi sequences now
Integer cols = terminal.getNumericCapability(Capability.max_colors);
if (cols == null || cols < 8) {
newLines = newLines.stream().map(s -> new AttributedString(s.toString()))
.collect(Collectors.toList());
}

// Detect scrolling
if (fullScreen && newLines.size() == oldLines.size() && canScroll) {
int nbHeaders = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/jline/utils/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static void error(final Object... messages) {
log(Level.SEVERE, messages);
}

public static boolean isDebugEnabled() {
return isEnabled(Level.FINE);
}

/**
* Helper to support rendering messages.
*/
Expand Down Expand Up @@ -107,4 +111,9 @@ static void logr(final Level level, final Supplier<LogRecord> record) {
}
}

static boolean isEnabled(Level level) {
Logger logger = Logger.getLogger("org.jline");
return logger.isLoggable(level);
}

}

0 comments on commit 456c131

Please sign in to comment.