Improved formatting of ascii texts.

This commit is contained in:
hneemann 2016-10-30 20:25:39 +01:00
parent 5f492205a9
commit 3a79819bfc
2 changed files with 36 additions and 24 deletions

View File

@ -5,7 +5,7 @@ package de.neemann.gui;
*/ */
public final class StringUtils { public final class StringUtils {
private static final int DEF_COLS = 60; private static final int DEF_COLS = 70;
private StringUtils() { private StringUtils() {
} }
@ -113,38 +113,49 @@ public final class StringUtils {
if (text == null) if (text == null)
return null; return null;
StringBuilder sb = new StringBuilder(label); StringBuilder outText = new StringBuilder(label);
for (int i = 0; i < indent - label.length(); i++) for (int i = 0; i < indent - label.length(); i++)
sb.append(" "); outText.append(" ");
StringBuilder word = new StringBuilder();
boolean isFirst = true;
int pos = indent; int pos = indent;
boolean wasBlank = false;
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i); char c = text.charAt(i);
switch (c) { switch (c) {
case '\n': case '\n':
case '\r': case '\r':
case ' ': case ' ':
if (!wasBlank) { pos = addWord(indent, cols, outText, word, pos, isFirst);
if (pos > cols) { isFirst = false;
sb.append('\n'); break;
default:
word.append(c);
}
}
addWord(indent, cols, outText, word, pos, isFirst);
return outText.toString();
}
private static int addWord(int indent, int cols, StringBuilder outText, StringBuilder word, int pos, boolean isFirst) {
if (word.length() > 0) {
if (pos + (isFirst ? word.length() : word.length() + 1) > cols) {
outText.append('\n');
for (int j = 0; j < indent; j++) for (int j = 0; j < indent; j++)
sb.append(" "); outText.append(" ");
pos = indent; pos = indent;
} else { } else {
sb.append(' '); if (!isFirst) {
} outText.append(" ");
}
wasBlank = true;
break;
default:
sb.append(c);
wasBlank = false;
pos++; pos++;
} }
} }
return sb.toString(); outText.append(word);
pos += word.length();
word.setLength(0);
}
return pos;
} }
} }

View File

@ -12,16 +12,17 @@ public class StringUtilsTest extends TestCase {
public void testBreakLines() throws Exception { public void testBreakLines() throws Exception {
assertEquals("this is a test string", StringUtils.breakLines("this \n\n is \n a test \n\r string", 60)); assertEquals("this is a test string", StringUtils.breakLines("this \n\n is \n a test \n\r string", 60));
assertEquals("this is a test\nstring", StringUtils.breakLines("this \n\n is \n a test \n\r string", 10)); assertEquals("this is a test\nstring", StringUtils.breakLines("this \n\n is \n a test \n\r string", 14));
assertEquals("This is a test string. This\n" + assertEquals("This is a test string. This\n" +
"is a test string. This is a\n" + "is a test string. This is a\n" +
"test string.", StringUtils.breakLines("This is a test string. This is a test string. This is a test string.", 20)); "test string.", StringUtils.breakLines("This is a test string. This is a test string. This is a test string.", 27));
assertEquals("this is\naWordThatIsFarToLongToFitInASingleLine\nThis is a test string", StringUtils.breakLines("this is aWordThatIsFarToLongToFitInASingleLine This is a test string", 21));
} }
public void testBreakLinesLabel() throws Exception { public void testBreakLinesLabel() throws Exception {
assertEquals("a) This is a test string. This\n" + assertEquals("a) This is a test string. This\n" +
" is a test string. This is\n" + " is a test string. This is a\n" +
" a test string.", StringUtils.breakLinesLabel("a)", 3, "This is a test string. This is a test string. This is a test string.", 22)); " test string.", StringUtils.breakLinesLabel("a)", 3, "This is a test string. This is a test string. This is a test string.", 30));
} }
} }