1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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));
55 }
56 historyItems.add(commandLine);
57 if (currentItem == -1) {
58 currentItem = 0;
59 } else {
60 currentItem++;
61 }
62
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
74
75 if (currentItem != -1) {
76 return (String) historyItems.get(currentItem--);
77 } else {
78 return "";
79 }
80 }
81 }