Bug#576814: [PATCH] Fix behaviour of ConsoleReader.setBuffer() if cursor is not at end of line

Peter Collingbourne peter at pcc.me.uk
Fri Apr 9 21:52:39 UTC 2010


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.
---
 src/main/java/jline/ConsoleReader.java |    6 +++++-
 src/test/java/jline/TestHistory.java   |    1 +
 2 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/src/main/java/jline/ConsoleReader.java b/src/main/java/jline/ConsoleReader.java
index c72627a..db55b1a 100644
--- a/src/main/java/jline/ConsoleReader.java
+++ b/src/main/java/jline/ConsoleReader.java
@@ -1003,7 +1003,11 @@ public class ConsoleReader implements ConsoleOperations {
             }
         }
 
-        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
diff --git a/src/test/java/jline/TestHistory.java b/src/test/java/jline/TestHistory.java
index a39afa5..32bebd7 100644
--- a/src/test/java/jline/TestHistory.java
+++ b/src/test/java/jline/TestHistory.java
@@ -29,6 +29,7 @@ public class TestHistory extends JLineTestCase {
         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));
-- 
1.6.5






More information about the pkg-java-maintainers mailing list