From 34036d275a40a5baf279e93765d764fa2e5d28a2 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 23 Jul 2020 11:07:54 +0200 Subject: [PATCH] Added mechanism to check whether a raw text contains a command --- .../data/commands/SystemCommandsMap.java | 78 ++++++++++++++++--- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java b/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java index 0add151..9e0cc82 100644 --- a/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java +++ b/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java @@ -26,6 +26,8 @@ public class SystemCommandsMap { private final Pattern commandBounds = Pattern.compile("^[a-zA-Z0-9_:!\\(\\)\\?\\.\\,\\;\\-]+$"); + private boolean commandExecuted = false; + /** * Adds a command with according action and the number of arguments that should * be parsed if the command does not violate API constrictions to the map. @@ -155,6 +157,46 @@ public class SystemCommandsMap { return Optional.ofNullable(systemCommands.get(firstWord)); } + /** + * Takes a 'raw' string (the whole input) and checks if a command is present + * after a "/". If that is the case, it will be executed. + *

+ * Only one system command can be present, afterwards checking will not be + * continued. + * + * @param raw the raw input string + * @since Envoy Client v0.1-beta + */ + public void checkForCommands(String raw) { checkForCommands(raw, 0); } + + /** + * Takes a 'raw' string (the whole input) and checks from {@code fromIndex} on + * if a command is present + * after a "/". If that is the case, it will be executed. + *

+ * Only one system command can be present, afterwards checking will not be + * continued. + * + * @param raw the raw input string + * @param fromIndex the index to start checking on + * @since Envoy Client v0.1-beta + */ + public void checkForCommands(String raw, int fromIndex) { + // The minimum length of a command is "/" + a letter, hence raw.length()-2 is + // the highest index needed + for (int i = fromIndex; i < raw.length() - 2; i++) + // possibly a command was detected + if (raw.charAt(i) == '/') { + // + executeIfPresent(raw.substring(i + 1)); + // the command was executed successfully - no further checking needed + if (commandExecuted) { + commandExecuted = false; + break; + } + } + } + /** * This method checks if the input String is a key in the map and executes the * wrapped System command if present. @@ -165,11 +207,12 @@ public class SystemCommandsMap { *

* Usage example:
* {@code SystemCommandsMap systemCommands = new SystemCommandsMap();}
- * {@code systemCommands.add("example", (words)-> <Button>.setText(words[0]), 1);}
+ * {@code Button button = new Button();}
+ * {@code systemCommands.add("example", (words)-> button.setText(words[0]), 1);}
* {@code ....}
* user input: {@code "/example xyz ..."}
* {@code systemCommands.executeIfPresent("example xyz ...")} - * result: {@code