added: examples/composable-views/main.go +158 added 1 + package main2 + 3 + import (4 + "fmt"5 + "log"6 + "time"7 + 8 + "github.com/charmbracelet/bubbles/spinner"9 + "github.com/charmbracelet/bubbles/timer"10 + tea "github.com/charmbracelet/bubbletea"11 + "github.com/charmbracelet/lipgloss"12 + )13 + 14 + /*15 + This example assumes an existing understanding of commands and messages. If you16 + haven't already read our tutorials on the basics of Bubble Tea and working17 + with commands, we recommend reading those first.18 + 19 + Find them at:20 + https://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands21 + https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics22 + */23 + 24 + // sessionState is used to track which model is focused25 + type sessionState uint26 + 27 + const (28 + defaultTime = time.Minute29 + timerView sessionState = iota30 + spinnerView31 + )32 + 33 + var (34 + // Available spinners35 + spinners = []spinner.Spinner{36 + spinner.Line,37 + spinner.Dot,38 + spinner.MiniDot,39 + spinner.Jump,40 + spinner.Pulse,41 + spinner.Points,42 + spinner.Globe,43 + spinner.Moon,44 + spinner.Monkey,45 + }46 + modelStyle = lipgloss.NewStyle().47 + Padding(1, 2).48 + BorderStyle(lipgloss.HiddenBorder())49 + focusedModelStyle = lipgloss.NewStyle().50 + Padding(1, 2).51 + BorderStyle(lipgloss.NormalBorder()).52 + BorderForeground(lipgloss.Color("69"))53 + spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))54 + helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241"))55 + )56 + 57 + type mainModel struct {58 + state sessionState59 + timer timer.Model60 + spinner spinner.Model61 + index int62 + }63 + 64 + func newModel(timeout time.Duration) mainModel {65 + m := mainModel{state: timerView}66 + m.timer = timer.New(timeout)67 + m.spinner = spinner.New()68 + return m69 + }70 + 71 + func (m mainModel) Init() tea.Cmd {72 + // start the timer and spinner on program start73 + return tea.Batch(m.timer.Init(), m.spinner.Tick)74 + }75 + 76 + func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {77 + var cmd tea.Cmd78 + var cmds []tea.Cmd79 + switch msg := msg.(type) {80 + case tea.KeyMsg:81 + switch msg.String() {82 + case "ctrl+c", "q":83 + return m, tea.Quit84 + case "tab":85 + if m.state == timerView {86 + m.state = spinnerView87 + } else {88 + m.state = timerView89 + }90 + case "n":91 + if m.state == timerView {92 + m.timer = timer.New(defaultTime)93 + cmds = append(cmds, m.timer.Init())94 + } else {95 + m.Next()96 + m.resetSpinner()97 + cmds = append(cmds, spinner.Tick)98 + }99 + }100 + switch m.state {101 + // update whichever model is focused102 + case spinnerView:103 + m.spinner, cmd = m.spinner.Update(msg)104 + cmds = append(cmds, cmd)105 + default:106 + m.timer, cmd = m.timer.Update(msg)107 + cmds = append(cmds, cmd)108 + }109 + case spinner.TickMsg:110 + m.spinner, cmd = m.spinner.Update(msg)111 + cmds = append(cmds, cmd)112 + case timer.TickMsg:113 + m.timer, cmd = m.timer.Update(msg)114 + cmds = append(cmds, cmd)115 + }116 + return m, tea.Batch(cmds...)117 + }118 + 119 + func (m mainModel) View() string {120 + var s string121 + model := m.currentFocusedModel()122 + if m.state == timerView {123 + s += lipgloss.JoinHorizontal(lipgloss.Top, focusedModelStyle.Render(fmt.Sprintf("%4s", m.timer.View())), modelStyle.Render( m.spinner.View()))124 + } else {125 + s += lipgloss.JoinHorizontal(lipgloss.Top, modelStyle.Render(fmt.Sprintf("%4s", m.timer.View())), focusedModelStyle.Render(m.spinner.View()))126 + }127 + s += helpStyle.Render(fmt.Sprintf("\ntab: change focused model • n: new %s • q: exit\n", model))128 + return s129 + }130 + 131 + func (m mainModel) currentFocusedModel() string {132 + if m.state == timerView {133 + return "timer"134 + }135 + return "spinner"136 + }137 + 138 + func (m *mainModel) Next() {139 + if m.index == len(spinners)-1 {140 + m.index = 0141 + } else {142 + m.index++143 + }144 + }145 + 146 + func (m *mainModel) resetSpinner() {147 + m.spinner = spinner.New()148 + m.spinner.Style = spinnerStyle149 + m.spinner.Spinner = spinners[m.index]150 + }151 + 152 + func main() {153 + p := tea.NewProgram(newModel(defaultTime))154 + 155 + if err := p.Start(); err != nil {156 + log.Fatal(err)157 + }158 + }