main.go

 1package main
 2
 3import (
 4	"fmt"
 5	"image/color"
 6	"os"
 7
 8	tea "github.com/charmbracelet/bubbletea/v2"
 9	anim "github.com/charmbracelet/crush/internal/tui/components/anim"
10	"github.com/charmbracelet/crush/internal/tui/styles"
11	"github.com/charmbracelet/lipgloss/v2"
12)
13
14type model struct {
15	anim     tea.Model
16	bgColor  color.Color
17	quitting bool
18	w, h     int
19}
20
21func (m model) Init() tea.Cmd {
22	return m.anim.Init()
23}
24
25func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
26	switch msg := msg.(type) {
27	case tea.WindowSizeMsg:
28		m.w, m.h = msg.Width, msg.Height
29		return m, nil
30	case tea.KeyMsg:
31		switch msg.String() {
32		case "q", "ctrl+c":
33			m.quitting = true
34			return m, tea.Quit
35		default:
36			return m, nil
37		}
38	case anim.StepMsg:
39		var cmd tea.Cmd
40		m.anim, cmd = m.anim.Update(msg)
41		return m, cmd
42	default:
43		return m, nil
44	}
45}
46
47func (m model) View() tea.View {
48	if m.w == 0 || m.h == 0 {
49		return tea.NewView("")
50	}
51
52	v := tea.NewView("")
53	v.BackgroundColor = m.bgColor
54
55	if m.quitting {
56		return v
57	}
58
59	if a, ok := m.anim.(anim.Anim); ok {
60		l := lipgloss.NewLayer(a.View().String()).
61			Width(a.Width()).
62			X(m.w/2 - a.Width()/2).
63			Y(m.h / 2)
64
65		v = tea.NewView(lipgloss.NewCanvas(l))
66		v.BackgroundColor = m.bgColor
67		return v
68	}
69	return v
70}
71
72func main() {
73	t := styles.CurrentTheme()
74	p := tea.NewProgram(model{
75		bgColor: t.BgBase,
76		anim: anim.New(anim.Settings{
77			Label:       "Hello",
78			Size:        50,
79			LabelColor:  t.FgBase,
80			GradColorA:  t.Primary,
81			GradColorB:  t.Secondary,
82			CycleColors: true,
83		}),
84	}, tea.WithAltScreen())
85
86	if _, err := p.Run(); err != nil {
87		fmt.Fprintf(os.Stderr, "Uh oh: %v\n", err)
88		os.Exit(1)
89	}
90}