Skip to content

Commit

Permalink
Empty tables threw an exception because left/top border was then not …
Browse files Browse the repository at this point in the history
…included.
  • Loading branch information
pkriens committed Jan 8, 2025
1 parent 2e3ae6c commit a572612
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public interface Cell {
*
* @param w
* @param h
* @return
*/
default Canvas render(int w, int h) {
Canvas canvas = new Canvas(w, h);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,30 @@ public Table(int rows, int cols, int headers) {
*/
@Override
public int width() {
if (isEmpty())
return 3;

int w = 0;
for (int c = 0; c < cols; c++) {
w += width(c) - 1; // remove right border width because we overlap
}
return w + 1; // add right border
}

private boolean isEmpty() {
return cells.length == 0;
}

/**
* Height including borders
*/
@Override
public int height() {
if (isEmpty())
return 3;
int h = 0;
for (int r = 0; r < rows; r++) {
h += height(r) - 1;// remove bottom border width because we overlap
h += height(r) - 1;// remove bottom border height because we overlap
}
return h + 1; // add bottom border
}
Expand All @@ -79,6 +88,9 @@ public Cell[] row(int row) {

@Override
public String toString() {
if (isEmpty()) {
return "∅";
}
if (cols == 1 && rows > 3)
return transpose(0).toString("⁻¹");
else
Expand All @@ -95,25 +107,27 @@ public Canvas render(int width, int height, int left, int top, int right, int bo
Canvas canvas = new Canvas(width + left + right, height + top + bottom);
canvas.box(left, top, width, height, style);

int y = top;

for (int r = 0; r < rows; r++) {
int ch = height(r);
int x = left;
for (int c = 0; c < cols; c++) {
int cw;
if (c == cols - 1) {
// adjust last column width
cw = width - x - left;
} else {
cw = width(c);
if (!isEmpty()) {
int y = top;

for (int r = 0; r < rows; r++) {
int ch = height(r);
int x = left;
for (int c = 0; c < cols; c++) {
int cw;
if (c == cols - 1) {
// adjust last column width
cw = width - x - left;
} else {
cw = width(c);
}
Cell cell = cells[r][c];
Canvas foo = cell.render(cw, ch);
canvas.merge(foo, x, y);
x += cw - 1;
}
Cell cell = cells[r][c];
Canvas foo = cell.render(cw, ch);
canvas.merge(foo, x, y);
x += cw - 1;
y += ch - 1;
}
y += ch - 1;
}
return canvas;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.alloytools.util.table;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
Expand All @@ -20,6 +21,20 @@ public void testEmptyTable() {

}

@Test
public void testEmptyTable2() {
Table empty = new Table(0, 0, 0);
Table t2 = new Table(1, 1, 0);
t2.set(0, 0, empty);
String render = t2.toString();
assertThat(render).isEqualTo("""
┌─┐
│ │
└─┘
""");
System.out.println(render);
}


@Test
public void testSimpleTable() {
Expand Down

0 comments on commit a572612

Please sign in to comment.