From c29ca03e3508fbbd9d230e3504aa89ad3b0fddc7 Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 30 Jun 2014 22:08:08 -0700 Subject: [PATCH] Improved re-use of command help formatting. --- .../worldedit/command/UtilityCommands.java | 32 ++------ .../util/command/SimpleDispatcher.java | 2 +- .../{Cmd.java => components/Code.java} | 9 ++- .../{ => components}/CommandListBox.java | 4 +- .../components/CommandUsageBox.java | 74 +++++++++++++++++++ .../util/formatting/components/Label.java | 37 ++++++++++ .../{ => components}/MessageBox.java | 6 +- .../util/formatting/components/Subtle.java | 37 ++++++++++ 8 files changed, 169 insertions(+), 32 deletions(-) rename src/main/java/com/sk89q/worldedit/util/formatting/{Cmd.java => components/Code.java} (81%) rename src/main/java/com/sk89q/worldedit/util/formatting/{ => components}/CommandListBox.java (92%) create mode 100644 src/main/java/com/sk89q/worldedit/util/formatting/components/CommandUsageBox.java create mode 100644 src/main/java/com/sk89q/worldedit/util/formatting/components/Label.java rename src/main/java/com/sk89q/worldedit/util/formatting/{ => components}/MessageBox.java (90%) create mode 100644 src/main/java/com/sk89q/worldedit/util/formatting/components/Subtle.java diff --git a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index 0f0633491..e231c1c2e 100644 --- a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -41,13 +41,13 @@ import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.util.command.CommandCallable; import com.sk89q.worldedit.util.command.CommandMapping; -import com.sk89q.worldedit.util.command.Description; import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.util.command.PrimaryAliasComparator; import com.sk89q.worldedit.util.command.parametric.Optional; -import com.sk89q.worldedit.util.formatting.Cmd; +import com.sk89q.worldedit.util.formatting.components.Code; import com.sk89q.worldedit.util.formatting.ColorCodeBuilder; -import com.sk89q.worldedit.util.formatting.CommandListBox; +import com.sk89q.worldedit.util.formatting.components.CommandListBox; +import com.sk89q.worldedit.util.formatting.components.CommandUsageBox; import com.sk89q.worldedit.util.formatting.Style; import com.sk89q.worldedit.util.formatting.StyledFragment; import com.sk89q.worldedit.world.World; @@ -623,7 +623,7 @@ public class UtilityCommands { List list = aliases.subList(offset, Math.min(offset + perPage, aliases.size())); tip.append("Type "); - tip.append(new Cmd().append("//help ").append(" []")); + tip.append(new Code().append("//help ").append(" []")); tip.append(" for more information.").newLine(); // Add each command @@ -643,28 +643,8 @@ public class UtilityCommands { actor.printRaw(ColorCodeBuilder.asColorCodes(box)); } else { - CommandListBox box = new CommandListBox(String.format("Help: %s", Joiner.on(" ").join(visited))); - StyledFragment contents = box.getContents(); - - Description description = callable.getDescription(); - - if (description.getUsage() != null) { - contents.createFragment(Style.YELLOW).append("Usage: "); - contents.append(description.getUsage()); - } else { - contents.createFragment(Style.GRAY).append("Usage information is not available."); - } - - contents.newLine(); - - if (description.getHelp() != null) { - contents.createFragment(Style.YELLOW_DARK).append(description.getHelp()); - } else if (description.getShortDescription() != null) { - contents.createFragment(Style.YELLOW_DARK).append(description.getShortDescription()); - } else { - contents.createFragment(Style.GRAY).append("No further help is available."); - } - + String title = String.format("Help: %s", Joiner.on(" ").join(visited)); + CommandUsageBox box = new CommandUsageBox(callable.getDescription(), title); actor.printRaw(ColorCodeBuilder.asColorCodes(box)); } } diff --git a/src/main/java/com/sk89q/worldedit/util/command/SimpleDispatcher.java b/src/main/java/com/sk89q/worldedit/util/command/SimpleDispatcher.java index a84c6b497..e4733a4f0 100644 --- a/src/main/java/com/sk89q/worldedit/util/command/SimpleDispatcher.java +++ b/src/main/java/com/sk89q/worldedit/util/command/SimpleDispatcher.java @@ -22,7 +22,7 @@ package com.sk89q.worldedit.util.command; import com.google.common.base.Joiner; import com.sk89q.minecraft.util.commands.*; import com.sk89q.worldedit.util.formatting.ColorCodeBuilder; -import com.sk89q.worldedit.util.formatting.CommandListBox; +import com.sk89q.worldedit.util.formatting.components.CommandListBox; import com.sk89q.worldedit.util.formatting.StyledFragment; import java.util.*; diff --git a/src/main/java/com/sk89q/worldedit/util/formatting/Cmd.java b/src/main/java/com/sk89q/worldedit/util/formatting/components/Code.java similarity index 81% rename from src/main/java/com/sk89q/worldedit/util/formatting/Cmd.java rename to src/main/java/com/sk89q/worldedit/util/formatting/components/Code.java index c940b73fa..dd26e7aff 100644 --- a/src/main/java/com/sk89q/worldedit/util/formatting/Cmd.java +++ b/src/main/java/com/sk89q/worldedit/util/formatting/components/Code.java @@ -17,17 +17,20 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.util.formatting; +package com.sk89q.worldedit.util.formatting.components; + +import com.sk89q.worldedit.util.formatting.Style; +import com.sk89q.worldedit.util.formatting.StyledFragment; /** * Represents a fragment representing a command that is to be typed. */ -public class Cmd extends StyledFragment { +public class Code extends StyledFragment { /** * Create a new instance. */ - public Cmd() { + public Code() { super(Style.CYAN); } diff --git a/src/main/java/com/sk89q/worldedit/util/formatting/CommandListBox.java b/src/main/java/com/sk89q/worldedit/util/formatting/components/CommandListBox.java similarity index 92% rename from src/main/java/com/sk89q/worldedit/util/formatting/CommandListBox.java rename to src/main/java/com/sk89q/worldedit/util/formatting/components/CommandListBox.java index 03fa0b754..b1eb0a644 100644 --- a/src/main/java/com/sk89q/worldedit/util/formatting/CommandListBox.java +++ b/src/main/java/com/sk89q/worldedit/util/formatting/components/CommandListBox.java @@ -17,7 +17,9 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.util.formatting; +package com.sk89q.worldedit.util.formatting.components; + +import com.sk89q.worldedit.util.formatting.Style; public class CommandListBox extends MessageBox { diff --git a/src/main/java/com/sk89q/worldedit/util/formatting/components/CommandUsageBox.java b/src/main/java/com/sk89q/worldedit/util/formatting/components/CommandUsageBox.java new file mode 100644 index 000000000..d14683e09 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/util/formatting/components/CommandUsageBox.java @@ -0,0 +1,74 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.util.formatting.components; + +import com.sk89q.worldedit.util.command.Description; +import com.sk89q.worldedit.util.formatting.Style; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A box to describe usage of a command. + */ +public class CommandUsageBox extends MessageBox { + + /** + * Create a new box. + * + * @param description the command to describe + * @param title the title + */ + public CommandUsageBox(Description description, String title) { + super(title); + checkNotNull(description); + attachCommandUsage(description); + } + + /** + * Create a new box. + * + * @param description the command to describe + */ + public CommandUsageBox(Description description) { + super("Usage Help"); + checkNotNull(description); + attachCommandUsage(description); + } + + private void attachCommandUsage(Description description) { + if (description.getUsage() != null) { + getContents().append(new Label().append("Usage: ")); + getContents().append(description.getUsage()); + } else { + getContents().append(new Subtle().append("Usage information is not available.")); + } + + getContents().newLine(); + + if (description.getHelp() != null) { + getContents().createFragment(Style.YELLOW_DARK).append(description.getHelp()); + } else if (description.getShortDescription() != null) { + getContents().createFragment(Style.YELLOW_DARK).append(description.getShortDescription()); + } else { + getContents().append(new Subtle().append("No further help is available.")); + } + } + +} diff --git a/src/main/java/com/sk89q/worldedit/util/formatting/components/Label.java b/src/main/java/com/sk89q/worldedit/util/formatting/components/Label.java new file mode 100644 index 000000000..a646a1899 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/util/formatting/components/Label.java @@ -0,0 +1,37 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.util.formatting.components; + +import com.sk89q.worldedit.util.formatting.Style; +import com.sk89q.worldedit.util.formatting.StyledFragment; + +/** + * Represents a fragment representing a label. + */ +public class Label extends StyledFragment { + + /** + * Create a new instance. + */ + public Label() { + super(Style.YELLOW); + } + +} diff --git a/src/main/java/com/sk89q/worldedit/util/formatting/MessageBox.java b/src/main/java/com/sk89q/worldedit/util/formatting/components/MessageBox.java similarity index 90% rename from src/main/java/com/sk89q/worldedit/util/formatting/MessageBox.java rename to src/main/java/com/sk89q/worldedit/util/formatting/components/MessageBox.java index 66cf81ab3..d0a97e8c3 100644 --- a/src/main/java/com/sk89q/worldedit/util/formatting/MessageBox.java +++ b/src/main/java/com/sk89q/worldedit/util/formatting/components/MessageBox.java @@ -17,7 +17,11 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.util.formatting; +package com.sk89q.worldedit.util.formatting.components; + +import com.sk89q.worldedit.util.formatting.ColorCodeBuilder; +import com.sk89q.worldedit.util.formatting.Style; +import com.sk89q.worldedit.util.formatting.StyledFragment; import static com.google.common.base.Preconditions.checkNotNull; diff --git a/src/main/java/com/sk89q/worldedit/util/formatting/components/Subtle.java b/src/main/java/com/sk89q/worldedit/util/formatting/components/Subtle.java new file mode 100644 index 000000000..48e927b89 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/util/formatting/components/Subtle.java @@ -0,0 +1,37 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.util.formatting.components; + +import com.sk89q.worldedit.util.formatting.Style; +import com.sk89q.worldedit.util.formatting.StyledFragment; + +/** + * Represents a subtle part of the message. + */ +public class Subtle extends StyledFragment { + + /** + * Create a new instance. + */ + public Subtle() { + super(Style.GRAY); + } + +}