Fix command formatting after refactor

- Fix spacing issues (too few or too many spaces)
- Handle base commands with invalid arguments properly
- Add highlighting function for command syntax
This commit is contained in:
ljacqu 2015-12-19 12:20:52 +01:00
parent bf9724dd34
commit 42c34c56a0
3 changed files with 57 additions and 53 deletions

View File

@ -189,17 +189,21 @@ public class CommandHandler {
}
private FoundCommandResult getCommandWithSmallestDifference(CommandDescription base, List<String> parts) {
final String childLabel = parts.size() >= 2 ? parts.get(1) : null;
// Return the base command with incorrect arg count error if we only have one part
if (parts.size() <= 1) {
return new FoundCommandResult(
base, parts, new ArrayList<String>(), 0.0, FoundResultStatus.INCORRECT_ARGUMENTS);
}
final String childLabel = parts.get(1);
double minDifference = Double.POSITIVE_INFINITY;
CommandDescription closestCommand = null;
if (childLabel != null) {
for (CommandDescription child : base.getChildren()) {
double difference = getLabelDifference(child, childLabel);
if (difference < minDifference) {
minDifference = difference;
closestCommand = child;
}
for (CommandDescription child : base.getChildren()) {
double difference = getLabelDifference(child, childLabel);
if (difference < minDifference) {
minDifference = difference;
closestCommand = child;
}
}

View File

@ -0,0 +1,36 @@
package fr.xephi.authme.command.help;
import fr.xephi.authme.command.CommandArgumentDescription;
import fr.xephi.authme.command.CommandDescription;
import org.bukkit.ChatColor;
import java.util.List;
/**
* Helper class for displaying the syntax of a command properly to a user.
*/
class CommandSyntaxHelper {
private CommandSyntaxHelper() {
}
public static String getSyntax(CommandDescription command, List<String> correctLabels) {
String commandSyntax = ChatColor.WHITE + "/" + correctLabels.get(0) + ChatColor.YELLOW;
for (int i = 1; i < correctLabels.size(); ++i) {
commandSyntax += " " + correctLabels.get(i);
}
for (CommandArgumentDescription argument : command.getArguments()) {
commandSyntax += " " + formatArgument(argument);
}
return commandSyntax;
}
/** Format a command argument with the proper type of brackets. */
private static String formatArgument(CommandArgumentDescription argument) {
if (argument.isOptional()) {
return "[" + argument.getName() + "]";
}
return "<" + argument.getName() + ">";
}
}

View File

@ -12,15 +12,13 @@ import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.CollectionUtils;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
/**
@ -67,7 +65,7 @@ public final class HelpProvider {
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
if (!hasFlag(HIDE_COMMAND, options)) {
printCommand(command, correctLabels, lines);
lines.add(ChatColor.GOLD + "Command: " + CommandSyntaxHelper.getSyntax(command, correctLabels));
}
if (hasFlag(SHOW_LONG_DESCRIPTION, options)) {
printDetailedDescription(command, lines);
@ -88,15 +86,6 @@ public final class HelpProvider {
return lines;
}
private static void printCommand(CommandDescription command, List<String> correctLabels, List<String> lines) {
// FIXME: Create highlight logic to mark arguments and the 2nd label as yellow
String syntaxLine = "/" + CommandUtils.labelsToString(correctLabels);
for (CommandArgumentDescription argument : command.getArguments()) {
syntaxLine += " " + formatArgument(argument);
}
lines.add(syntaxLine);
}
private static void printDetailedDescription(CommandDescription command, List<String> lines) {
lines.add(ChatColor.GOLD + "Short Description: " + ChatColor.WHITE + command.getDescription());
lines.add(ChatColor.GOLD + "Detailed Description:");
@ -104,7 +93,7 @@ public final class HelpProvider {
}
private static void printArguments(CommandDescription command, List<String> lines) {
if (!command.getArguments().isEmpty()) {
if (command.getArguments().isEmpty()) {
return;
}
@ -122,39 +111,22 @@ public final class HelpProvider {
}
private static void printAlternatives(CommandDescription command, List<String> correctLabels, List<String> lines) {
// TODO ljacqu 20151219: Need to show alternatives for base labels too? E.g. /r for /register
if (command.getLabels().size() <= 1) {
return;
}
lines.add(ChatColor.GOLD + "Alternatives:");
// Get the label used
final String usedLabel = correctLabels.get(correctLabels.size() - 1);
final String parentLabel = correctLabels.get(0);
final String childLabel = correctLabels.get(1);
// Create a list of alternatives
List<String> alternatives = new ArrayList<>();
for (String entry : command.getLabels()) {
if (!entry.equalsIgnoreCase(usedLabel)) {
alternatives.add(entry);
if (!entry.equalsIgnoreCase(childLabel)) {
lines.add(" " + CommandSyntaxHelper.getSyntax(command, asList(parentLabel, entry)));
}
}
// Sort the alternatives
Collections.sort(alternatives, new Comparator<String>() {
// TODO ljacqu 20151212: This computes the difference each time anew. It might make sense to compute the
// difference once and to store it in some map-like structure (Guava has some interesting ones)
@Override
public int compare(String o1, String o2) {
return Double.compare(StringUtils.getDifference(usedLabel, o1),
StringUtils.getDifference(usedLabel, o2));
}
});
// Print each alternative with proper syntax
for (String alternative : alternatives) {
// fixme add highlight functionality (see commented old line)
// sender.sendMessage(" " + _HelpSyntaxHelper.getCommandSyntax(command, commandReference, alternative, true));
lines.add(" " + CommandUtils.labelsToString(correctLabels) + " " + alternative);
}
}
private static void printPermissions(CommandDescription command, CommandSender sender,
@ -199,19 +171,11 @@ public final class HelpProvider {
lines.add(ChatColor.GOLD + "Commands:");
String parentCommandPath = CommandUtils.labelsToString(parentLabels);
for (CommandDescription child : command.getChildren()) {
lines.add(" " + parentCommandPath + child.getLabels().get(0)
lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0)
+ ChatColor.GRAY + ChatColor.ITALIC + ": " + child.getDescription());
}
}
/** Format a command argument with the proper type of brackets. */
private static String formatArgument(CommandArgumentDescription argument) {
if (argument.isOptional()) {
return "[" + argument.getName() + "]";
}
return "<" + argument.getName() + ">";
}
private static boolean hasFlag(int flag, int options) {
return (flag & options) != 0;
}