Skip to content

Commit 3b9aaed

Browse files
committed
feat(examples): add impure examples
1 parent 94fe7b7 commit 3b9aaed

File tree

4 files changed

+223
-8
lines changed

4 files changed

+223
-8
lines changed

examples/go.mod

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ module examples
22

33
go 1.19
44

5-
replace github.com/charmbracelet/lipgloss => ../
6-
7-
replace github.com/charmbracelet/lipgloss/list => ../list
8-
9-
replace github.com/charmbracelet/lipgloss/table => ../table
5+
replace github.com/charmbracelet/lipgloss/v2 => ../
106

117
require (
12-
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20241016204941-d95e2750ec12
13-
github.com/charmbracelet/colorprofile v0.1.2
8+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20241031180054-a3bec0b7b8a8
9+
github.com/charmbracelet/colorprofile v0.1.6
1410
github.com/charmbracelet/lipgloss v0.13.1-0.20240822211938-b89f1a3db2a4
11+
github.com/charmbracelet/lipgloss/v2 v2.0.0-00010101000000-000000000000
1512
github.com/charmbracelet/ssh v0.0.0-20240401141849-854cddfa2917
1613
github.com/charmbracelet/wish v1.4.0
1714
github.com/charmbracelet/x/term v0.2.0
@@ -25,7 +22,7 @@ require (
2522
github.com/charmbracelet/bubbletea v0.25.0 // indirect
2623
github.com/charmbracelet/keygen v0.5.0 // indirect
2724
github.com/charmbracelet/log v0.4.0 // indirect
28-
github.com/charmbracelet/x/ansi v0.3.2 // indirect
25+
github.com/charmbracelet/x/ansi v0.4.2 // indirect
2926
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 // indirect
3027
github.com/charmbracelet/x/exp/term v0.0.0-20240328150354-ab9afc214dfd // indirect
3128
github.com/charmbracelet/x/input v0.2.0 // indirect

examples/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt
77
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
88
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20241016204941-d95e2750ec12 h1:w1Dt/pl0gXE6PO2U+EUMoeMiSHe07/RR1Slcy9cH6kQ=
99
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20241016204941-d95e2750ec12/go.mod h1:8vhUTCIihfP3GfyyfznbOU7XyFe/ID1n7pdh/uT5Rug=
10+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20241031180054-a3bec0b7b8a8 h1:DQCVs696jYRcewAVwTkT/jJnE150/YrmTcCOvrz/J6U=
11+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20241031180054-a3bec0b7b8a8/go.mod h1:/w0ccIhyZqnVm+47JBkA/872fruM99gHxXTibc2kEMY=
1012
github.com/charmbracelet/colorprofile v0.1.2 h1:nuB1bd/yAExT4fkcZvpqtQ2N5/8cJHSRIKb6CzT7lAM=
1113
github.com/charmbracelet/colorprofile v0.1.2/go.mod h1:1htIKZYeI4TQs+OykPvpuBTUbUJxBYeSYBDIZuejMj0=
14+
github.com/charmbracelet/colorprofile v0.1.6 h1:nMMqCns0c0DfCwNGdagBh6SxutFqkltSxxKk5S9kt+Y=
15+
github.com/charmbracelet/colorprofile v0.1.6/go.mod h1:3EMXDxwRDJl0c17eJ1jX99MhtlP9OxE/9Qw0C5lvyUg=
1216
github.com/charmbracelet/keygen v0.5.0 h1:XY0fsoYiCSM9axkrU+2ziE6u6YjJulo/b9Dghnw6MZc=
1317
github.com/charmbracelet/keygen v0.5.0/go.mod h1:DfvCgLHxZ9rJxdK0DGw3C/LkV4SgdGbnliHcObV3L+8=
18+
github.com/charmbracelet/lipgloss v0.13.1-0.20240822211938-b89f1a3db2a4 h1:BuZojjzjQ89wObGNVxJzFqyeh2S6Crbwhybb8bZNveI=
19+
github.com/charmbracelet/lipgloss v0.13.1-0.20240822211938-b89f1a3db2a4/go.mod h1:ZbN5GVH/VrXHPRa1lZqZ1HWJyayJHJYaf9ywsCuI9zQ=
1420
github.com/charmbracelet/log v0.4.1-0.20241010222913-47ce960d4847 h1:3uto8OPDaseTS92giSw/8GfVQ+TCjcVE+QOaf8lT0Zc=
1521
github.com/charmbracelet/log v0.4.1-0.20241010222913-47ce960d4847/go.mod h1:3UCs04fasl8pA1HoNNJ8HCYCaKoeMBZrNXo9iy5F0vY=
1622
github.com/charmbracelet/ssh v0.0.0-20240401141849-854cddfa2917 h1:NZKjJ7d/pzk/AfcJYEzmF8M48JlIrrY00RR5JdDc3io=
@@ -19,6 +25,8 @@ github.com/charmbracelet/wish v1.4.0 h1:pL1uVP/YuYgJheHEj98teZ/n6pMYnmlZq/fcHvom
1925
github.com/charmbracelet/wish v1.4.0/go.mod h1:ew4/MjJVfW/akEO9KmrQHQv1F7bQRGscRMrA+KtovTk=
2026
github.com/charmbracelet/x/ansi v0.3.2 h1:wsEwgAN+C9U06l9dCVMX0/L3x7ptvY1qmjMwyfE6USY=
2127
github.com/charmbracelet/x/ansi v0.3.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
28+
github.com/charmbracelet/x/ansi v0.4.2 h1:0JM6Aj/g/KC154/gOP4vfxun0ff6itogDYk41kof+qk=
29+
github.com/charmbracelet/x/ansi v0.4.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
2230
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 h1:3RXpZWGWTOeVXCTv0Dnzxdv/MhNUkBfEcbaTY0zrTQI=
2331
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651/go.mod h1:2P0UgXMEa6TsToMSuFqKFQR+fZTO9CNGUNokkPatT/0=
2432
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a h1:G99klV19u0QnhiizODirwVksQB91TJKV/UaTnACcG30=

examples/impure/bubbletea/main.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
tea "github.com/charmbracelet/bubbletea/v2"
8+
"github.com/charmbracelet/lipgloss/v2"
9+
"github.com/charmbracelet/lipgloss/v2/impure"
10+
)
11+
12+
var (
13+
frameColor = impure.AdaptiveColor{Light: lipgloss.Color("#C5ADF9"), Dark: lipgloss.Color("#864EFF")}
14+
textColor = impure.AdaptiveColor{Light: lipgloss.Color("#696969"), Dark: lipgloss.Color("#bdbdbd")}
15+
keywordColor = impure.AdaptiveColor{Light: lipgloss.Color("#37CD96"), Dark: lipgloss.Color("#22C78A")}
16+
inactiveBgColor = impure.AdaptiveColor{Light: lipgloss.Color(0x988F95), Dark: lipgloss.Color(0x978692)}
17+
inactiveFgColor = impure.AdaptiveColor{Light: lipgloss.Color(0xFDFCE3), Dark: lipgloss.Color(0xFBFAE7)}
18+
)
19+
20+
// Style definitions.
21+
type styles struct {
22+
frame,
23+
paragraph,
24+
text,
25+
keyword,
26+
activeButton,
27+
inactiveButton lipgloss.Style
28+
}
29+
30+
// Styles are initialized based on the background color of the terminal.
31+
func newStyles() (s styles) {
32+
// Define some styles. adaptive.Color() can be used to choose the
33+
// appropriate light or dark color based on the detected background color.
34+
s.frame = lipgloss.NewStyle().
35+
Border(lipgloss.RoundedBorder()).
36+
BorderForeground(frameColor).
37+
Padding(1, 3).
38+
Margin(1, 3)
39+
s.paragraph = lipgloss.NewStyle().
40+
Width(40).
41+
MarginBottom(1).
42+
Align(lipgloss.Center)
43+
s.text = lipgloss.NewStyle().
44+
Foreground(textColor)
45+
s.keyword = lipgloss.NewStyle().
46+
Foreground(keywordColor).
47+
Bold(true)
48+
49+
s.activeButton = lipgloss.NewStyle().
50+
Padding(0, 3).
51+
Background(lipgloss.Color(0xFF6AD2)). // you can also use octal format for colors, i.e 0xff38ec.
52+
Foreground(lipgloss.Color(0xFFFCC2))
53+
s.inactiveButton = s.activeButton.
54+
Background(inactiveBgColor).
55+
Foreground(inactiveFgColor)
56+
return s
57+
}
58+
59+
type model struct {
60+
styles styles
61+
yes bool
62+
chosen bool
63+
aborted bool
64+
}
65+
66+
func (m model) Init() (tea.Model, tea.Cmd) {
67+
m.yes = true
68+
m.styles = newStyles()
69+
return m, nil
70+
}
71+
72+
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
73+
switch msg := msg.(type) {
74+
case tea.KeyPressMsg:
75+
switch msg.String() {
76+
case "q", "esc", "ctrl+c":
77+
m.aborted = true
78+
return m, tea.Quit
79+
case "enter":
80+
m.chosen = true
81+
return m, tea.Quit
82+
case "left", "right", "h", "l":
83+
m.yes = !m.yes
84+
case "y":
85+
m.yes = true
86+
m.chosen = true
87+
return m, tea.Quit
88+
case "n":
89+
m.yes = false
90+
m.chosen = true
91+
return m, tea.Quit
92+
}
93+
}
94+
95+
return m, nil
96+
}
97+
98+
func (m model) View() string {
99+
if m.chosen || m.aborted {
100+
// We're about to exit, so wipe the UI.
101+
return ""
102+
}
103+
104+
var (
105+
s = m.styles
106+
y = "Yes"
107+
n = "No"
108+
)
109+
110+
if m.yes {
111+
y = s.activeButton.Render(y)
112+
n = s.inactiveButton.Render(n)
113+
} else {
114+
y = s.inactiveButton.Render(y)
115+
n = s.activeButton.Render(n)
116+
}
117+
118+
return s.frame.Render(
119+
lipgloss.JoinVertical(lipgloss.Center,
120+
s.paragraph.Render(
121+
s.text.Render("Are you sure you want to eat that ")+
122+
s.keyword.Render("moderatly ripe")+
123+
s.text.Render(" banana?"),
124+
),
125+
y+" "+n,
126+
),
127+
)
128+
}
129+
130+
func main() {
131+
m, err := tea.NewProgram(model{}).Run()
132+
if err != nil {
133+
fmt.Fprintf(os.Stderr, "Uh oh: %v", err)
134+
os.Exit(1)
135+
}
136+
137+
if m := m.(model); m.chosen {
138+
if m.yes {
139+
fmt.Println("Are you sure? It's not ripe yet.")
140+
} else {
141+
fmt.Println("Well, alright. It was probably good, though.")
142+
}
143+
}
144+
}

examples/impure/standalone/main.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This example illustrates how to detect the terminal's background color and
2+
// choose either light or dark colors accordingly when using Lip Gloss in a.
3+
// standalone fashion, i.e. independent of Bubble Tea.
4+
//
5+
// For an example of how to do this in a Bubble Tea program, see the
6+
// 'bubbletea' example.
7+
package main
8+
9+
import (
10+
"github.com/charmbracelet/lipgloss/v2"
11+
"github.com/charmbracelet/lipgloss/v2/impure"
12+
)
13+
14+
var (
15+
frameColor = impure.AdaptiveColor{Light: lipgloss.Color("#C5ADF9"), Dark: lipgloss.Color("#864EFF")}
16+
textColor = impure.AdaptiveColor{Light: lipgloss.Color("#696969"), Dark: lipgloss.Color("#bdbdbd")}
17+
keywordColor = impure.AdaptiveColor{Light: lipgloss.Color("#37CD96"), Dark: lipgloss.Color("#22C78A")}
18+
inactiveBgColor = impure.AdaptiveColor{Light: lipgloss.Color(0x988F95), Dark: lipgloss.Color(0x978692)}
19+
inactiveFgColor = impure.AdaptiveColor{Light: lipgloss.Color(0xFDFCE3), Dark: lipgloss.Color(0xFBFAE7)}
20+
)
21+
22+
func main() {
23+
// Define some styles. adaptive.Color() can be used to choose the
24+
// appropriate light or dark color based on the detected background color.
25+
frameStyle := lipgloss.NewStyle().
26+
Border(lipgloss.RoundedBorder()).
27+
BorderForeground(frameColor).
28+
Padding(1, 3).
29+
Margin(1, 3)
30+
paragraphStyle := lipgloss.NewStyle().
31+
Width(40).
32+
MarginBottom(1).
33+
Align(lipgloss.Center)
34+
textStyle := lipgloss.NewStyle().
35+
Foreground(textColor)
36+
keywordStyle := lipgloss.NewStyle().
37+
Foreground(keywordColor).
38+
Bold(true)
39+
40+
activeButton := lipgloss.NewStyle().
41+
Padding(0, 3).
42+
Background(lipgloss.Color(0xFF6AD2)). // you can also use octal format for colors, i.e 0xff38ec.
43+
Foreground(lipgloss.Color(0xFFFCC2))
44+
inactiveButton := activeButton.
45+
Background(inactiveBgColor).
46+
Foreground(inactiveFgColor)
47+
48+
// Build layout.
49+
text := paragraphStyle.Render(
50+
textStyle.Render("Are you sure you want to eat that ") +
51+
keywordStyle.Render("moderatly ripe") +
52+
textStyle.Render(" banana?"),
53+
)
54+
buttons := activeButton.Render("Yes") + " " + inactiveButton.Render("No")
55+
block := frameStyle.Render(
56+
lipgloss.JoinVertical(lipgloss.Center, text, buttons),
57+
)
58+
59+
// Print the block to stdout. It's important to use Lip Gloss's print
60+
// functions to ensure that colors are downsampled correctly. If output
61+
// isn't a TTY (i.e. we're logging to a file) colors will be stripped
62+
// entirely.
63+
//
64+
// Note that in Bubble Tea downsampling happens automatically.
65+
impure.Println(block)
66+
}

0 commit comments

Comments
 (0)