@@ -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}
0 commit comments