diff --git a/.gitignore b/.gitignore index 469f38c..9a94f14 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,7 @@ target pom.xml.* release.properties + +.classpath +.project +.settings diff --git a/CHANGELOG.md b/CHANGELOG.md index cbb0e65..c979f7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Change Log ========== +Version 1.0.3 *(2017-05-24)* +---------------------------- + + * Support for custom TableFormat: https://github.com/JakeWharton/flip-tables/issues/14 + * Added AsciiTableFormat + + Version 1.0.2 *(2014-11-22)* ---------------------------- diff --git a/README.md b/README.md index dd0df69..5d928da 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,42 @@ System.out.println(FlipTableConverters.fromObjects(headers, data)); ╚════════════╧═══════════╧═════╧═════════╝ ``` +TableFormat +-------- + +There are two available `TableFormat`s. + +**DefaultTableFormat** + +`System.out.println(FlipTable.of(headers, data));` + +OR + +`System.out.println(FlipTable.of(new DefaultTableFormat(), headers, data));` + +``` +╔══════╤════════╗ +║ Test │ Header ║ +╠══════╪════════╣ +║ Foo │ Bar ║ +╟──────┼────────╢ +║ Kit │ Kat ║ +╚══════╧════════╝ +``` + +**AsciiTableFormat** + +`System.out.println(FlipTable.of(new AsciiTableFormat(), headers, data));` + +``` ++======+========+ +| Test | Header | +|======|========| +| Foo | Bar | +|------|--------| +| Kit | Kat | ++------+--------+ +``` Download diff --git a/src/main/java/com/jakewharton/fliptables/AsciiTableFormat.java b/src/main/java/com/jakewharton/fliptables/AsciiTableFormat.java new file mode 100644 index 0000000..ac55e49 --- /dev/null +++ b/src/main/java/com/jakewharton/fliptables/AsciiTableFormat.java @@ -0,0 +1,69 @@ +/* + * Copyright 2014 Jake Wharton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */package com.jakewharton.fliptables; + +public class AsciiTableFormat implements TableFormat { + + @Override + public String getHeaderRowTopChars() { + return "+=+=+"; + } + + @Override + public String getHeaderRowBottomChars() { + return "|=|=|"; + } + + @Override + public String getHeaderRowNoDataBottomChars() { + return "|=|=|"; + } + + @Override + public String getFooterRowBottomChars() { + return "+-+-+"; + } + + @Override + public String getFooterRowNoDataBottomChars() { + return "+-+-+"; + } + + @Override + public String getDataRowDividerChars() { + return "|-|-|"; + } + + @Override + public String getRowStartChars() { + return "|"; + } + + @Override + public String getRowEndChars() { + return "|"; + } + + @Override + public String getColumnSeparatorChars() { + return "|"; + } + + @Override + public String getRowEndTerminatorChars() { + return "\n"; + } + +} diff --git a/src/main/java/com/jakewharton/fliptables/DefaultTableFormat.java b/src/main/java/com/jakewharton/fliptables/DefaultTableFormat.java new file mode 100644 index 0000000..378771e --- /dev/null +++ b/src/main/java/com/jakewharton/fliptables/DefaultTableFormat.java @@ -0,0 +1,70 @@ +/* + * Copyright 2014 Jake Wharton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jakewharton.fliptables; + +public class DefaultTableFormat implements TableFormat { + + @Override + public String getHeaderRowTopChars() { + return "╔═╤═╗"; + } + + @Override + public String getHeaderRowBottomChars() { + return "╠═╪═╣"; + } + + @Override + public String getHeaderRowNoDataBottomChars() { + return "╠═╧═╣"; + } + + @Override + public String getFooterRowBottomChars() { + return "╚═╧═╝"; + } + + @Override + public String getFooterRowNoDataBottomChars() { + return "╚═══╝"; + } + + @Override + public String getDataRowDividerChars() { + return "╟─┼─╢"; + } + + @Override + public String getRowStartChars() { + return "║"; + } + + @Override + public String getRowEndChars() { + return "║"; + } + + @Override + public String getColumnSeparatorChars() { + return "│"; + } + + @Override + public String getRowEndTerminatorChars() { + return "\n"; + } + +} diff --git a/src/main/java/com/jakewharton/fliptables/FlipTable.java b/src/main/java/com/jakewharton/fliptables/FlipTable.java index c2021f0..888e159 100644 --- a/src/main/java/com/jakewharton/fliptables/FlipTable.java +++ b/src/main/java/com/jakewharton/fliptables/FlipTable.java @@ -29,10 +29,15 @@ public final class FlipTable { /** Create a new table with the specified headers and row data. */ public static String of(String[] headers, String[][] data) { + return of(new DefaultTableFormat(), headers, data); + } + + /** Create a new table with the specified TableFormat, headers and row data. */ + public static String of(TableFormat tableFormat, String[] headers, String[][] data) { if (headers == null) throw new NullPointerException("headers == null"); if (headers.length == 0) throw new IllegalArgumentException("Headers must not be empty."); if (data == null) throw new NullPointerException("data == null"); - return new FlipTable(headers, data).toString(); + return new FlipTable(tableFormat, headers, data).toString(); } private final String[] headers; @@ -40,8 +45,11 @@ public static String of(String[] headers, String[][] data) { private final int columns; private final int[] columnWidths; private final int emptyWidth; + private TableFormat tableFormat = null; - private FlipTable(String[] headers, String[][] data) { + private FlipTable(TableFormat tableFormat, String[] headers, String[][] data) { + + this.tableFormat = tableFormat; this.headers = headers; this.data = data; @@ -73,18 +81,20 @@ private FlipTable(String[] headers, String[][] data) { @Override public String toString() { StringBuilder builder = new StringBuilder(); - printDivider(builder, "╔═╤═╗"); + printDivider(builder, tableFormat.getHeaderRowTopChars()); printData(builder, headers); if (data.length == 0) { - printDivider(builder, "╠═╧═╣"); - builder.append('║').append(pad(emptyWidth, EMPTY)).append("║\n"); - printDivider(builder, "╚═══╝"); + printDivider(builder, tableFormat.getHeaderRowNoDataBottomChars()); + builder.append(tableFormat.getRowStartChars()) + .append(pad(emptyWidth, EMPTY)) + .append(tableFormat.getRowEndChars() + tableFormat.getRowEndTerminatorChars()); + printDivider(builder, tableFormat.getFooterRowNoDataBottomChars()); } else { for (int row = 0; row < data.length; row++) { - printDivider(builder, row == 0 ? "╠═╪═╣" : "╟─┼─╢"); + printDivider(builder, row == 0 ? tableFormat.getHeaderRowBottomChars() : tableFormat.getDataRowDividerChars()); printData(builder, data[row]); } - printDivider(builder, "╚═╧═╝"); + printDivider(builder, tableFormat.getFooterRowBottomChars()); } return builder.toString(); } @@ -94,19 +104,19 @@ private void printDivider(StringBuilder out, String format) { out.append(column == 0 ? format.charAt(0) : format.charAt(2)); out.append(pad(columnWidths[column], "").replace(' ', format.charAt(1))); } - out.append(format.charAt(4)).append('\n'); + out.append(format.charAt(4)).append(tableFormat.getRowEndTerminatorChars()); } private void printData(StringBuilder out, String[] data) { for (int line = 0, lines = 1; line < lines; line++) { for (int column = 0; column < columns; column++) { - out.append(column == 0 ? '║' : '│'); + out.append(column == 0 ? tableFormat.getRowStartChars() : tableFormat.getColumnSeparatorChars()); String[] cellLines = data[column].split("\\n"); lines = Math.max(lines, cellLines.length); String cellLine = line < cellLines.length ? cellLines[line] : ""; out.append(pad(columnWidths[column], cellLine)); } - out.append("║\n"); + out.append(tableFormat.getRowEndChars() + tableFormat.getRowEndTerminatorChars()); } } diff --git a/src/main/java/com/jakewharton/fliptables/TableFormat.java b/src/main/java/com/jakewharton/fliptables/TableFormat.java new file mode 100644 index 0000000..7a13041 --- /dev/null +++ b/src/main/java/com/jakewharton/fliptables/TableFormat.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Jake Wharton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jakewharton.fliptables; + +public interface TableFormat { + + public String getHeaderRowTopChars(); + public String getHeaderRowBottomChars(); + public String getHeaderRowNoDataBottomChars(); + + public String getFooterRowBottomChars(); + public String getFooterRowNoDataBottomChars(); + + public String getDataRowDividerChars(); + + public String getRowStartChars(); + public String getRowEndChars(); + public String getColumnSeparatorChars(); + + public String getRowEndTerminatorChars(); +} diff --git a/src/test/java/com/jakewharton/fliptables/FlipTableTest.java b/src/test/java/com/jakewharton/fliptables/FlipTableTest.java index efb54cc..fa0bc3c 100644 --- a/src/test/java/com/jakewharton/fliptables/FlipTableTest.java +++ b/src/test/java/com/jakewharton/fliptables/FlipTableTest.java @@ -92,6 +92,27 @@ public class FlipTableTest { + "╚══════╧════════╝\n"; assertThat(FlipTable.of(headers, data)).isEqualTo(expected); } + + + @Test public void simpleAscii() { + String[] headers = { "Hi", "Header" }; + String[][] data = { // + { "Foo", "Bar" }, // + { "Kit", "Kat" }, // + { "Ping", "Pong" }, // + }; + String expected = "" + + "+======+========+\n" + + "| Hi | Header |\n" + + "|======|========|\n" + + "| Foo | Bar |\n" + + "|------|--------|\n" + + "| Kit | Kat |\n" + + "|------|--------|\n" + + "| Ping | Pong |\n" + + "+------+--------+\n"; + assertThat(FlipTable.of(new AsciiTableFormat(), headers, data)).isEqualTo(expected); + } @Test public void dataNewlineFirstLineLong() { String[] headers = { "One", "Two" };