Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W8.1g][W13-2]Peck Kai Ting #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions src/seedu/addressbook/ui/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package seedu.addressbook.ui;

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

import seedu.addressbook.data.person.ReadOnlyPerson;

public class Formatter {
/**
* A decorative prefix added to the beginning of lines printed by AddressBook
*/
static final String LINE_PREFIX = "|| ";
/**
* A platform independent line separator.
*/
static final String LS = System.lineSeparator();

/** Format of indexed list item */
private static final String MESSAGE_INDEXED_LIST_ITEM = "\t%1$d. %2$s";

public Formatter() {
}

String formatMessage(String m) {
return addLinePrefix(replaceNewline(m));
}

String replaceNewline(String m) {
return m.replace("\n", LS + LINE_PREFIX);
}

String addLinePrefix(String message) {
return LINE_PREFIX + message;
}

List<String> getFormattedPersons(List<? extends ReadOnlyPerson> persons) {
final List<String> formattedPersons = new ArrayList<String>();
for (ReadOnlyPerson person : persons) {
formattedPersons.add(person.getAsTextHidePrivate());
}
return formattedPersons;
}

static StringBuilder getFormattedIndexedList(List<String> listItems) {
final StringBuilder formatted = new StringBuilder();
int displayIndex = 0 + TextUi.DISPLAYED_INDEX_OFFSET;
for (String listItem : listItems) {
formatted.append(getIndexedListItem(displayIndex, listItem)).append("\n");
displayIndex++;
}
return formatted;
}

/**
* Formats a string as a viewable indexed list item.
*
* @param visibleIndex visible index for this listing
*/
private static String getIndexedListItem(int visibleIndex, String listItem) {
return String.format(MESSAGE_INDEXED_LIST_ITEM, visibleIndex, listItem);
}

String formatCommand(String fullInputLine) {
return "[Command entered:" + fullInputLine + "]";
}
}
40 changes: 6 additions & 34 deletions src/seedu/addressbook/ui/TextUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
Expand All @@ -21,18 +20,8 @@
*/
public class TextUi {

/** A decorative prefix added to the beginning of lines printed by AddressBook */
private static final String LINE_PREFIX = "|| ";

/** A platform independent line separator. */
private static final String LS = System.lineSeparator();

private static final String DIVIDER = "===================================================";

/** Format of indexed list item */
private static final String MESSAGE_INDEXED_LIST_ITEM = "\t%1$d. %2$s";


/** Offset required to convert between 1-indexing and 0-indexing. */
public static final int DISPLAYED_INDEX_OFFSET = 1;

Expand All @@ -41,6 +30,7 @@ public class TextUi {

private final Scanner in;
private final PrintStream out;
private final Formatter formatter = new Formatter();

public TextUi() {
this(System.in, System.out);
Expand Down Expand Up @@ -79,15 +69,15 @@ private boolean isCommentLine(String rawInputLine) {
* @return command (full line) entered by the user
*/
public String getUserCommand() {
out.print(LINE_PREFIX + "Enter command: ");
out.print(formatter.addLinePrefix("Enter command: "));
String fullInputLine = in.nextLine();

// silently consume all ignored lines
while (shouldIgnore(fullInputLine)) {
fullInputLine = in.nextLine();
}

showToUser("[Command entered:" + fullInputLine + "]");
showToUser(formatter.formatCommand(fullInputLine));
return fullInputLine;
}

Expand Down Expand Up @@ -116,7 +106,7 @@ public void showInitFailedMessage() {
/** Shows message(s) to the user */
public void showToUser(String... message) {
for (String m : message) {
out.println(LINE_PREFIX + m.replace("\n", LS + LINE_PREFIX));
out.println(formatter.formatMessage(m));
}
}

Expand All @@ -137,10 +127,7 @@ public void showResultToUser(CommandResult result) {
* Private contact details are hidden.
*/
private void showPersonListView(List<? extends ReadOnlyPerson> persons) {
final List<String> formattedPersons = new ArrayList<>();
for (ReadOnlyPerson person : persons) {
formattedPersons.add(person.getAsTextHidePrivate());
}
final List<String> formattedPersons = formatter.getFormattedPersons(persons);
showToUserAsIndexedList(formattedPersons);
}

Expand All @@ -151,22 +138,7 @@ private void showToUserAsIndexedList(List<String> list) {

/** Formats a list of strings as a viewable indexed list. */
private static String getIndexedListForViewing(List<String> listItems) {
final StringBuilder formatted = new StringBuilder();
int displayIndex = 0 + DISPLAYED_INDEX_OFFSET;
for (String listItem : listItems) {
formatted.append(getIndexedListItem(displayIndex, listItem)).append("\n");
displayIndex++;
}
final StringBuilder formatted = Formatter.getFormattedIndexedList(listItems);
return formatted.toString();
}

/**
* Formats a string as a viewable indexed list item.
*
* @param visibleIndex visible index for this listing
*/
private static String getIndexedListItem(int visibleIndex, String listItem) {
return String.format(MESSAGE_INDEXED_LIST_ITEM, visibleIndex, listItem);
}

}