Setting table width

The width of a table can be set via its context. The following example creates a table and then sets the width and shows the output. The first width is 50 (line 8), the second 40 (line 11), and the third 30 characters (line 14).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("row 1 col 1", "row 1 col 2");
at.addRule();
at.addRow("row 2 col 1", "row 2 col 2");
at.addRule();

at.getContext().setWidth(50);
System.out.println(at.render());

at.getContext().setWidth(40);
System.out.println(at.render());

at.getContext().setWidth(30);
System.out.println(at.render());

The first output is for a width of 50:

┌────────────────────────┬───────────────────────┐
│row 1 col 1             │row 1 col 2            │
├────────────────────────┼───────────────────────┤
│row 2 col 1             │row 2 col 2            │
└────────────────────────┴───────────────────────┘

The second output is for a width of 40:

┌───────────────────┬──────────────────┐
│row 1 col 1        │row 1 col 2       │
├───────────────────┼──────────────────┤
│row 2 col 1        │row 2 col 2       │
└───────────────────┴──────────────────┘

The third output is for a width of 30:

┌──────────────┬─────────────┐
│row 1 col 1   │row 1 col 2  │
├──────────────┼─────────────┤
│row 2 col 1   │row 2 col 2  │
└──────────────┴─────────────┘