Added mechanism to check whether a raw text contains a command
This commit is contained in:
parent
7bf35977f0
commit
38c57c997f
@ -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.
|
||||
* <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
|
||||
* wrapped System command if present.
|
||||
@ -165,11 +207,12 @@ public class SystemCommandsMap {
|
||||
* <p>
|
||||
* Usage example:<br>
|
||||
* {@code SystemCommandsMap systemCommands = new SystemCommandsMap();}<br>
|
||||
* {@code systemCommands.add("example", (words)-> <Button>.setText(words[0]), 1);}<br>
|
||||
* {@code Button button = new Button();}<br>
|
||||
* {@code systemCommands.add("example", (words)-> button.setText(words[0]), 1);}<br>
|
||||
* {@code ....}<br>
|
||||
* user input: {@code "/example xyz ..."}<br>
|
||||
* {@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>
|
||||
* @since Envoy Client v0.1-beta
|
||||
@ -186,6 +229,7 @@ public class SystemCommandsMap {
|
||||
// Executing the function
|
||||
try {
|
||||
systemCommand.getAction().apply(arguments);
|
||||
commandExecuted = true;
|
||||
} catch (final Exception e) {
|
||||
EnvoyLog.getLogger(SystemCommandsMap.class).log(Level.WARNING, "The system command " +
|
||||
// detected command
|
||||
@ -195,18 +239,34 @@ public class SystemCommandsMap {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the recommendations based on the current command entered (i.e the
|
||||
* String "/exam" should be entered as "exam"). If none are present, nothing
|
||||
* will be done.
|
||||
* Otherwise the given function will be executed on the recommendations.
|
||||
* Retrieves the recommendations based on the current input entered.<br>
|
||||
* The first word 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 String between "/" and " "
|
||||
* @param input the input
|
||||
* @param action the action that should be taken for the recommendations, if any
|
||||
* are present
|
||||
* @since Envoy Client v0.1-beta
|
||||
*/
|
||||
public void requestRecommendations(String input, Function<Set<String>, Void> action) {
|
||||
final var partialCommand = input.substring(0, input.indexOf(" "));
|
||||
public void requestRecommendations(String input, Function<Set<String>, Void> action) { requestRecommendations(input, 0, action); }
|
||||
|
||||
/**
|
||||
* 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
|
||||
final var recommendations = recommendCommands(partialCommand);
|
||||
if (recommendations.isEmpty()) return;
|
||||
|
Reference in New Issue
Block a user