001 /* 002 * Copyright (c) 2002-2006, Marc Prud'hommeaux. All rights reserved. 003 * 004 * This software is distributable under the BSD license. See the terms of the 005 * BSD license in the documentation provided with this software. 006 */ 007 package jline; 008 009 010 /** 011 * A CursorBuffer is a holder for a {@link StringBuffer} that 012 * also contains the current cursor position. 013 * 014 * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a> 015 */ 016 public class CursorBuffer { 017 public int cursor = 0; 018 public final StringBuffer buffer = new StringBuffer(); 019 020 public int length() { 021 return buffer.length(); 022 } 023 024 public char current() { 025 if (cursor <= 0) { 026 return 0; 027 } 028 029 return buffer.charAt(cursor - 1); 030 } 031 032 /** 033 * Insert the specific character into the buffer, setting the 034 * cursor position ahead one. 035 * 036 * @param c the character to insert 037 */ 038 public void insert(final char c) { 039 buffer.insert(cursor++, c); 040 } 041 042 /** 043 * Insert the specified {@link String} into the buffer, setting 044 * the cursor to the end of the insertion point. 045 * 046 * @param str the String to insert. Must not be null. 047 */ 048 public void insert(final String str) { 049 if (buffer.length() == 0) { 050 buffer.append(str); 051 } else { 052 buffer.insert(cursor, str); 053 } 054 055 cursor += str.length(); 056 } 057 058 public String toString() { 059 return buffer.toString(); 060 } 061 }