ascii-table
ascii-table copied to clipboard
Can span columns be implemented?
It works well for most of my requirements. Is it possible to implement span columns using this library?
It's not currently possible. Do you want to span header/footer columns only or also some body columns?
Hi @freva, thanks for your feedback. Both header and body columns require span column feature.
I guess this is possible, but it's not trivial. This library takes in column definition and data separately, there is no way to control a single cell, so if this were implemented, you'd have to pass some sort of Function<T, Integer>
to ColumnData
which would return number of column spans for a given element, e.g.
List<Planet> planets = Arrays.asList(
new Planet(1, "Mercury", 0.382, 0.06, "minimal"),
new Planet(2, "Venus", 0.949, 0.82, "Carbon dioxide, Nitrogen"),
new Planet(3, "Earth", 1.0, 1.0, "Nitrogen, Oxygen, Argon"),
new Planet(4, "Mars", 0.532, 0.11, "Carbon dioxide, Nitrogen, Argon"));
System.out.println(AsciiTable.getTable(planets, Arrays.asList(
new Column().with(planet -> Integer.toString(planet.num)),
new Column().header("Name").with(planet -> planet.name).span(planet -> planet.name.equals("Earth") ? 3 : 1),
new Column().header("Diameter").with(planet -> String.format("%.03f", planet.diameter)),
new Column().header("Mass").with(planet -> String.format("%.02f", planet.mass)));
Thanks @freva for your feedback, I will look at at.