Width: Longest Word

This width calculator takes the longest word in each column and sets the column width to it.

The following code shows a few examples for the behavior of this width calculator for a table with 2 columns:

  • First example: no padding, longes line determines the column width

  • Second example: table with a left/right padding of 1

  • Third example: cells with individual padding, chaning the longest word

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
AsciiTable at = new AsciiTable();
at.addRule();
AT_Row row1 = at.addRow("first", "information");
at.addRule();
AT_Row row2 = at.addRow("second", "info");
at.addRule();

at.getRenderer().setCWC(new CWC_LongestWord());
System.out.println(at.render());

at.setPaddingLeftRight(1);
System.out.println(at.render());

at.setPaddingLeftRight(0);
row1.getCells().get(0).getContext().setPaddingLeftRight(2);
row1.getCells().get(1).getContext().setPaddingLeftRight(3);
row2.getCells().get(0).getContext().setPaddingLeftRight(3);
row2.getCells().get(1).getContext().setPaddingLeftRight(4);
System.out.println(at.render());

The resulting output for the first example:

┌──────┬───────────┐
│first │information│
├──────┼───────────┤
│second│info       │
└──────┴───────────┘

The resulting output for the second example:

┌────────┬─────────────┐
│ first  │ information │
├────────┼─────────────┤
│ second │ info        │
└────────┴─────────────┘

The resulting output for the third example:

┌────────────┬─────────────────┐
│  first     │   information   │
├────────────┼─────────────────┤
│   second   │    info         │
└────────────┴─────────────────┘