Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tree): example showing items in open and closed states (wip)
- Loading branch information
1 parent
04ada29
commit b243fb1
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
"github.com/charmbracelet/lipgloss/tree" | ||
) | ||
|
||
func openEnumerator(data tree.Data, i int) (indent, prefix string) { | ||
if data.Length()-1 == i { | ||
return " ", "▼ " | ||
} | ||
return " ", "▼ " | ||
} | ||
|
||
func closedEnumerator(data tree.Data, i int) (indent, prefix string) { | ||
if data.Length()-1 == i { | ||
return " ", "▶ " | ||
} | ||
return " ", "▶ " | ||
} | ||
|
||
func normalEnumerator(data tree.Data, _ int) (indent, prefix string) { | ||
return " ", "• " | ||
} | ||
|
||
func main() { | ||
pink := lipgloss.NewStyle().MarginRight(1) | ||
gray := lipgloss.NewStyle().Foreground(lipgloss.Color("8")).MarginRight(1) | ||
|
||
t := tree.New(). | ||
Root("Groceries"). | ||
Items( | ||
tree.New(). | ||
Root("Fruits"). | ||
Items( | ||
"Blood Orange", | ||
"Papaya", | ||
"Dragonfruit", | ||
"Yuzu", | ||
).EnumeratorStyle(gray).Enumerator(normalEnumerator), | ||
tree.New(). | ||
Root("Items"). | ||
Items( | ||
"Cat Food", | ||
"Nutella", | ||
"Powdered Sugar", | ||
).EnumeratorStyle(gray).Enumerator(closedEnumerator).Hide(true), | ||
tree.New(). | ||
Root("Veggies"). | ||
Items( | ||
"Leek", | ||
"Artichoke", | ||
).EnumeratorStyle(gray).Enumerator(normalEnumerator), | ||
). | ||
ItemStyle(pink). | ||
EnumeratorStyle(gray). | ||
Enumerator(openEnumerator) | ||
|
||
fmt.Println(t) | ||
} |