Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public void setEnabled(boolean enabled) {
registrations = null;
} else if (enabled && registrations == null) {
registrations = new ArrayList<>();
registrations.add(((ITerminalConsole) terminal).addLineListener(ev -> add(ev.getLine())));
registrations.add(((ITerminalConsole) terminal).addLineListener(ev -> {
add(ev.getLine());
resetIterator();
}));
registrations.add(terminal.addCustomKeyListener(ev -> handleArrowUp(), Key.ARROW_UP));
registrations.add(terminal.addCustomKeyListener(ev -> handleArrowDown(), Key.ARROW_DOWN));
}
Expand Down Expand Up @@ -168,13 +171,19 @@ public void add(String line) {
line = line.trim();
if (!line.isEmpty()) {
history.add(Objects.requireNonNull(line));
iterator = null;
resetIterator();
if (maxSize != null && history.size() > maxSize) {
history.removeLast();
}
}
}

private void resetIterator()
{
lastRet = null;
iterator = null;
}

private Optional<String> find(Iterator<String> iterator, Predicate<String> predicate) {
while (iterator.hasNext()) {
String line = iterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,44 @@ public void testArrowKeysAndRestore() {
assertThat(term.currentLine(), is("bar"));
}

@Test
public void testArrowUpAfterRunningLastCommandFromHistory() {
XTermElement term = $(XTermElement.class).first();

term.sendKeys("foo1\n");
term.sendKeys("foo2\n");

assertThat(term.currentLine(), isEmptyString());

term.sendKeys(Keys.ARROW_UP);
assertThat(term.currentLine(), is("foo2"));
term.sendKeys("\n");

term.sendKeys(Keys.ARROW_UP);
assertThat(term.currentLine(), is("foo2"));

term.sendKeys(Keys.ARROW_UP);
assertThat(term.currentLine(), is("foo1"));
}

@Test
public void testArrowUpAfterRunningEmptyCommand() {
XTermElement term = $(XTermElement.class).first();

term.sendKeys("foo1\n");
term.sendKeys("foo2\n");

assertThat(term.currentLine(), isEmptyString());

term.sendKeys(Keys.ARROW_UP);
assertThat(term.currentLine(), is("foo2"));
term.sendKeys("\u0008\u0008\u0008\u0008"); // 4 backspaces
assertThat(term.currentLine(), isEmptyString());
term.sendKeys("\n");

term.sendKeys(Keys.ARROW_UP);
// The position in the history should be back at the end
assertThat(term.currentLine(), is("foo2"));
}

}