Skip to content

Commit

Permalink
Merge pull request #15 from sebkur/add-align-support
Browse files Browse the repository at this point in the history
Add align support
  • Loading branch information
MitchTalmadge authored Mar 22, 2019
2 parents 1520617 + bb01cc3 commit bafdab4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/main/java/com/mitchtalmadge/asciidata/table/ASCIITable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ASCIITable {
private final String[][] data;
private final int columnsCount;
private final int[] columnWidths;
private Align[] aligns;
private final int emptyWidth;
private final String emptyMessage = "(empty)";
private String nullValue = "";
Expand Down Expand Up @@ -83,6 +84,11 @@ private ASCIITable(String[] headers, String[][] data) {
}
}

aligns = new Align[columnsCount];
for (int i = 0; i < columnsCount; i++) {
aligns[i] = Align.LEFT;
}

// Determine the width of everything including borders.
// This is to be used in case there is no data and we must write the empty message to the table.

Expand Down Expand Up @@ -122,6 +128,11 @@ public ASCIITable withNullValue(String nullValue) {
return this;
}

public ASCIITable alignColumn(int column, Align align) {
this.aligns[column] = align;
return this;
}

@Override
public String toString() {
StringBuilder output = new StringBuilder();
Expand All @@ -134,7 +145,7 @@ public String toString() {
tableFormat.getTopRightCorner());

// Append the headers of the table.
appendRow(output, headers);
appendRow(output, headers, true);

// Check if the data is empty, in which case, we will only write the empty message into the table contents.
if (data.length == 0) {
Expand All @@ -146,7 +157,7 @@ public String toString() {

// Empty message row
output.append(tableFormat.getVerticalBorderFill(true))
.append(pad(emptyWidth, emptyMessage))
.append(pad(emptyWidth, Align.LEFT, emptyMessage))
.append(tableFormat.getVerticalBorderFill(true))
.append('\n');

Expand Down Expand Up @@ -178,7 +189,7 @@ public String toString() {
tableFormat.getRightEdgeBorderDivider(false));

// Append the data for the current row.
appendRow(output, data[row]);
appendRow(output, data[row], false);
}

// Horizontal divider at the bottom of the table.
Expand All @@ -197,7 +208,7 @@ public String toString() {
* @param output The output to append to.
* @param data The data of the row to append. Each index corresponds to a column.
*/
private void appendRow(StringBuilder output, String[] data) {
private void appendRow(StringBuilder output, String[] data, boolean isHeader) {
// Step 1: Determine the row height from the maximum number of lines out of each cell.
int rowHeight = 0;
for (int column = 0; column < columnsCount; column++) {
Expand All @@ -224,7 +235,11 @@ private void appendRow(StringBuilder output, String[] data) {
String cellLine = line < cellLines.length ? cellLines[line] : "";

// Pad and append the data.
output.append(pad(columnWidths[column], cellLine));
Align align = Align.LEFT;
if (!isHeader) {
align = aligns[column];
}
output.append(pad(columnWidths[column], align, cellLine));
}

// Add the right border.
Expand Down Expand Up @@ -257,7 +272,7 @@ private void appendHorizontalDivider(StringBuilder output, char left, char fill,
output.append(column == 0 ? left : middle);

// For the contents of the column, create a padding of the correct width and replace it with the fill border.
output.append(pad(columnWidths[column], "").replace(' ', fill));
output.append(pad(columnWidths[column], Align.LEFT, "").replace(' ', fill));
}

// Add the right border
Expand All @@ -271,8 +286,12 @@ private void appendHorizontalDivider(StringBuilder output, char left, char fill,
* @param data The data to pad.
* @return The data, padded with spaces to the given width.
*/
private static String pad(int width, String data) {
return String.format(" %1$-" + width + "s ", data);
private static String pad(int width, Align align, String data) {
if (align == null || align == Align.LEFT) {
return String.format(" %1$-" + width + "s ", data);
} else {
return String.format(" %1$" + width + "s ", data);
}
}

}
8 changes: 8 additions & 0 deletions src/main/java/com/mitchtalmadge/asciidata/table/Align.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mitchtalmadge.asciidata.table;

public enum Align {

LEFT,
RIGHT

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ public void testSimpleTable() throws IOException {

}

/**
* Tests a simple table with example data and a right-aligned column.
*/
@Test
public void testSimpleTableRightAlign() throws IOException {

String[] headers = new String[]{"ID", "Name", "Email"};
String[][] data = new String[][]{
{"123", "Alfred Alan", "[email protected]"},
{"223", "Alison Smart", "[email protected]"},
{"256", "Ben Bessel", "[email protected]"},
{"374", "John Roberts", "[email protected]"},
};

assertEquals(
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/utf8/simpleTableRight.txt")),
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).alignColumn(2, Align.RIGHT).toString())
);
// ASCII Table Format
assertEquals(
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/ascii/simpleTableRight.txt")),
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).alignColumn(2, Align.RIGHT).withTableFormat(new ASCIITableFormat()).toString())
);

}

/**
* Tests tables with no data.
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/tables/ascii/simpleTableRight.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
+=====+==============+=====================+
| ID | Name | Email |
|=====|==============|=====================|
| 123 | Alfred Alan | [email protected] |
|-----|--------------|---------------------|
| 223 | Alison Smart | [email protected] |
|-----|--------------|---------------------|
| 256 | Ben Bessel | [email protected] |
|-----|--------------|---------------------|
| 374 | John Roberts | [email protected] |
+=====+==============+=====================+
11 changes: 11 additions & 0 deletions src/test/resources/tables/utf8/simpleTableRight.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
╔═════╤══════════════╤═════════════════════╗
║ ID │ Name │ Email ║
╠═════╪══════════════╪═════════════════════╣
║ 123 │ Alfred Alan │ [email protected]
╟─────┼──────────────┼─────────────────────╢
║ 223 │ Alison Smart │ [email protected]
╟─────┼──────────────┼─────────────────────╢
║ 256 │ Ben Bessel │ [email protected]
╟─────┼──────────────┼─────────────────────╢
║ 374 │ John Roberts │ [email protected]
╚═════╧══════════════╧═════════════════════╝

0 comments on commit bafdab4

Please sign in to comment.