Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ target

pom.xml.*
release.properties

.classpath
.project
.settings
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)*
----------------------------

Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/jakewharton/fliptables/AsciiTableFormat.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
70 changes: 70 additions & 0 deletions src/main/java/com/jakewharton/fliptables/DefaultTableFormat.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
32 changes: 21 additions & 11 deletions src/main/java/com/jakewharton/fliptables/FlipTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,27 @@ 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;
private final 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;

Expand Down Expand Up @@ -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();
}
Expand All @@ -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());
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/jakewharton/fliptables/TableFormat.java
Original file line number Diff line number Diff line change
@@ -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();
}
21 changes: 21 additions & 0 deletions src/test/java/com/jakewharton/fliptables/FlipTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand Down