ascii-table icon indicating copy to clipboard operation
ascii-table copied to clipboard

Can span columns be implemented?

Open sibiarunachalam opened this issue 2 years ago • 4 comments

It works well for most of my requirements. Is it possible to implement span columns using this library?

sibiarunachalam avatar Aug 05 '22 05:08 sibiarunachalam

It's not currently possible. Do you want to span header/footer columns only or also some body columns?

freva avatar Aug 05 '22 21:08 freva

Hi @freva, thanks for your feedback. Both header and body columns require span column feature.

sibiarunachalam avatar Aug 08 '22 01:08 sibiarunachalam

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)));

freva avatar Aug 08 '22 22:08 freva

Thanks @freva for your feedback, I will look at at.

sibiarunachalam avatar Aug 11 '22 06:08 sibiarunachalam