Skip to content

Commit

Permalink
Add alias command
Browse files Browse the repository at this point in the history
  • Loading branch information
yadobler committed Sep 14, 2024
1 parent 19bbb6b commit 79ba04d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/main/java/yappingbot/YappingBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ private void mainLoop() {
case EXIT:
// exits the function, ending the main loop
return;
case ALIAS:
// creates new alias
// TODO: abstract this out to a Command object
parser.addAlias(userInputSlices[1], userInputSlices[2]);
ui.printf(ReplyTextMessages.ALIAS_ADDED_TEXT_2s,
userInputSlices[1],
userInputSlices[2]);
break;
case RESET_LIST:
// resets any filter on the list
userList = new ResetViewCommand()
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/yappingbot/commands/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashMap;

import yappingbot.exceptions.YappingBotIncorrectCommandException;
import yappingbot.exceptions.YappingBotInvalidTaskNumberException;
import yappingbot.exceptions.YappingBotOobException;
import yappingbot.exceptions.YappingBotUnknownCommandException;
Expand All @@ -18,7 +19,7 @@ public class Parser {
* @see <a href="https://github.com/nus-cs2103-AY2425S1/forum/issues/22#issuecomment-2309939016"> Uses for enum </a>
*/
public enum CommandTypes {
FIND, LIST, MARK, UNMARK, DELETE, TODO, EVENT, DEADLINE, EXIT, RESET_LIST, UNKNOWN
FIND, LIST, MARK, UNMARK, DELETE, TODO, EVENT, DEADLINE, EXIT, RESET_LIST, ALIAS, UNKNOWN
}

/**
Expand Down Expand Up @@ -63,6 +64,8 @@ public Parser() {
commandsHashMap.put("exit", CommandTypes.EXIT);
commandsHashMap.put(":q", CommandTypes.EXIT);

commandsHashMap.put("alias", CommandTypes.ALIAS);

}

/**
Expand All @@ -88,6 +91,25 @@ public CommandTypes parseCommand(String commandString)
}
}

/**
* Adds new aliases for commands.
*
* @param currentCommand String of the current command to execute
* @param newAlias New alias to launch same command
*/
public void addAlias(String currentCommand, String newAlias) {
// TODO: persistant settings
assert currentCommand != null;
assert newAlias != null;
if (!commandsHashMap.containsKey(currentCommand)) {
throw new YappingBotIncorrectCommandException(
ReplyTextMessages.ALIAS_USAGE,
currentCommand);
}

commandsHashMap.putIfAbsent(newAlias, commandsHashMap.get(currentCommand));
}

/**
* Parses any text that must be interpreted as an integer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ public final class ReplyTextMessages {
// todo: reset usage text
public static final String RESET_USAGE = "";
public static final String FIND_USAGE = "";
public static final String ALIAS_ADDED_TEXT_2s = "Alias added! %s can now be executed with %s.";
public static final String ALIAS_USAGE = "";
}

0 comments on commit 79ba04d

Please sign in to comment.