Skip to content

Commit

Permalink
feat: change styles
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Nov 27, 2023
1 parent 293eda8 commit 5a0119b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
24 changes: 24 additions & 0 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ func (l *List) Enumerator(enumerator Enumerator) *List {
return l
}

// Style sets the list style.
func (l *List) Style(style Style) *List {
l.style = style
return l
}

// EnumeratorStyle sets the enumerator style.
func (l *List) EnumeratorStyle(style lipgloss.Style) *List {
l.style.Enumerator = style
return l
}

// ItemStyle sets the item style.
func (l *List) ItemStyle(style lipgloss.Style) *List {
l.style.Item = style
return l
}

// BaseStyle sets the base style.
func (l *List) BaseStyle(style lipgloss.Style) *List {
l.style.Base = style
return l
}

// Indent sets the indent level.
func (l *List) Indent(indent int) *List {
if indent < 0 {
Expand Down
92 changes: 91 additions & 1 deletion list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package list
import (
"strings"
"testing"

"github.com/charmbracelet/lipgloss"
)

func TestList(t *testing.T) {
Expand Down Expand Up @@ -148,7 +150,7 @@ func TestDeepNestedList(t *testing.T) {
}
}

func TestEnumeration(t *testing.T) {
func TestEnumerators(t *testing.T) {
tests := []struct {
enumeration Enumerator
expected string
Expand Down Expand Up @@ -221,6 +223,94 @@ III. Baz
}
}

func TestEnumeratorsTransform(t *testing.T) {
tests := []struct {
enumeration Enumerator
style lipgloss.Style
expected string
}{
{
enumeration: Alphabet,
style: lipgloss.NewStyle().MarginRight(1).Transform(strings.ToLower),
expected: `
a. Foo
b. Bar
c. Baz
a. Qux
b. Quux
`,
},
{
enumeration: Arabic,
style: lipgloss.NewStyle().MarginRight(1).Transform(func(s string) string {
return strings.Replace(s, ".", ")", 1)
}),
expected: `
1) Foo
2) Bar
3) Baz
1) Qux
2) Quux
`,
},
{
enumeration: Roman,
style: lipgloss.NewStyle().Transform(func(s string) string {
return "(" + strings.Replace(strings.ToLower(s), ".", "", 1) + ") "
}),
expected: `
(i) Foo
(ii) Bar
(iii) Baz
(i) Qux
(ii) Quux
`,
},
{
enumeration: Bullet,
style: lipgloss.NewStyle().Transform(func(s string) string {
return "- " // this is better done by replacing the enumerator.
}),
expected: `
- Foo
- Bar
- Baz
- Qux
- Quux
`,
},
{
enumeration: Tree,
style: lipgloss.NewStyle().MarginRight(1).Transform(func(s string) string {
return strings.Replace(s, "─", "───", 1)
}),
expected: `
β”œβ”€β”€β”€ Foo
β”œβ”€β”€β”€ Bar
└─── Baz
β”œβ”€β”€β”€ Qux
└─── Quux
`,
},
}

for _, test := range tests {
expected := strings.TrimPrefix(test.expected, "\n")

l := New().
Enumerator(test.enumeration).
EnumeratorStyle(test.style).
Item("Foo").
Item("Bar").
Item("Baz").
Item(New("Qux", "Quux").Enumerator(test.enumeration).EnumeratorStyle(test.style))

if l.String() != expected {
t.Errorf("expected:\n\n%s\n\ngot:\n\n%s\n", expected, l.String())
}
}
}

func TestBullet(t *testing.T) {
tests := []struct {
enum Enumerator
Expand Down

0 comments on commit 5a0119b

Please sign in to comment.