Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility method to get table rows as Collection #10

Open
sibiarunachalam opened this issue Aug 15, 2022 · 6 comments
Open

Add a utility method to get table rows as Collection #10

sibiarunachalam opened this issue Aug 15, 2022 · 6 comments

Comments

@sibiarunachalam
Copy link

sibiarunachalam commented Aug 15, 2022

It would be flexible if you provide a utility method that simply returns collection of rows in the table.
To say exactly the result of below method.
private static List<String> getTableRows(int[] colWidths, HorizontalAlign[] headerAligns, HorizontalAlign[] dataAligns, HorizontalAlign[] footerAligns, Character[] borderChars, String[] header, String[][] data, String[] footer) {

@sibiarunachalam
Copy link
Author

sibiarunachalam commented Aug 15, 2022

These two utility methods are required

1. public static <T> List<String> getTableRows(Character[] borderChars, Collection<T> objects, List<ColumnData<T>> columns)
2. public static List<String> getTableRows(Character[] borderChars, Column[] rawColumns, Object[][] data)

@sibiarunachalam
Copy link
Author

Hi @freva , could you review my PR #11 ?

@freva
Copy link
Owner

freva commented Aug 15, 2022

@sibiarunachalam Not sure how much utility these methods provide when you can achieve the same effect in 1 line by creating the table normally and then splitting the result by new line? E.g.:

String table = AsciiTable.getTable(planets, Arrays.asList(
        new Column().with(planet -> Integer.toString(planet.num)),
        new Column().header("Name").with(planet -> planet.name),
        new Column().header("Diameter").with(planet -> String.format(Locale.US, "%.03f", planet.diameter)),
        new Column().header("Mass").with(planet -> String.format(Locale.US, "%.02f", planet.mass)),
        new Column().header("Atmosphere").maxColumnWidth(8).with(planet -> planet.atmosphere)));

List<String> rows = Stream.of(table.split(System.lineSeparator())).collect(Collectors.toList());

Or just Java 11:

List<String> rows = List.of(table.split(System.lineSeparator()));

@sibiarunachalam
Copy link
Author

sibiarunachalam commented Aug 16, 2022

Yes, @freva this workaround will solve the problem.
But, my points to have utility methods:

  1. In my case, I split the table into rows for some additional processing and merge them again as table. This util methods will remove these two steps (reverse processing) split and merge.
  2. Theses two steps are unnecessary overheads in Ascii Table construction especially when few hundred thousands of records involved
  3. It may cause problem when the data has line/row separator
  4. Other issue Custom line/row separator #9 need not be addressed immediately, but that would be nice to have feature.
  5. Library provides options to the developers (shouldn't enforce them to implement in one way that they consider as not good practice for many reasons)
  6. Even I am not sure about how much utility it provides, but ease of use.

Let say if we had those utility method, which approach would you prefer in my scenario?

@freva
Copy link
Owner

freva commented Aug 16, 2022

Since ASCII tables is inherently for humans and not machines, I don't think/hope anyone would use this for anything more than a thousand rows, which makes any performance issue coming from the reverse processing negligible.

The library supports linebreaks in the data, feel free to try the split on this example: https://github.com/freva/ascii-table/blob/d19f522aaccd4a0e057f9843b7d722957c595559/src/test/java/com/github/freva/asciitable/AsciiTableTest.java#L320:L363

To make a utility method worthwhile, it should either solve a non-trivial problem or solve a frequent problem to the users. In my opinion, the solution/workaround is trivial, so it's not that. And I'm not convinced this is a frequent problem, how often would someone need to post-process something a table, specifically on row level? Maybe if this output all the cells, without borders, I could see it, because then the developers could actually customize a lot. With pure rows (including borders), all you can do is join it back together, or filter/insert some rows.

@sibiarunachalam
Copy link
Author

Okay @freva, thanks for your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants