Skip to content

Commit

Permalink
feat(examples): add list examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Nov 28, 2023
1 parent 380b842 commit 12c3f51
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module examples

go 1.17
go 1.18

replace github.com/charmbracelet/lipgloss => ../

Expand Down
59 changes: 59 additions & 0 deletions examples/list/grocery/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"fmt"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/list"
)

var purchased = []string{
"Bananas",
"Barley",
"Cashews",
"Coconut Milk",
"Dill",
"Eggs",
"Fish Cake",
"Leeks",
"Papaya",
}

func GroceryEnumerator(l *list.List, i int) string {
item := l.Items[i-1]
for _, p := range purchased {
if item == p {
return "✓"
}
}
return " "
}

func newList(items ...any) *list.List {
enumStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("10")).MarginRight(1)

return list.New(items...).
Enumerator(GroceryEnumerator).
EnumeratorStyle(enumStyle)
}

func main() {
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
enumStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12")).MarginRight(1)

l := list.New(
"A", newList("Artichoke"),
"B", newList("Baking Flour", "Bananas", "Barley", "Bean Sprouts"),
"C", newList("Cashew Apple", "Cashews", "Coconut Milk", "Curry Paste", "Currywurst"),
"D", newList("Dill", "Dragonfruit", "Dried Shrimp"),
"E", newList("Eggs"),
"F", newList("Fish Cake", "Furikake"),
"J", newList("Jicama"),
"K", newList("Kohlrabi"),
"L", newList("Leeks", "Lentils", "Licorice Root"),
).
ItemStyle(itemStyle).
EnumeratorStyle(enumStyle)

fmt.Println(l)
}
6 changes: 3 additions & 3 deletions list/enumerations.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ func Bullet(_ *List, _ int) string {
// └─ Qux.
func Tree(l *List, index int) string {
// out of bounds?
if index < 0 || index > len(l.items) {
if index < 0 || index > len(l.Items) {
return ""
}

switch index {
// is last item of list.
case len(l.items):
case len(l.Items):
return "└─"
default:
switch l.items[index].(type) {
switch l.Items[index].(type) {
case *List:
return "└─"
default:
Expand Down
11 changes: 6 additions & 5 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ type List struct {
enumerator Enumerator
hide bool
indent int
items []any
Items []any
style Style
}

// New returns a new list.
func New(items ...any) *List {
return &List{
Items: items,

enumerator: Bullet,
indent: 0,
items: items,
style: Style{
Enumerator: lipgloss.NewStyle().MarginRight(1),
Item: lipgloss.NewStyle(),
Expand All @@ -42,7 +43,7 @@ func New(items ...any) *List {

// Item appends an item to a list.
func (l *List) Item(item any) *List {
l.items = append(l.items, item)
l.Items = append(l.Items, item)
return l
}

Expand Down Expand Up @@ -106,13 +107,13 @@ func (l *List) String() string {

// find the longest enumerator value of this list.
var maxLen int
for i := 0; i < len(l.items); i++ {
for i := 0; i < len(l.Items); i++ {
enum := l.style.Enumerator.Render(l.enumerator(l, i+1))
maxLen = max(runewidth.StringWidth(enum), maxLen)
}

var s strings.Builder
for i, item := range l.items {
for i, item := range l.Items {
switch item := item.(type) {
case *List:
if item.indent <= 0 {
Expand Down

0 comments on commit 12c3f51

Please sign in to comment.