Skip to content
Myr

main.go

This file

Path

examples/composable-views/main.go

163 lines · 3.8 KiB · Go

Last changed by

docs: fix links to tutorials & examples (#1645)mhdnabefore adoption882727b

History of this file

Take it

1package main23import (4	"fmt"5	"log"6	"strings"7	"time"89	"charm.land/bubbles/v2/spinner"10	"charm.land/bubbles/v2/timer"11	tea "charm.land/bubbletea/v2"12	"charm.land/lipgloss/v2"13)1415/*16This example assumes an existing understanding of commands and messages. If you17haven't already read our tutorials on the basics of Bubble Tea and working with18commands, we recommend reading those first.1920Find them at:21https://github.com/charmbracelet/bubbletea/tree/main/tutorials/commands22https://github.com/charmbracelet/bubbletea/tree/main/tutorials/basics23*/2425// sessionState is used to track which model is focused26type sessionState uint2728const (29	defaultTime              = time.Minute30	timerView   sessionState = iota31	spinnerView32)3334var (35	// Available spinners36	spinners = []spinner.Spinner{37		spinner.Line,38		spinner.Dot,39		spinner.MiniDot,40		spinner.Jump,41		spinner.Pulse,42		spinner.Points,43		spinner.Globe,44		spinner.Moon,45		spinner.Monkey,46	}47	modelStyle = lipgloss.NewStyle().48			Width(15).49			Height(5).50			Align(lipgloss.Center, lipgloss.Center).51			BorderStyle(lipgloss.HiddenBorder())52	focusedModelStyle = lipgloss.NewStyle().53				Width(15).54				Height(5).55				Align(lipgloss.Center, lipgloss.Center).56				BorderStyle(lipgloss.NormalBorder()).57				BorderForeground(lipgloss.Color("69"))58	spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))59	helpStyle    = lipgloss.NewStyle().Foreground(lipgloss.Color("241"))60)6162type mainModel struct {63	state   sessionState64	timer   timer.Model65	spinner spinner.Model66	index   int67}6869func newModel(timeout time.Duration) mainModel {70	m := mainModel{state: timerView}71	m.timer = timer.New(timeout)72	m.spinner = spinner.New()73	return m74}7576func (m mainModel) Init() tea.Cmd {77	// start the timer and spinner on program start78	return tea.Batch(m.timer.Init(), m.spinner.Tick)79}8081func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {82	var cmd tea.Cmd83	var cmds []tea.Cmd84	switch msg := msg.(type) {85	case tea.KeyPressMsg:86		switch msg.String() {87		case "ctrl+c", "q":88			return m, tea.Quit89		case "tab":90			if m.state == timerView {91				m.state = spinnerView92			} else {93				m.state = timerView94			}95		case "n":96			if m.state == timerView {97				m.timer = timer.New(defaultTime)98				cmds = append(cmds, m.timer.Init())99			} else {100				m.Next()101				m.resetSpinner()102				cmds = append(cmds, m.spinner.Tick)103			}104		}105		switch m.state {106		// update whichever model is focused107		case spinnerView:108			m.spinner, cmd = m.spinner.Update(msg)109			cmds = append(cmds, cmd)110		default:111			m.timer, cmd = m.timer.Update(msg)112			cmds = append(cmds, cmd)113		}114	case spinner.TickMsg:115		m.spinner, cmd = m.spinner.Update(msg)116		cmds = append(cmds, cmd)117	case timer.TickMsg:118		m.timer, cmd = m.timer.Update(msg)119		cmds = append(cmds, cmd)120	}121	return m, tea.Batch(cmds...)122}123124func (m mainModel) View() tea.View {125	var s strings.Builder126	model := m.currentFocusedModel()127	if m.state == timerView {128		s.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, focusedModelStyle.Render(fmt.Sprintf("%4s", m.timer.View())), modelStyle.Render(m.spinner.View())))129	} else {130		s.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, modelStyle.Render(fmt.Sprintf("%4s", m.timer.View())), focusedModelStyle.Render(m.spinner.View())))131	}132	s.WriteString(helpStyle.Render(fmt.Sprintf("\ntab: focus next • n: new %s • q: exit\n", model)))133	return tea.NewView(s.String())134}135136func (m mainModel) currentFocusedModel() string {137	if m.state == timerView {138		return "timer"139	}140	return "spinner"141}142143func (m *mainModel) Next() {144	if m.index == len(spinners)-1 {145		m.index = 0146	} else {147		m.index++148	}149}150151func (m *mainModel) resetSpinner() {152	m.spinner = spinner.New()153	m.spinner.Style = spinnerStyle154	m.spinner.Spinner = spinners[m.index]155}156157func main() {158	p := tea.NewProgram(newModel(defaultTime))159160	if _, err := p.Run(); err != nil {161		log.Fatal(err)162	}163}