Width: Longest Word (Maxnimum)

This width calculator takes the longest word in each column and sets the column width to it. It can be further configured to a maximum column width.

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

  • First example: maximum width set to 8 for all columns

  • Second example: maximum width of first column set to 4, maximum width of second column removed

1
2
3
4
5
6
7
8
9
10
11
12
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("first", "information");
at.addRule();
at.addRow("second", "info");
at.addRule();

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

at.getRenderer().setCWC(new CWC_LongestWordMax(new int[]{4,-1}));
System.out.println(at.render());

The resulting output for the first example:

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

The resulting output for the second example:

┌────┬───────────┐
│firs│information│
│t   │           │
├────┼───────────┤
│seco│info       │
│nd  │           │
└────┴───────────┘