List of usage examples for org.apache.commons.lang.text StrBuilder rightString
public String rightString(int length)
From source file:me.taylorkelly.mywarp.bukkit.util.FormattingUtils.java
/** * Joins the given parts, separated by blanks. The resulting string will be wrapped into multiple lines so that each * line is at most as wide as the given width. Each new line will start with the given prefix. */*from w ww . j a v a 2 s.c o m*/ * @param parts the parts * @param newLinePrefix the prefix for created new lines * @param wrappedWidth the width of each line * @return the wrapped string */ private static String wrappedJoin(Iterable<String> parts, String newLinePrefix, int wrappedWidth) { StrBuilder ret = new StrBuilder(); StrBuilder line = new StrBuilder(); for (String part : parts) { // if the word is longer than the maximum length, add chars as long // as possible than make a new line if (getWidth(part) > wrappedWidth) { for (char c : part.toCharArray()) { // if the maximum width is reached, make a new line if (getWidth(line.toString()) + getWidth(c) > wrappedWidth) { ret.appendln(line.toString()); line.clear(); line.append(newLinePrefix); } line.append(c); } // if the world AND the needed black is longer than the maximum // width make a new line and insert the word there } else { // if the maximum width is reached, make a new line if (getWidth(line.toString()) + getWidth(part) + getWidth(' ') > wrappedWidth) { ret.appendln(line.toString()); line.clear(); line.append(newLinePrefix); } // a blank is only needed if the line is not empty AND the last // char is not a a blank (e.g. from prefix) if (!line.isEmpty() && !line.rightString(1).equals(" ")) { line.append(' '); } line.append(part); } } // commit the line if (!line.isEmpty()) { ret.append(line); } return ret.toString(); }