Skip to content

Commit

Permalink
fix: apply ItemStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed May 15, 2024
1 parent a9f7bc2 commit a8bfc36
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
9 changes: 8 additions & 1 deletion examples/list/duckduckgoose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ func DuckDuckGooseEnumerator(l list.List) list.Enumerator {
func main() {
l := list.New("Duck", "Duck", "Duck", "Duck", "Goose", "Duck", "Duck")

l = l.Enumerator(DuckDuckGooseEnumerator(l)).EnumeratorStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("48")).MarginRight(1))
l = l.Enumerator(DuckDuckGooseEnumerator(l)).
EnumeratorStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("48")).MarginRight(1)).
ItemStyleFunc(func(i int) lipgloss.Style {
if l.At(i) == "Goose" {
return lipgloss.NewStyle().Foreground(lipgloss.Color("255")).Bold(true)
}
return lipgloss.NewStyle()
})

fmt.Println(l)
}
9 changes: 7 additions & 2 deletions examples/list/makeup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import (
)

func main() {
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212")).MarginRight(1)
enumeratorStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("99")).
MarginRight(1)

itemStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("255")).
MarginRight(1)

l := list.New(
"Glossier",
Expand Down
27 changes: 19 additions & 8 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// stacked on top of each other. Like the following:
//
// list.New("A", "B", "C").
// (list.Bullet).
// Enumerator(list.Bullet).
// String()
//
// • A
Expand Down Expand Up @@ -67,11 +67,26 @@ func New(items ...Item) List {
enumeratorStyleFunc: func(_ int) lipgloss.Style {
return lipgloss.NewStyle().MarginRight(1)
},
itemStyleFunc: func(_ int) lipgloss.Style {
return lipgloss.NewStyle()
},
}
}

// Item appends the given item to the list.
// Alias for AddItem for a more fluent API.
//
// New().
// Item("A").
// Item("B").
// Item("C").
// Item("D").
func (l List) Item(item Item) List {
return l.AddItem(item)
}

// AddItem appends the given item to the list.
func (l List) AddItem(item Item) List {
l.items = append(l.items, item)
return l
}
Expand All @@ -87,13 +102,9 @@ func (l List) Separator(s lipgloss.Style) List {
//
// list.New(...).Prefix(s) is equivalent to:
//
// list.New(...).Enumerator(func(_ int) string {
// return s
// })
// list.New(...).Enumerator(func(_ int) string { return s })
func (l List) Prefix(s string) List {
l.enumerator = func(_ int) string {
return s
}
l.enumerator = func(_ int) string { return s }
return l
}

Expand Down Expand Up @@ -172,7 +183,7 @@ func (l List) String() string {
lipgloss.Top,
indent,
enumerator,
fmt.Sprintf("%v", item),
l.itemStyleFunc(i).Render(fmt.Sprintf("%v", item)),
)
s.WriteString(listItem)
if i != last {
Expand Down

0 comments on commit a8bfc36

Please sign in to comment.