Fix Vertical aligner when rows are empty

Fixes https://github.com/spring-projects/spring-shell/issues/107
This commit is contained in:
Eric Bottard
2016-10-10 13:04:48 +02:00
parent 5d36787786
commit 71f9da5578
3 changed files with 23 additions and 8 deletions

View File

@@ -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()]);
}

View File

@@ -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);

View File

@@ -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);
}
}