Compare commits
10 commits
main
...
v2-viewabl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c76fa36f1b | ||
|
|
43689cfc01 | ||
|
|
5681225ad3 | ||
|
|
c64c67337d | ||
|
|
9ee956418d | ||
|
|
0f41736c4d | ||
|
|
434f93986a | ||
|
|
3cd4d5d09d | ||
|
|
39fb053ee7 | ||
|
|
204268b00c |
5 changed files with 196 additions and 8 deletions
93
examples/canvas/main.go
Normal file
93
examples/canvas/main.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea/v2"
|
||||
"github.com/charmbracelet/lipgloss/v2"
|
||||
"github.com/charmbracelet/x/exp/charmtone"
|
||||
)
|
||||
|
||||
type model struct {
|
||||
width int
|
||||
flip bool
|
||||
quitting bool
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
m.width = msg.Width
|
||||
return m, nil
|
||||
case tea.KeyPressMsg:
|
||||
switch msg.String() {
|
||||
case "q", "ctrl+c", "esc":
|
||||
m.quitting = true
|
||||
return m, tea.Quit
|
||||
default:
|
||||
m.flip = !m.flip
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m model) View() tea.View {
|
||||
if m.quitting {
|
||||
return tea.NewView("")
|
||||
}
|
||||
|
||||
z := []int{0, 1}
|
||||
if m.flip {
|
||||
z = reverse(z)
|
||||
}
|
||||
|
||||
footer := lipgloss.NewStyle().
|
||||
Height(13).
|
||||
Foreground(charmtone.Oyster).
|
||||
AlignVertical(lipgloss.Bottom).
|
||||
Render("Press any key to swap the cards, or q to quit.")
|
||||
|
||||
cardA := newCard("Hello").Z(z[0])
|
||||
cardB := newCard("Goodbye").Z(z[1])
|
||||
|
||||
c := lipgloss.NewCanvas(
|
||||
lipgloss.NewLayer(footer),
|
||||
cardA,
|
||||
cardB.X(10).Y(2),
|
||||
)
|
||||
return tea.NewView(c)
|
||||
}
|
||||
|
||||
func newCard(str string) *lipgloss.Layer {
|
||||
return lipgloss.NewLayer(
|
||||
lipgloss.NewStyle().
|
||||
Width(20).
|
||||
Height(10).
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderForeground(charmtone.Charple).
|
||||
Align(lipgloss.Center, lipgloss.Center).
|
||||
Render(str),
|
||||
)
|
||||
}
|
||||
|
||||
// Reverse a slice, returning a new slice.
|
||||
func reverse[T any](s []T) []T {
|
||||
n := len(s)
|
||||
r := make([]T, n)
|
||||
for i, v := range s {
|
||||
r[n-1-i] = v
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
if _, err := tea.NewProgram(model{}).Run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Urgh:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
@ -59,19 +59,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
return m, cmd
|
||||
}
|
||||
|
||||
func (m model) View() (string, *tea.Cursor) {
|
||||
func (m model) View() tea.View {
|
||||
var c *tea.Cursor
|
||||
if !m.textInput.VirtualCursor() {
|
||||
c = m.textInput.Cursor()
|
||||
c.Y += lipgloss.Height(m.headerView())
|
||||
}
|
||||
|
||||
str := lipgloss.JoinVertical(lipgloss.Top, m.headerView(), m.textInput.View(), m.footerView())
|
||||
if m.quitting {
|
||||
str += "\n"
|
||||
}
|
||||
|
||||
return str, c
|
||||
v := tea.NewView(str)
|
||||
v.SetCursor(c)
|
||||
return v
|
||||
}
|
||||
|
||||
func (m model) headerView() string { return "What’s your favorite Pokémon?\n" }
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -6,6 +6,7 @@ toolchain go1.23.7
|
|||
|
||||
require (
|
||||
github.com/charmbracelet/colorprofile v0.3.1
|
||||
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1.0.20250523195325-2d1af06b557c
|
||||
github.com/charmbracelet/x/ansi v0.9.3-0.20250602153603-fb931ed90413
|
||||
github.com/charmbracelet/x/cellbuf v0.0.14-0.20250505150409-97991a1f17d1
|
||||
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -2,6 +2,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp
|
|||
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
|
||||
github.com/charmbracelet/colorprofile v0.3.1 h1:k8dTHMd7fgw4bnFd7jXTLZrSU/CQrKnL3m+AxCzDz40=
|
||||
github.com/charmbracelet/colorprofile v0.3.1/go.mod h1:/GkGusxNs8VB/RSOh3fu0TJmQ4ICMMPApIIVn0KszZ0=
|
||||
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1.0.20250523195325-2d1af06b557c h1:177KMz8zHRlEZJsWzafbKYh6OdjgvTspoH+UjaxgIXY=
|
||||
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1.0.20250523195325-2d1af06b557c/go.mod h1:EJWvaCrhOhNGVZMvcjc0yVryl4qqpMs8tz0r9WyEkdQ=
|
||||
github.com/charmbracelet/x/ansi v0.9.2 h1:92AGsQmNTRMzuzHEYfCdjQeUzTrgE1vfO5/7fEVoXdY=
|
||||
github.com/charmbracelet/x/ansi v0.9.2/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=
|
||||
github.com/charmbracelet/x/ansi v0.9.3-0.20250602153603-fb931ed90413 h1:L07QkDqRF274IZ2UJ/mCTL8DR95efU9BNWLYCDXEjvQ=
|
||||
|
|
|
|||
101
tea.go
101
tea.go
|
|
@ -25,6 +25,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/charmbracelet/colorprofile"
|
||||
"github.com/charmbracelet/lipgloss/v2"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
"github.com/charmbracelet/x/input"
|
||||
"github.com/charmbracelet/x/term"
|
||||
|
|
@ -105,6 +106,74 @@ type CursorModel interface {
|
|||
View() (string, *Cursor)
|
||||
}
|
||||
|
||||
// View represents a view in a program.
|
||||
type View struct {
|
||||
body string
|
||||
canvas *lipgloss.Canvas
|
||||
cursor *Cursor
|
||||
bgColor color.Color
|
||||
}
|
||||
|
||||
// ViewConstructor is a type constraint for the [NewView] function.
|
||||
type ViewConstructor interface {
|
||||
string | *lipgloss.Canvas
|
||||
}
|
||||
|
||||
// NewView is a helper function to create a new view. It takes a string,
|
||||
// representing the general portion of the user interface.
|
||||
func NewView[T ViewConstructor](frame T) View {
|
||||
var v View
|
||||
switch t := any(frame).(type) {
|
||||
case string:
|
||||
v.body = t
|
||||
case *lipgloss.Canvas:
|
||||
v.canvas = t
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// String returns the main body of the view, without additional metadata like
|
||||
// the cursor.
|
||||
func (v View) String() string {
|
||||
if v.canvas != nil {
|
||||
return v.canvas.Render()
|
||||
}
|
||||
return v.body
|
||||
}
|
||||
|
||||
// SetCursor renders a cursor. If nil, the cursor will be hidden. Use [NewCursor]
|
||||
// to create a cursor for a given position with default styles.
|
||||
func (v *View) SetCursor(c *Cursor) {
|
||||
v.cursor = c
|
||||
}
|
||||
|
||||
// Cursor returns the cursor of the view. If the view does not have a
|
||||
// cursor, it returns nil.
|
||||
func (v View) Cursor() *Cursor {
|
||||
return v.cursor
|
||||
}
|
||||
|
||||
// SetBackgroundColor sets the background color of the terminal window. If nil,
|
||||
// the background color will be removed.
|
||||
func (v *View) SetBackgroundColor(c color.Color) {
|
||||
v.bgColor = c
|
||||
}
|
||||
|
||||
// BackgroundColor returns the background color of the view. If the view does
|
||||
// not have a background color, it returns nil.
|
||||
func (v View) BackgroundColor() color.Color {
|
||||
return v.bgColor
|
||||
}
|
||||
|
||||
// Viewable is an optional interface that can be implemented by the main model
|
||||
// to provide a [View]. If the main model does not implement a view interface,
|
||||
// the program won't render anything.
|
||||
//
|
||||
// Use [NewView] to create a new view.
|
||||
type Viewable interface {
|
||||
View() View
|
||||
}
|
||||
|
||||
// Cmd is an IO operation that returns a message when it's complete. If it's
|
||||
// nil it's considered a no-op. Use it for things like HTTP requests, timers,
|
||||
// saving and loading from disk, and so on.
|
||||
|
|
@ -279,6 +348,9 @@ type Program struct {
|
|||
// when the program is resumed.
|
||||
setBg, setFg, setCc color.Color
|
||||
|
||||
// Lip Gloss canvas instance, used for hit detection.
|
||||
canvas *lipgloss.Canvas
|
||||
|
||||
// Initial window size. Mainly used for testing.
|
||||
width, height int
|
||||
|
||||
|
|
@ -780,7 +852,7 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
|
|||
// hasView returns true if the model has a view.
|
||||
func hasView(model Model) (ok bool) {
|
||||
switch model.(type) {
|
||||
case ViewModel, CursorModel:
|
||||
case Viewable, ViewModel, CursorModel:
|
||||
ok = true
|
||||
}
|
||||
return
|
||||
|
|
@ -788,13 +860,32 @@ func hasView(model Model) (ok bool) {
|
|||
|
||||
// render renders the given view to the renderer.
|
||||
func (p *Program) render(model Model) {
|
||||
var view string
|
||||
var frame string
|
||||
var cur *Cursor
|
||||
switch model := model.(type) {
|
||||
case Viewable:
|
||||
v := model.View()
|
||||
frame = v.String()
|
||||
cur = v.cursor
|
||||
p.canvas = v.canvas // store or clear the lipgloss.Canvas
|
||||
|
||||
// Set or clear the background color.
|
||||
c := v.bgColor
|
||||
if c != p.setBg {
|
||||
if c != nil {
|
||||
col, ok := colorful.MakeColor(c)
|
||||
if ok {
|
||||
p.execute(ansi.SetBackgroundColor(col.Hex()))
|
||||
}
|
||||
} else {
|
||||
p.execute(ansi.ResetBackgroundColor)
|
||||
}
|
||||
p.setBg = c
|
||||
}
|
||||
case ViewModel:
|
||||
view = model.View()
|
||||
frame = model.View()
|
||||
case CursorModel:
|
||||
view, cur = model.View()
|
||||
frame, cur = model.View()
|
||||
}
|
||||
|
||||
// Ensure we reset the cursor color on exit.
|
||||
|
|
@ -802,7 +893,7 @@ func (p *Program) render(model Model) {
|
|||
p.setCc = cur.Color
|
||||
}
|
||||
|
||||
p.renderer.render(view, cur) // send view to renderer
|
||||
p.renderer.render(frame, cur) // send view to renderer
|
||||
}
|
||||
|
||||
// Run initializes the program and runs its event loops, blocking until it gets
|
||||
|
|
|
|||
Loading…
Reference in a new issue