charmbracelet / lipgloss

Style definitions for nice terminal layouts ๐Ÿ‘„
MIT License
7.89k stars 225 forks source link

setting table height doesn't do anything #356

Closed bashbunni closed 1 day ago

bashbunni commented 2 weeks ago

We should cut off additional rows when a height is explicitly set. The last allowed row should probably just show '...' to show that there's more content than the height allows. Checked the source code and the resizing logic is only applied to width

code to reproduce:

package main

import (
    "fmt"

    "github.com/charmbracelet/lipgloss"

    glossytable "github.com/charmbracelet/lipgloss/table"
)

func main() {
    gloss := glossytable.New().
        Headers("ID", "title", "description").
        Row("1", "Lost in Time", "A thrilling adventure through the ages.").
        Row("2", "Whispering Shadows", "Secrets unravel in a haunted town.").
        Row("3", "Stolen Identity", "An innocent man's life turned upside down.").
        Row("4", "Into the Abyss", "Exposing deep-rooted conspiracies.").
        Border(lipgloss.NormalBorder()).
        Width(20).
        Height(4)

    fmt.Println(gloss)
}
Broderick-Westrope commented 2 weeks ago

@bashbunni can you clarify what is meant to impact the height of the table?

  1. Just the number of lines?
  2. Or the number of rows?
  3. Or the rows + header?

To demonstrate, the following is a table of height 4 using each of these approaches:

1.
โ•ญโ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚IDโ”‚title          โ”‚description        โ”‚
โ”œโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ•ฐโ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

2.
โ•ญโ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚IDโ”‚title          โ”‚description        โ”‚
โ”œโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚1 โ”‚Lost in Time   โ”‚A thrilling adventโ€ฆโ”‚
โ”‚2 โ”‚Whispering Shaโ€ฆโ”‚Secrets unravel inโ€ฆโ”‚
โ”‚3 โ”‚Stolen Identityโ”‚An innocent man's โ€ฆโ”‚
โ”‚4 โ”‚Into the Abyss โ”‚Exposing deep-rootโ€ฆโ”‚
โ•ฐโ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

3.
โ•ญโ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚IDโ”‚title          โ”‚description        โ”‚
โ”œโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚1 โ”‚Lost in Time   โ”‚A thrilling adventโ€ฆโ”‚
โ”‚2 โ”‚Whispering Shaโ€ฆโ”‚Secrets unravel inโ€ฆโ”‚
โ”‚3 โ”‚Stolen Identityโ”‚An innocent man's โ€ฆโ”‚
โ•ฐโ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

NOTE: This example isn't concerned with adding ellipsis as the bottom row ;)

Currently bubbles/table takes a hybrid approach where the height is the sum of two things:

Meaning the bubbles/table equivalent of length 4 would be:

โ•ญโ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚IDโ”‚title          โ”‚description        โ”‚
โ”œโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚1 โ”‚Lost in Time   โ”‚A thrilling adventโ€ฆโ”‚
โ•ฐโ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

3 lines for the header with styling + 1 line for the row content (no styling)
Broderick-Westrope commented 2 weeks ago

The PR I mentioned is currently taking approach 1 from my original message. I am happy to fix the unit tests and close this out once you confirm the desired approach for calculating table height :)

It's also worth clarifying whether height should be increased beyond what is needed. Since bubbles/table sets the height on a viewport you can make it as long as you want, but in my current lipgloss/table solution you cannot increase the height of a table beyond the total number of rows (ie. no inserting empty rows).

bashbunni commented 2 weeks ago

@Broderick-Westrope I think the height should be counted by cell (number of lines) for consistency's sake. The width is set based on the total width (number of cells) of the table, including borders, so I think height should be the same

meowgorithm commented 2 weeks ago

I agree: the first approach makes the most sense to me and will generally help when reasoning about layout.

Broderick-Westrope commented 2 weeks ago

Great, thank you both for clarifying. That's what's currently implemented. I'll just fix up the unit tests and mark it ready for review.

Broderick-Westrope commented 2 weeks ago

Done. I've written a detailed description in the PR demonstrating different cases :)