Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Allow user to see the command history #439 #440

Merged
merged 5 commits into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ Selects the 2nd person in the address book.
`select 1` +
Selects the 1st person in the results of the `find` command.

=== Listing entered commands : `history`

Lists all the commands that you have entered in chronological order. +
Format: `history`

=== Clearing all entries : `clear`

Clears all entries from the address book. +
Expand Down Expand Up @@ -183,3 +188,4 @@ e.g. `find James Jake`
* *Help* : `help`
* *Select* : `select INDEX` +
e.g.`select 2`
* *History* : `history`
Binary file modified docs/diagrams/LogicComponentClassDiagram.pptx
Binary file not shown.
Binary file modified docs/images/LogicClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions src/main/java/seedu/address/logic/CommandHistory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.logic;

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.List;

/**
* Stores the history of commands executed.
*/
public class CommandHistory {
private ArrayList<String> userInputHistory;

public CommandHistory() {
userInputHistory = new ArrayList<>();
}

/**
* Appends {@code userInput} to the list of user input entered.
*/
public void add(String userInput) {
requireNonNull(userInput);
userInputHistory.add(userInput);
}

/**
* Returns a defensive copy of {@code userInputHistory}.
*/
public List<String> getHistory() {
return new ArrayList<>(userInputHistory);
}
}
12 changes: 9 additions & 3 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ public class LogicManager extends ComponentManager implements Logic {
private final Logger logger = LogsCenter.getLogger(LogicManager.class);

private final Model model;
private final CommandHistory history;
private final Parser parser;

public LogicManager(Model model, Storage storage) {
this.model = model;
this.history = new CommandHistory();
this.parser = new Parser();
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");
Command command = parser.parseCommand(commandText);
command.setData(model);
return command.execute();
try {
Command command = parser.parseCommand(commandText);
command.setData(model, history);
return command.execute();
} finally {
history.add(commandText);
}
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import seedu.address.commons.core.Messages;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -9,6 +10,7 @@
*/
public abstract class Command {
protected Model model;
protected CommandHistory history;

/**
* Constructs a feedback message to summarise an operation that displayed a listing of persons.
Expand All @@ -33,7 +35,7 @@ public static String getMessageForPersonListShownSummary(int displaySize) {
* Commands making use of any of these should override this method to gain
* access to the dependencies.
*/
public void setData(Model model) {
public void setData(Model model, CommandHistory history) {
this.model = model;
}
}
35 changes: 35 additions & 0 deletions src/main/java/seedu/address/logic/commands/HistoryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;

/**
* Lists all the commands entered by user from the start of app launch.
*/
public class HistoryCommand extends Command {

public static final String COMMAND_WORD = "history";
public static final String MESSAGE_SUCCESS = "Entered commands (from earliest to most recent):\n%1$s";
public static final String MESSAGE_NO_HISTORY = "You have not yet entered any commands.";

@Override
public CommandResult execute() {
List<String> previousCommands = history.getHistory();

if (previousCommands.isEmpty()) {
return new CommandResult(MESSAGE_NO_HISTORY);
}

return new CommandResult(String.format(MESSAGE_SUCCESS, String.join("\n", previousCommands)));
}

@Override
public void setData(Model model, CommandHistory history) {
requireNonNull(history);
this.history = history;
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.HistoryCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -66,6 +67,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case HistoryCommand.COMMAND_WORD:
return new HistoryCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

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

import static org.junit.Assert.assertEquals;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;

public class CommandHistoryTest {
private CommandHistory history;

@Before
public void setUp() {
history = new CommandHistory();
}

@Test
public void add() {
final String validCommand = "clear";
final String invalidCommand = "adds Bob";

history.add(validCommand);
history.add(invalidCommand);
assertEquals(Arrays.asList(validCommand, invalidCommand), history.getHistory());
}
}
27 changes: 27 additions & 0 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;
Expand Down Expand Up @@ -41,6 +42,7 @@
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.HistoryCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.commands.exceptions.CommandException;
Expand Down Expand Up @@ -432,6 +434,31 @@ public void execute_find_matchesIfAnyKeywordPresent() throws Exception {
expectedModel);
}

@Test
public void execute_verifyHistory_success() throws Exception {
String validCommand = "clear";
logic.execute(validCommand);

String invalidCommandParse = " adds Bob ";
try {
logic.execute(invalidCommandParse);
fail("The expected ParseException was not thrown.");
} catch (ParseException pe) {
assertEquals(MESSAGE_UNKNOWN_COMMAND, pe.getMessage());
}

String invalidCommandExecute = "delete 1"; // address book is of size 0; index out of bounds
try {
logic.execute(invalidCommandExecute);
fail("The expected CommandException was not thrown.");
} catch (CommandException ce) {
assertEquals(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, ce.getMessage());
}

String expectedMessage = String.format(HistoryCommand.MESSAGE_SUCCESS,
String.join("\n", validCommand, invalidCommandParse, invalidCommandExecute));
assertCommandSuccess("history", expectedMessage, model);
}

/**
* A utility class to generate test data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.commons.core.UnmodifiableObservableList;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void execute_duplicatePerson_throwsCommandException() throws Exception {
*/
private AddCommand getAddCommandForPerson(Person person, Model model) throws IllegalValueException {
AddCommand command = new AddCommand(person);
command.setData(model);
command.setData(model, new CommandHistory());
return command;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void equals() {
*/
private EditCommand prepareCommand(Index index, EditPersonDescriptor descriptor) {
EditCommand editCommand = new EditCommand(index, descriptor);
editCommand.setData(model);
editCommand.setData(model, new CommandHistory());
return editCommand;
}

Expand Down
49 changes: 49 additions & 0 deletions src/test/java/seedu/address/logic/commands/HistoryCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package seedu.address.logic.commands;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;

public class HistoryCommandTest {
private HistoryCommand historyCommand;
private CommandHistory history;

@Before
public void setUp() {
Model model = new ModelManager();
history = new CommandHistory();
historyCommand = new HistoryCommand();
historyCommand.setData(model, history);
}

@Test
public void execute() {
assertCommandResult(historyCommand, HistoryCommand.MESSAGE_NO_HISTORY);

String command1 = "clear";
history.add(command1);
assertCommandResult(historyCommand, String.format(HistoryCommand.MESSAGE_SUCCESS, command1));

String command2 = "randomCommand";
String command3 = "select 1";
history.add(command2);
history.add(command3);

String expectedMessage = String.format(HistoryCommand.MESSAGE_SUCCESS,
String.join("\n", command1, command2, command3));

assertCommandResult(historyCommand, expectedMessage);
}

/**
* Asserts that the result message from the execution of {@code historyCommand} equals to {@code expectedMessage}
*/
private void assertCommandResult(HistoryCommand historyCommand, String expectedMessage) {
assertEquals(expectedMessage, historyCommand.execute().feedbackToUser);
}
}
28 changes: 28 additions & 0 deletions src/test/java/seedu/address/logic/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.logic.parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;

import org.junit.Test;

import seedu.address.logic.commands.HistoryCommand;
import seedu.address.logic.parser.exceptions.ParseException;

public class ParserTest {
private final Parser parser = new Parser();

@Test
public void parseCommand_history() throws Exception {
assertTrue(parser.parseCommand(HistoryCommand.COMMAND_WORD) instanceof HistoryCommand);
assertTrue(parser.parseCommand(HistoryCommand.COMMAND_WORD + " 3") instanceof HistoryCommand);

try {
parser.parseCommand("histories");
fail("The expected ParseException was not thrown.");
} catch (ParseException pe) {
assertEquals(MESSAGE_UNKNOWN_COMMAND, pe.getMessage());
}
}
}