Added mechanism to check whether a raw text contains a command

This commit is contained in:
delvh 2020-07-23 11:07:54 +02:00
parent 7bf35977f0
commit 38c57c997f

View File

@ -26,6 +26,8 @@ public class SystemCommandsMap {
private final Pattern commandBounds = Pattern.compile("^[a-zA-Z0-9_:!\\(\\)\\?\\.\\,\\;\\-]+$"); 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 * 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. * 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)); 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.
* <p>
* 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.
* <p>
* 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 * This method checks if the input String is a key in the map and executes the
* wrapped System command if present. * wrapped System command if present.
@ -165,11 +207,12 @@ public class SystemCommandsMap {
* <p> * <p>
* Usage example:<br> * Usage example:<br>
* {@code SystemCommandsMap systemCommands = new SystemCommandsMap();}<br> * {@code SystemCommandsMap systemCommands = new SystemCommandsMap();}<br>
* {@code systemCommands.add("example", (words)-> &lt;Button&gt;.setText(words[0]), 1);}<br> * {@code Button button = new Button();}<br>
* {@code systemCommands.add("example", (words)-> button.setText(words[0]), 1);}<br>
* {@code ....}<br> * {@code ....}<br>
* user input: {@code "/example xyz ..."}<br> * user input: {@code "/example xyz ..."}<br>
* {@code systemCommands.executeIfPresent("example xyz ...")} * {@code systemCommands.executeIfPresent("example xyz ...")}
* result: {@code <Button>.getText()=="xyz"} * result: {@code button.getText()=="xyz"}
* *
* @param input the input string given by the user, <b>excluding the "/"</b> * @param input the input string given by the user, <b>excluding the "/"</b>
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
@ -186,6 +229,7 @@ public class SystemCommandsMap {
// Executing the function // Executing the function
try { try {
systemCommand.getAction().apply(arguments); systemCommand.getAction().apply(arguments);
commandExecuted = true;
} catch (final Exception e) { } catch (final Exception e) {
EnvoyLog.getLogger(SystemCommandsMap.class).log(Level.WARNING, "The system command " + EnvoyLog.getLogger(SystemCommandsMap.class).log(Level.WARNING, "The system command " +
// detected command // detected command
@ -195,18 +239,34 @@ public class SystemCommandsMap {
} }
/** /**
* Retrieves the recommendations based on the current command entered (i.e the * Retrieves the recommendations based on the current input entered.<br>
* String "/exam" should be entered as "exam"). If none are present, nothing * The first word is used for the recommendations and
* will be done. * it does not matter if the "/" is at its beginning or not.<br>
* Otherwise the given function will be executed on the recommendations. * If none are present, nothing will be done.<br>
* Otherwise the given function will be executed on the recommendations.<br>
* *
* @param input the String between "/" and " " * @param input the input
* @param action the action that should be taken for the recommendations, if any * @param action the action that should be taken for the recommendations, if any
* are present * are present
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void requestRecommendations(String input, Function<Set<String>, Void> action) { public void requestRecommendations(String input, Function<Set<String>, Void> action) { requestRecommendations(input, 0, action); }
final var partialCommand = input.substring(0, input.indexOf(" "));
/**
* Retrieves the recommendations based on the current input entered.<br>
* The word beginning at {@code fromIndex} is used for the recommendations and
* it does not matter if the "/" is at its beginning or not.<br>
* If none are present, nothing will be done.<br>
* Otherwise the given function will be executed on the recommendations.<br>
*
* @param input the input
* @param fromIndex the index to start checking on
* @param action the action that should be taken for the recommendations, if
* any are present
* @since Envoy Client v0.1-beta
*/
private void requestRecommendations(String input, int fromIndex, Function<Set<String>, Void> action) {
final var partialCommand = input.substring(fromIndex + (input.charAt(fromIndex) == '/' ? 1 : 0), input.indexOf(" "));
// Get the expected commands // Get the expected commands
final var recommendations = recommendCommands(partialCommand); final var recommendations = recommendCommands(partialCommand);
if (recommendations.isEmpty()) return; if (recommendations.isEmpty()) return;