Skip to content

Expandable column#62

Closed
dmke wants to merge 4 commits intorivo:masterfrom
dmke:expandable-column
Closed

Expandable column#62
dmke wants to merge 4 commits intorivo:masterfrom
dmke:expandable-column

Conversation

@dmke
Copy link
Copy Markdown

@dmke dmke commented Feb 28, 2018

I've added a bit of code to mark a column as "expandable", i.e. to use the remaining horizontal space, effectively making any table a full-width table.

My use case is multiping, which should align the statistic column on the right, and the host information on the left (kind of like mtr)

expanded

For now, only one column can be made "expandable", but the API could later be upgraded to func (t *Table) SetExpandable(columns ...int) *Table to allow sharing the remaining space between multiple columns.

dmke added 4 commits March 1, 2018 20:11
This also fixes two bugs:

1. the background for selected cells/colums now matches the
   actual column
2. the remaining space was off by one, when the table has no
   border
@rivo
Copy link
Copy Markdown
Owner

rivo commented Mar 1, 2018

Thanks. I understand the request. It's similar to what is already available with the Grid class. Let me think about the best way to solve this.

I'd rather keep this in line with similar features in Grid and Flex where you can set a proportional width for multiple columns. Also, all column-based attributes in Table are derived from the TableCell class and I wouldn't want to make an exception for this case. (You suggested upgrading the SetExpandable() function in the future but I'm trying to avoid breaking changes so a good solution should be found now already.)

Give me some time and I'll figure out a solution to this.

@dmke
Copy link
Copy Markdown
Author

dmke commented Mar 1, 2018

I'd rather keep this in line with similar features in Grid and Flex where you can set a proportional width for multiple columns.

Understandable, I too wanted to keep the API similar to existing functionality, but I also wanted it to be simple and unintrusive. I'm open for suggestions though, and willing to reimplement this with another API.

You suggested upgrading the SetExpandable() function in the future but I'm trying to avoid breaking changes so a good solution should be found now already.

Yeah, my suggested upgrade would be backward-compatible. The API of this PR is

func (t *Table) SetExpandable(int) *Table

and the upgrade would be

func (t *Table) SetExpandable(...int) *Table

In both cases, the consumers would call

table := tview.NewTable().SetExpandable(1)

to mark the column with index 1 to be expandable.

The "upgrade" would then also allow the consumer to mark multiple columns expandable:

table := tview.NewTable().SetExpandable(1, 4)

I haven't come around implementing this yet, because the calculations for the remaining space are a bit fiddly, and easily introduce off-by-one errors, resulting in tables a character too wide or to narrow...

I'm also not sure how to handle cases where the remaining horizontal space is, say 3 characters, but you have more than 3 columns marked as expandable. The naïve solution might be to distribute an extra space to the first three columns—but that might not produce the visually most pleasing result. Imagine the first columns to be sparsely populated, i.e. they already have a "natural" spacing, while the other more densely populated columns would benefit more from extra whitespace...

Also, all column-based attributes in Table are derived from the TableCell class and I wouldn't want to make an exception for this case.

Hm. I don't know if expandability is a concern of a single TableCell. When moved to TableCell, the consumer would need to tell each cell to be expandable or not (this also introduces problems when cousumers add expandable cell in different columns).

Let me rephrase my goal: I need a way to tell the Table to use all of the horizontally available space. Then maybe the API could be something like this:

func (t *Table) SetFullWidth(enabled bool, columnWeights ...int) *Table

where enabled toggles the behaviour, and columnWeights define the Grid/Flex-like proportional widths? Expanding column 3 in a 5 column-table then would be something like

t.SetFullWidth(true, 1, 1, 100, 1, 1) // col 3 is at most 100 times as wide as cols 1,2,4,5

Give me some time and I'll figure out a solution to this.

Sure, no problem :-)

@rivo
Copy link
Copy Markdown
Owner

rivo commented Mar 2, 2018

The idea of putting it in TableCell is to have a value, e.g. Expansion which is 0 by default. The column value is the maximum over all cells in that column (so you really only need to define it once, e.g. in the table header). The value will be used as a proportional weight indicating how much horizontal space will be added to the column if the table is smaller than the available width.

Other values in TableCell (e.g. MaxWidth) already work that way, i.e. the maximum is used for the column width, therefore I'd like this to be in line with that.

Also, if I only want to expand column 8, I don't have to do this: SetFullWidth(true,0,0,0,0,0,0,0,1).

@dmke
Copy link
Copy Markdown
Author

dmke commented Mar 2, 2018

Ah, I see, thanks for the explanation.

Let me try to implement this.

@rivo
Copy link
Copy Markdown
Owner

rivo commented Mar 2, 2018 via email

@rivo rivo closed this in 8cb36ea Mar 5, 2018
@rivo
Copy link
Copy Markdown
Owner

rivo commented Mar 5, 2018

Please have a look at the referenced commit. This should be in line with what you requested.

I actually also tried implementing a "contraction" value which shrinks columns if the table is larger than the available width. But then I realized this is in conflict with the table's behaviour of moving columns off-screen and letting you navigate to them. So there was no easy solution to that. I guess using Grid would be better to achieve a width adaption in both directions.

@dmke
Copy link
Copy Markdown
Author

dmke commented Mar 5, 2018

At a first glance, this looks good, thank you very much.

I'll try this out later when I'm back home.

@dmke dmke deleted the expandable-column branch March 5, 2018 13:37
@dmke
Copy link
Copy Markdown
Author

dmke commented Mar 5, 2018

Works like a charm :-)

multiping

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

Successfully merging this pull request may close these issues.

2 participants