[GH-ISSUE #242] Tablecells incorrectly expand in width #188

Closed
opened 2026-03-04 01:02:46 +03:00 by kerem · 8 comments
Owner

Originally created by @Bios-Marcel on GitHub (Feb 23, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/242

I have a table containing 3 columns, each having Expand set to 1. However, when putting a longer text into the cell, it just expands, even though there is still enough space left.

Table incorrectly expanding

Originally created by @Bios-Marcel on GitHub (Feb 23, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/242 I have a table containing 3 columns, each having `Expand` set to `1`. However, when putting a longer text into the cell, it just expands, even though there is still enough space left. ![Table incorrectly expanding](https://user-images.githubusercontent.com/19377618/53292473-2a56e900-37c3-11e9-888f-6ffd96b00ba8.gif)
kerem closed this issue 2026-03-04 01:02:46 +03:00
Author
Owner

@rivo commented on GitHub (Mar 9, 2019):

Yes, this would be expected. The Expansion value distributes "remaining available space" so that narrow tables can fill the entire available width. If you add a long text to a column, the table's remaining space becomes less. Thus, there's less to distribute to other columns.

In this example, you may want to set the Expansion value to 0 for the first two columns.

<!-- gh-comment-id:471172412 --> @rivo commented on GitHub (Mar 9, 2019): Yes, this would be expected. The `Expansion` value distributes "remaining available space" so that narrow tables can fill the entire available width. If you add a long text to a column, the table's remaining space becomes less. Thus, there's less to distribute to other columns. In this example, you may want to set the `Expansion` value to `0` for the first two columns.
Author
Owner

@Bios-Marcel commented on GitHub (Mar 9, 2019):

As far as I get it, setting the expansion on all cells to the same value, they should all take an equal amount of space.

<!-- gh-comment-id:471172870 --> @Bios-Marcel commented on GitHub (Mar 9, 2019): As far as I get it, setting the expansion on all cells to the same value, they should all take an equal amount of space.
Author
Owner

@rivo commented on GitHub (Mar 9, 2019):

I think what you want is a SetWidth() function (relative or absolute), not a SetExpansion() function.

Again, Expansion determines how the table grows, not the initial width of a column.

<!-- gh-comment-id:471174315 --> @rivo commented on GitHub (Mar 9, 2019): I think what you want is a `SetWidth()` function (relative or absolute), not a `SetExpansion()` function. Again, `Expansion` determines how the table grows, not the initial width of a column.
Author
Owner

@Bios-Marcel commented on GitHub (Mar 9, 2019):

So, shouldn't it be a bool instead? Because currently it is an int.

<!-- gh-comment-id:471183545 --> @Bios-Marcel commented on GitHub (Mar 9, 2019): So, shouldn't it be a `bool` instead? Because currently it is an `int`.
Author
Owner

@rivo commented on GitHub (Mar 9, 2019):

You mean the Expansion value? I'm not sure what you mean.

<!-- gh-comment-id:471201758 --> @rivo commented on GitHub (Mar 9, 2019): You mean the `Expansion` value? I'm not sure what you mean.
Author
Owner

@Bios-Marcel commented on GitHub (Mar 9, 2019):

Okay, I am a bit confused.

github.com/rivo/tview@03d744dee3/table.go (L83)

I have had 3 columns all having their Expansion value set to 1, meaning they were all equal, so why when changing the Text attribute of one of those three columns, did it expand?

<!-- gh-comment-id:471206874 --> @Bios-Marcel commented on GitHub (Mar 9, 2019): Okay, I am a bit confused. https://github.com/rivo/tview/blob/03d744dee35eb7cd4f59bf139745bff07412b759/table.go#L83 I have had 3 columns all having their `Expansion` value set to `1`, meaning they were all equal, so why when changing the `Text` attribute of one of those three columns, did it expand?
Author
Owner

@rivo commented on GitHub (Mar 9, 2019):

This is the function description:

SetExpansion sets the value by which the column of this cell expands if the available width for the table is more than the table width (prior to applying this expansion value). This is a proportional value. The amount of unused horizontal space is divided into widths to be added to each column. How much extra width a column receives depends on the expansion value: A value of 0 (the default) will not cause the column to increase in width. Other values are proportional, e.g. a value of 2 will cause a column to grow by twice the amount of a column with a value of 1.

Again, the Expansion value does not determine the designated width of the column. It only grows it after the column width has already been established.

The algorithm works somewhat like this:

  1. For each column i, calculate column width(i) = max(text width) over all rows.
  2. Calculate table width = sum(column width) over all columns.
  3. If table width < available width, distribute available width - table width according to expansion values: column width(i) += expansion(i) * (available width - table width) / sum(expansion)
  4. If table width >= available width, do nothing.

This is not the same as, say, Grid.SetColumns(-1,-1,-1). If it was, I would have given it a different name, as "to expand" generally means "to grow".

Thus, because of Step 1, it all depends on the width of your text. If you change it, everything changes.

I'm guessing a solution to what you're asking for is described in #184.

<!-- gh-comment-id:471215769 --> @rivo commented on GitHub (Mar 9, 2019): This is the function description: > SetExpansion sets the value by which the column of this cell expands if the available width for the table is more than the table width (prior to applying this expansion value). This is a proportional value. The amount of unused horizontal space is divided into widths to be added to each column. How much extra width a column receives depends on the expansion value: A value of 0 (the default) will not cause the column to increase in width. Other values are proportional, e.g. a value of 2 will cause a column to grow by twice the amount of a column with a value of 1. Again, the `Expansion` value does *not* determine the designated width of the column. It only grows it after the column width has already been established. The algorithm works somewhat like this: 1. For each column `i`, calculate `column width(i) = max(text width)` over all rows. 1. Calculate `table width = sum(column width)` over all columns. 1. If `table width < available width`, distribute `available width - table width` according to `expansion` values: `column width(i) += expansion(i) * (available width - table width) / sum(expansion)` 1. If `table width >= available width`, do nothing. This is *not* the same as, say, [`Grid.SetColumns(-1,-1,-1)`](https://godoc.org/github.com/rivo/tview#Grid.SetColumns). If it was, I would have given it a different name, as "to expand" generally means "to grow". Thus, because of Step 1, it all depends on the width of your text. If you change it, everything changes. I'm guessing a solution to what you're asking for is described in #184.
Author
Owner

@Bios-Marcel commented on GitHub (Mar 9, 2019):

I see. Thanks a lot man! I'll be closing the issue then.

<!-- gh-comment-id:471218427 --> @Bios-Marcel commented on GitHub (Mar 9, 2019): I see. Thanks a lot man! I'll be closing the issue then.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#188
No description provided.