main.go 1.6 KB

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