Skip to content
Myr

Composable view (#394)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* docs: creating nested models * docs: move nested model to example * docs: add working nested model example * refactor: use tea.Batch in nested model example * refactor: switch to composable view example * refactor: tab select, add padding to boxes, only focused has border * fix: add padding to timer to remove UI shift

authored by bashbunni · committed by GitHub · parent 31800cd · Browse files · 1 file+158 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+158 added−0 removed

  • other:+158 added−0 removed

1 file+158 added−0 removed

added:examples/composable-views/main.go+158 added
1package main23import (4 "fmt"5 "log"6 "time"78 "github.com/charmbracelet/bubbles/spinner"9 "github.com/charmbracelet/bubbles/timer"10 tea "github.com/charmbracelet/bubbletea"11 "github.com/charmbracelet/lipgloss"12)1314/*15This example assumes an existing understanding of commands and messages. If you16haven't already read our tutorials on the basics of Bubble Tea and working17with commands, we recommend reading those first.1819Find them at:20https://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands21https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics22*/2324// sessionState is used to track which model is focused25type sessionState uint2627const (28 defaultTime = time.Minute29 timerView sessionState = iota30 spinnerView31)3233var (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)5657type mainModel struct {58 state sessionState59 timer timer.Model60 spinner spinner.Model61 index int62}6364func newModel(timeout time.Duration) mainModel {65 m := mainModel{state: timerView}66 m.timer = timer.New(timeout)67 m.spinner = spinner.New()68 return m69}7071func (m mainModel) Init() tea.Cmd {72 // start the timer and spinner on program start73 return tea.Batch(m.timer.Init(), m.spinner.Tick)74}7576func (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}118119func (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}130131func (m mainModel) currentFocusedModel() string {132 if m.state == timerView {133 return "timer"134 }135 return "spinner"136}137138func (m *mainModel) Next() {139 if m.index == len(spinners)-1 {140 m.index = 0141 } else {142 m.index++143 }144}145146func (m *mainModel) resetSpinner() {147 m.spinner = spinner.New()148 m.spinner.Style = spinnerStyle149 m.spinner.Spinner = spinners[m.index]150}151152func main() {153 p := tea.NewProgram(newModel(defaultTime))154155 if err := p.Start(); err != nil {156 log.Fatal(err)157 }158}