Skip to content

Commit

Permalink
Merge branch 'branch-C-FriendlierSyntax'
Browse files Browse the repository at this point in the history
  • Loading branch information
yadobler committed Sep 14, 2024
2 parents 1a12e7f + 79ba04d commit f2c4b47
Show file tree
Hide file tree
Showing 20 changed files with 1,400 additions and 433 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ checkstyle {
toolVersion = "10.18.0"
}



dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.0'
Expand Down Expand Up @@ -65,4 +63,5 @@ shadowJar {

run {
standardInput = System.in
enableAssertions = true
}
76 changes: 3 additions & 73 deletions src/main/java/yappingbot/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;

import yappingbot.commands.Parser;
import yappingbot.commands.commands.LauncherCommand;
import yappingbot.storage.Storage;
import yappingbot.ui.UiCli;
import yappingbot.ui.gui.MainGuiApplication;
Expand All @@ -11,9 +13,6 @@
*/
public class Launcher {

// default savefile path is './savefile'
private static String savefilePath = "./savefile";

/**
* MainGuiApplication entry point. Parses arguments and launches YappingBot appropriately.
* Currently supported flags:
Expand All @@ -24,75 +23,6 @@ public class Launcher {
*/
public static void main(String[] args) {
// NOTE: args DOES NOT INCLUDE FILENAME AT ARGS[0]
boolean isUsingGui = true;
boolean stopTakingInputs = false;
String[] jfxArgs = new String[0];

// loops through arguments to parse them.
// any arguments preceeding a '--' are not processed and directly passed on to javaFX
for (int i = 0; i < args.length; i++) {
if (stopTakingInputs) {
// -- has been detected. Stop and just copy everything to be passed to javaFX
jfxArgs = Arrays.copyOfRange(args, i + 1, args.length);
break;
} else {
switch (args[i]) {
case "-c":
isUsingGui = false;
continue;
case "-s":
case "--savefile":
// peek the next arguemnt to get the savefile name
int savefilePathIndex = i + 1;
if (savefilePathIndex >= args.length
|| args[savefilePathIndex].startsWith("-")) {
System.out.printf("Error: %s missing argument: savefile path!\n", args[i]);
} else {
savefilePath = args[savefilePathIndex];
// only advance the pointer if there was a valid value for this flag
i = i + 1;
}
continue;
case "--":
stopTakingInputs = true;
continue;
default:
System.out.printf("Error: unknown flag %s!\n", args[i]);
}
}
}

if (isUsingGui) {
launchGui(jfxArgs);
} else {
launchCli();
}
}

/**
* Static method to launchGui MainGuiApplication without GUI.
* Use as fallback method.
*
* @param args String ArrayList of arguments passed in via CLI when launching this app.
*/
public static void launchGui(String[] args) {
MainGuiApplication.launch(MainGuiApplication.class, args);
}

/**
* Static method to launchGui MainGuiApplication with JavaFX GUI.
*/
public static void launchCli() {
YappingBot yp = new YappingBot(new UiCli(), new Storage(savefilePath));
yp.start();
}

/**
* Get savefile path captured by launcher.
*
* @return String of savefile path
*/
public static String getSavefilePath() {
return savefilePath;
new LauncherCommand(args).runCommand();
}
}
80 changes: 56 additions & 24 deletions src/main/java/yappingbot/YappingBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

import java.util.ArrayList;

import yappingbot.commands.CommandDispatcher;
import yappingbot.commands.Parser;
import yappingbot.commands.commands.ChangeTaskListStatusCommand;
import yappingbot.commands.commands.CreateDeadlineCommand;
import yappingbot.commands.commands.CreateEventCommand;
import yappingbot.commands.commands.CreateTodoCommand;
import yappingbot.commands.commands.DeleteTaskCommand;
import yappingbot.commands.commands.FindStringInTasksCommand;
import yappingbot.commands.commands.PrintUserTaskListCommand;
import yappingbot.commands.commands.ResetViewCommand;
import yappingbot.exceptions.YappingBotException;
import yappingbot.exceptions.YappingBotExceptionList;
import yappingbot.exceptions.YappingBotSaveFileNotFoundException;
import yappingbot.exceptions.YappingBotUnknownCommandException;
import yappingbot.storage.Storage;
import yappingbot.stringconstants.ReplyTextMessages;
import yappingbot.tasks.tasklist.TaskList;
import yappingbot.tasks.tasklist.TaskTypes;
import yappingbot.ui.Ui;


Expand All @@ -20,7 +26,6 @@
*/
public class YappingBot {

private final CommandDispatcher commandDispatch;
private final Parser parser;
private final Storage storage;
private final Ui ui;
Expand All @@ -37,7 +42,6 @@ public YappingBot(Ui ui, Storage storage) {
this.ui = ui;
this.storage = storage;
this.parser = new Parser();
this.commandDispatch = new CommandDispatcher(ui);
}

/**
Expand All @@ -61,9 +65,12 @@ private void init() {
* Saves the task list to disk using the already-created Storage object.
*/
private void saveAndCleanup() {
// REVERT LIST TO MAIN PARENT!
userList = commandDispatch.resetView(userList, true);
try {
// REVERT LIST TO MAIN PARENT!
userList = new ResetViewCommand()
.setEnvironment(ui, userList, true)
.runCommand()
.getNewUserList();
storage.saveListToFile(userList.toRawFormat());
} catch (YappingBotException e) {
ui.printError(String.format(ReplyTextMessages.SAVE_FILE_ERROR_1s, e.getMessage()));
Expand All @@ -76,58 +83,83 @@ private void saveAndCleanup() {
*/
private void mainLoop() {
while (ui.hasInputLines()) {

String userInput = ui.getNextInputLine().trim();
String[] userInputSlices = userInput.split(" ");
int taskListIndexPtr; // task list pointer

try {
switch (parser.parseCommand(userInputSlices[0])) {
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 = commandDispatch.resetView(userList, false);
userList = new ResetViewCommand()
.setEnvironment(ui, userList, false)
.runCommand()
.getNewUserList();
break;
case LIST:
// lists out all tasks that fits current filter (if any)
commandDispatch.printUserList(userList);
new PrintUserTaskListCommand()
.setEnvironment(ui, userList).runCommand();
break;
case MARK:
// marks a task as Done, given the index
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
taskListIndexPtr = Parser.parseTaskNumberSelected(userInputSlices[1]);
commandDispatch.changeTaskListStatus(taskListIndexPtr, true, userList);
userList = new ChangeTaskListStatusCommand(userInputSlices)
.setEnvironment(ui, userList, true)
.runCommand()
.getNewUserList();
break;
case UNMARK:
// unmarks a task as Done, given the index
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
taskListIndexPtr = Parser.parseTaskNumberSelected(userInputSlices[1]);
commandDispatch.changeTaskListStatus(taskListIndexPtr, false, userList);
userList = new ChangeTaskListStatusCommand(userInputSlices)
.setEnvironment(ui, userList, false)
.runCommand()
.getNewUserList();
break;
case DELETE:
// deletes a task from list, given the index
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
taskListIndexPtr = Parser.parseTaskNumberSelected(userInputSlices[1]);
commandDispatch.deleteTask(taskListIndexPtr, userList);
userList = new DeleteTaskCommand(userInputSlices)
.setEnvironment(ui, userList)
.runCommand()
.getNewUserList();
break;
case TODO:
// creates a new to-do task
commandDispatch.createNewTask(userInputSlices, TaskTypes.TODO, userList);
userList = new CreateTodoCommand(userInputSlices)
.setEnvironment(ui, userList)
.runCommand()
.getNewUserList();
break;
case EVENT:
// creates a new event task
commandDispatch.createNewTask(userInputSlices, TaskTypes.EVENT, userList);
userList = new CreateEventCommand(userInputSlices)
.setEnvironment(ui, userList)
.runCommand()
.getNewUserList();
break;
case DEADLINE:
// creates a new deadline task
commandDispatch.createNewTask(userInputSlices, TaskTypes.DEADLINE, userList);
userList = new CreateDeadlineCommand(userInputSlices)
.setEnvironment(ui, userList)
.runCommand()
.getNewUserList();
break;
case FIND:
// creates a filtered list view with tasks matching the given criteria
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
String searchString = userInput.substring(userInput.indexOf(" ") + 1);
userList = commandDispatch.findStringInTasks(searchString, userList);
userList = new FindStringInTasksCommand(userInputSlices)
.setEnvironmen(ui, userList)
.runCommand()
.getNewUserList();
break;
case UNKNOWN:
default:
Expand Down
Loading

0 comments on commit f2c4b47

Please sign in to comment.