Skip to content

Commit

Permalink
Tree API Changes (#307)
Browse files Browse the repository at this point in the history
* refactor: clean tree_test.go

* refactor: remove subtests

* refactor: rename `Data` to `Items` for `Lists` and `Children` for `Trees`

* chore: support only NodeData

* fix: enumerations -> enumerators for consistency

* fix: item -> children

* fix: use l for List, rather than n

* fix: move list Enumerator definition to enumerator.go

* fix: list enumerator comments

* docs: add package documentation

* fix: examples

* feat: split enumerator and indenters

* docs: add comments

* refactor: package comment

* docs: use more tree terminology

* docs: refactor indentor comments

* fix: lint
  • Loading branch information
maaslalani authored May 30, 2024
1 parent d21c576 commit 1afeca0
Show file tree
Hide file tree
Showing 16 changed files with 995 additions and 1,037 deletions.
4 changes: 2 additions & 2 deletions examples/list/duckduckgoose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/charmbracelet/lipgloss/list"
)

func duckDuckGooseEnumerator(data list.Data, i int) string {
if data.At(i).Name() == "Goose" {
func duckDuckGooseEnumerator(items list.Items, i int) string {
if items.At(i).Value() == "Goose" {
return "Honk →"
}
return " "
Expand Down
7 changes: 3 additions & 4 deletions examples/list/glow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

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

type Document struct {
Expand Down Expand Up @@ -36,20 +35,20 @@ func main() {
hightlightColor := lipgloss.Color("#EE6FF8")

l := list.New().
Enumerator(func(_ list.Data, i int) string {
Enumerator(func(_ list.Items, i int) string {
if i == selectedIndex {
return "│\n│"
}
return " "
}).
ItemStyleFunc(func(_ tree.Data, i int) lipgloss.Style {
ItemStyleFunc(func(_ list.Items, i int) lipgloss.Style {
st := baseStyle
if selectedIndex == i {
return st.Foreground(hightlightColor)
}
return st.Foreground(dimColor)
}).
EnumeratorStyleFunc(func(_ tree.Data, i int) lipgloss.Style {
EnumeratorStyleFunc(func(_ list.Items, i int) lipgloss.Style {
if selectedIndex == i {
return lipgloss.NewStyle().Foreground(hightlightColor)
}
Expand Down
13 changes: 6 additions & 7 deletions examples/list/grocery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

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

var purchased = []string{
Expand All @@ -20,9 +19,9 @@ var purchased = []string{
"Papaya",
}

func groceryEnumerator(data list.Data, i int) string {
func groceryEnumerator(items list.Items, i int) string {
for _, p := range purchased {
if data.At(i).Name() == p {
if items.At(i).Value() == p {
return "✓"
}
}
Expand All @@ -37,19 +36,19 @@ var highlightedEnumStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("10")).
MarginRight(1)

func enumStyleFunc(data tree.Data, i int) lipgloss.Style {
func enumStyleFunc(items list.Items, i int) lipgloss.Style {
for _, p := range purchased {
if data.At(i).Name() == p {
if items.At(i).Value() == p {
return highlightedEnumStyle
}
}
return dimEnumStyle
}

func itemStyleFunc(data tree.Data, i int) lipgloss.Style {
func itemStyleFunc(items list.Items, i int) lipgloss.Style {
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("255"))
for _, p := range purchased {
if data.At(i).Name() == p {
if items.At(i).Value() == p {
return itemStyle.Strikethrough(true)
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/list/sublist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,20 @@ func main() {
Item("Item 4.2.3.3.4.3.3").
Item(
list.New().
Enumerator(func(_ list.Data, i int) string {
Enumerator(func(_ list.Items, i int) string {
if i == 1 {
return "│\n│"
}
return " "
}).
ItemStyleFunc(func(_ tree.Data, i int) lipgloss.Style {
st := baseStyle.Copy()
ItemStyleFunc(func(_ list.Items, i int) lipgloss.Style {
st := baseStyle
if i == 1 {
return st.Foreground(hightlightColor)
}
return st.Foreground(dimColor)
}).
EnumeratorStyleFunc(func(_ tree.Data, i int) lipgloss.Style {
EnumeratorStyleFunc(func(_ list.Items, i int) lipgloss.Style {
if i == 1 {
return lipgloss.NewStyle().Foreground(hightlightColor)
}
Expand Down
41 changes: 41 additions & 0 deletions examples/tree/background/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"

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

func main() {
enumeratorStyle := lipgloss.NewStyle().
Background(lipgloss.Color("0")).
Padding(0, 1)

headerItemStyle := lipgloss.NewStyle().
Background(lipgloss.Color("#ee6ff8")).
Foreground(lipgloss.Color("#ecfe65")).
Bold(true).
Padding(0, 1)

itemStyle := headerItemStyle.Background(lipgloss.Color("0"))

t := tree.New().
ItemStyle(itemStyle).
EnumeratorStyle(enumeratorStyle).
Root("# Table of Contents").
Item(
tree.New().
Root("## Chapter 1").
Item("Chapter 1.1").
Item("Chapter 1.2"),
).
Item(
tree.New().
Root("## Chapter 2").
Item("Chapter 2.1").
Item("Chapter 2.2"),
)

fmt.Println(t)
}
33 changes: 0 additions & 33 deletions examples/tree/bg/main.go

This file was deleted.

3 changes: 2 additions & 1 deletion examples/tree/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func main() {
Root(".").
Item("Item 1").
Item(
tree.New().Root("Item 2").
tree.New().
Root("Item 2").
Item("Item 2.1").
Item("Item 2.2").
Item("Item 2.3"),
Expand Down
34 changes: 0 additions & 34 deletions internal/require/equal.go

This file was deleted.

74 changes: 47 additions & 27 deletions list/enumerations.go → list/enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@ import (
"strings"
)

const abcLen = 26
// Enumerator enumerates a list. Given a list of items and the index of the
// current enumeration, it returns the prefix that should be displayed for the
// current item.
//
// For example, a simple Arabic numeral enumeration would be:
//
// func Arabic(_ Items, i int) string {
// return fmt.Sprintf("%d.", i+1)
// }
//
// There are several predefined enumerators:
// • Alphabet
// • Arabic
// • Bullet
// • Dash
// • Roman
//
// Or, define your own.
type Enumerator func(items Items, index int) string

// Alphabet is the enumeration for alphabetical listing.
//
// a. Foo
// b. Bar
// c. Baz
// d. Qux.
func Alphabet(_ Data, i int) string {
// a. Foo
// b. Bar
// c. Baz
// d. Qux.
func Alphabet(_ Items, i int) string {
if i >= abcLen*abcLen+abcLen {
return fmt.Sprintf("%c%c%c.", 'A'+i/abcLen/abcLen-1, 'A'+(i/abcLen)%abcLen-1, 'A'+i%abcLen)
}
Expand All @@ -23,13 +41,15 @@ func Alphabet(_ Data, i int) string {
return fmt.Sprintf("%c.", 'A'+i%abcLen)
}

const abcLen = 26

// Arabic is the enumeration for arabic numerals listing.
//
// 1. Foo
// 2. Bar
// 3. Baz
// 4. Qux.
func Arabic(_ Data, i int) string {
// 1. Foo
// 2. Bar
// 3. Baz
// 4. Qux.
func Arabic(_ Items, i int) string {
return fmt.Sprintf("%d.", i+1)
}

Expand All @@ -39,7 +59,7 @@ func Arabic(_ Data, i int) string {
// II. Bar
// III. Baz
// IV. Qux.
func Roman(_ Data, i int) string {
func Roman(_ Items, i int) string {
var (
roman = []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
arabic = []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
Expand All @@ -57,30 +77,30 @@ func Roman(_ Data, i int) string {

// Bullet is the enumeration for bullet listing.
//
// \• Foo
// \• Bar
// \• Baz
// \• Qux.
func Bullet(Data, int) string {
// • Foo
// • Bar
// • Baz
// • Qux.
func Bullet(Items, int) string {
return "•"
}

// Asterisk is an enumeration using asterisks.
//
// \* Foo
// \* Bar
// \* Baz
// \* Qux.
func Asterisk(Data, int) string {
// * Foo
// * Bar
// * Baz
// * Qux.
func Asterisk(Items, int) string {
return "*"
}

// Dash is an enumeration using dashes.
//
// \- Foo
// \- Bar
// \- Baz
// \- Qux.
func Dash(Data, int) string {
// - Foo
// - Bar
// - Baz
// - Qux.
func Dash(Items, int) string {
return "-"
}
Loading

0 comments on commit 1afeca0

Please sign in to comment.