Skip to content

Commit ba83ed6

Browse files
Jorik De Waenjavier-godoy
authored andcommitted
#48: improve history behavior
1 parent ce95cd9 commit ba83ed6

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ public void setEnabled(boolean enabled) {
9898
registrations = null;
9999
} else if (enabled && registrations == null) {
100100
registrations = new ArrayList<>();
101-
registrations.add(((ITerminalConsole) terminal).addLineListener(ev -> add(ev.getLine())));
101+
registrations.add(((ITerminalConsole) terminal).addLineListener(ev -> {
102+
add(ev.getLine());
103+
resetIterator();
104+
}));
102105
registrations.add(terminal.addCustomKeyListener(ev -> handleArrowUp(), Key.ARROW_UP));
103106
registrations.add(terminal.addCustomKeyListener(ev -> handleArrowDown(), Key.ARROW_DOWN));
104107
}
@@ -168,13 +171,19 @@ public void add(String line) {
168171
line = line.trim();
169172
if (!line.isEmpty()) {
170173
history.add(Objects.requireNonNull(line));
171-
iterator = null;
174+
resetIterator();
172175
if (maxSize != null && history.size() > maxSize) {
173176
history.removeLast();
174177
}
175178
}
176179
}
177180

181+
private void resetIterator()
182+
{
183+
lastRet = null;
184+
iterator = null;
185+
}
186+
178187
private Optional<String> find(Iterator<String> iterator, Predicate<String> predicate) {
179188
while (iterator.hasNext()) {
180189
String line = iterator.next();

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,44 @@ public void testArrowKeysAndRestore() {
7171
assertThat(term.currentLine(), is("bar"));
7272
}
7373

74+
@Test
75+
public void testArrowUpAfterRunningLastCommandFromHistory() {
76+
XTermElement term = $(XTermElement.class).first();
77+
78+
term.sendKeys("foo1\n");
79+
term.sendKeys("foo2\n");
80+
81+
assertThat(term.currentLine(), isEmptyString());
82+
83+
term.sendKeys(Keys.ARROW_UP);
84+
assertThat(term.currentLine(), is("foo2"));
85+
term.sendKeys("\n");
86+
87+
term.sendKeys(Keys.ARROW_UP);
88+
assertThat(term.currentLine(), is("foo2"));
89+
90+
term.sendKeys(Keys.ARROW_UP);
91+
assertThat(term.currentLine(), is("foo1"));
92+
}
93+
94+
@Test
95+
public void testArrowUpAfterRunningEmptyCommand() {
96+
XTermElement term = $(XTermElement.class).first();
97+
98+
term.sendKeys("foo1\n");
99+
term.sendKeys("foo2\n");
100+
101+
assertThat(term.currentLine(), isEmptyString());
102+
103+
term.sendKeys(Keys.ARROW_UP);
104+
assertThat(term.currentLine(), is("foo2"));
105+
term.sendKeys("\u0008\u0008\u0008\u0008"); // 4 backspaces
106+
assertThat(term.currentLine(), isEmptyString());
107+
term.sendKeys("\n");
108+
109+
term.sendKeys(Keys.ARROW_UP);
110+
// The position in the history should be back at the end
111+
assertThat(term.currentLine(), is("foo2"));
112+
}
113+
74114
}

0 commit comments

Comments
 (0)