Skip to content

Commit

Permalink
Revert the use of Integer.MAX_VALUE for display colums when size is z…
Browse files Browse the repository at this point in the history
…ero (fixes #982, see #975) (#1011)
  • Loading branch information
gnodet authored Jun 13, 2024
1 parent 0de32ab commit f712b51
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.jline.reader.LineReader;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.terminal.impl.DumbTerminal;
import org.jline.utils.InfoCmp.Capability;
Expand All @@ -34,6 +35,7 @@ private SupportedDumbTerminal() throws IOException {
strings.put(Capability.save_cursor, "");
strings.put(Capability.restore_cursor, "");
strings.put(Capability.cursor_address, "");
setSize(new Size(160, 50));
}
}

Expand Down
12 changes: 12 additions & 0 deletions reader/src/test/java/org/jline/reader/impl/LineReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,16 @@ public void terminalLineInfiniteLoop() throws IOException {
assertEquals("hello", read1);
assertEquals("world", read2);
}

@Test
public void testNoBackspaceInOutputOnDumbTerminal() throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {'\n'});
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (Terminal terminal = new DumbTerminal(in, out)) {
LineReader r = new LineReaderImpl(terminal);
r.readLine("123");
String written = out.toString();
assertEquals("123", written);
}
}
}
8 changes: 5 additions & 3 deletions terminal/src/main/java/org/jline/utils/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void setDelayLineWrap(boolean v) {

public void resize(int rows, int columns) {
if (rows == 0 || columns == 0) {
columns = 1;
columns = Integer.MAX_VALUE - 1;
rows = 1;
}
if (this.rows != rows || this.columns != columns) {
Expand Down Expand Up @@ -209,7 +209,8 @@ public void update(List<AttributedString> newLines, int targetCursorPos, boolean
cursorPos++;
if (newLength == 0 || newLine.isHidden(0)) {
// go to next line column zero
rawPrint(new AttributedString(" \b"));
rawPrint(' ');
terminal.puts(Capability.key_backspace);
} else {
AttributedString firstChar = newLine.substring(0, 1);
// go to next line column one
Expand Down Expand Up @@ -309,7 +310,8 @@ public void update(List<AttributedString> newLines, int targetCursorPos, boolean
} else if (atRight) {
if (this.wrapAtEol) {
if (!fullScreen || (fullScreen && lineIndex < numLines)) {
terminal.writer().write(" \b");
rawPrint(' ');
terminal.puts(Capability.key_backspace);
cursorPos++;
}
} else {
Expand Down
7 changes: 6 additions & 1 deletion terminal/src/main/java/org/jline/utils/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public Status(Terminal terminal) {
this.supported = terminal.getStringCapability(Capability.change_scroll_region) != null
&& terminal.getStringCapability(Capability.save_cursor) != null
&& terminal.getStringCapability(Capability.restore_cursor) != null
&& terminal.getStringCapability(Capability.cursor_address) != null;
&& terminal.getStringCapability(Capability.cursor_address) != null
&& isValid(terminal.getSize());
if (supported) {
display = new MovingCursorDisplay(terminal);
resize();
Expand All @@ -57,6 +58,10 @@ public Status(Terminal terminal) {
}
}

private boolean isValid(Size size) {
return size.getRows() > 0 && size.getRows() < 1000 && size.getColumns() > 0 && size.getColumns() < 1000;
}

public void close() {
terminal.puts(Capability.save_cursor);
terminal.puts(Capability.change_scroll_region, 0, display.rows - 1);
Expand Down

0 comments on commit f712b51

Please sign in to comment.