diff options
author | Omari Stephens <xsdg@android.com> | 2011-11-09 16:20:51 -0800 |
---|---|---|
committer | Omari Stephens <xsdg@android.com> | 2011-11-09 17:02:51 -0800 |
commit | 3aa4d1f0c4f24e2d2788ec9886de8a5ca3cd806b (patch) | |
tree | 4aeab040b46c1d854ce3afef3c3410153a9b792b /src/src/main/java/jline/CursorBuffer.java | |
parent | da0155bcd8fac5fc2f3cb2b5451b3c56948aa3ac (diff) | |
download | jline-3aa4d1f0c4f24e2d2788ec9886de8a5ca3cd806b.tar.gz |
Initial import
Downloaded jline-1.0.zip from and unpacked it:
http://sourceforge.net/projects/jline/files/jline/1.0/
Bug: 3457774
Change-Id: Ic7e456baeff00fa2be059d3762bb2dbf822c7c56
Diffstat (limited to 'src/src/main/java/jline/CursorBuffer.java')
-rw-r--r-- | src/src/main/java/jline/CursorBuffer.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/src/main/java/jline/CursorBuffer.java b/src/src/main/java/jline/CursorBuffer.java new file mode 100644 index 0000000..120d705 --- /dev/null +++ b/src/src/main/java/jline/CursorBuffer.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ +package jline; + +/** + * A CursorBuffer is a holder for a {@link StringBuffer} that also contains the + * current cursor position. + * + * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a> + */ +public class CursorBuffer { + public int cursor = 0; + + StringBuffer buffer = new StringBuffer(); + + private boolean overtyping = false; + + public int length() { + return buffer.length(); + } + + public char current() { + if (cursor <= 0) { + return 0; + } + + return buffer.charAt(cursor - 1); + } + + public boolean clearBuffer() { + if (buffer.length() == 0) { + return false; + } + + buffer.delete(0, buffer.length()); + cursor = 0; + return true; + } + + /** + * Write the specific character into the buffer, setting the cursor position + * ahead one. The text may overwrite or insert based on the current setting + * of isOvertyping(). + * + * @param c + * the character to insert + */ + public void write(final char c) { + buffer.insert(cursor++, c); + if (isOvertyping() && cursor < buffer.length()) { + buffer.deleteCharAt(cursor); + } + } + + /** + * Insert the specified {@link String} into the buffer, setting the cursor + * to the end of the insertion point. + * + * @param str + * the String to insert. Must not be null. + */ + public void write(final String str) { + if (buffer.length() == 0) { + buffer.append(str); + } else { + buffer.insert(cursor, str); + } + + cursor += str.length(); + + if (isOvertyping() && cursor < buffer.length()) { + buffer.delete(cursor, (cursor + str.length())); + } + } + + public String toString() { + return buffer.toString(); + } + + public boolean isOvertyping() { + return overtyping; + } + + public void setOvertyping(boolean b) { + overtyping = b; + } + + public StringBuffer getBuffer() { + return buffer; + } + + public void setBuffer(StringBuffer buffer) { + buffer.setLength(0); + buffer.append(this.buffer.toString()); + + this.buffer = buffer; + } + + +} |