|
@@ -29,7 +29,9 @@ import com.dmdirc.config.IdentityManager;
|
29
|
29
|
import com.dmdirc.ui.interfaces.InputWindow;
|
30
|
30
|
|
31
|
31
|
import java.io.Serializable;
|
|
32
|
+import java.util.ArrayList;
|
32
|
33
|
import java.util.Hashtable;
|
|
34
|
+import java.util.List;
|
33
|
35
|
import java.util.Map;
|
34
|
36
|
|
35
|
37
|
/**
|
|
@@ -52,6 +54,11 @@ public abstract class CommandParser implements Serializable {
|
52
|
54
|
*/
|
53
|
55
|
private final Map<String, Command> commands;
|
54
|
56
|
|
|
57
|
+ /**
|
|
58
|
+ * A history of commands that have been entered into this parser.
|
|
59
|
+ */
|
|
60
|
+ private final List<PreviousCommand> history = new ArrayList<PreviousCommand>();
|
|
61
|
+
|
55
|
62
|
/** Creates a new instance of CommandParser. */
|
56
|
63
|
public CommandParser() {
|
57
|
64
|
commands = new Hashtable<String, Command>();
|
|
@@ -90,12 +97,14 @@ public abstract class CommandParser implements Serializable {
|
90
|
97
|
return;
|
91
|
98
|
}
|
92
|
99
|
|
93
|
|
- if (line.charAt(0) == IdentityManager.getGlobalConfig().getOption("general", "commandchar").charAt(0)) {
|
|
100
|
+ if (line.charAt(0) == IdentityManager.getGlobalConfig()
|
|
101
|
+ .getOption("general", "commandchar").charAt(0)) {
|
94
|
102
|
int offset = 1;
|
95
|
103
|
boolean silent = false;
|
96
|
104
|
|
97
|
105
|
if (line.length() > offset
|
98
|
|
- && line.charAt(offset) == IdentityManager.getGlobalConfig().getOption("general", "silencechar").charAt(0)) {
|
|
106
|
+ && line.charAt(offset) == IdentityManager.getGlobalConfig()
|
|
107
|
+ .getOption("general", "silencechar").charAt(0)) {
|
99
|
108
|
silent = true;
|
100
|
109
|
offset++;
|
101
|
110
|
}
|
|
@@ -106,7 +115,8 @@ public abstract class CommandParser implements Serializable {
|
106
|
115
|
|
107
|
116
|
assert args.length > 0;
|
108
|
117
|
|
109
|
|
- if (args.length >= 2 && parseChannel && origin != null && origin.getContainer().getServer() != null
|
|
118
|
+ if (args.length >= 2 && parseChannel && origin != null
|
|
119
|
+ && origin.getContainer().getServer() != null
|
110
|
120
|
&& origin.getContainer().getServer().getParser().isValidChannelName(args[1])
|
111
|
121
|
&& CommandManager.isChannelCommand(command)) {
|
112
|
122
|
final Server server = origin.getContainer().getServer();
|
|
@@ -138,8 +148,10 @@ public abstract class CommandParser implements Serializable {
|
138
|
148
|
// have error handlers if there are too few arguments (e.g., msg/0 and
|
139
|
149
|
// msg/1 would return errors, so msg only gets called with 2+ args).
|
140
|
150
|
if (commands.containsKey(signature.toLowerCase())) {
|
|
151
|
+ addHistory(command, comargs);
|
141
|
152
|
executeCommand(origin, silent, commands.get(signature.toLowerCase()), comargs);
|
142
|
153
|
} else if (commands.containsKey(command.toLowerCase())) {
|
|
154
|
+ addHistory(command, comargs);
|
143
|
155
|
executeCommand(origin, silent, commands.get(command.toLowerCase()), comargs);
|
144
|
156
|
} else {
|
145
|
157
|
handleInvalidCommand(origin, command, comargs);
|
|
@@ -149,6 +161,47 @@ public abstract class CommandParser implements Serializable {
|
149
|
161
|
}
|
150
|
162
|
}
|
151
|
163
|
|
|
164
|
+ /**
|
|
165
|
+ * Adds a command to this parser's history.
|
|
166
|
+ *
|
|
167
|
+ * @param command The command name that was used
|
|
168
|
+ * @param args The arguments for the command, if any
|
|
169
|
+ */
|
|
170
|
+ private void addHistory(final String command, final String... args) {
|
|
171
|
+ final StringBuilder builder = new StringBuilder(command);
|
|
172
|
+
|
|
173
|
+ for (String arg : args) {
|
|
174
|
+ builder.append(' ');
|
|
175
|
+ builder.append(arg);
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ history.add(new PreviousCommand(builder.toString()));
|
|
179
|
+
|
|
180
|
+ while (history.size() >
|
|
181
|
+ IdentityManager.getGlobalConfig().getOptionInt("general", "commandhistory", 10)) {
|
|
182
|
+ history.remove(0);
|
|
183
|
+ }
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ /**
|
|
187
|
+ * Retrieves the most recent time that the specified command was used.
|
|
188
|
+ * Commands should not include command or silence chars.
|
|
189
|
+ *
|
|
190
|
+ * @param command The command to search for
|
|
191
|
+ * @return The timestamp that the command was used, or 0 if it wasn't
|
|
192
|
+ */
|
|
193
|
+ public long getCommandTime(final String command) {
|
|
194
|
+ long res = 0;
|
|
195
|
+
|
|
196
|
+ for (PreviousCommand pc : history) {
|
|
197
|
+ if (pc.getLine().equalsIgnoreCase(command)) {
|
|
198
|
+ res = Math.max(res, pc.getTime());
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+
|
|
202
|
+ return res;
|
|
203
|
+ }
|
|
204
|
+
|
152
|
205
|
/**
|
153
|
206
|
* Parses the specified string as a command.
|
154
|
207
|
* @param origin The window in which the command was typed
|