refactor: use lipgloss copy for example (#308)

This commit is contained in:
Maas Lalani 2024-05-30 18:37:17 -04:00 committed by GitHub
parent 70c908de3e
commit 6e07abb123
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 212 additions and 125 deletions

View File

@ -4,6 +4,10 @@ go 1.18
replace github.com/charmbracelet/lipgloss => ../
replace github.com/charmbracelet/lipgloss/tree => ../tree
replace github.com/charmbracelet/lipgloss/list => ../list
require (
github.com/charmbracelet/lipgloss v0.4.0
github.com/charmbracelet/wish v0.5.0

View File

@ -25,7 +25,7 @@ var docs = []Document{
{"secrets.md", "1 week ago"},
}
const selectedIndex = 1
const selected = 1
func main() {
baseStyle := lipgloss.NewStyle().
@ -36,20 +36,20 @@ func main() {
l := list.New().
Enumerator(func(_ list.Items, i int) string {
if i == selectedIndex {
if i == selected {
return "│\n│"
}
return " "
}).
ItemStyleFunc(func(_ list.Items, i int) lipgloss.Style {
st := baseStyle
if selectedIndex == i {
if selected == i {
return st.Foreground(hightlightColor)
}
return st.Foreground(dimColor)
}).
EnumeratorStyleFunc(func(_ list.Items, i int) lipgloss.Style {
if selectedIndex == i {
if selected == i {
return lipgloss.NewStyle().Foreground(hightlightColor)
}
return lipgloss.NewStyle().Foreground(dimColor)

View File

@ -7,151 +7,234 @@ import (
"github.com/charmbracelet/lipgloss/list"
"github.com/charmbracelet/lipgloss/table"
"github.com/charmbracelet/lipgloss/tree"
"github.com/lucasb-eyer/go-colorful"
)
// This example abuses both lists and trees.
// Its a list that goes deep, and items varies from other lists, trees, tables,
// etc.
func main() {
style1 := lipgloss.NewStyle().
purple := lipgloss.NewStyle().
Foreground(lipgloss.Color("99")).
MarginRight(1)
style2 := lipgloss.NewStyle().
pink := lipgloss.NewStyle().
Foreground(lipgloss.Color("212")).
MarginRight(1)
baseStyle := lipgloss.NewStyle().
base := lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(1)
dimColor := lipgloss.Color("250")
hightlightColor := lipgloss.Color("#EE6FF8")
faint := lipgloss.NewStyle().Faint(true)
dim := lipgloss.Color("250")
highlight := lipgloss.Color("#EE6FF8")
special := lipgloss.AdaptiveColor{Light: "#43BF6D", Dark: "#73F59F"}
checklistEnumStyle := func(items list.Items, index int) lipgloss.Style {
switch index {
case 1, 2, 4:
return lipgloss.NewStyle().
Foreground(special).
PaddingRight(1)
default:
return lipgloss.NewStyle().PaddingRight(1)
}
}
checklistEnum := func(items list.Items, index int) string {
switch index {
case 1, 2, 4:
return "✓"
default:
return "•"
}
}
checklistStyle := func(items list.Items, index int) lipgloss.Style {
switch index {
case 1, 2, 4:
return lipgloss.NewStyle().
Strikethrough(true).
Foreground(lipgloss.AdaptiveColor{Light: "#969B86", Dark: "#696969"})
default:
return lipgloss.NewStyle()
}
}
colors := colorGrid(1, 5)
titleStyle := lipgloss.NewStyle().
Italic(true).
Foreground(lipgloss.Color("#FFF7DB"))
lipglossStyleFunc := func(items list.Items, index int) lipgloss.Style {
return titleStyle.Padding(0, index+1).Background(lipgloss.Color(colors[index][0]))
}
history := "Medieval quince preserves, which went by the French name cotignac, produced in a clear version and a fruit pulp version, began to lose their medieval seasoning of spices in the 16th century. In the 17th century, La Varenne provided recipes for both thick and clear cotignac."
l := list.New().
Item("Item 1").
Item("Item 2").
Item(list.New("Item 2.1", "Item 2.2")).
Item("Item 3").
Item(
list.New("Item 3.1", "Item 3.2").
EnumeratorStyle(style1).
Enumerator(list.Roman),
).
Item("Item 4").
EnumeratorStyle(purple).
Item("Lip Gloss").
Item("Blush").
Item("Eye Shadow").
Item("Mascara").
Item("Foundation").
Item(
list.New().
EnumeratorStyle(style2).
Enumerator(list.Alphabet).
Item("Item 4.1").
Item("Item 4.2").
EnumeratorStyle(pink).
Item("Citrus Fruits to Try").
Item(
list.New().
EnumeratorStyle(style1).
Enumerator(list.Arabic).
Item("Item 4.2.1").
Item("Item 4.2.2").
Item("Item 4.2.3").
ItemStyleFunc(checklistStyle).
EnumeratorStyleFunc(checklistEnumStyle).
Enumerator(checklistEnum).
Item("Grapefruit").
Item("Yuzu").
Item("Citron").
Item("Kumquat").
Item("Pomelo"),
).
Item("Actual Lip Gloss Vendors").
Item(
list.New().
ItemStyleFunc(checklistStyle).
EnumeratorStyleFunc(checklistEnumStyle).
Enumerator(checklistEnum).
Item("Glossier").
Item("Claires Boutique").
Item("Nyx").
Item("Mac").
Item("Milk").
Item(
list.New().
Enumerator(list.Asterisk).
EnumeratorStyle(style2).
Item("Item 4.2.3.1").
Item("Item 4.2.3.3").
Item("Item 4.2.3.4").
Item("Item 4.2.3.5").
EnumeratorStyle(purple).
Enumerator(list.Dash).
ItemStyleFunc(lipglossStyleFunc).
Item("Lip Gloss").
Item("Lip Gloss").
Item("Lip Gloss").
Item("Lip Gloss").
Item(
list.New().
EnumeratorStyle(style1).
Enumerator(list.Dash).
Item("Item 4.2.3.3.1\nis a multiline string").
Item("Item 4.2.3.3.2").
Item("Item 4.2.3.3.3").
Item("Item 4.2.3.3.4").
Item(
tree.New().
EnumeratorStyle(lipgloss.NewStyle().Foreground(lipgloss.Color(colors[4][0])).MarginRight(1)).
Child("\nStyle Definitions for Nice Terminal Layouts\n─────").
Child("From Charm").
Child("https://github.com/charmbracelet/lipgloss").
Child(
tree.New().
EnumeratorStyle(style2).
Item("Item 4.2.3.3.4.1\nThis is a tree within a list.\nLists are actually syntactic sugar for trees!").
Item("Item 4.2.3.3.4.2").
Item("Item 4.2.3.3.4.3").
Item(
tree.New().
EnumeratorStyle(style2).
Item("Item 4.2.3.3.4.3.1\nanother tree").
Item("Item 4.2.3.3.4.3.2").
Item(
lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#7D56F4")).
AlignHorizontal(lipgloss.Center).
AlignVertical(lipgloss.Center).
Padding(1, 3).
Width(40).
Render("Item 4.2.3.3.4.3.3\n\nItems can be any string, including tables!"),
).
Item(
list.New("A list within a tree", "a", "b", "c").
Enumerator(list.Roman),
).
Item(
table.New().
Width(40).
BorderStyle(style1.MarginRight(0)).
StyleFunc(func(row, col int) lipgloss.Style {
style := lipgloss.NewStyle()
if col == 1 {
style = style.Align(lipgloss.Center)
}
if row == 0 {
return style.Bold(true)
}
return style.Faint(true)
}).
Headers("ITEM", "QTY").
Row("Banana", "10").
Row("Orange", "2").
Row("Apple", "6").
Row("Strawberry", "12"),
).
Item("Item 4.2.3.3.4.3.3").
Item(
list.New().
Enumerator(func(_ list.Items, i int) string {
if i == 1 {
return "│\n│"
}
return " "
}).
ItemStyleFunc(func(_ list.Items, i int) lipgloss.Style {
st := baseStyle
if i == 1 {
return st.Foreground(hightlightColor)
}
return st.Foreground(dimColor)
}).
EnumeratorStyleFunc(func(_ list.Items, i int) lipgloss.Style {
if i == 1 {
return lipgloss.NewStyle().Foreground(hightlightColor)
}
return lipgloss.NewStyle().Foreground(dimColor)
}).
Item("Item a\n1 day ago").
Item("Item b\n2 days ago").
Item("Item c\n10 minutes ago").
Item("Item d\n1 month ago"),
).
Item("Item 4.2.3.3.4.3.4"),
EnumeratorStyle(lipgloss.NewStyle().Foreground(lipgloss.Color(colors[3][0])).MarginRight(1)).
Child("Emperors: Julio-Claudian dynasty").
Child(
lipgloss.NewStyle().Padding(1).Render(
list.New(
"Augustus",
"Tiberius",
"Caligula",
"Claudius",
"Nero",
).Enumerator(list.Roman).String(),
),
).
Item("Item 4.2.3.3.4.4").
Item("Item 4.2.3.3.4.5"),
Child(
lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#7D56F4")).
AlignHorizontal(lipgloss.Center).
AlignVertical(lipgloss.Center).
Padding(1, 3).
Margin(0, 1, 1, 1).
Width(40).
Render(history),
).
Child(
table.New().
Width(30).
BorderStyle(purple.MarginRight(0)).
StyleFunc(func(row, col int) lipgloss.Style {
style := lipgloss.NewStyle()
if col == 0 {
style = style.Align(lipgloss.Center)
} else {
style = style.Align(lipgloss.Right).PaddingRight(2)
}
if row == 0 {
return style.Bold(true).Align(lipgloss.Center).PaddingRight(0)
}
return style.Faint(true)
}).
Headers("ITEM", "QUANTITY").
Row("Apple", "6").
Row("Banana", "10").
Row("Orange", "2").
Row("Strawberry", "12"),
).
Child("Documents").
Child(
list.New().
Enumerator(func(_ list.Items, i int) string {
if i == 1 {
return "│\n│"
}
return " "
}).
ItemStyleFunc(func(_ list.Items, i int) lipgloss.Style {
if i == 1 {
return base.Foreground(highlight)
}
return base.Foreground(dim)
}).
EnumeratorStyleFunc(func(_ list.Items, i int) lipgloss.Style {
if i == 1 {
return lipgloss.NewStyle().Foreground(highlight)
}
return lipgloss.NewStyle().Foreground(dim)
}).
Item("Foo Document\n" + faint.Render("1 day ago")).
Item("Bar Document\n" + faint.Render("2 days ago")).
Item("Baz Document\n" + faint.Render("10 minutes ago")).
Item("Qux Document\n" + faint.Render("1 month ago")),
).
Child("EOF"),
).
Item("Item 4.2.3.3.5"),
),
Child("go get github.com/charmbracelet/lipgloss/list\n"),
).
Item("Lip Gloss"),
),
).
Item("Item 4.3"),
Item("List"),
).
Item("item 5")
Item("xoxo, Charm_™")
fmt.Println(l)
}
func colorGrid(xSteps, ySteps int) [][]string {
x0y0, _ := colorful.Hex("#F25D94")
x1y0, _ := colorful.Hex("#EDFF82")
x0y1, _ := colorful.Hex("#643AFF")
x1y1, _ := colorful.Hex("#14F9D5")
x0 := make([]colorful.Color, ySteps)
for i := range x0 {
x0[i] = x0y0.BlendLuv(x0y1, float64(i)/float64(ySteps))
}
x1 := make([]colorful.Color, ySteps)
for i := range x1 {
x1[i] = x1y0.BlendLuv(x1y1, float64(i)/float64(ySteps))
}
grid := make([][]string, ySteps)
for x := 0; x < ySteps; x++ {
y0 := x0[x]
grid[x] = make([]string, xSteps)
for y := 0; y < xSteps; y++ {
grid[x][y] = y0.BlendLuv(x1[x], float64(y)/float64(xSteps)).Hex()
}
}
return grid
}