charmbracelet/lipgloss

Dynamic stringer list item

nervo opened this issue · 0 comments

In upcoming list component, i've noticed that you can add items of any type.

There is a switch in Tree.Child method to handle this properly (see:

func (t *Tree) Child(children ...any) *Tree {
).

In cases of simple custom application items, one can choose between two options:

  • implement Node interface (see:
    type Node interface {
    ): value is then computed dynamically (during render time), BUT, you MUST implement Value, Children and Hidden methods, even if you don't really need them
  • implement go Stringer interface, which, in this case, is converted to a Leaf (see:
    case fmt.Stringer:
    ) . Sadly, the value is now strictly static (obtained during implementation, without the possibility of mutation during the item lifetime).

I think the Leaf struct should keep the Stringer as a property, and call its String() method during the render time, via its Value() method.
This way, it will become totally dynamic, and give the option to change its value during the item lifetime.

And if you're ok with that, i can make a pull request :)