Skip to content

Commit

Permalink
Change all command error/output to use events.
Browse files Browse the repository at this point in the history
  • Loading branch information
csmith committed Feb 5, 2016
1 parent 2204bf0 commit 621a53c
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/com/dmdirc/commandparser/aliases/AliasCommandHandler.java
Expand Up @@ -50,9 +50,9 @@ public void execute(@Nonnull final WindowModel origin, final CommandArguments ar
origin.getCommandParser().parseCommand(origin, getSubstituteCommand(line, args));
}
} else {
sendLine(origin, args.isSilent(), FORMAT_ERROR, alias.getName() + " requires at least "
showError(origin, args.isSilent(), alias.getName() + " requires at least "
+ alias.getMinArguments() + " argument"
+ (alias.getMinArguments() == 1 ? "" : "s") + ".");
+ (alias.getMinArguments() == 1 ? "" : "s") + '.');
}
}

Expand All @@ -72,7 +72,7 @@ private String getSubstituteCommand(final String line, final CommandArguments ar

final String[] arguments = args.getArguments();
for (int i = 0; i < arguments.length; i++) {
replaceAll(builder, "$" + (i + 1) + "-", args.getArgumentsAsString(i));
replaceAll(builder, "$" + (i + 1) + '-', args.getArgumentsAsString(i));
replaceAll(builder, "$" + (i + 1), arguments[i]);
}

Expand Down
6 changes: 0 additions & 6 deletions src/com/dmdirc/commandparser/commands/Command.java
Expand Up @@ -38,12 +38,6 @@
*/
public abstract class Command {

/** The format name used for command output. */
@Deprecated
protected static final String FORMAT_OUTPUT = "commandOutput";
/** The format name used for command errors. */
@Deprecated
protected static final String FORMAT_ERROR = "commandError";
/** The controller this command is associated with. */
private final CommandController controller;

Expand Down
6 changes: 2 additions & 4 deletions src/com/dmdirc/commandparser/commands/channel/Invite.java
Expand Up @@ -66,8 +66,7 @@ public Invite(final CommandController controller) {
public void execute(@Nonnull final WindowModel origin,
final CommandArguments args, final CommandContext context) {
if (args.getArguments().length < 1) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
"Insufficient arguments: must specify user");
showError(origin, args.isSilent(), "Insufficient arguments: must specify user");
} else {
final GroupChat groupChat = ((ChannelCommandContext) context).getGroupChat();
groupChat.getConnection().flatMap(Connection::getParser)
Expand All @@ -79,8 +78,7 @@ public void execute(@Nonnull final WindowModel origin,
public void execute(final WindowModel origin, final Connection connection,
final String channel, final boolean isSilent, final CommandArguments args) {
if (args.getArguments().length < 1) {
sendLine(origin, isSilent, FORMAT_ERROR,
"Insufficient arguments: must specify user");
showError(origin, isSilent, "Insufficient arguments: must specify user");
} else {
connection.getParser().get().sendInvite(channel, args.getArgumentsAsString());
}
Expand Down
7 changes: 3 additions & 4 deletions src/com/dmdirc/commandparser/commands/global/Echo.java
Expand Up @@ -97,14 +97,14 @@ public void execute(@Nonnull final WindowModel origin,
try {
time = new Date(Long.parseLong(results.getArgumentsAsString(timeStampFlag)));
} catch (NumberFormatException ex) {
sendLine(origin, args.isSilent(), FORMAT_ERROR, "Unable to process timestamp");
showError(origin, args.isSilent(), "Unable to process timestamp");
return;
}
}

if (results.hasFlag(targetFlag)) {
WindowModel frame = null;
Optional<WindowModel> target = Optional.ofNullable(origin);
Optional<WindowModel> target = Optional.of(origin);

while (frame == null && target.isPresent()) {
frame = windowManager.findCustomWindow(target.get(),
Expand All @@ -117,8 +117,7 @@ public void execute(@Nonnull final WindowModel origin,
}

if (frame == null) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
"Unable to find target window");
showError(origin, args.isSilent(), "Unable to find target window");
} else if (!args.isSilent()) {
frame.getEventBus().publishAsync(new CommandOutputEvent(frame, time.getTime(),
results.getArgumentsAsString()));
Expand Down
20 changes: 10 additions & 10 deletions src/com/dmdirc/commandparser/commands/global/Help.java
Expand Up @@ -87,14 +87,14 @@ private void showAllCommands(final WindowModel origin, final boolean isSilent) {

Collections.sort(commands);

sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
showOutput(origin, isSilent, Styliser.CODE_FIXED
+ "----------------------- Available commands -------");

final StringBuilder builder = new StringBuilder();

for (String command : commands) {
if (builder.length() + command.length() + 1 > 50) {
sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
showOutput(origin, isSilent, Styliser.CODE_FIXED + builder.toString());
builder.delete(0, builder.length());
} else if (builder.length() > 0) {
builder.append(' ');
Expand All @@ -104,10 +104,10 @@ private void showAllCommands(final WindowModel origin, final boolean isSilent) {
}

if (builder.length() > 0) {
sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
showOutput(origin, isSilent, Styliser.CODE_FIXED + builder.toString());
}

sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
showOutput(origin, isSilent, Styliser.CODE_FIXED
+ "--------------------------------------------------");
}

Expand All @@ -129,17 +129,17 @@ private void showCommand(final WindowModel origin, final boolean isSilent,
}

if (command == null) {
sendLine(origin, isSilent, FORMAT_ERROR, "Command '" + name + "' not found.");
showError(origin, isSilent, "Command '" + name + "' not found.");
} else {
sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
showOutput(origin, isSilent, Styliser.CODE_FIXED
+ "---------------------- Command information -------");
sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
showOutput(origin, isSilent, Styliser.CODE_FIXED
+ " Name: " + name);
sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
showOutput(origin, isSilent, Styliser.CODE_FIXED
+ " Type: " + command.getKey().getType());
sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
showOutput(origin, isSilent, Styliser.CODE_FIXED
+ "Usage: " + command.getKey().getHelp());
sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
showOutput(origin, isSilent, Styliser.CODE_FIXED
+ "--------------------------------------------------");
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/com/dmdirc/commandparser/commands/global/LoadPlugin.java
Expand Up @@ -78,21 +78,17 @@ public void execute(@Nonnull final WindowModel origin,
final PluginInfo plugin = pluginManager.getPluginInfo(args.getArgumentsAsString());

if (plugin == null) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
"Plugin loading failed");
showError(origin, args.isSilent(), "Plugin loading failed");
} else if (plugin.isLoaded()) {
sendLine(origin, args.isSilent(), FORMAT_OUTPUT,
"Plugin already loaded.");
showError(origin, args.isSilent(), "Plugin already loaded.");
} else {
plugin.loadPlugin();
if (plugin.isLoaded()) {
sendLine(origin, args.isSilent(), FORMAT_OUTPUT,
"Plugin loaded.");
showOutput(origin, args.isSilent(), "Plugin loaded.");
pluginManager.updateAutoLoad(plugin);
} else {
sendLine(origin, args.isSilent(), FORMAT_OUTPUT,
"Loading plugin failed. ("
+ plugin.getLastError() + ")");
showOutput(origin, args.isSilent(),
"Loading plugin failed. (" + plugin.getLastError() + ')');
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/com/dmdirc/commandparser/commands/global/OpenWindow.java
Expand Up @@ -91,7 +91,7 @@ public void execute(@Nonnull final WindowModel origin,
if (args.getArguments().length > 0 && "--server".equals(args.getArguments()[0])) {
final Optional<Connection> connection = origin.getConnection();
if (!connection.isPresent()) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
showError(origin, args.isSilent(),
"This window doesn't have an associated server.");
return;
}
Expand Down Expand Up @@ -130,8 +130,7 @@ public void execute(@Nonnull final WindowModel origin,
windowManager.addWindow(parent, newWindow);
}
} else {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
"A custom window by that name already exists.");
showError(origin, args.isSilent(), "A custom window by that name already exists.");
}
}
}
Expand Down
Expand Up @@ -66,8 +66,7 @@ public ReloadIdentities(final CommandController controller,
public void execute(@Nonnull final WindowModel origin,
final CommandArguments args, final CommandContext context) {
identityController.loadUserIdentities();

sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Identities reloaded.");
showOutput(origin, args.isSilent(), "Identities reloaded.");
}

@Override
Expand Down
Expand Up @@ -74,12 +74,12 @@ public void execute(@Nonnull final WindowModel origin,

final PluginInfo plugin = pluginManager.getPluginInfoByName(args.getArguments()[0]);
if (plugin == null) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
showError(origin, args.isSilent(),
"Plugin Reloading failed - Plugin not loaded");
} else if (pluginManager.reloadPlugin(plugin.getMetaData().getRelativeFilename())) {
sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Plugin Reloaded.");
showOutput(origin, args.isSilent(), "Plugin Reloaded.");
} else {
sendLine(origin, args.isSilent(), FORMAT_ERROR, "Plugin Reloading failed");
showError(origin, args.isSilent(), "Plugin Reloading failed");
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/com/dmdirc/commandparser/commands/global/SaveConfig.java
Expand Up @@ -66,8 +66,7 @@ public SaveConfig(final CommandController controller,
public void execute(@Nonnull final WindowModel origin,
final CommandArguments args, final CommandContext context) {
identityController.saveAll();

sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Configuration file saved.");
showOutput(origin, args.isSilent(), "Configuration file saved.");
}

@Override
Expand Down
22 changes: 9 additions & 13 deletions src/com/dmdirc/commandparser/commands/global/SetCommand.java
Expand Up @@ -118,8 +118,7 @@ public void execute(@Nonnull final WindowModel origin,

if (res.hasFlag(serverFlag)) {
if (!connection.isPresent()) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
"Cannot use --server in this context");
showError(origin, args.isSilent(), "Cannot use --server in this context");
return;
}

Expand All @@ -129,8 +128,7 @@ public void execute(@Nonnull final WindowModel origin,

if (res.hasFlag(channelFlag)) {
if (!(context instanceof ChannelCommandContext)) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
"Cannot use --channel in this context");
showError(origin, args.isSilent(),"Cannot use --channel in this context");
return;
}

Expand Down Expand Up @@ -193,7 +191,7 @@ private void doDomainList(final WindowModel origin, final boolean isSilent,
output.append(", ");
}

sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
showOutput(origin, isSilent, output.substring(0, output.length() - 2));
}

/**
Expand Down Expand Up @@ -221,10 +219,9 @@ private void doOptionsList(final WindowModel origin,
}

if (found) {
sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
showOutput(origin, isSilent, output.substring(0, output.length() - 2));
} else {
sendLine(origin, isSilent, FORMAT_ERROR,
"There are no options in the domain '" + domain + "'.");
showError(origin, isSilent, "There are no options in the domain '" + domain + "'.");
}
}

Expand All @@ -241,10 +238,10 @@ private void doShowOption(final WindowModel origin,
final boolean isSilent, final ReadOnlyConfigProvider manager,
final String domain, final String option) {
if (manager.hasOptionString(domain, option)) {
sendLine(origin, isSilent, FORMAT_OUTPUT, "The current value of "
showOutput(origin, isSilent, "The current value of "
+ domain + '.' + option + " is: " + manager.getOption(domain, option));
} else {
sendLine(origin, isSilent, FORMAT_ERROR, "Option not found: " + domain + '.' + option);
showError(origin, isSilent, "Option not found: " + domain + '.' + option);
}
}

Expand All @@ -263,8 +260,7 @@ private void doSetOption(final WindowModel origin,
final String domain, final String option, final String newvalue) {
identity.setOption(domain, option, newvalue);

sendLine(origin, isSilent, FORMAT_OUTPUT, domain + '.' + option
+ " has been set to: " + newvalue);
showOutput(origin, isSilent, domain + '.' + option + " has been set to: " + newvalue);
}

/**
Expand Down Expand Up @@ -301,7 +297,7 @@ private void doUnsetOption(final WindowModel origin,
final String option) {
identity.unsetOption(domain, option);

sendLine(origin, isSilent, FORMAT_OUTPUT, domain + '.' + option + " has been unset.");
showOutput(origin, isSilent, domain + '.' + option + " has been unset.");
}

@Override
Expand Down
Expand Up @@ -74,13 +74,13 @@ public void execute(@Nonnull final WindowModel origin,

final PluginInfo plugin = pluginManager.getPluginInfoByName(args.getArguments()[0]);
if (plugin == null) {
sendLine(origin, args.isSilent(), FORMAT_ERROR,
showError(origin, args.isSilent(),
"Plugin unloading failed - Plugin not loaded");
} else if (pluginManager.delPlugin(plugin.getMetaData().getRelativeFilename())) {
sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Plugin Unloaded.");
showOutput(origin, args.isSilent(), "Plugin Unloaded.");
pluginManager.updateAutoLoad(plugin);
} else {
sendLine(origin, args.isSilent(), FORMAT_ERROR, "Plugin Unloading failed");
showError(origin, args.isSilent(), "Plugin Unloading failed");
}
}

Expand Down

0 comments on commit 621a53c

Please sign in to comment.