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

Flexible max number of rows based on size of the terminal

Open PrimalPimmy opened this issue 3 years ago • 11 comments

I wonder if it's possible to set flexible Max page size of the table, which changes with every terminal height resize.. is this possible? Could be a really nice feature. If it is possible, could you guide me on how I would proceed?

There is indeed GetTargetWidth function for the table, but not Height it seems.

I've seen examples of Dynamic Page size using keybinds, but not when it's relevant to the height of the terminal

PrimalPimmy avatar Jan 09 '23 18:01 PrimalPimmy

Hey there, I'd definitely go with WithPageSize for this. You'll want to feed in the desired height yourself, which could be the height of the terminal or it could be some other dynamic height as part of a larger layout. I'd rather avoid coupling directly to the terminal height as there may be many different layouts to deal with, and WithPageSize should take care of the height. Let me know if you need a code sample beyond https://github.com/Evertras/bubble-table/blob/main/examples/pagination/main.go but I'd use that as a starting point.

Evertras avatar Jan 11 '23 11:01 Evertras

but WithPageSize returns the number of rows per page right? And not the height of the table. Do you imply msg.Height in bubbletea (from tea.WindowSizeMsg) can be compared with WithPageSize ? Would be neat if the number of rows decreased as I lower the height of the terminal

PrimalPimmy avatar Jan 11 '23 21:01 PrimalPimmy

You would set WithPageSize depending on the desired height. You'd take msg.Height and subtract a bit of a constant value to account for the border + footer + header, then apply the result to WithPageSize to set the table height accordingly.

I'm not against making this a bit easier, but there are some non-trivial things to sort through for this as there may be dynamic footers, etc. I may try to play with this a bit over the weekend.

Evertras avatar Jan 12 '23 16:01 Evertras

We could apply the same pattern as bubbles.list. With the set size method it invokes updatePagination that handles it. Let me see what can I do about it

prgres avatar Nov 27 '23 21:11 prgres

My first concert is a collision with recently added multiline feature

prgres avatar Nov 27 '23 21:11 prgres

I'm trying to do this as we speak. However, WithPageSize doesn't seem to have any effect. The only way I can affect the height of the table is calling WithPageSize at the time of instantiation. This means that the table size is fixed at launch time.

Calling m.table.WIthPageSize(m.height - 8) in my View() function seems to do nothing.

rendicott avatar Jan 21 '24 21:01 rendicott

@rendicott Are you setting the table with the result, like this? https://github.com/Evertras/bubble-table/blob/main/examples/pagination/main.go#L95 Calling WithX will always create a copy that needs to be assigned, in order to preserve the immutability constraints of Bubble Tea.

Evertras avatar Jan 21 '24 23:01 Evertras

@Evertras I was not. That did it. Thank you!

rendicott avatar Jan 22 '24 09:01 rendicott

My first concert is a collision with recently added multiline feature

This is my current issue. WithPageSize works pretty well paired with WindowSizeMsgs from Bubble Tea, but only if Multiline is disabled (which is a shame, because mutliline works great). Without being able to hard limit the height, Multiline+PageSize is a no-go, as we can no longer assume each record is a single line and therefore pagesize ~= height for records.

rflandau avatar Jul 17 '24 16:07 rflandau

For anyone else with this issue, you can wrap the table's .View() in a viewport bubble to mandate a height. I currently have the table's .Update() supplanted by the viewport's, but this only works because I am not using the table's built-in cursor functionality. You'll have to get creative if you want to run both .Update()s while multiline is enabled.

rflandau avatar Jul 17 '24 16:07 rflandau