Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Tables #218

Merged
merged 11 commits into from
Oct 10, 2023
Merged

Feature: Tables #218

merged 11 commits into from
Oct 10, 2023

Conversation

maaslalani
Copy link
Member

@maaslalani maaslalani commented Aug 26, 2023

Adds NewTable + features to Lip Gloss.

API:

t := lipgloss.NewTable().
    Border(lipgloss.NormalBorder()).
    BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
    StyleFunc(func(row, col int) lipgloss.Style {
        switch {
        case row == 0:
            return HeaderStyle
        case row%2 == 0:
            return EvenRowStyle
        default:
            return OddRowStyle
        }
    }).
    Headers("LANGUAGE", "FORMAL", "INFORMAL").
    Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
    Row("French", "Bonjour", "Salut").
    Row("Russian", "Zdravstvuyte", "Privet").
    Row("Spanish", "Hola", "¿Qué tal?")

Results:

┌──────────┬──────────────┬───────────┐
│ LANGUAGE │    FORMAL    │ INFORMAL  │
├──────────┼──────────────┼───────────┤
│ Chinese  │ Nǐn hǎo      │ Nǐ hǎo    │
│ French   │ Bonjour      │ Salut     │
│ Russian  │ Zdravstvuyte │ Privet    │
│ Spanish  │ Hola         │ ¿Qué tal? │
└──────────┴──────────────┴───────────┘

@maaslalani
Copy link
Member Author

image

@bashbunni
Copy link
Member

bashbunni commented Sep 15, 2023

I think we should have a "DefaultStyle" function that returns some basic styling for a table

Also, I think when setting headers and rows we should accept []string as the argument 🤔

@bashbunni
Copy link
Member

I think we also need to add some kind of restrictions or a check to make sure that we don't try to index out of range when iterating over the cells in the rows since there is the possibility right now that there are more cells in a row than headings in the header.
Here is where the issue surfaces if that's the case. Happy to add a test for this case :)
https://github.com/charmbracelet/lipgloss/pull/218/files#diff-134c48f173480a9772719053c1b3e967756a71c75f84ed4f94f12c71cd91ed30R169

@bashbunni
Copy link
Member

Note to self:

  • uses the struct's String function when rendering outputs to the table
  • use the spread operator to use array values as the any type

@bashbunni
Copy link
Member

This has been great to work with btw Maas!

@maaslalani
Copy link
Member Author

Thanks so much @bashbunni!

BorderStyle(re.NewStyle().Foreground(lipgloss.Color("238"))).
Headers(CapitalizeHeaders(headers)...).
Rows(data...).
StyleFunc(func(row, col int) lipgloss.Style {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maaslalani I think StyleFunc should be named something else to indicate that it's for cell styling. It's a bit vague imo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note that this may become a more general RenderFunc in order to allow more flexibility in text transormation (see image).

image-1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah gotcha, good to know! Thank you :)

table/rows.go Outdated
// Data is the interface that wraps the basic methods of a table model.
type Data interface {
Row(row int) Row
Append(row Row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Append should remain optional. Not all data models can/will support that.

table/table.go Outdated
}

// Rows sets the table data.
func (t *Table) Rows(data Data) *Table {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Rows(data Data) is even weirder then Data(data Data). Also comparing Row with Rows , I would expect them to accept the same type, just singular vs plural?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm trying to say: this function sets an entire data model, and hence the names should probably match. In addition to Row we can then have Rows, which lets you set multiple string rows at once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep you're right! I will change this.

I'll keep the Rows method and then also add a Data method.

table/table.go Outdated
Comment on lines 118 to 120
case *StringData:
t.data.(*StringData).Append(StringRow(row))
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muesli does this seem reasonable to you? This is how we would avoid Append being a required message, but not sure if this is the correct way to do it.

@maaslalani maaslalani merged commit 4476263 into master Oct 10, 2023
18 checks passed
@maaslalani maaslalani deleted the table branch October 10, 2023 14:28
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.

None yet

4 participants