Skip to content

Commit fa80d3d

Browse files
javier-godoymlopezFC
authored andcommitted
fix: restore the initial line on key down
1 parent afe71ec commit fa80d3d

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/main/java/com/flowingcode/vaadin/addons/xterm/TerminalHistory.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class TerminalHistory implements Serializable {
3232

3333
private String lastRet;
3434

35+
private String initialLine;
36+
3537
private Integer maxSize;
3638

3739
protected <T extends XTermBase & ITerminalConsole> TerminalHistory(T terminal) {
@@ -93,7 +95,14 @@ public boolean isEnabled() {
9395
}
9496

9597
private void handleArrowUp() {
96-
write(previous());
98+
if (initialLine == null) {
99+
((ITerminalConsole) terminal).getCurrentLine().thenAccept(currentLine -> {
100+
initialLine = currentLine;
101+
write(previous());
102+
});
103+
} else {
104+
write(previous());
105+
}
97106
}
98107

99108
private void handleArrowDown() {
@@ -161,6 +170,7 @@ public void add(String line) {
161170

162171
private void setCurrentLine(String currentLine) {
163172
if (!currentLine.equals(lastRet)) {
173+
initialLine = currentLine;
164174
prefix = currentLine;
165175
iterator = null;
166176
}
@@ -181,7 +191,11 @@ private String previous() {
181191
}
182192

183193
private String next() {
184-
return find(forwardIterator(), line -> true).orElse("");
194+
return find(forwardIterator(), line -> true).orElseGet(() -> {
195+
String result = initialLine;
196+
initialLine = null;
197+
return result;
198+
});
185199
}
186200

187201
private String findPrevious(String currentLine) {

src/test/java/com/flowingcode/vaadin/addons/xterm/integration/TerminalHistoryIT.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ public void testArrowKeys() {
4040
assertThat(currentLine(term), isEmptyString());
4141
}
4242

43+
@Test
44+
public void testArrowKeysAndRestore() {
45+
TestBenchElement term = $("fc-xterm").first();
46+
47+
WebElement input = (WebElement) waitUntil(driver -> ((HasTestBenchCommandExecutor) driver)
48+
.getCommandExecutor().executeScript("return arguments[0].terminal.textarea", term));
49+
50+
int y = cursorPosition(term).y;
51+
input.sendKeys("foo1\nfoo2\n");
52+
53+
assertThat(cursorPosition(term), is(at(0, y += 2)));
54+
assertThat(lineAtOffset(term, 0), isEmptyString());
55+
56+
input.sendKeys("bar");
57+
input.sendKeys(Keys.ARROW_UP);
58+
assertThat(currentLine(term), is("foo2"));
59+
60+
input.sendKeys(Keys.ARROW_DOWN);
61+
assertThat(currentLine(term), is("bar"));
62+
}
63+
4364
@Test
4465
public void testPageUpDown() {
4566
TestBenchElement term = $("fc-xterm").first();

0 commit comments

Comments
 (0)