private void fixTabs(int tabSize) { int rowIndex = 0; for (StringBuilder row1 : rows) { String row = row1.toString(); StringBuilder newRow = new StringBuilder(); char[] chars = row.toCharArray(); for (char c : chars) { if (c == '\t') { int spacesLeft = tabSize - newRow.length() % tabSize; if (DEBUG) { System.out.println("Found tab. Spaces left: " + spacesLeft); } String spaces = StringUtils.repeatString(" ", spacesLeft); newRow.append(spaces); } else { String character = Character.toString(c); newRow.append(character); } } rows.set(rowIndex, newRow); rowIndex++; } }
public static String formatElapsedTime(long elapsedTime) { StringBuilder buffer = new StringBuilder(); if (elapsedTime < 60) { buffer.append(" - "); } else { long hours = elapsedTime / 3600; if (hours > 0) { NumberFormat nf = NumberFormat.getNumberInstance(); FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD); nf.setMaximumIntegerDigits(3); String h = nf.format(hours, new StringBuffer(), fp).toString(); buffer.append(StringUtils.repeatString(" ", 3 - fp.getEndIndex())).append(h).append("h "); elapsedTime -= 3600 * hours; } else { buffer.append(" "); } long minutes = elapsedTime / 60; if (minutes < 10) { buffer.append(' '); } buffer.append(minutes).append("m"); } return buffer.toString(); }
public void removeMarkupTags() { for (CellTagPair pair : findMarkupTags()) { String tagName = pair.tag; if (tagName == null) continue; int length = 2 + tagName.length(); writeStringTo(pair.cell, StringUtils.repeatString(" ", length)); } }
public void printDebug() { Iterator<StringBuilder> it = rows.iterator(); int i = 0; System.out.println( " " + StringUtils.repeatString("0123456789", (int) Math.floor(getWidth() / 10) + 1)); while (it.hasNext()) { String row = it.next().toString(); String index = Integer.toString(i); if (i < 10) index = " " + index; System.out.println(index + " (" + row + ")"); i++; } }
public String getDebugString() { StringBuilder buffer = new StringBuilder(); Iterator<StringBuilder> it = rows.iterator(); int i = 0; buffer .append(" ") .append(StringUtils.repeatString("0123456789", (int) Math.floor(getWidth() / 10) + 1)) .append("\n"); while (it.hasNext()) { String row = it.next().toString(); String index = Integer.toString(i); if (i < 10) index = " " + index; row = row.replaceAll("\n", "\\\\n"); row = row.replaceAll("\r", "\\\\r"); buffer.append(index).append(" (").append(row).append(")\n"); i++; } return buffer.toString(); }
public static String formatEventTime(final Date eventTime) { StringBuilder buffer = new StringBuilder(); long now = System.currentTimeMillis(); long days = (now - eventTime.getTime()) / 86400000L; // milliseconds in a day if (days > 0) { NumberFormat nf = NumberFormat.getNumberInstance(); FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD); nf.setMaximumIntegerDigits(3); String ds = nf.format(days, new StringBuffer(), fp).toString(); buffer.append(StringUtils.repeatString(" ", 3 - fp.getEndIndex())).append(ds).append('+'); } else { buffer.append(" "); } SimpleDateFormat df = new SimpleDateFormat("hh:mm aa"); df.setTimeZone(TimeZone.getDefault()); buffer.append(df.format(eventTime)); return buffer.toString(); }
public TextGrid(int width, int height) { String space = StringUtils.repeatString(" ", width); rows = new ArrayList<StringBuilder>(); for (int i = 0; i < height; i++) rows.add(new StringBuilder(space)); }
public void initialiseWithLines(ArrayList<StringBuilder> lines, ProcessingOptions options) throws UnsupportedEncodingException { // remove blank rows at the bottom boolean done = false; int i; for (i = lines.size() - 1; i >= 0 && !done; i--) { StringBuilder row = lines.get(i); if (!StringUtils.isBlank(row.toString())) done = true; } rows = new ArrayList<StringBuilder>(lines.subList(0, i + 2)); if (options != null) fixTabs(options.getTabSize()); else fixTabs(ProcessingOptions.DEFAULT_TAB_SIZE); // make all lines of equal length // add blank outline around the buffer to prevent fill glitch // convert tabs to spaces (or remove them if setting is 0) int blankBorderSize = 2; int maxLength = 0; int index = 0; String encoding = null; if (options != null) encoding = options.getCharacterEncoding(); Iterator<StringBuilder> it = rows.iterator(); while (it.hasNext()) { String row = it.next().toString(); if (encoding != null) { byte[] bytes = row.getBytes(); row = new String(bytes, encoding); } if (row.length() > maxLength) maxLength = row.length(); rows.set(index, new StringBuilder(row)); index++; } it = rows.iterator(); ArrayList<StringBuilder> newRows = new ArrayList<StringBuilder>(); // TODO: make the following depend on blankBorderSize StringBuilder topBottomRow = new StringBuilder(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2)); newRows.add(topBottomRow); newRows.add(topBottomRow); while (it.hasNext()) { StringBuilder row = it.next(); if (row.length() < maxLength) { String borderString = StringUtils.repeatString(" ", blankBorderSize); StringBuilder newRow = new StringBuilder(); newRow.append(borderString); newRow.append(row); newRow.append(StringUtils.repeatString(" ", maxLength - row.length())); newRow.append(borderString); newRows.add(newRow); } else { // TODO: why is the following line like that? newRows.add(new StringBuilder(" ").append(row).append(" ")); } } // TODO: make the following depend on blankBorderSize newRows.add(topBottomRow); newRows.add(topBottomRow); rows = newRows; replaceBullets(); replaceHumanColorCodes(); }