001 /** 002 * jline - Java console input library 003 * Copyright (c) 2002,2003 Marc Prud'hommeaux marc@apocalypse.org 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 package jline; 020 021 import java.io.*; 022 import java.util.*; 023 024 025 /** 026 * A command history buffer. 027 * 028 * @author <a href="mailto:marc@apocalypse.org">Marc Prud'hommeaux</a> 029 */ 030 public class History 031 { 032 private List history = new ArrayList (); 033 private PrintWriter output = null; 034 private int maxSize = 500; 035 private int currentIndex = 0; 036 037 038 /** 039 * Load the history buffer from the specified InputStream. 040 */ 041 public void load (InputStream in) 042 throws IOException 043 { 044 load (new InputStreamReader (in)); 045 } 046 047 048 /** 049 * Load the history buffer from the specified Reader. 050 */ 051 public void load (Reader reader) 052 throws IOException 053 { 054 BufferedReader breader = new BufferedReader (reader); 055 String line; 056 while ((line = breader.readLine ()) != null) 057 { 058 addToHistory (line); 059 } 060 } 061 062 063 public int size () 064 { 065 return history.size (); 066 } 067 068 069 /** 070 * Clear the history buffer 071 */ 072 public void clear () 073 { 074 history.clear (); 075 currentIndex = 0; 076 } 077 078 079 /** 080 * Add the specified buffer to the end of the history. The pointer is 081 * set to the end of the history buffer. 082 */ 083 public void addToHistory (String buffer) 084 { 085 // don't append duplicates to the end of the buffer 086 if (history.size () != 0 && buffer.equals ( 087 history.get (history.size () - 1))) 088 return; 089 090 history.add (buffer); 091 while (history.size () > getMaxSize ()) 092 history.remove (0); 093 094 currentIndex = history.size (); 095 096 if (getOutput () != null) 097 { 098 getOutput ().println (buffer); 099 getOutput ().flush (); 100 } 101 } 102 103 104 /** 105 * Move to the end of the history buffer. 106 */ 107 public void moveToEnd () 108 { 109 currentIndex = history.size (); 110 } 111 112 113 /** 114 * Set the maximum size that the history buffer will store. 115 */ 116 public void setMaxSize (int maxSize) 117 { 118 this.maxSize = maxSize; 119 } 120 121 122 /** 123 * Get the maximum size that the history buffer will store. 124 */ 125 public int getMaxSize () 126 { 127 return this.maxSize; 128 } 129 130 131 /** 132 * The output to which all history elements will be written (or null 133 * of history is not saved to a buffer). 134 */ 135 public void setOutput (PrintWriter output) 136 { 137 this.output = output; 138 } 139 140 141 /** 142 * Returns the PrintWriter that is used to store history elements. 143 */ 144 public PrintWriter getOutput () 145 { 146 return this.output; 147 } 148 149 150 /** 151 * Returns the current history index. 152 */ 153 public int getCurrentIndex () 154 { 155 return this.currentIndex; 156 } 157 158 159 /** 160 * Return the content of the current buffer. 161 */ 162 public String current () 163 { 164 if (currentIndex >= history.size ()) 165 return ""; 166 167 return (String)history.get (currentIndex); 168 } 169 170 171 /** 172 * Move the pointer to the previous element in the buffer. 173 * 174 * @return true if we successfully went to the previous element 175 */ 176 public boolean previous () 177 { 178 if (currentIndex <= 0) 179 return false; 180 181 currentIndex--; 182 return true; 183 } 184 185 186 /** 187 * Move the pointer to the next element in the buffer. 188 * 189 * @return true if we successfully went to the next element 190 */ 191 public boolean next () 192 { 193 if (currentIndex >= history.size ()) 194 return false; 195 196 currentIndex++; 197 return true; 198 } 199 200 201 /** 202 * Returns an immutable list of the history buffer. 203 */ 204 public List getHistoryList () 205 { 206 return Collections.unmodifiableList (history); 207 } 208 209 210 /** 211 * Returns the standard {@link AbstractCollection#toString} representation 212 * of the history list. 213 */ 214 public String toString () 215 { 216 return history.toString (); 217 } 218 } 219