summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java21
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java (renamed from BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java)103
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java36
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java23
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java17
5 files changed, 173 insertions, 27 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java
index 99951cc..522dfbd 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java
@@ -7,27 +7,6 @@ package bjc.utils.cli;
*
*/
public class GenericCommand implements ICommand {
- private static class GenericHelp implements ICommandHelp {
- private String summary;
- private String description;
-
- public GenericHelp(String summary, String description) {
- this.summary = summary;
- this.description = description;
- }
-
- @Override
- public String getSummary() {
- return summary;
- }
-
- @Override
- public String getDescription() {
- return description;
- }
-
- }
-
private ICommandHandler handler;
private ICommandHelp help;
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java
index 880e8c5..5b35a0b 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java
@@ -1,5 +1,6 @@
package bjc.utils.cli;
+import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@@ -16,7 +17,7 @@ import bjc.utils.funcdata.IFunctionalMap;
* @author ben
*
*/
-public class GeneralCommandMode implements ICommandMode {
+public class GenericCommandMode implements ICommandMode {
private IFunctionalMap<String, ICommand> commandHandlers;
private IFunctionalMap<String, ICommand> defaultHandlers;
@@ -32,20 +33,21 @@ public class GeneralCommandMode implements ICommandMode {
private String customPrompt;
/**
- * Create a new general command mode
+ * Create a new generic command mode
*
* @param normalOutput
* The function to use for normal output
* @param errorOutput
* The function to use for error output
*/
- public GeneralCommandMode(Consumer<String> normalOutput,
+ public GenericCommandMode(Consumer<String> normalOutput,
Consumer<String> errorOutput) {
this.normalOutput = normalOutput;
this.errorOutput = errorOutput;
- commandHandlers = new FunctionalMap<>();
- defaultHandlers = new FunctionalMap<>();
+ commandHandlers = new FunctionalMap<>(new TreeMap<>());
+ defaultHandlers = new FunctionalMap<>(new TreeMap<>());
+ helpTopics = new FunctionalMap<>(new TreeMap<>());
defaultHandlers.put("list", new GenericCommand((args) -> {
listCommands();
@@ -65,7 +67,14 @@ public class GeneralCommandMode implements ICommandMode {
+ ", and the alias to give that command."));
defaultHandlers.put("help", new GenericCommand((args) -> {
- // TODO implement help system
+ if (args == null || args.length == 0) {
+ // Invoke general help
+ helpSummary();
+ } else {
+ // Invoke help for a command
+ helpCommand(args[0]);
+ }
+
return this;
}, "help\tConsult the help system",
"help consults the internal help system."
@@ -73,6 +82,25 @@ public class GeneralCommandMode implements ICommandMode {
+ " causes it to print out all the topics you can ask for details on,"
+ " while invoking it with the name of a topic will print the entry"
+ " for that topic"));
+
+ // Add commands handled in a upper layer
+ defaultHandlers.put("clear", new GenericCommand((args) -> {
+ errorOutput.accept(
+ "ERROR: This is a bug. Please report to the developer");
+
+ return this;
+ }, "clear\tClear the screen",
+ "clear clears the screen of all the text on it,"
+ + " and prepares a fresh prompt."));
+
+ defaultHandlers.put("exit", new GenericCommand((args) -> {
+ errorOutput.accept(
+ "ERROR: This is a bug. Please report to the developer");
+
+ return this;
+ }, "exit\tExit the game",
+ "exit first prompts the user to make sure they want to exit,"
+ + " and if they affirm it, it quits the game"));
}
/**
@@ -131,6 +159,18 @@ public class GeneralCommandMode implements ICommandMode {
}
}
+ /**
+ * Add a help topic to this command mode that isn't tied to a command
+ *
+ * @param topicName
+ * The name of the topic
+ * @param help
+ * The contents of the topic
+ */
+ public void addHelpTopic(String topicName, ICommandHelp help) {
+ helpTopics.put(topicName, help);
+ }
+
private void aliasCommands(String[] args) {
if (args.length != 2) {
errorOutput.accept("ERROR: Alias requires two arguments. "
@@ -175,6 +215,57 @@ public class GeneralCommandMode implements ICommandMode {
return ICommandMode.super.getName();
}
+ private void helpCommand(String commandName) {
+ if (commandHandlers.containsKey(commandName)) {
+ normalOutput.accept("\n" + commandHandlers.get(commandName)
+ .getHelp().getDescription());
+ } else if (defaultHandlers.containsKey(commandName)) {
+ normalOutput.accept("\n" + defaultHandlers.get(commandName)
+ .getHelp().getDescription());
+ } else if (helpTopics.containsKey(commandName)) {
+ normalOutput.accept(
+ "\n" + helpTopics.get(commandName).getDescription());
+ } else {
+ errorOutput
+ .accept("ERROR: I'm sorry, but there is no help available for '"
+ + commandName + "'");
+ }
+ }
+
+ private void helpSummary() {
+ normalOutput.accept(
+ "Help topics for this command mode are as follows:\n");
+ if (commandHandlers.getSize() > 0) {
+ commandHandlers.forEachValue((command) -> {
+ normalOutput.accept(
+ "\t" + command.getHelp().getSummary() + "\n");
+ });
+ } else {
+ normalOutput.accept("\tNone available\n");
+ }
+
+ normalOutput.accept(
+ "\nHelp topics available in all command modes are as follows\n");
+ if (defaultHandlers.getSize() > 0) {
+ defaultHandlers.forEachValue((command) -> {
+ normalOutput.accept(
+ "\t" + command.getHelp().getSummary() + "\n");
+ });
+ } else {
+ normalOutput.accept("\tNone available\n");
+ }
+
+ normalOutput.accept(
+ "\nHelp topics not associated with a command are as follows\n");
+ if (helpTopics.getSize() > 0) {
+ helpTopics.forEachValue((topic) -> {
+ normalOutput.accept("\t" + topic.getSummary() + "\n");
+ });
+ } else {
+ normalOutput.accept("\tNone available\n");
+ }
+ }
+
private void listCommands() {
normalOutput.accept(
"The available commands for this mode are as follows:\n");
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java
new file mode 100644
index 0000000..f508de8
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java
@@ -0,0 +1,36 @@
+package bjc.utils.cli;
+
+/**
+ * Generic implementation of a help topic
+ *
+ * @author ben
+ *
+ */
+public class GenericHelp implements ICommandHelp {
+ private String summary;
+ private String description;
+
+ /**
+ * Create a new help topic
+ *
+ * @param summary
+ * The summary of this help topic
+ * @param description
+ * The description of this help topic
+ */
+ public GenericHelp(String summary, String description) {
+ this.summary = summary;
+ this.description = description;
+ }
+
+ @Override
+ public String getSummary() {
+ return summary;
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
+ }
+
+} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java
index fc4c4de..b505ebd 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java
@@ -3,6 +3,7 @@ package bjc.utils.funcdata;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
+import java.util.function.Consumer;
import java.util.function.Function;
import bjc.utils.data.Pair;
@@ -89,6 +90,18 @@ public class FunctionalMap<K, V> implements IFunctionalMap<K, V> {
public int getSize() {
return mapToTransform.getSize();
}
+
+ @Override
+ public void forEachKey(Consumer<K> action) {
+ mapToTransform.forEachKey(action);
+ }
+
+ @Override
+ public void forEachValue(Consumer<V2> action) {
+ mapToTransform.forEachValue((val) -> {
+ action.accept(transformer.apply(val));
+ });
+ }
}
private Map<K, V> wrappedMap;
@@ -220,4 +233,14 @@ public class FunctionalMap<K, V> implements IFunctionalMap<K, V> {
public int getSize() {
return wrappedMap.size();
}
+
+ @Override
+ public void forEachKey(Consumer<K> action) {
+ wrappedMap.keySet().forEach(action);
+ }
+
+ @Override
+ public void forEachValue(Consumer<V> action) {
+ wrappedMap.values().forEach(action);
+ }
} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java
index 9bd62bc..e089850 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java
@@ -1,6 +1,7 @@
package bjc.utils.funcdata;
import java.util.function.BiConsumer;
+import java.util.function.Consumer;
import java.util.function.Function;
/**
@@ -101,4 +102,20 @@ public interface IFunctionalMap<K, V> {
* @return The number of entries in this map
*/
int getSize();
+
+ /**
+ * Perform an action for each key in the map
+ *
+ * @param action
+ * The action to perform on each key in the map
+ */
+ void forEachKey(Consumer<K> action);
+
+ /**
+ * Perform an action for each value in the map
+ *
+ * @param action
+ * The action to perform on each value in the map
+ */
+ void forEachValue(Consumer<V> action);
} \ No newline at end of file