Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-T12-4#122 from flyingsalsa/branch…
Browse files Browse the repository at this point in the history
…-stateful-command-history

Add Command history navigation
  • Loading branch information
Xczheng0105 authored Oct 19, 2024
2 parents 89dbe99 + 5b1034e commit 24c1af2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Region;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
Expand All @@ -18,9 +19,13 @@ public class CommandBox extends UiPart<Region> {

private final CommandExecutor commandExecutor;

private CommandHistory commandHistory = new CommandHistory();

@FXML
private TextField commandTextField;



/**
* Creates a {@code CommandBox} with the given {@code CommandExecutor}.
*/
Expand All @@ -31,6 +36,28 @@ public CommandBox(CommandExecutor commandExecutor) {
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
}

/**
* initialize() method from javaFx, used to check for arrow inputs inside the commandTextField
*/
@FXML
public void initialize() {
commandTextField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.UP) {
String previousCommand = commandHistory.getPreviousCommand();
if (previousCommand != null) {
commandTextField.setText(previousCommand);
commandTextField.positionCaret(previousCommand.length()); // Move cursor to end
}
} else if (event.getCode() == KeyCode.DOWN) {
String nextCommand = commandHistory.getNextCommand();
if (nextCommand != null) {
commandTextField.setText(nextCommand);
commandTextField.positionCaret(nextCommand.length()); // Move cursor to end
}
}
});
}

/**
* Handles the Enter button pressed event.
*/
Expand All @@ -46,7 +73,9 @@ private void handleCommandEntered() {
commandTextField.setText("");
} catch (CommandException | ParseException e) {
setStyleToIndicateCommandFailure();
return;
}
commandHistory.addCommand(commandText);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/seedu/address/ui/CommandHistory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package seedu.address.ui;
import java.util.ArrayList;

/**
* Container for previous commands
*/
public class CommandHistory {
private ArrayList<String> commandHistory = new ArrayList<>();
private int currentHistoryIndex = -1;

/**
* Adds a command to the ArrayList
*/
public void addCommand(String command) {
commandHistory.add(command);
currentHistoryIndex = commandHistory.size(); // Reset index to point after the latest command
}

/**
* Retrieves the previous command from the ArrayList, outputs null if already at the front of the arraylist
*/
public String getPreviousCommand() {
if (currentHistoryIndex > 0) {
currentHistoryIndex--;
return commandHistory.get(currentHistoryIndex);
}
return null; // No previous command
}

/**
* Retrieves the next command from the ArrayList, outputs null if already at the end of the arraylist
*/
public String getNextCommand() {
if (currentHistoryIndex < commandHistory.size() - 1) {
currentHistoryIndex++;
return commandHistory.get(currentHistoryIndex);
}
return null; // No next command
}
}
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void fillInnerParts() {

CommandBox commandBox = new CommandBox(this::executeCommand);
commandBoxPlaceholder.getChildren().add(commandBox.getRoot());


}

/**
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/seedu/address/ui/CommandHistoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package seedu.address.ui;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;



public class CommandHistoryTest {

private CommandHistory commandHistory;

@BeforeEach
public void setUp() {
commandHistory = new CommandHistory();
}

@Test
public void testAddCommand() {
commandHistory.addCommand("first command");
commandHistory.addCommand("second command");

// Moving back in history
assertEquals("second command", commandHistory.getPreviousCommand());
assertEquals("first command", commandHistory.getPreviousCommand());

// Should return null as there's no more previous command
assertNull(commandHistory.getPreviousCommand());
}

@Test
public void testNextCommand() {
commandHistory.addCommand("first command");
commandHistory.addCommand("second command");

// Go back twice to previous commands
commandHistory.getPreviousCommand();
commandHistory.getPreviousCommand();

// Moving forward in history
assertEquals("second command", commandHistory.getNextCommand());
assertNull(commandHistory.getNextCommand()); // No further next command after latest
}

@Test
public void testEmptyHistory() {
// Initially, history should return null when getting previous/next command
assertNull(commandHistory.getPreviousCommand());
assertNull(commandHistory.getNextCommand());
}

@Test
public void testMixedNavigation() {
commandHistory.addCommand("first command");
commandHistory.addCommand("second command");
commandHistory.addCommand("third command");

// Navigate through commands
assertEquals("third command", commandHistory.getPreviousCommand());
assertEquals("second command", commandHistory.getPreviousCommand());

// Moving forward should bring back "third command"
assertEquals("third command", commandHistory.getNextCommand());

// After the latest command, there should be no more next command
assertNull(commandHistory.getNextCommand());
}
}

0 comments on commit 24c1af2

Please sign in to comment.