/
Command.java
162 lines (140 loc) · 5.77 KB
/
Command.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.dmdirc.commandparser.commands;
import com.dmdirc.commandparser.CommandArguments;
import com.dmdirc.commandparser.commands.context.CommandContext;
import com.dmdirc.events.CommandErrorEvent;
import com.dmdirc.events.CommandOutputEvent;
import com.dmdirc.interfaces.CommandController;
import com.dmdirc.interfaces.WindowModel;
import com.dmdirc.ui.messages.Styliser;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Represents a generic command.
*/
public abstract class Command {
/** The controller this command is associated with. */
private final CommandController controller;
public Command(final CommandController controller) {
this.controller = controller;
}
protected CommandController getController() {
return controller;
}
/**
* Sends an output line, if appropriate, to the specified target.
*
* @param target The command window to send the line to
* @param isSilent Whether this command is being silenced or not
* @param message The output to send
*/
protected final void showOutput(@Nullable final WindowModel target,
final boolean isSilent, final String message) {
if (!isSilent && target != null) {
target.getEventBus().publishAsync(new CommandOutputEvent(target, message));
}
}
/**
* Sends an error line, if appropriate, to the specified target.
*
* @param target The command window to send the line to
* @param isSilent Whether this command is being silenced or not
* @param message The error message to send
*/
protected final void showError(@Nullable final WindowModel target,
final boolean isSilent, final String message) {
if (!isSilent && target != null) {
target.getEventBus().publishAsync(new CommandErrorEvent(target, message));
}
}
/**
* Sends a usage line, if appropriate, to the specified target.
*
* @param target The command window to send the line to
* @param isSilent Whether this command is being silenced or not
* @param name The name of the command that's raising the error
* @param args The arguments that the command accepts or expects
*/
protected final void showUsage(@Nullable final WindowModel target,
final boolean isSilent, final String name, final String args) {
if (!isSilent && target != null) {
target.getEventBus().publishAsync(new CommandErrorEvent(target,
"Usage: " + controller.getCommandChar() + name + ' ' + args));
}
}
/**
* Formats the specified data into a table suitable for output in the textpane. It is expected
* that each String[] in data has the same number of elements as the headers array.
*
* @param headers The headers of the table.
* @param data The contents of the table.
*
* @return A string containing an ASCII table
*/
protected static String doTable(final String[] headers, final String[][] data) {
final StringBuilder res = new StringBuilder();
res.append(Styliser.CODE_FIXED);
res.append(Styliser.CODE_BOLD);
final int[] maxsizes = new int[headers.length];
for (int i = 0; i < headers.length; i++) {
maxsizes[i] = headers[i].length() + 3;
for (String[] row : data) {
maxsizes[i] = Math.max(maxsizes[i], row[i].length() + 3);
}
doPadding(res, headers[i], maxsizes[i]);
}
for (String[] source : data) {
res.append('\n');
res.append(Styliser.CODE_FIXED);
for (int i = 0; i < source.length; i++) {
doPadding(res, source[i], maxsizes[i]);
}
}
return res.toString();
}
/**
* Adds the specified data to the stringbuilder, padding with spaces to the specified size.
*
* @param builder The stringbuilder to append data to
* @param data The data to be added
* @param size The minimum size that should be used
*/
private static void doPadding(final StringBuilder builder, final String data,
final int size) {
builder.append(data);
for (int i = 0; i < size - data.length(); i++) {
builder.append(' ');
}
}
/**
* Executes this command.
*
* @param origin The container which received the command
* @param args Arguments passed to this command
* @param context The context the command was executed in
*
* @since 0.6.4
*/
public abstract void execute(@Nonnull WindowModel origin, CommandArguments args,
CommandContext context);
}