Fix Vertical aligner when rows are empty
Fixes https://github.com/spring-projects/spring-shell/issues/107
This commit is contained in:
@@ -46,7 +46,9 @@ public class DelimiterTextWrapper implements TextWrapper {
|
||||
result.add(String.format("%-" + columnWidth + "s", toAdd));
|
||||
line = line.substring(split == -1 ? columnWidth : split + 1);
|
||||
}
|
||||
result.add(String.format("%-" + columnWidth + "s", line)); // right pad if necessary
|
||||
if (columnWidth > 0) {
|
||||
result.add(String.format("%-" + columnWidth + "s", line)); // right pad if necessary
|
||||
}
|
||||
}
|
||||
return result.toArray(new String[result.size()]);
|
||||
}
|
||||
|
||||
@@ -32,20 +32,25 @@ public enum SimpleVerticalAligner implements Aligner {
|
||||
String[] result = new String[cellHeight];
|
||||
int blanksBefore = 0;
|
||||
int blanksAfter = 0;
|
||||
boolean atLeastOneNonEmptyRow = false;
|
||||
for (int row = 0; row < text.length; row++) {
|
||||
if (text[row] == null || text[row].trim().equals("")) {
|
||||
blanksBefore++;
|
||||
}
|
||||
else {
|
||||
atLeastOneNonEmptyRow = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int row = text.length - 1; row >= 0; row--) {
|
||||
if (text[row] == null || text[row].trim().equals("")) {
|
||||
blanksAfter++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
// In case of full blank, don't count blank rows twice
|
||||
if (atLeastOneNonEmptyRow) {
|
||||
for (int row = text.length - 1; row >= 0; row--) {
|
||||
if (text[row] == null || text[row].trim().equals("")) {
|
||||
blanksAfter++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
String filler = spaces(cellWidth);
|
||||
|
||||
@@ -90,4 +90,12 @@ public class TableTest extends AbstractTestWithSample {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void testEmptyCellsVerticalAligner() {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"a", "b"}, {null, null}});
|
||||
Table table = new TableBuilder(model).on(CellMatchers.table()).addAligner(SimpleVerticalAligner.middle).build();
|
||||
String result = table.render(3);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user