Skip to content

Commit bafdab4

Browse files
Merge pull request MitchTalmadge#15 from sebkur/add-align-support
Add align support
2 parents 1520617 + bb01cc3 commit bafdab4

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

src/main/java/com/mitchtalmadge/asciidata/table/ASCIITable.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ASCIITable {
1717
private final String[][] data;
1818
private final int columnsCount;
1919
private final int[] columnWidths;
20+
private Align[] aligns;
2021
private final int emptyWidth;
2122
private final String emptyMessage = "(empty)";
2223
private String nullValue = "";
@@ -83,6 +84,11 @@ private ASCIITable(String[] headers, String[][] data) {
8384
}
8485
}
8586

87+
aligns = new Align[columnsCount];
88+
for (int i = 0; i < columnsCount; i++) {
89+
aligns[i] = Align.LEFT;
90+
}
91+
8692
// Determine the width of everything including borders.
8793
// This is to be used in case there is no data and we must write the empty message to the table.
8894

@@ -122,6 +128,11 @@ public ASCIITable withNullValue(String nullValue) {
122128
return this;
123129
}
124130

131+
public ASCIITable alignColumn(int column, Align align) {
132+
this.aligns[column] = align;
133+
return this;
134+
}
135+
125136
@Override
126137
public String toString() {
127138
StringBuilder output = new StringBuilder();
@@ -134,7 +145,7 @@ public String toString() {
134145
tableFormat.getTopRightCorner());
135146

136147
// Append the headers of the table.
137-
appendRow(output, headers);
148+
appendRow(output, headers, true);
138149

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

147158
// Empty message row
148159
output.append(tableFormat.getVerticalBorderFill(true))
149-
.append(pad(emptyWidth, emptyMessage))
160+
.append(pad(emptyWidth, Align.LEFT, emptyMessage))
150161
.append(tableFormat.getVerticalBorderFill(true))
151162
.append('\n');
152163

@@ -178,7 +189,7 @@ public String toString() {
178189
tableFormat.getRightEdgeBorderDivider(false));
179190

180191
// Append the data for the current row.
181-
appendRow(output, data[row]);
192+
appendRow(output, data[row], false);
182193
}
183194

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

226237
// Pad and append the data.
227-
output.append(pad(columnWidths[column], cellLine));
238+
Align align = Align.LEFT;
239+
if (!isHeader) {
240+
align = aligns[column];
241+
}
242+
output.append(pad(columnWidths[column], align, cellLine));
228243
}
229244

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

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

263278
// Add the right border
@@ -271,8 +286,12 @@ private void appendHorizontalDivider(StringBuilder output, char left, char fill,
271286
* @param data The data to pad.
272287
* @return The data, padded with spaces to the given width.
273288
*/
274-
private static String pad(int width, String data) {
275-
return String.format(" %1$-" + width + "s ", data);
289+
private static String pad(int width, Align align, String data) {
290+
if (align == null || align == Align.LEFT) {
291+
return String.format(" %1$-" + width + "s ", data);
292+
} else {
293+
return String.format(" %1$" + width + "s ", data);
294+
}
276295
}
277296

278297
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mitchtalmadge.asciidata.table;
2+
3+
public enum Align {
4+
5+
LEFT,
6+
RIGHT
7+
8+
}

src/test/java/com/mitchtalmadge/asciidata/table/ASCIITableTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ public void testSimpleTable() throws IOException {
3636

3737
}
3838

39+
/**
40+
* Tests a simple table with example data and a right-aligned column.
41+
*/
42+
@Test
43+
public void testSimpleTableRightAlign() throws IOException {
44+
45+
String[] headers = new String[]{"ID", "Name", "Email"};
46+
String[][] data = new String[][]{
47+
{"123", "Alfred Alan", "aalan@gmail.com"},
48+
{"223", "Alison Smart", "asmart@gmail.com"},
49+
{"256", "Ben Bessel", "benb@outlook.com"},
50+
{"374", "John Roberts", "johnrob@company.com"},
51+
};
52+
53+
assertEquals(
54+
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/utf8/simpleTableRight.txt")),
55+
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).alignColumn(2, Align.RIGHT).toString())
56+
);
57+
// ASCII Table Format
58+
assertEquals(
59+
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/ascii/simpleTableRight.txt")),
60+
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).alignColumn(2, Align.RIGHT).withTableFormat(new ASCIITableFormat()).toString())
61+
);
62+
63+
}
3964

4065
/**
4166
* Tests tables with no data.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
+=====+==============+=====================+
2+
| ID | Name | Email |
3+
|=====|==============|=====================|
4+
| 123 | Alfred Alan | aalan@gmail.com |
5+
|-----|--------------|---------------------|
6+
| 223 | Alison Smart | asmart@gmail.com |
7+
|-----|--------------|---------------------|
8+
| 256 | Ben Bessel | benb@outlook.com |
9+
|-----|--------------|---------------------|
10+
| 374 | John Roberts | johnrob@company.com |
11+
+=====+==============+=====================+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
╔═════╤══════════════╤═════════════════════╗
2+
║ ID │ Name │ Email ║
3+
╠═════╪══════════════╪═════════════════════╣
4+
║ 123 │ Alfred Alan │ aalan@gmail.com ║
5+
╟─────┼──────────────┼─────────────────────╢
6+
║ 223 │ Alison Smart │ asmart@gmail.com ║
7+
╟─────┼──────────────┼─────────────────────╢
8+
║ 256 │ Ben Bessel │ benb@outlook.com ║
9+
╟─────┼──────────────┼─────────────────────╢
10+
║ 374 │ John Roberts │ johnrob@company.com ║
11+
╚═════╧══════════════╧═════════════════════╝

0 commit comments

Comments
 (0)