Skip to content

Commit

Permalink
Fix out lineseparators in ouput are dealt with for hard-wrapping. Esc…
Browse files Browse the repository at this point in the history
…ape characters should behave as expected.
  • Loading branch information
EvanQuan committed Aug 3, 2018
1 parent 1ea068d commit 0fc68ed
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/game/menu/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void appendTitleScreen() {
" \\_/_| |_|_|\\___|_| |_| |_|_| |_| |_|\\__,_|" + System.lineSeparator(),
SemanticColor.ITEM);
out.appendln();
out.appendln(" A Text Adventure Game");
out.appendln(" A Text Adventure Game");
out.appendln();
// Cheese Quest
// printLocation(" .---.\n"
Expand Down
40 changes: 23 additions & 17 deletions src/game/system/output/ConsolePrintBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,34 +205,40 @@ public void append(String output, ConsoleColor consoleColor) {
// VERSION 2 -- cleaner, but wrapping is slightly different, potentially doesn't work
// for multiple line input text
String[] words = output.split(" ", -1);
// out.print("[ Before" + cursorColumn + "]");
// DEBUG START
// out.println("[ Before" + cursorColumn + "]");
// ArrayList<String> a = new ArrayList(Arrays.asList(words));
// out.print("[Length: " + output.length() + "]");
// out.print(a);
// out.println("[Length: " + output.length() + "]");
// out.println(a);
// int visualLength = 0;
// for (String e : a) {
// visualLength += e.length();
// out.print("Visual length: ");
// for (int i = 0; i < a.size(); i++) {
// visualLength += a.get(i).length();
// out.print("(" + i + ") length:" + a.get(i).length() + "[" + a.get(i) + "], ");
// }
// out.print("[Visual length: " + visualLength + "]");
// out.print("[Tokens: " + words.length + "]");
// out.println("[Visual length: " + visualLength + "]");
// out.println("[Tokens: " + words.length + "]");
// DEBUG END
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (word.equals("\n")) {
out.println();
cursorColumn = 0;
} else if (words.equals("\r")) {
} else if (cursorColumn + word.length() > wrapWidth) {
boolean wordEndsWithNewline = word.endsWith(System.lineSeparator());
if (wordEndsWithNewline) {
word = word.substring(0, word.length() - System.lineSeparator().length());
}

if (cursorColumn + word.length() > wrapWidth) {
printDirect(System.lineSeparator() + word, consoleColor);
cursorColumn = System.lineSeparator().length() + word.length();
if (word.startsWith("\\")) {
cursorColumn--;
}
}
else { // Add space before word, if word is not itself a space
}else { // Add space before word, if word is not itself a space
boolean addSpace = i == 0 || output.isEmpty();
printDirect((addSpace ? "" : " ") + word, consoleColor);
cursorColumn += (addSpace ? 0 : 1) + word.length();
}

if (wordEndsWithNewline) {
out.println();
cursorColumn = 0;
}
}
if (output.endsWith(System.lineSeparator())) {
cursorColumn = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/test/TestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static void main(String[] args) {
String output = "\\" + System.lineSeparator();
c.appendln(output);

// Looks like line separator connects to the end of the previous element
// add to its visual size, without increasing the token size
c.appendln("\\");
c.appendln(System.lineSeparator());
c.appendln("1 2 3");
Expand Down

0 comments on commit 0fc68ed

Please sign in to comment.