Skip to content

Commit 6348d59

Browse files
authored
docs: list documentation (#331)
1 parent 2a67670 commit 6348d59

File tree

1 file changed

+47
-140
lines changed

1 file changed

+47
-140
lines changed

README.md

Lines changed: 47 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -455,114 +455,9 @@ fmt.Println(t)
455455

456456
For more on tables see [the docs](https://pkg.go.dev/github.com/charmbracelet/lipgloss?tab=doc) and [examples](https://github.com/charmbracelet/lipgloss/tree/master/examples/table).
457457

458-
## Rendering Trees
459-
460-
Lip Gloss ships with a tree rendering sub-package.
461-
462-
```go
463-
import "github.com/charmbracelet/lipgloss/tree"
464-
```
465-
466-
Define a new tree.
467-
468-
```go
469-
t := tree.New("root", "child 1", "child 2", tree.New("child 3", "child 3.1"))
470-
```
471-
472-
Print the tree.
473-
474-
```go
475-
fmt.Println(t)
476-
477-
// root
478-
// ├── child 1
479-
// ├── child 2
480-
// └── child 3
481-
// └── child 3.1
482-
```
483-
484-
### Customization
485-
486-
Trees can be customized via their enumeration function as well as using
487-
`lipgloss.Style`s.
488-
489-
```go
490-
style1 := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
491-
style2 := lipgloss.NewStyle().Foreground(lipgloss.Color("10")).MarginRight(1)
492-
493-
t := tree.New().
494-
Items(
495-
"Glossier",
496-
"Claire’s Boutique",
497-
tree.New().
498-
Root("Nyx").
499-
Items("Qux", "Quux").
500-
EnumeratorStyle(style2),
501-
"Mac",
502-
"Milk",
503-
).
504-
EnumeratorStyle(style1)
505-
```
506-
507-
Print the tree:
508-
509-
<p align="center">
510-
<img
511-
width="600"
512-
alt="Tree example"
513-
src="https://github.com/charmbracelet/lipgloss/assets/245435/5a875269-f6d6-43fa-9916-5d8360e66964"
514-
/>
515-
</p>
516-
You may also define custom enumerator implementations:
517-
518-
```go
519-
t := tree.New().
520-
Items(
521-
"Glossier",
522-
"Claire’s Boutique",
523-
tree.New().
524-
Root("Nyx").
525-
Items(
526-
"Qux",
527-
"Quux",
528-
),
529-
"Mac",
530-
"Milk",
531-
).
532-
Enumerator(func(tree.Data, int) (string, string) {
533-
return "->", "->"
534-
})
535-
```
536-
537-
Print the tree.
538-
539-
<p align="center">
540-
<img
541-
width="600"
542-
alt="Tree example"
543-
src="https://github.com/charmbracelet/lipgloss/assets/245435/811e8b39-124f-48bb-b3dd-e015a65b1065"
544-
/>
545-
</p>
546-
547-
### Building
548-
549-
If you need, you can also build trees incrementally:
550-
551-
```go
552-
t := tree.New("")
553-
554-
for i := 0; i < repeat; i++ {
555-
t.Item("Lip Gloss")
556-
}
557-
```
558-
559-
560458
## Rendering Lists
561459

562460
Lip Gloss ships with a list rendering sub-package.
563-
Implementation-wise, lists are still trees.
564-
The `list` package provides many common `Enumerator` implementations, as well as
565-
some syntactic sugar.
566461

567462
```go
568463
import "github.com/charmbracelet/lipgloss/list"
@@ -584,77 +479,89 @@ fmt.Println(l)
584479
// • C
585480
```
586481

482+
Lists have the ability to nest.
483+
484+
```go
485+
l := list.New(
486+
"A", list.New("Artichoke"),
487+
"B", list.New("Baking Flour", "Bananas", "Barley", "Bean Sprouts"),
488+
"C", list.New("Cashew Apple", "Cashews", "Coconut Milk", "Curry Paste", "Currywurst"),
489+
"D", list.New("Dill", "Dragonfruit", "Dried Shrimp"),
490+
"E", list.New("Eggs"),
491+
"F", list.New("Fish Cake", "Furikake"),
492+
"J", list.New("Jicama"),
493+
"K", list.New("Kohlrabi"),
494+
"L", list.New("Leeks", "Lentils", "Licorice Root"),
495+
)
496+
```
497+
498+
Print the list.
499+
500+
```go
501+
fmt.Println(l)
502+
```
587503

588-
### Customization
504+
<p align="center">
505+
<img width="600" alt="image" src="https://github.com/charmbracelet/lipgloss/assets/42545625/0dc9f440-0748-4151-a3b0-7dcf29dfcdb0">
506+
</p>
589507

590508
Lists can be customized via their enumeration function as well as using
591509
`lipgloss.Style`s.
592510

593511
```go
594512
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
595-
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("10")).MarginRight(1)
513+
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212")).MarginRight(1)
596514

597515
l := list.New(
598-
"Glossier",
599-
"Claire’s Boutique",
600-
"Nyx",
601-
"Mac",
602-
"Milk",
516+
"Glossier",
517+
"Claire’s Boutique",
518+
"Nyx",
519+
"Mac",
520+
"Milk",
603521
).
604-
Enumerator(list.Roman).
605-
EnumeratorStyle(enumeratorStyle).
606-
ItemStyle(itemStyle)
522+
Enumerator(list.Roman).
523+
EnumeratorStyle(enumeratorStyle).
524+
ItemStyle(itemStyle)
607525
```
608526

609527
Print the list.
610528

611529
<p align="center">
612-
<img
613-
width="600"
614-
alt="List example"
615-
src="https://github.com/charmbracelet/lipgloss/assets/245435/8f5e5e0b-7bf9-4e3b-a8ba-3af10825320e"
616-
/>
530+
<img width="600" alt="List example" src="https://github.com/charmbracelet/lipgloss/assets/42545625/360494f1-57fb-4e13-bc19-0006efe01561">
617531
</p>
532+
618533
In addition to the predefined enumerators (`Arabic`, `Alphabet`, `Roman`, `Bullet`, `Tree`),
619534
you may also define your own custom enumerator:
620535

621536
```go
622-
var DuckDuckGooseEnumerator Enumerator = func(l *List, i int) string {
623-
if l.At(i) == "Goose" {
624-
return "Honk →"
625-
}
626-
return ""
627-
}
628-
```
537+
l := list.New("Duck", "Duck", "Duck", "Duck", "Goose", "Duck", "Duck")
629538

630-
Use it in a list:
539+
func DuckDuckGooseEnumerator(l list.Items, i int) string {
540+
if l.At(i).Value() == "Goose" {
541+
return "Honk →"
542+
}
543+
return ""
544+
}
631545

632-
```go
633-
l := list.New("Duck", "Duck", "Duck", "Duck", "Goose", "Duck", "Duck")
634-
l.Enumerator(DuckDuckGooseEnumerator)
546+
l = l.Enumerator(DuckDuckGooseEnumerator)
635547
```
636548

637549
Print the list:
638550

639551
<p align="center">
640-
<img
641-
width="600"
642-
alt="image"
643-
src="https://github.com/charmbracelet/lipgloss/assets/245435/44e37a5b-5124-4f49-a332-1756a355002e"
644-
/>
552+
<img width="600" alt="image" src="https://github.com/charmbracelet/lipgloss/assets/42545625/157aaf30-140d-4948-9bb4-dfba46e5b87e">
645553
</p>
646554

647-
### Building
648-
649-
If you need, you can also build trees incrementally:
555+
If you need, you can also build lists incrementally:
650556

651557
```go
652558
l := list.New()
653559

654560
for i := 0; i < repeat; i++ {
655-
l.Item("Lip Gloss")
561+
l.Item("Lip Gloss")
656562
}
657563
```
564+
658565
---
659566

660567
## FAQ

0 commit comments

Comments
 (0)