Created
February 22, 2022 23:07
-
-
Save 76creates/8f529f2b975f3cd6dee1d2a70260d53e to your computer and use it in GitHub Desktop.
charmbracelet/lipgloss sample code for #69
This file contains hidden or 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
package main | |
import ( | |
tea "github.com/charmbracelet/bubbletea" | |
"github.com/charmbracelet/lipgloss" | |
"os" | |
) | |
type m struct { | |
s lipgloss.Style | |
x int | |
y int | |
rr []*r | |
} | |
type r struct { | |
s lipgloss.Style | |
cc []*c | |
} | |
type c struct { | |
s lipgloss.Style | |
val string | |
} | |
var ( | |
rBase = lipgloss.NewStyle().Background(lipgloss.Color("#6c5ce7")) | |
rAlt = lipgloss.NewStyle().Background(lipgloss.Color("#0984e3")) | |
sBase = lipgloss.NewStyle().Width(3).Height(1).Align(lipgloss.Center) | |
sAlt = lipgloss.NewStyle().Width(3).Height(1).Align(lipgloss.Center).Background(lipgloss.Color("#74b9ff")) | |
) | |
func main() { | |
_m := m{ | |
s: lipgloss.NewStyle().Bold(true), | |
x: 0, | |
y: 0, | |
rr: []*r{ | |
{ | |
s: rBase, | |
cc: []*c{ | |
{ | |
s: sBase, | |
val: "a", | |
}, | |
{ | |
s: sBase, | |
val: "b", | |
}, | |
}, | |
}, | |
{ | |
s: rBase, | |
cc: []*c{ | |
{ | |
s: sBase, | |
val: "c", | |
}, | |
{ | |
s: sBase, | |
val: "d", | |
}, | |
}, | |
}, | |
}, | |
} | |
p := tea.NewProgram(&_m) | |
if err := p.Start(); err != nil { | |
os.Exit(1) | |
} | |
} | |
func (_m *m) Init() tea.Cmd { | |
return nil | |
} | |
func (_m *m) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | |
switch msg := msg.(type) { | |
// Is it a key press? | |
case tea.KeyMsg: | |
switch msg.String() { | |
case "ctrl+c", "q": | |
return _m, tea.Quit | |
case "up": | |
if _m.y > 0 { | |
_m.y-- | |
} | |
case "down": | |
if _m.y < len(_m.rr)-1 { | |
_m.y++ | |
} | |
case "left": | |
if _m.x > 0 { | |
_m.x-- | |
} | |
case "right": | |
if _m.x+1 < len(_m.rr[_m.y].cc) { | |
_m.x++ | |
} | |
} | |
} | |
return _m, nil | |
} | |
func (_m *m) View() string { | |
var rScreen []string | |
for ir, r := range _m.rr { | |
if ir == _m.y { | |
r.s = rAlt | |
} else { | |
r.s = rBase | |
} | |
cScreen := []string{} | |
for ic, c := range r.cc { | |
if ir == _m.y && ic == _m.x { | |
c.s = sAlt.Copy().Inherit(r.s) | |
} else { | |
c.s = sBase.Copy().Inherit(r.s) | |
} | |
cScreen = append(cScreen, c.s.Render(c.val)) | |
} | |
rScreen = append(rScreen, lipgloss.JoinHorizontal(lipgloss.Top, cScreen...)) | |
} | |
return lipgloss.JoinVertical(lipgloss.Left, rScreen...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment