Skip to content

Commit

Permalink
Fix behaviour of ConsoleReader.setBuffer() if cursor is not at end of…
Browse files Browse the repository at this point in the history
… line

If the cursor is not at the end of the line and the old and new
buffers have a common prefix then setBuffer will call backspace
with an incorrect argument and the buffer will be mangled.  This
patch fixes the bug.

Signed-off-by: Charles Oliver Nutter <headius@headius.com>
  • Loading branch information
pcc authored and headius committed May 13, 2010
1 parent 1256af3 commit 406854b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/jline/ConsoleReader.java
Expand Up @@ -1006,7 +1006,11 @@ private final void setBuffer(final String buffer) throws IOException {
}
}

int diff = buf.buffer.length() - sameIndex;
int diff = buf.cursor - sameIndex;
if (diff < 0) { // we can't backspace here so try from the end of the buffer
moveToEnd();
diff = buf.buffer.length() - sameIndex;
}

backspace(diff); // go back for the differences
killLine(); // clear to the end of the line
Expand Down
1 change: 1 addition & 0 deletions src/test/java/jline/TestHistory.java
Expand Up @@ -29,6 +29,7 @@ public void testSingleHistory() throws Exception {
assertBuffer("", b);

assertBuffer("test line 5", b = b.op(ConsoleReader.PREV_HISTORY));
assertBuffer("test line 5", b = b.op(ConsoleReader.PREV_CHAR));
assertBuffer("test line 4", b = b.op(ConsoleReader.PREV_HISTORY));
assertBuffer("test line 5", b = b.op(ConsoleReader.NEXT_HISTORY));
assertBuffer("test line 4", b = b.op(ConsoleReader.PREV_HISTORY));
Expand Down

0 comments on commit 406854b

Please sign in to comment.