From 12c3f51f1c1ef79f667e73bc86326ce406fd98b7 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Tue, 28 Nov 2023 14:18:28 -0500 Subject: [PATCH] feat(examples): add list examples --- examples/go.mod | 2 +- examples/list/grocery/main.go | 59 +++++++++++++++++++++++++++++++++++ list/enumerations.go | 6 ++-- list/list.go | 11 ++++--- 4 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 examples/list/grocery/main.go diff --git a/examples/go.mod b/examples/go.mod index c87579b3..a7051f44 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -1,6 +1,6 @@ module examples -go 1.17 +go 1.18 replace github.com/charmbracelet/lipgloss => ../ diff --git a/examples/list/grocery/main.go b/examples/list/grocery/main.go new file mode 100644 index 00000000..182074f6 --- /dev/null +++ b/examples/list/grocery/main.go @@ -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) +} diff --git a/list/enumerations.go b/list/enumerations.go index 38e75bcf..747164d8 100644 --- a/list/enumerations.go +++ b/list/enumerations.go @@ -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: diff --git a/list/list.go b/list/list.go index a2b8b571..5f91df9c 100644 --- a/list/list.go +++ b/list/list.go @@ -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(), @@ -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 } @@ -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 {