View Javadoc

1   /*
2    * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
3    *
4    * This software is distributable under the BSD license. See the terms of the
5    * BSD license in the documentation provided with this software.
6    */
7   package jline;
8   
9   import java.io.*;
10  import java.util.*;
11  
12  /***
13   * A command history buffer.
14   *
15   * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
16   */
17  public class History {
18      private List history = new ArrayList();
19  
20      private PrintWriter output = null;
21  
22      private int maxSize = 500;
23  
24      private int currentIndex = 0;
25  
26      /***
27       * Construstor: initialize a blank history.
28       */
29      public History() {
30      }
31  
32      /***
33       * Construstor: initialize History object the the specified {@link File} for
34       * storage.
35       */
36      public History(final File historyFile) throws IOException {
37          setHistoryFile(historyFile);
38      }
39  
40      public void setHistoryFile(final File historyFile) throws IOException {
41          if (historyFile.isFile()) {
42              load(new FileInputStream(historyFile));
43          }
44  
45          setOutput(new PrintWriter(new FileWriter(historyFile), true));
46          flushBuffer();
47      }
48  
49      /***
50       * Load the history buffer from the specified InputStream.
51       */
52      public void load(final InputStream in) throws IOException {
53          load(new InputStreamReader(in));
54      }
55  
56      /***
57       * Load the history buffer from the specified Reader.
58       */
59      public void load(final Reader reader) throws IOException {
60          BufferedReader breader = new BufferedReader(reader);
61          List lines = new ArrayList();
62          String line;
63  
64          while ((line = breader.readLine()) != null) {
65              lines.add(line);
66          }
67  
68          for (Iterator i = lines.iterator(); i.hasNext();) {
69              addToHistory((String) i.next());
70          }
71      }
72  
73      public int size() {
74          return history.size();
75      }
76  
77      /***
78       * Clear the history buffer
79       */
80      public void clear() {
81          history.clear();
82          currentIndex = 0;
83      }
84  
85      /***
86       * Add the specified buffer to the end of the history. The pointer is set to
87       * the end of the history buffer.
88       */
89      public void addToHistory(final String buffer) {
90          // don't append duplicates to the end of the buffer
91          if ((history.size() != 0)
92                  && buffer.equals(history.get(history.size() - 1))) {
93              return;
94          }
95  
96          history.add(buffer);
97  
98          while (history.size() > getMaxSize()) {
99              history.remove(0);
100         }
101 
102         currentIndex = history.size();
103 
104         if (getOutput() != null) {
105             getOutput().println(buffer);
106             getOutput().flush();
107         }
108     }
109 
110     /***
111      * Flush the entire history buffer to the output PrintWriter.
112      */
113     public void flushBuffer() throws IOException {
114         if (getOutput() != null) {
115             for (Iterator i = history.iterator(); i.hasNext(); getOutput()
116                     .println((String) i.next())) {
117                 ;
118             }
119 
120             getOutput().flush();
121         }
122     }
123 
124     /***
125      * This moves the history to the last entry. This entry is one position
126      * before the moveToEnd() position.
127      *
128      * @return Returns false if there were no history entries or the history
129      *         index was already at the last entry.
130      */
131     public boolean moveToLastEntry() {
132         int lastEntry = history.size() - 1;
133         if (lastEntry >= 0 && lastEntry != currentIndex) {
134             currentIndex = history.size() - 1;
135             return true;
136         }
137 
138         return false;
139     }
140 
141     /***
142      * Move to the end of the history buffer. This will be a blank entry, after
143      * all of the other entries.
144      */
145     public void moveToEnd() {
146         currentIndex = history.size();
147     }
148 
149     /***
150      * Set the maximum size that the history buffer will store.
151      */
152     public void setMaxSize(final int maxSize) {
153         this.maxSize = maxSize;
154     }
155 
156     /***
157      * Get the maximum size that the history buffer will store.
158      */
159     public int getMaxSize() {
160         return this.maxSize;
161     }
162 
163     /***
164      * The output to which all history elements will be written (or null of
165      * history is not saved to a buffer).
166      */
167     public void setOutput(final PrintWriter output) {
168         this.output = output;
169     }
170 
171     /***
172      * Returns the PrintWriter that is used to store history elements.
173      */
174     public PrintWriter getOutput() {
175         return this.output;
176     }
177 
178     /***
179      * Returns the current history index.
180      */
181     public int getCurrentIndex() {
182         return this.currentIndex;
183     }
184 
185     /***
186      * Return the content of the current buffer.
187      */
188     public String current() {
189         if (currentIndex >= history.size()) {
190             return "";
191         }
192 
193         return (String) history.get(currentIndex);
194     }
195 
196     /***
197      * Move the pointer to the previous element in the buffer.
198      *
199      * @return true if we successfully went to the previous element
200      */
201     public boolean previous() {
202         if (currentIndex <= 0) {
203             return false;
204         }
205 
206         currentIndex--;
207 
208         return true;
209     }
210 
211     /***
212      * Move the pointer to the next element in the buffer.
213      *
214      * @return true if we successfully went to the next element
215      */
216     public boolean next() {
217         if (currentIndex >= history.size()) {
218             return false;
219         }
220 
221         currentIndex++;
222 
223         return true;
224     }
225 
226     /***
227      * Returns an immutable list of the history buffer.
228      */
229     public List getHistoryList() {
230         return Collections.unmodifiableList(history);
231     }
232 
233     /***
234      * Returns the standard {@link AbstractCollection#toString} representation
235      * of the history list.
236      */
237     public String toString() {
238         return history.toString();
239     }
240 
241     /***
242      * Moves the history index to the first entry.
243      *
244      * @return Return false if there are no entries in the history or if the
245      *         history is already at the beginning.
246      */
247     public boolean moveToFirstEntry() {
248         if (history.size() > 0 && currentIndex != 0) {
249             currentIndex = 0;
250             return true;
251         }
252 
253         return false;
254     }
255 }