Skip to content

Commit 1ab69e3

Browse files
VerKWerKay Werndli
and
Kay Werndli
authoredApr 24, 2024··
Fix OutOfMemoryError when using TailTipWidget (fix #974) (#975)
Co-authored-by: Kay Werndli <kay@spinque.com>
1 parent 98d3d08 commit 1ab69e3

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2024, the original author(s).
3+
*
4+
* This software is distributable under the BSD license. See the terms of the
5+
* BSD license in the documentation provided with this software.
6+
*
7+
* https://opensource.org/licenses/BSD-3-Clause
8+
*/
9+
package org.jline.widget;
10+
11+
import java.io.ByteArrayInputStream;
12+
import java.io.ByteArrayOutputStream;
13+
import java.io.IOException;
14+
15+
import org.jline.reader.LineReader;
16+
import org.jline.reader.impl.LineReaderImpl;
17+
import org.jline.terminal.Terminal;
18+
import org.jline.terminal.impl.DumbTerminal;
19+
import org.jline.utils.InfoCmp.Capability;
20+
import org.jline.utils.Status;
21+
import org.jline.widget.TailTipWidgets.TipType;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
public final class TailTipWidgetsTest {
27+
28+
/** A simple extension of {@link DumbTerminal} doing the minimal amount of work so that a {@link Status} constructed
29+
* from it is "supported". */
30+
private static final class SupportedDumbTerminal extends DumbTerminal {
31+
private SupportedDumbTerminal() throws IOException {
32+
super(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream());
33+
strings.put(Capability.change_scroll_region, "");
34+
strings.put(Capability.save_cursor, "");
35+
strings.put(Capability.restore_cursor, "");
36+
strings.put(Capability.cursor_address, "");
37+
}
38+
}
39+
40+
/** Subclass of {@link Status} exposing the {@code supported} field. For testing only. */
41+
private static final class TestStatus extends Status {
42+
private TestStatus(Terminal terminal) {
43+
super(terminal);
44+
}
45+
46+
private boolean isSupported() {
47+
return supported;
48+
}
49+
}
50+
51+
/** A dummy {@link LineReader} that's immediately resized to 0x0. */
52+
private static final class ZeroSizeLineReader extends LineReaderImpl {
53+
private ZeroSizeLineReader(Terminal terminal) throws IOException {
54+
super(terminal);
55+
display.resize(0, 0);
56+
}
57+
}
58+
59+
@Test
60+
public void enableTest() throws Exception {
61+
Terminal terminal = new SupportedDumbTerminal();
62+
assertTrue(new TestStatus(terminal).isSupported());
63+
LineReader reader = new ZeroSizeLineReader(terminal);
64+
new TailTipWidgets(reader, __ -> null, 1, TipType.COMBINED).enable();
65+
}
66+
}

‎terminal/src/main/java/org/jline/utils/Display.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void setDelayLineWrap(boolean v) {
6868

6969
public void resize(int rows, int columns) {
7070
if (rows == 0 || columns == 0) {
71-
columns = Integer.MAX_VALUE - 1;
Has conversations. Original line has conversations.
71+
columns = 1;
7272
rows = 1;
7373
}
7474
if (this.rows != rows || this.columns != columns) {

‎terminal/src/main/java/org/jline/utils/Status.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ public void update(List<AttributedString> lines, boolean flush) {
117117

118118
lines = new ArrayList<>(lines);
119119
// add border
120+
int rows = display.rows;
120121
int columns = display.columns;
121-
if (border == 1 && !lines.isEmpty()) {
122+
if (border == 1 && !lines.isEmpty() && rows > 1) {
122123
lines.add(0, getBorderString(columns));
123124
}
124125
// trim or complete lines to the full width

0 commit comments

Comments
 (0)
Please sign in to comment.