View Javadoc

1   /*
2   Karma CLI - Command Line Interface for the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.cli;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /***
25   * Class managing the CLI history of commands that have been run. The CLI is capable of fetching arrow-up and
26   * arrow-down commands
27   *
28   * @author D.A. Smedes
29   * @version $Id: HistoryManager.java,v 1.3 2004/11/02 23:57:05 asmedes Exp $
30   */
31  
32  // todo maybe abstract to karma-core; usable for aura as well ?
33  public final class HistoryManager {
34  
35    private static final int MAX_HISTORY_ITEMS = 100;
36  
37    private int maxHistoryItems = 1;
38    private List historyItems = null;
39    private int currentItem = -1;
40  
41    public HistoryManager(int maxHistoryItems) {
42      this.maxHistoryItems = (maxHistoryItems < 1 ? MAX_HISTORY_ITEMS : maxHistoryItems);
43      historyItems = new ArrayList(this.maxHistoryItems);
44    }
45  
46    /***
47     * Adds a command line string to the history. The current history
48     *
49     * @param commandLine The string that is fetched from <code>stdin</code>.
50     */
51    public synchronized void addHistoryItem(String commandLine) {
52  
53      if (historyItems.size() == maxHistoryItems) {
54        historyItems.remove(historyItems.get(0)); // Fifo ...
55      }
56      historyItems.add(commandLine);
57      if (currentItem == -1) {
58        currentItem = 0;
59      } else {
60        currentItem++;
61      }
62  //    currentItem = (currentItem == -1 ? 0 : currentItem++); // Initialize pointer when -1, or leave intact.
63    }
64  
65    /***
66     * Returns the current history item that is been pointed at. The history maintains a pointer to the last selected
67     * item, and returns that one.
68     *
69     * @return A command line item as a string or an empty string when no history items were available.
70     */
71    public String getCurrentHistoryItem() {
72  
73  //    currentItem = (currentItem == 0 ? currentItem : --currentItem);
74  
75      if (currentItem != -1) {
76        return (String) historyItems.get(currentItem--);
77      } else {
78        return "";
79      }
80    }
81  }