diff --git a/README.md b/README.md index 1b4209d..f4e46ec 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Display data in a tabular form with Java The data can be output in several different different forms: * **text** - attractive ASCII format +* **rich** - Unicode box drawing * **csv** - comma separated values format * **html** - HyperText Markup Language (HTML) format * **json** - JavaScript Object Notation (JSON) format @@ -38,7 +39,7 @@ import rickwporter.prettytable.PrettyTable.OutputFormat; System.out.println(table.formattedString(OutputFormat.TEXT)); ``` -You can use `PrettyTable::toString()` as an alternative to `PrettyTable::formattedString()`. If you want a format other than **text**, you can set it with `PrettyTable::setOutputFormat()`. Repeated leading cells in **text** and **html** follow `PrettyTable::setRemoveRedundant()`, which removes them by default. The code below would replace the `println()` call from above: +You can use `PrettyTable::toString()` as an alternative to `PrettyTable::formattedString()`. If you want a format other than **text**, you can set it with `PrettyTable::setOutputFormat()`. Repeated leading cells in **text**, **rich**, and **html** follow `PrettyTable::setRemoveRedundant()`, which removes them by default. The code below would replace the `println()` call from above: ```Java table.setOutputFormat(OutputFormat.HTML); System.out.println(table.toString()); @@ -85,6 +86,22 @@ Using the **html** format (e.g. `OutputFormat.HTML`), you get the following outp ``` +Using the **rich** format (e.g. `OutputFormat.RICH`), you more aesthetically please lines for the table. The header (if shown) gets a heavier weight border. The header, border, row-divider, alignment, and deduplication settings are the same as **text**. Hiding the border (`setShowLines(false)`) leaves padded text, so a nested table stays borderless. + +```Java + System.out.println(table.formattedString(OutputFormat.RICH)); +``` + +``` +┏━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━┓ +┃ Column A ┃ Column 2 ┃ Column III ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━┩ +│ A │ B │ c │ +│ 1 │ 1024 │ 65535 │ +│ Washington │ Adams │ Jefferson │ +└────────────┴──────────┴────────────┘ +``` + Using the **json** format (e.g. `OutputFormat.JSON`), you get the following output: ``` [ @@ -112,7 +129,7 @@ Using the **json** format (e.g. `OutputFormat.JSON`), you get the following outp ``` ### Alignment -Both ASCII **text** and **html** formats support different alignments. Here's how you would update the above table: +ASCII **text**, **rich**, and **html** support different alignments. Here's how you would update the above table: ```Java import rickwporter.prettytable.PrettyTable.HorizontalAlign; @@ -140,7 +157,7 @@ The **html** output will have the appropriate `style="text-align:xxx"` attribute ``` ### Text options -ASCII **text** output can hide the header row, drop the border, draw a rule under data rows, and print a caption under the table. **csv**, **html**, and **json** ignore these settings. +ASCII **text** and **rich** output can hide the header row, drop the border, draw a rule under data rows, and print a caption under the table. **csv**, **html**, and **json** ignore these settings. A `null` or empty caption is skipped. Any other caption is printed unchanged. `PrettyTable::setShowHeader(false)` omits the header. Column widths then come only from the data cells, so the columns can get narrower. A table built with no headers already skips that row. @@ -250,7 +267,7 @@ US presidents ``` ### Deduplication -Both ASCII **text** and **html** formats remove redundancy by default. This de-duplication generally makes the output more aesthetically pleasing, as demonstrated below. +ASCII **text**, **rich**, and **html** remove redundancy by default. This de-duplication generally makes the output more aesthetically pleasing, as demonstrated below. A table created like this: ```Java @@ -342,6 +359,8 @@ import rickwporter.prettytable.TableBuilder; Found 1 items ``` +`table.setOutputFormat(OutputFormat.RICH)` prints effectively the same table with prettier lines/borders. + ### BuilderConfig `BuilderConfig` holds the labels, caption, truncation limits, and column selection used by `build`. Each setter returns the config, so calls can be chained. A `null` config uses the defaults. diff --git a/pom.xml b/pom.xml index e6588b6..df33808 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ 1.8 ${java.version} ${java.version} + UTF-8 3.20.0 diff --git a/src/main/java/rickwporter/prettytable/PrettyTable.java b/src/main/java/rickwporter/prettytable/PrettyTable.java index 684fe1d..be0a84a 100644 --- a/src/main/java/rickwporter/prettytable/PrettyTable.java +++ b/src/main/java/rickwporter/prettytable/PrettyTable.java @@ -26,6 +26,7 @@ public String getHtmlStyle() { public enum OutputFormat { TEXT, + RICH, CSV, HTML, JSON, @@ -211,6 +212,10 @@ String toText(boolean removeRedundant) { return formattedString(OutputFormat.TEXT, removeRedundant); } + String toRich(boolean removeRedundant) { + return formattedString(OutputFormat.RICH, removeRedundant); + } + String toCsv() { return formattedString(OutputFormat.CSV, false); } @@ -231,6 +236,8 @@ public String formattedString(OutputFormat format, boolean removeRedundant) { final TableRenderInterface renderer; if (format == OutputFormat.TEXT) { renderer = new TextTableRender(); + } else if (format == OutputFormat.RICH) { + renderer = new RichTableRender(); } else if (format == OutputFormat.CSV) { renderer = new CsvTableRender(); } else if (format == OutputFormat.JSON) { diff --git a/src/main/java/rickwporter/prettytable/RichTableRender.java b/src/main/java/rickwporter/prettytable/RichTableRender.java new file mode 100644 index 0000000..b976a95 --- /dev/null +++ b/src/main/java/rickwporter/prettytable/RichTableRender.java @@ -0,0 +1,56 @@ +package rickwporter.prettytable; + +import java.util.ArrayList; +import java.util.List; + +import rickwporter.prettytable.PrettyTable.HorizontalAlign; + +class RichTableRender extends BaseTextTableRender { + // Four characters are left, fill, junction, right. + private static final TableGlyphs HEAVY_TOP = new TableGlyphs('┏', '━', '┳', '┓'); + private static final TableGlyphs HEAVY_HEAD = new TableGlyphs('┡', '━', '╇', '┩'); + private static final TableGlyphs HEAVY_VERT = new TableGlyphs('┃', ' ', '┃', '┃'); + private static final TableGlyphs SQUARE_TOP = new TableGlyphs('┌', '─', '┬', '┐'); + private static final TableGlyphs LIGHT_VERT = new TableGlyphs('│', ' ', '│', '│'); + private static final TableGlyphs LIGHT_ROW = new TableGlyphs('├', '─', '┼', '┤'); + private static final TableGlyphs LIGHT_BOTTOM = new TableGlyphs('└', '─', '┴', '┘'); + + @Override + public String render(PrettyTable table, boolean removeRedundant) { + List maxWidths = table.getMaxWidths(); + boolean showLines = table.isShowLines(); + boolean rowDividers = table.isRowDividers() && showLines; + boolean header = table.isShowHeader() && !table.getHeaders().isEmpty(); + List hAligns = this.alignments(table); + StringBuilder result = new StringBuilder(); + + if (showLines && header) { + result.append(this.rule(HEAVY_TOP, maxWidths, true)); + result.append(this.textRow(table.getHeaders(), maxWidths, hAligns, HEAVY_VERT)); + result.append(this.rule(HEAVY_HEAD, maxWidths, true)); + } else if (header) { + result.append(this.textRow(table.getHeaders(), maxWidths, hAligns, null)); + } else if (showLines) { + result.append(this.rule(SQUARE_TOP, maxWidths, true)); + } + + List> rows = table.getRows(); + List lastRow = new ArrayList(); + for (int rIdx = 0; rIdx < rows.size(); rIdx++) { + List currentRow = this.cellText(rows.get(rIdx)); + List fullRow = new ArrayList(currentRow); + boolean divider = true; + if (removeRedundant) { + divider = !this.sharesLeadingCells(lastRow, currentRow); + lastRow = fullRow; + } + TableGlyphs vertical = showLines ? LIGHT_VERT : null; + result.append(this.textRow(currentRow, maxWidths, hAligns, vertical)); + boolean last = rIdx == rows.size() - 1; + result.append(this.rule(LIGHT_ROW, maxWidths, rowDividers && divider && !last)); + } + result.append(this.rule(LIGHT_BOTTOM, maxWidths, showLines)); + result.append(this.caption(table.getCaption())); + return result.toString(); + } +} diff --git a/src/test/java/rickwporter/prettytable/PrettyTableTest.java b/src/test/java/rickwporter/prettytable/PrettyTableTest.java index 0c2474e..f8a3b7d 100644 --- a/src/test/java/rickwporter/prettytable/PrettyTableTest.java +++ b/src/test/java/rickwporter/prettytable/PrettyTableTest.java @@ -16,6 +16,8 @@ public class PrettyTableTest { Map OUTPUT_FORMAT_EXTENSIONS = new HashMap() {{ put(OutputFormat.TEXT, "text"); + // NOTE: this is a little hacky, but named rich text as `txt` to distinguish from plain/ascii text + put(OutputFormat.RICH, "txt"); put(OutputFormat.CSV, "csv"); put(OutputFormat.HTML, "html"); put(OutputFormat.JSON, "json"); @@ -112,12 +114,30 @@ public void testBasicText() { Assertions.assertEquals(expected, result); } + @Test + public void testBasicRich() { + PrettyTable table = createBasicTable(); + String result = table.toRich(true); + String expected = loadFileContent("PrettyTable_basic.txt"); + Assertions.assertEquals(expected, result); + // same answer when going through 'formattedString()' + result = table.formattedString(OutputFormat.RICH); + Assertions.assertEquals(expected, result); + // same answer in this case because there are no duplicates + result = table.toRich(false); + Assertions.assertEquals(expected, result); + table.setOutputFormat(OutputFormat.RICH); + result = table.toString(); + Assertions.assertEquals(expected, result); + } + @Test public void testFormatted() { // this checks all the formatted outputs Map formats = new HashMap() {{ put(OutputFormat.HTML, "html"); put(OutputFormat.TEXT, "text"); + put(OutputFormat.RICH, "txt"); }}; PrettyTable table = createFormattedTable(); for (Map.Entry entry: formats.entrySet()) { @@ -179,6 +199,12 @@ public void testDuplicated() { Assertions.assertEquals(expected, result); result = table.formattedString(OutputFormat.TEXT, false); Assertions.assertEquals(expected, result); + + expected = loadFileContent("PrettyTable_duplicate_undup.txt"); + result = table.toRich(false); + Assertions.assertEquals(expected, result); + result = table.formattedString(OutputFormat.RICH, false); + Assertions.assertEquals(expected, result); expected = loadFileContent("PrettyTable_duplicate_undup.html"); result = table.toHtml(false); @@ -201,6 +227,9 @@ public void testDuplicatedMixed() { expected = loadFileContent("PrettyTable_duplicate_mixed.html"); Assertions.assertEquals(expected, table.formattedString(OutputFormat.HTML)); + + expected = loadFileContent("PrettyTable_duplicate_mixed.txt"); + Assertions.assertEquals(expected, table.formattedString(OutputFormat.RICH)); } @Test @@ -326,6 +355,8 @@ public void testTextNewlines() { String expected = loadFileContent("PrettyTable_newlines.text"); Assertions.assertEquals(expected, table.formattedString(OutputFormat.TEXT)); + expected = loadFileContent("PrettyTable_newlines.txt"); + Assertions.assertEquals(expected, table.formattedString(OutputFormat.RICH)); } @Test @@ -372,6 +403,12 @@ public void testNested() { numbers.setOutputFormat(OutputFormat.CSV); big.setOutputFormat(OutputFormat.JSON); Assertions.assertEquals(expected, big.toString()); + + expected = loadFileContent("PrettyTable_nested_mixed.txt"); + letters.setOutputFormat(OutputFormat.JSON); + numbers.setOutputFormat(OutputFormat.CSV); + big.setOutputFormat(OutputFormat.RICH); + Assertions.assertEquals(expected, big.toString()); } @Test @@ -498,29 +535,37 @@ public void testRemoveRedundantOption() { table.addRow("A", "B", "F", "E"); String dedupedText = loadFileContent("PrettyTable_duplicate.text"); + String dedupedRich = loadFileContent("PrettyTable_duplicate.txt"); String dedupedHtml = loadFileContent("PrettyTable_duplicate.html"); String fullText = loadFileContent("PrettyTable_duplicate_undup.text"); + String fullRich = loadFileContent("PrettyTable_duplicate_undup.txt"); String fullHtml = loadFileContent("PrettyTable_duplicate_undup.html"); Assertions.assertTrue(table.isRemoveRedundant()); Assertions.assertEquals(dedupedText, table.formattedString(OutputFormat.TEXT)); + Assertions.assertEquals(dedupedRich, table.formattedString(OutputFormat.RICH)); Assertions.assertEquals(dedupedHtml, table.formattedString(OutputFormat.HTML)); Assertions.assertEquals(dedupedText, table.toString()); table.setRemoveRedundant(false); Assertions.assertFalse(table.isRemoveRedundant()); Assertions.assertEquals(fullText, table.formattedString(OutputFormat.TEXT)); + Assertions.assertEquals(fullRich, table.formattedString(OutputFormat.RICH)); Assertions.assertEquals(fullHtml, table.formattedString(OutputFormat.HTML)); Assertions.assertEquals(fullText, table.toString()); table.setOutputFormat(OutputFormat.HTML); Assertions.assertEquals(fullHtml, table.toString()); + table.setOutputFormat(OutputFormat.RICH); + Assertions.assertEquals(fullRich, table.toString()); // the explicit flag still overrides the option Assertions.assertEquals(dedupedText, table.formattedString(OutputFormat.TEXT, true)); + Assertions.assertEquals(dedupedRich, table.formattedString(OutputFormat.RICH, true)); Assertions.assertEquals(dedupedHtml, table.formattedString(OutputFormat.HTML, true)); table.setRemoveRedundant(true); Assertions.assertEquals(fullText, table.formattedString(OutputFormat.TEXT, false)); + Assertions.assertEquals(fullRich, table.formattedString(OutputFormat.RICH, false)); Assertions.assertEquals(fullHtml, table.toHtml(false)); // CSV and JSON keep every value either way diff --git a/src/test/java/rickwporter/prettytable/RichTableRenderTest.java b/src/test/java/rickwporter/prettytable/RichTableRenderTest.java new file mode 100644 index 0000000..bdd0cd9 --- /dev/null +++ b/src/test/java/rickwporter/prettytable/RichTableRenderTest.java @@ -0,0 +1,378 @@ +package rickwporter.prettytable; + +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import rickwporter.prettytable.PrettyTable.HorizontalAlign; +import rickwporter.prettytable.PrettyTable.OutputFormat; + +public class RichTableRenderTest { + @Test + public void testBasicRich() { + PrettyTable table = new PrettyTable("Column A", "Column 2", "Column III"); + table.addRow("A", "B", "c"); + table.addRow(1, 1024, 65535); + table.addRow("Washington", "Adams", "Jefferson"); + + String expected = loadFileContent("PrettyTable_basic.txt"); + Assertions.assertEquals(expected, render(table)); + Assertions.assertEquals(expected, table.formattedString(OutputFormat.RICH)); + Assertions.assertEquals(expected, table.toRich(true)); + + table.setOutputFormat(OutputFormat.RICH); + Assertions.assertEquals(OutputFormat.RICH, table.getDefaultOutput()); + Assertions.assertEquals(expected, table.toString()); + } + + @Test + public void testAlignment() { + PrettyTable table = new PrettyTable("Column A", "Column 2", "Column III"); + table.setHorizAligns(HorizontalAlign.LEFT, HorizontalAlign.CENTER, HorizontalAlign.RIGHT); + table.addRow("A", "B", "c"); + table.addRow(1, 1024, 65535); + table.addRow("Washington", "Adams", "Jefferson"); + + Assertions.assertEquals(loadFileContent("PrettyTable_formatted.txt"), render(table)); + } + + @Test + public void testHiddenHeaderUsesSquareBox() { + PrettyTable table = nameAgeTable(); + table.setShowHeader(false); + Assertions.assertEquals("" + + "┌─────┬────┐\n" + + "│ Ada │ 36 │\n" + + "│ Bob │ 7 │\n" + + "└─────┴────┘\n", render(table)); + + PrettyTable noHeaders = new PrettyTable(); + noHeaders.addRow("Ada", 36); + Assertions.assertEquals("" + + "┌─────┬────┐\n" + + "│ Ada │ 36 │\n" + + "└─────┴────┘\n", render(noHeaders)); + + PrettyTable empty = new PrettyTable("Name", "Age"); + empty.setShowHeader(false); + Assertions.assertEquals("" + + "┌──┬──┐\n" + + "└──┴──┘\n", render(empty)); + } + + @Test + public void testEmptyTableClosesTheBox() { + PrettyTable empty = new PrettyTable("Name", "Age"); + Assertions.assertEquals("" + + "┏━━━━━━┳━━━━━┓\n" + + "┃ Name ┃ Age ┃\n" + + "┡━━━━━━╇━━━━━┩\n" + + "└──────┴─────┘\n", render(empty)); + + empty.setRowDividers(true); + Assertions.assertEquals("" + + "┏━━━━━━┳━━━━━┓\n" + + "┃ Name ┃ Age ┃\n" + + "┡━━━━━━╇━━━━━┩\n" + + "└──────┴─────┘\n", render(empty)); + } + + @Test + public void testShowLinesFalseMatchesText() { + PrettyTable table = nameAgeTable(); + table.setShowLines(false); + Assertions.assertEquals(new TextTableRender().render(table, false), render(table)); + + table.setHorizAligns(HorizontalAlign.LEFT, HorizontalAlign.RIGHT); + Assertions.assertEquals(new TextTableRender().render(table, false), render(table)); + + table.setShowHeader(false); + table.setRowDividers(true); + Assertions.assertEquals(new TextTableRender().render(table, false), render(table)); + } + + @Test + public void testRowDividers() { + PrettyTable table = nameAgeTable(); + table.setRowDividers(true); + Assertions.assertEquals("" + + "┏━━━━━━┳━━━━━┓\n" + + "┃ Name ┃ Age ┃\n" + + "┡━━━━━━╇━━━━━┩\n" + + "│ Ada │ 36 │\n" + + "├──────┼─────┤\n" + + "│ Bob │ 7 │\n" + + "└──────┴─────┘\n", render(table, false)); + + table.setShowHeader(false); + Assertions.assertEquals("" + + "┌─────┬────┐\n" + + "│ Ada │ 36 │\n" + + "├─────┼────┤\n" + + "│ Bob │ 7 │\n" + + "└─────┴────┘\n", render(table, false)); + + PrettyTable wrapped = new PrettyTable("A", "B"); + wrapped.setRowDividers(true); + wrapped.addRow("a\nbb", "c"); + wrapped.addRow("d", "ee"); + Assertions.assertEquals("" + + "┏━━━━┳━━━━┓\n" + + "┃ A ┃ B ┃\n" + + "┡━━━━╇━━━━┩\n" + + "│ a │ c │\n" + + "│ bb │ │\n" + + "├────┼────┤\n" + + "│ d │ ee │\n" + + "└────┴────┘\n", render(wrapped, false)); + } + + @Test + public void testSharedPrefixOmitsInnerRules() { + PrettyTable table = new PrettyTable("Col1", "Col2", "Col3", "Col4"); + table.addRow("A", "B", "C", "D"); + table.addRow("A", "B", "C", "Z"); + table.addRow("A", "B", "F", "E"); + table.addRow("A", "B", "F", "E"); + table.setRowDividers(true); + + Assertions.assertEquals("" + + "┏━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┓\n" + + "┃ Col1 ┃ Col2 ┃ Col3 ┃ Col4 ┃\n" + + "┡━━━━━━╇━━━━━━╇━━━━━━╇━━━━━━┩\n" + + "│ A │ B │ C │ D │\n" + + "├──────┼──────┼──────┼──────┤\n" + + "│ │ │ │ Z │\n" + + "│ │ │ F │ E │\n" + + "│ │ │ │ │\n" + + "└──────┴──────┴──────┴──────┘\n", render(table, true)); + + Assertions.assertEquals("" + + "┏━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┓\n" + + "┃ Col1 ┃ Col2 ┃ Col3 ┃ Col4 ┃\n" + + "┡━━━━━━╇━━━━━━╇━━━━━━╇━━━━━━┩\n" + + "│ A │ B │ C │ D │\n" + + "├──────┼──────┼──────┼──────┤\n" + + "│ A │ B │ C │ Z │\n" + + "├──────┼──────┼──────┼──────┤\n" + + "│ A │ B │ F │ E │\n" + + "├──────┼──────┼──────┼──────┤\n" + + "│ A │ B │ F │ E │\n" + + "└──────┴──────┴──────┴──────┘\n", render(table, false)); + } + + @Test + public void testCaption() { + PrettyTable table = nameAgeTable(); + Assertions.assertEquals(borderedNameAge(), render(table)); + + table.setCaption(""); + Assertions.assertEquals(borderedNameAge(), render(table)); + + table.setCaption("Friends"); + Assertions.assertEquals(borderedNameAge() + "Friends\n", render(table)); + } + + @Test + public void testNullAlignment() { + PrettyTable table = new PrettyTable("A"); + table.addRow("x"); + table.setHorizAligns((HorizontalAlign) null); + IllegalArgumentException ex = Assertions.assertThrows( + IllegalArgumentException.class, + () -> render(table) + ); + Assertions.assertEquals("Unsupported HorizontalAlignment: null", ex.getMessage()); + } + + @Test + public void testNestedTableUsesItsOwnFormat() { + PrettyTable inner = new PrettyTable("A"); + inner.addRow("hi"); + inner.setOutputFormat(OutputFormat.RICH); + PrettyTable outer = new PrettyTable("Data"); + outer.setHorizAligns(HorizontalAlign.LEFT); + outer.addRow(inner); + + Assertions.assertEquals("" + + "┏━━━━━━━━┓\n" + + "┃ Data ┃\n" + + "┡━━━━━━━━┩\n" + + "│ ┏━━━━┓ │\n" + + "│ ┃ A ┃ │\n" + + "│ ┡━━━━┩ │\n" + + "│ │ hi │ │\n" + + "│ └────┘ │\n" + + "└────────┘\n", outer.formattedString(OutputFormat.RICH)); + + inner.setOutputFormat(OutputFormat.TEXT); + Assertions.assertTrue(outer.formattedString(OutputFormat.RICH).contains("| A |")); + } + + @Test + public void testRichObjectsSamples() { + Assertions.assertEquals("" + + "┏━━━━━━┳━━━━━━━━━━━━━━━━┓\n" + + "┃ Name ┃ Properties ┃\n" + + "┡━━━━━━╇━━━━━━━━━━━━━━━━┩\n" + + "│ sna │ prop1 1 │\n" + + "│ │ prop B None │\n" + + "│ │ blah zay │\n" + + "├──────┼────────────────┤\n" + + "│ foo │ prop2 2 │\n" + + "│ │ prop B True │\n" + + "├──────┼────────────────┤\n" + + "│ bar │ 1 inverse │\n" + + "└──────┴────────────────┘\n" + + "Found 3 items\n", + rich(readmeList())); + + Assertions.assertEquals("" + + "┏━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n" + + "┃ Property ┃ Value ┃\n" + + "┡━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n" + + "│ abc │ def │\n" + + "│ ghi │ False │\n" + + "│ jkl │ mno, pqr, stu │\n" + + "│ vwx │ 1, 2, 4 │\n" + + "│ 2 │ 3 │\n" + + "│ yxa │ None │\n" + + "└──────────┴───────────────┘\n", + rich(simpleDict())); + + Assertions.assertEquals("" + + "┏━━━━━━━━━━━┓\n" + + "┃ Items ┃\n" + + "┡━━━━━━━━━━━┩\n" + + "│ str value │\n" + + "├───────────┤\n" + + "│ 3 │\n" + + "├───────────┤\n" + + "│ 4 │\n" + + "├───────────┤\n" + + "│ False │\n" + + "├───────────┤\n" + + "│ None │\n" + + "├───────────┤\n" + + "│ foo │\n" + + "└───────────┘\n" + + "Found 6 items\n", + rich(Arrays.asList("str value", "3", 4, false, null, "foo"))); + + List summary = new ArrayList(); + summary.add(mapOf("name", "sna", "data", 1)); + summary.add(mapOf("name", "foo", "data", mapOf("a", 1, "b", 2))); + summary.add(mapOf("name", "bar", "data", null)); + Assertions.assertEquals("" + + "┏━━━━━━┳━━━━━━━━┓\n" + + "┃ Name ┃ Data ┃\n" + + "┡━━━━━━╇━━━━━━━━┩\n" + + "│ sna │ 1 │\n" + + "├──────┼────────┤\n" + + "│ foo │ a 1 │\n" + + "│ │ b 2 │\n" + + "├──────┼────────┤\n" + + "│ bar │ None │\n" + + "└──────┴────────┘\n" + + "Found 3 items\n", + rich(summary)); + + BuilderConfig options = new BuilderConfig() + .setItemsLabel("Simple stuff") + .setItemsCaption("Got {} simple things"); + Assertions.assertEquals("" + + "┏━━━━━━━━━━━━━━┓\n" + + "┃ Simple stuff ┃\n" + + "┡━━━━━━━━━━━━━━┩\n" + + "│ str value │\n" + + "├──────────────┤\n" + + "│ 3 │\n" + + "├──────────────┤\n" + + "│ 4 │\n" + + "├──────────────┤\n" + + "│ False │\n" + + "├──────────────┤\n" + + "│ None │\n" + + "├──────────────┤\n" + + "│ foo │\n" + + "└──────────────┘\n" + + "Got 6 simple things\n", + rich(Arrays.asList("str value", "3", 4, false, null, "foo"), options)); + } + + private String loadFileContent(String filename) { + try { + URL resource = getClass().getClassLoader().getResource(filename); + return Files.readString(Paths.get(resource.toURI())); + } catch (Exception ex) { + throw new IllegalStateException("Unable to read " + filename, ex); + } + } + + private String borderedNameAge() { + return "" + + "┏━━━━━━┳━━━━━┓\n" + + "┃ Name ┃ Age ┃\n" + + "┡━━━━━━╇━━━━━┩\n" + + "│ Ada │ 36 │\n" + + "│ Bob │ 7 │\n" + + "└──────┴─────┘\n"; + } + + private PrettyTable nameAgeTable() { + PrettyTable table = new PrettyTable("Name", "Age"); + table.addRow("Ada", 36); + table.addRow("Bob", 7); + return table; + } + + private List readmeList() { + List items = new ArrayList(); + items.add(mapOf("name", "sna", "prop1", 1, "prop B", null, "blah", "zay")); + items.add(mapOf("name", "foo", "prop2", 2, "prop B", true)); + items.add(mapOf("name", "bar", 1, "inverse")); + return items; + } + + private Map simpleDict() { + return mapOf( + "abc", "def", + "ghi", false, + "jkl", Arrays.asList("mno", "pqr", "stu"), + "vwx", Arrays.asList(1, 2, 4), + 2, 3, + "yxa", null); + } + + private static Map mapOf(Object... pairs) { + Map result = new LinkedHashMap(); + for (int i = 0; i < pairs.length; i += 2) { + result.put(pairs[i], pairs[i + 1]); + } + return result; + } + + private String rich(Object data) { + return rich(data, null); + } + + private String rich(Object data, BuilderConfig options) { + return new TableBuilder(options).build(data).formattedString(OutputFormat.RICH); + } + + private String render(PrettyTable table) { + return render(table, false); + } + + private String render(PrettyTable table, boolean removeRedundant) { + return new RichTableRender().render(table, removeRedundant); + } +} diff --git a/src/test/resources/PrettyTable_basic.txt b/src/test/resources/PrettyTable_basic.txt new file mode 100644 index 0000000..745afaf --- /dev/null +++ b/src/test/resources/PrettyTable_basic.txt @@ -0,0 +1,7 @@ +┏━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━┓ +┃ Column A ┃ Column 2 ┃ Column III ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━┩ +│ A │ B │ c │ +│ 1 │ 1024 │ 65535 │ +│ Washington │ Adams │ Jefferson │ +└────────────┴──────────┴────────────┘ diff --git a/src/test/resources/PrettyTable_duplicate.txt b/src/test/resources/PrettyTable_duplicate.txt new file mode 100644 index 0000000..1fb4958 --- /dev/null +++ b/src/test/resources/PrettyTable_duplicate.txt @@ -0,0 +1,8 @@ +┏━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┓ +┃ Col1 ┃ Col2 ┃ Col3 ┃ Col4 ┃ +┡━━━━━━╇━━━━━━╇━━━━━━╇━━━━━━┩ +│ A │ B │ C │ D │ +│ │ │ │ Z │ +│ │ │ F │ E │ +│ │ │ │ │ +└──────┴──────┴──────┴──────┘ diff --git a/src/test/resources/PrettyTable_duplicate_mixed.txt b/src/test/resources/PrettyTable_duplicate_mixed.txt new file mode 100644 index 0000000..8776f58 --- /dev/null +++ b/src/test/resources/PrettyTable_duplicate_mixed.txt @@ -0,0 +1,8 @@ +┏━━━━┳━━━━┳━━━━┓ +┃ A ┃ B ┃ C ┃ +┡━━━━╇━━━━╇━━━━┩ +│ 10 │ 2 │ 3 │ +│ │ │ 45 │ +│ │ 20 │ 12 │ +│ x │ y │ z │ +└────┴────┴────┘ diff --git a/src/test/resources/PrettyTable_duplicate_undup.txt b/src/test/resources/PrettyTable_duplicate_undup.txt new file mode 100644 index 0000000..808b4b4 --- /dev/null +++ b/src/test/resources/PrettyTable_duplicate_undup.txt @@ -0,0 +1,8 @@ +┏━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┓ +┃ Col1 ┃ Col2 ┃ Col3 ┃ Col4 ┃ +┡━━━━━━╇━━━━━━╇━━━━━━╇━━━━━━┩ +│ A │ B │ C │ D │ +│ A │ B │ C │ Z │ +│ A │ B │ F │ E │ +│ A │ B │ F │ E │ +└──────┴──────┴──────┴──────┘ diff --git a/src/test/resources/PrettyTable_empty.txt b/src/test/resources/PrettyTable_empty.txt new file mode 100644 index 0000000..65bbcba --- /dev/null +++ b/src/test/resources/PrettyTable_empty.txt @@ -0,0 +1,4 @@ +┏━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┓ +┃ Col1 ┃ Col2 ┃ Col3 ┃ Col4 ┃ +┡━━━━━━╇━━━━━━╇━━━━━━╇━━━━━━┩ +└──────┴──────┴──────┴──────┘ diff --git a/src/test/resources/PrettyTable_formatted.txt b/src/test/resources/PrettyTable_formatted.txt new file mode 100644 index 0000000..bc4b257 --- /dev/null +++ b/src/test/resources/PrettyTable_formatted.txt @@ -0,0 +1,7 @@ +┏━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━┓ +┃ Column A ┃ Column 2 ┃ Column III ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━┩ +│ A │ B │ c │ +│ 1 │ 1024 │ 65535 │ +│ Washington │ Adams │ Jefferson │ +└────────────┴──────────┴────────────┘ diff --git a/src/test/resources/PrettyTable_nested.txt b/src/test/resources/PrettyTable_nested.txt new file mode 100644 index 0000000..e46cf3e --- /dev/null +++ b/src/test/resources/PrettyTable_nested.txt @@ -0,0 +1,18 @@ +┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +┃ Table name ┃ Data ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ +│ Letters │ ┏━━━━━━┳━━━━━┳━━━━━━┓ │ +│ │ ┃ A ┃ B ┃ C ┃ │ +│ │ ┡━━━━━━╇━━━━━╇━━━━━━┩ │ +│ │ │ abc │ def │ ghij │ │ +│ │ │ klmn │ o │ pqr │ │ +│ │ │ st │ uvx │ yz │ │ +│ │ └──────┴─────┴──────┘ │ +│ Numbers │ ┏━━━━━━━┳━━━━━━━━┳━━━━━━━┓ │ +│ │ ┃ First ┃ Second ┃ Third ┃ │ +│ │ ┡━━━━━━━╇━━━━━━━━╇━━━━━━━┩ │ +│ │ │ 123 │ 4567 │ 89012 │ │ +│ │ │ 3456 │ 78 │ 901 │ │ +│ │ │ 23456 │ 7890 │ 12 │ │ +│ │ └───────┴────────┴───────┘ │ +└────────────┴────────────────────────────┘ diff --git a/src/test/resources/PrettyTable_nested_mixed.txt b/src/test/resources/PrettyTable_nested_mixed.txt new file mode 100644 index 0000000..e0c6676 --- /dev/null +++ b/src/test/resources/PrettyTable_nested_mixed.txt @@ -0,0 +1,30 @@ +┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┓ +┃ Table name ┃ Data ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━┩ +│ Letters │ [ │ +│ │ [ │ +│ │ "A", │ +│ │ "B", │ +│ │ "C" │ +│ │ ], │ +│ │ { │ +│ │ "A": "abc", │ +│ │ "B": "def", │ +│ │ "C": "ghij" │ +│ │ }, │ +│ │ { │ +│ │ "A": "klmn", │ +│ │ "B": "o", │ +│ │ "C": "pqr" │ +│ │ }, │ +│ │ { │ +│ │ "A": "st", │ +│ │ "B": "uvx", │ +│ │ "C": "yz" │ +│ │ } │ +│ │ ] │ +│ Numbers │ First,Second,Third │ +│ │ 123,4567,89012 │ +│ │ 3456,78,901 │ +│ │ 23456,7890,12 │ +└────────────┴──────────────────────┘ diff --git a/src/test/resources/PrettyTable_newlines.txt b/src/test/resources/PrettyTable_newlines.txt new file mode 100644 index 0000000..d16ea35 --- /dev/null +++ b/src/test/resources/PrettyTable_newlines.txt @@ -0,0 +1,10 @@ +┏━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━┓ +┃ A ┃ B ┃ C ┃ D ┃ +┡━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━┩ +│ a │ 12 │ testing │ │ +│ │ 345 │ a │ empty │ +│ │ 67890 │ │ blank │ +│ │ abc │ │ │ +│ abc │ 123 │ aaa │ bbb │ +│ 123 │ 4567 │ │ │ +└─────┴───────┴─────────┴───────┘ diff --git a/src/test/resources/PrettyTable_no_headers.txt b/src/test/resources/PrettyTable_no_headers.txt new file mode 100644 index 0000000..b93d172 --- /dev/null +++ b/src/test/resources/PrettyTable_no_headers.txt @@ -0,0 +1,5 @@ +┌────────────┬───────┬───────────┐ +│ A │ B │ c │ +│ 1 │ 1024 │ 65535 │ +│ Washington │ Adams │ Jefferson │ +└────────────┴───────┴───────────┘