Nukesor / comfy-table

:large_orange_diamond: Build beautiful terminal tables with automatic content wrapping
MIT License
976 stars 31 forks source link

Reimagine column handling #144

Open Nukesor opened 8 months ago

Nukesor commented 8 months ago

The current column handling is rather convenient, as columns get populated as rows and cells are added to the table.

However, this logic is rather implicit. There's no way to remove or add columns half-way through a table creation process.

To fix this

I'm actually in favor of the second approach, since the goal of comfy-table is to be minimalistic and convenient to use. Explicit column handling is more of an edge-case and the 99% use case is covered by implicit column creation.

However, it could be very tricky to design the API around an optionally explicit column handling, since such APIs tend to get confusing quite quickly.

This ticket is mostly for discussion on how such an API would look like.

Relevant tickets/MRs: https://github.com/Nukesor/comfy-table/pull/121 Previous ticket: https://github.com/Nukesor/comfy-table/issues/124

Nukesor commented 8 months ago

I gave this a lot of thought and here's what I came up with.

Current Problem

The current implementation doesn't allow:

Design Proposal

Since this is about rethinking how the API would work and since this change will be breaking anyway, let's use this opportunity and do it properly. My proposal for a new API design would look like this.

Column improvements

Accessing columns is currently annoying and error prone. One needs to know the index of the column, which isn't ergonomic at all. My proposal would be to move more logic into the column, which is why we need to make it more accessible.

Adding columns

Removing columns

I found no good way of actually removing columns. The problem with column removal is, that it needs to implicitely remove the respective cells of all current rows as well. Without that, we wouldn't know which cells to display or not.

As the whole idea of this request is to remove implicit logic, I decided to forbid removal of columns. Instead of removing columns, it should be made more convenient to hide them, which is basically the same as a temporary "soft" removal.

Hiding columns

Currently, the functionality to hide a column is implemented as a Constraint, which is a bit dirty. It's not necessarily a constraint and having it as a constraint toggling the column in a program annoying, since the old constraint needs to be restored as soon as the column is made visible again.

Error handling

Previously, comfy-table was able to do things in an "optimistic" manner. It was just given some strings and from there on it tried to display things to the best of its abilities.

With the trend to giving developers more power, we also have to introduce error handling.

Requested Feature: Removing rows/swapping columns

There were two additional requested features:

With the current builder design, these features won't be implemented, as this would only lead to confusion. It would allow scenarios like this:

- Add some rows
- Swap some column positions
- Add some new rows in the original layout
-> The columns of the new rows are now in the wrong order.

There're two solutions to this problem:

  1. Make rows named as well.

    • We'll no longer use Vecs and only HashMaps instead.

    This solution makes the whole library harder to use for a very small userbase.

  2. Actually use an builder pattern.

    • Use a TableBuilder in which one may specify columns, rows and their content.
    • call builder.build(), which returns a Table.
    • That Table now only allows styling related actions, content may no longer be added/removed etc.

      This solution doesn't really solve the problem, as people also want to add content to the table after they've already built it once.

To be honest, I'm inclined to tell people that they should just rebuild the whole table if they want complex interactions. \ Comfy-table is designed to be a minimalistic table builder library and shouldn't be used as an interactive table renderer.

-> Until there's a good design proposal that doesn't make the library less usable/unusable for the vast majority of its users, these two features won't be added.

Who builds this?

I am very happy with the current state of comfy-table and I don't need any of said functionality myself. Even though I designed and specified the new architecture, I won't implement it. Also, if you find any design flaws, please point them out and let's try find a better solution.

As usual, I'll give any MRs thorough reviews as long as they're well documented and properly tested (via actual tests).

So, if anyone needs this and has some spare time at their hand, I would be happy to see this merged into comfy-table :).