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

List: pass item styles from Delegate to Item struct. #372

Open
antonio-leitao opened this issue Apr 23, 2023 · 1 comment
Open

List: pass item styles from Delegate to Item struct. #372

antonio-leitao opened this issue Apr 23, 2023 · 1 comment

Comments

@antonio-leitao
Copy link

antonio-leitao commented Apr 23, 2023

The Issue

Current implementation has an Item's style as part of the Delegate and not the Item. This results in all Items being rendered with the same style.

type DefaultDelegate struct {
	ShowDescription bool
	Styles          DefaultItemStyles
	UpdateFunc      func(tea.Msg, *Model) tea.Cmd
	ShortHelpFunc   func() []key.Binding
	FullHelpFunc    func() [][]key.Binding
	height          int
	spacing         int
}

Which makes it very hard to customize specially if you want the view of each Item be defined by some function of its internal state, since the render function for each item is defined in the delegate; Below:

func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item){
	var (
		title, desc  string
		matchedRunes []int
		s            = &d.Styles
	)
(...)

        title = s.SelectedTitle.Render(title)
        desc = s.SelectedDesc.Render(desc)

Proposed solution

Should the styles be actually part of the Item struct? This would make customisation easier. Default Delegate simply renders the item according to the model's state and the item state. For example the code above would become something like:

func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item){
	var (
		title, desc  string
		matchedRunes []int
	)
(...)
        title = item.Styles.SelectedTitle.Render(title)
        desc = item.Styles.SelectedDesc.Render(desc)

This would enhance customisation and maybe even make it the List more understandable to the user since it will have to deal less with the Delegate.

@muesli
Copy link
Member

muesli commented Apr 24, 2023

You can write your own item delegate that picks a different style for each item.

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

No branches or pull requests

2 participants