private static void writeDataLine( StringBuilder buf, int[] colLengths, List<AsciiTableAlignment> alignments, List<String> values) { for (int colIdx = 0; colIdx < colLengths.length; colIdx++) { String value = Strings.nullToEmpty(values.get(colIdx)); buf.append('|') .append(' ') .append(formatValue(buf, colLengths[colIdx], alignments.get(colIdx), value)) .append(' '); } buf.append('|').append(LINE_SEPARATOR); }
writeSeparatorLine(buf, colLengths); writeDataLine(buf, colLengths, alignments, headers); writeSeparatorLine(buf, colLengths); for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) { writeDataLine(buf, colLengths, alignments, cells.get(rowIdx)); writeSeparatorLine(buf, colLengths); return buf.toString();
/** * Outputs the report as an ASCII table. * * @param report the report * @param out the output stream to write to */ public void writeAsciiTable(R report, OutputStream out) { List<Class<?>> columnTypes = getColumnTypes(report); List<AsciiTableAlignment> alignments = IntStream.range(0, columnTypes.size()) .mapToObj(i -> calculateAlignment(columnTypes.get(i))) .collect(toImmutableList()); List<String> headers = report.getColumnHeaders(); ImmutableList<ImmutableList<String>> cells = formatAsciiTable(report); String asciiTable = AsciiTable.generate(headers, alignments, cells); PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); pw.println(asciiTable); pw.flush(); }
public void test_generate_padHeader() { List<AsciiTableAlignment> alignments = ImmutableList.of(AsciiTableAlignment.LEFT, AsciiTableAlignment.RIGHT); List<String> headers = ImmutableList.of("A", "B"); List<List<String>> cells = ImmutableList.of(ImmutableList.of("12", "23"), ImmutableList.of("12345", "")); String test = AsciiTable.generate(headers, alignments, cells); String expected = "" + "+-------+----+" + LINE_SEPARATOR + "| A | B |" + LINE_SEPARATOR + "+-------+----+" + LINE_SEPARATOR + "| 12 | 23 |" + LINE_SEPARATOR + "| 12345 | |" + LINE_SEPARATOR + "+-------+----+" + LINE_SEPARATOR; assertEquals(test, expected); }
public void test_generate_padData() { List<AsciiTableAlignment> alignments = ImmutableList.of(AsciiTableAlignment.LEFT, AsciiTableAlignment.RIGHT); List<String> headers = ImmutableList.of("Alpha", "Beta"); List<List<String>> cells = ImmutableList.of(ImmutableList.of("12", "23"), ImmutableList.of("12345", "")); String test = AsciiTable.generate(headers, alignments, cells); String expected = "" + "+-------+------+" + LINE_SEPARATOR + "| Alpha | Beta |" + LINE_SEPARATOR + "+-------+------+" + LINE_SEPARATOR + "| 12 | 23 |" + LINE_SEPARATOR + "| 12345 | |" + LINE_SEPARATOR + "+-------+------+" + LINE_SEPARATOR; assertEquals(test, expected); }