mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-29 16:01:19 -04:00
Improved formatting of ascii texts.
This commit is contained in:
parent
5f492205a9
commit
3a79819bfc
@ -5,7 +5,7 @@ package de.neemann.gui;
|
||||
*/
|
||||
public final class StringUtils {
|
||||
|
||||
private static final int DEF_COLS = 60;
|
||||
private static final int DEF_COLS = 70;
|
||||
|
||||
private StringUtils() {
|
||||
}
|
||||
@ -113,38 +113,49 @@ public final class StringUtils {
|
||||
if (text == null)
|
||||
return null;
|
||||
|
||||
StringBuilder sb = new StringBuilder(label);
|
||||
StringBuilder outText = new StringBuilder(label);
|
||||
for (int i = 0; i < indent - label.length(); i++)
|
||||
sb.append(" ");
|
||||
outText.append(" ");
|
||||
|
||||
StringBuilder word = new StringBuilder();
|
||||
boolean isFirst = true;
|
||||
int pos = indent;
|
||||
boolean wasBlank = false;
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
switch (c) {
|
||||
case '\n':
|
||||
case '\r':
|
||||
case ' ':
|
||||
if (!wasBlank) {
|
||||
if (pos > cols) {
|
||||
sb.append('\n');
|
||||
for (int j = 0; j < indent; j++)
|
||||
sb.append(" ");
|
||||
|
||||
pos = indent;
|
||||
} else {
|
||||
sb.append(' ');
|
||||
}
|
||||
}
|
||||
wasBlank = true;
|
||||
pos = addWord(indent, cols, outText, word, pos, isFirst);
|
||||
isFirst = false;
|
||||
break;
|
||||
default:
|
||||
sb.append(c);
|
||||
wasBlank = false;
|
||||
pos++;
|
||||
word.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
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++)
|
||||
outText.append(" ");
|
||||
|
||||
pos = indent;
|
||||
} else {
|
||||
if (!isFirst) {
|
||||
outText.append(" ");
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
outText.append(word);
|
||||
pos += word.length();
|
||||
word.setLength(0);
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,16 +12,17 @@ public class StringUtilsTest extends TestCase {
|
||||
|
||||
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\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" +
|
||||
"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 {
|
||||
assertEquals("a) This is a test string. This\n" +
|
||||
" is a test string. This is\n" +
|
||||
" a test string.", StringUtils.breakLinesLabel("a)", 3, "This is a test string. This is a test string. This is a test string.", 22));
|
||||
" is a test string. This is a\n" +
|
||||
" test string.", StringUtils.breakLinesLabel("a)", 3, "This is a test string. This is a test string. This is a test string.", 30));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user