001 /** 002 * jline - Java console input library 003 * Copyright (c) 2002-2006, Marc Prud'hommeaux <mwp1@cornell.edu> 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * Redistributions of source code must retain the above copyright 011 * notice, this list of conditions and the following disclaimer. 012 * 013 * Redistributions in binary form must reproduce the above copyright 014 * notice, this list of conditions and the following disclaimer 015 * in the documentation and/or other materials provided with 016 * the distribution. 017 * 018 * Neither the name of JLine nor the names of its contributors 019 * may be used to endorse or promote products derived from this 020 * software without specific prior written permission. 021 * 022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 024 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 025 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 026 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 028 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 029 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 030 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 031 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 033 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 034 * OF THE POSSIBILITY OF SUCH DAMAGE. 035 */ 036 package jline; 037 038 /** 039 * A CursorBuffer is a holder for a {@link StringBuffer} that 040 * also contains the current cursor position. 041 * 042 * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a> 043 */ 044 public class CursorBuffer 045 { 046 public int cursor = 0; 047 public final StringBuffer buffer = new StringBuffer (); 048 049 050 public int length () 051 { 052 return buffer.length (); 053 } 054 055 056 public char current () 057 { 058 if (cursor <= 0) 059 return 0; 060 061 return buffer.charAt (cursor - 1); 062 } 063 064 065 /** 066 * Insert the specific character into the buffer, setting the 067 * cursor position ahead one. 068 * 069 * @param c the character to insert 070 */ 071 public void insert (final char c) 072 { 073 buffer.insert (cursor++, c); 074 } 075 076 077 /** 078 * Insert the specified {@link String} into the buffer, setting 079 * the cursor to the end of the insertion point. 080 * 081 * @param str the String to insert. Must not be null. 082 */ 083 public void insert (final String str) 084 { 085 if (buffer.length () == 0) 086 buffer.append (str); 087 else 088 buffer.insert (cursor, str); 089 090 cursor += str.length (); 091 } 092 093 094 public String toString () 095 { 096 return buffer.toString (); 097 } 098 }