Skip to content

Commit

Permalink
Support TSV format in @table output
Browse files Browse the repository at this point in the history
  • Loading branch information
dblevins committed Feb 3, 2024
1 parent f6b388b commit 3386bba
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,15 @@ public enum Border {
* </pre>
*/
whitespaceSeparated,

/**
* Example:
* <pre>
* id[TAB]project[TAB]releaseDate[TAB]version
* 9[TAB]Apache TomEE[TAB]A2016-05-17[TAB]A7.0.x
* 523456789[TAB]ATomcat[TAB]A2018-01-17[TAB]A9.0.x
* 14[TAB]AApache ActiveMQ Classic[TAB]A2022-03-09[TAB]A5.17.x
* </pre>
*/
tsv,
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ public static Border.Builder unicodeSingleSeparated() {
;
}

public static Border.Builder tsv() {
return builder()
.first(null)
.header(null)
.inner(null)
.row(Line.builder().left("").inner("\t").right("").padded(false))
.last(null)
;
}

public static Builder builder() {
return new Builder();
}
Expand Down
18 changes: 16 additions & 2 deletions tomitribe-crest/src/main/java/org/tomitribe/crest/table/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public class Line {
private final String inner;
private final String middle;

public Line(final String left, final String right, final String inner, final String middle) {
private final boolean padded;

public Line(final String left, final String right, final String inner, final String middle, final boolean padded) {
this.left = left;
this.right = right;
this.inner = inner;
this.middle = middle;
this.padded = padded;
}

public String getLeft() {
Expand All @@ -45,6 +48,10 @@ public String getInner() {
return inner;
}

public boolean isPadded() {
return padded;
}

public static Builder builder() {
return new Builder();
}
Expand All @@ -54,6 +61,7 @@ public static final class Builder {
private String right;
private String inner;
private String middle;
private boolean padded = true;

private Builder() {
}
Expand Down Expand Up @@ -87,8 +95,14 @@ public Builder middle(String middle) {
return this;
}

public Builder padded(final boolean padded) {
this.padded = padded;
return this;
}


public Line build() {
return new Line(left, right, inner, middle);
return new Line(left, right, inner, middle, padded);
}
}
}
38 changes: 29 additions & 9 deletions tomitribe-crest/src/main/java/org/tomitribe/crest/table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,25 @@ public void format(final PrintStream out) {
*/

if (data.hasHeading()) {
Stream.of(rows.remove(0))
.map(Data.Row::toLines)
.flatMap(Stream::of)
.map(this::center)
.forEach(printRow);

/*
* Some formats like tsv do not pad or center the heading
* Check to see if padding is enabled
*/
if (border.getHeader() != null && border.getHeader().isPadded()) {

Stream.of(rows.remove(0))
.map(Data.Row::toLines)
.flatMap(Stream::of)
.map(this::center) // this is the line that adds padding
.forEach(printRow);
} else {

Stream.of(rows.remove(0))
.map(Data.Row::toLines)
.flatMap(Stream::of)
.forEach(printRow);
}

if (border.getHeader() != null) out.println(getLine(border.getHeader()));
}
Expand Down Expand Up @@ -108,10 +122,16 @@ private String[] center(final String[] strings) {

public String getFormat(final Line line) {
final List<Data.Column> columns = this.data.getColumns();
final List<String> formats = columns.stream().map(column -> {
final int width = column.getWidth().getMax();
return column.isNumeric() ? "%" + width + "s" : "%-" + width + "s";
}).collect(Collectors.toList());
final List<String> formats;

if (line.isPadded()) {
formats = columns.stream().map(column -> {
final int width = column.getWidth().getMax();
return column.isNumeric() ? "%" + width + "s" : "%-" + width + "s";
}).collect(Collectors.toList());
} else {
formats = columns.stream().map(column -> "%s").collect(Collectors.toList());
}

return line.getLeft() +
Join.join(line.getInner(), formats) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ public void whitespaceSeparated() {
"This is a row with only one cell \n");
}

@Test
public void tsv() {
assertTable(Border::tsv, "" +
"Col1\tCol2\tCol3\tNumeric Column\n" +
"Value 1\tValue 2\t123\t10.0\n" +
"Separate\tcols\twith a tab or 4 spaces\t-2,027.1\n" +
"This is a row with only one cell\t\t\t\n");
}


public void assertTable(final Supplier<Border.Builder> border, final String expected) {
final Table table = new Table(data, border.get().build(), 300);
final String actual = table.format();
Expand Down

0 comments on commit 3386bba

Please sign in to comment.