/** * 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); }