Skip to content
Myr

main.go

This file

Path

examples/progress-bar/main.go

65 lines · 1.3 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 1–65why, one step back
1package main
23import (4 "log"56 tea "charm.land/bubbletea/v2"7 "charm.land/lipgloss/v2"8)910var body = lipgloss.NewStyle().Padding(1, 2)1112type model struct {13 value int14 width int15 state tea.ProgressBarState16}1718func (m model) Init() tea.Cmd {19 return nil20}2122func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {23 switch msg := msg.(type) {24 case tea.WindowSizeMsg:25 m.width = msg.Width26 case tea.KeyPressMsg:27 switch msg.String() {28 case "q", "ctrl+c":29 return m, tea.Quit30 case "up", "k":31 if m.value < 100 {32 m.value += 1033 }34 case "down", "j":35 if m.value > 0 {36 m.value -= 1037 }38 case "left", "h":39 if m.state > 0 {40 m.state--41 }42 case "right", "l":43 if m.state < 4 {44 m.state++45 }46 }47 }48 return m, nil49}5051func (m model) View() tea.View {52 s := body.Width(m.width - body.GetHorizontalPadding()).Render(53 "This demo requires a terminal emulator that supports an indeterminate progress bar, such a Windows Terminal or Ghostty. In other terminals (including tmux in a supporting terminal) nothing will happen.\n\nPress up/down to change value, left/right to change state, q to quit.",54 )55 v := tea.NewView(s)56 v.ProgressBar = tea.NewProgressBar(m.state, m.value)57 return v58}5960func main() {61 p := tea.NewProgram(model{value: 50, state: tea.ProgressBarIndeterminate})62 if _, err := p.Run(); err != nil {63 log.Fatalf("Error: %v", err)64 }65}