main.go
This file
Path
examples/package-manager/main.go
131 lines · 3.3 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
1package main23import (4 "fmt"5 "math/rand"6 "os"7 "strings"8 "time"910 "charm.land/bubbles/v2/progress"11 "charm.land/bubbles/v2/spinner"12 tea "charm.land/bubbletea/v2"13 "charm.land/lipgloss/v2"14)1516type model struct {17 packages []string18 index int19 width int20 height int21 spinner spinner.Model22 progress progress.Model23 done bool24}2526var (27 currentPkgNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("211"))28 doneStyle = lipgloss.NewStyle().Margin(1, 2)29 checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓")30)3132func newModel() model {33 p := progress.New(34 progress.WithDefaultBlend(),35 progress.WithWidth(40),36 progress.WithoutPercentage(),37 )38 s := spinner.New()39 s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))40 return model{41 packages: getPackages(),42 spinner: s,43 progress: p,44 }45}4647func (m model) Init() tea.Cmd {48 return tea.Batch(downloadAndInstall(m.packages[m.index]), m.spinner.Tick)49}5051func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {52 switch msg := msg.(type) {53 case tea.WindowSizeMsg:54 m.width, m.height = msg.Width, msg.Height55 case tea.KeyPressMsg:56 switch msg.String() {57 case "ctrl+c", "esc", "q":58 return m, tea.Quit59 }60 case installedPkgMsg:61 pkg := m.packages[m.index]62 if m.index >= len(m.packages)-1 {63 // Everything's been installed. We're done!64 m.done = true65 return m, tea.Sequence(66 tea.Printf("%s %s", checkMark, pkg), // print the last success message67 tea.Quit, // exit the program68 )69 }7071 // Update progress bar72 m.index++73 progressCmd := m.progress.SetPercent(float64(m.index) / float64(len(m.packages)))7475 return m, tea.Batch(76 progressCmd,77 tea.Printf("%s %s", checkMark, pkg), // print success message above our program78 downloadAndInstall(m.packages[m.index]), // download the next package79 )80 case spinner.TickMsg:81 var cmd tea.Cmd82 m.spinner, cmd = m.spinner.Update(msg)83 return m, cmd84 case progress.FrameMsg:85 var cmd tea.Cmd86 m.progress, cmd = m.progress.Update(msg)87 return m, cmd88 }89 return m, nil90}9192func (m model) View() tea.View {93 n := len(m.packages)94 w := lipgloss.Width(fmt.Sprintf("%d", n))9596 if m.done {97 return tea.NewView(doneStyle.Render(fmt.Sprintf("Done! Installed %d packages.\n", n)))98 }99100 pkgCount := fmt.Sprintf(" %*d/%*d", w, m.index, w, n)101102 spin := m.spinner.View() + " "103 prog := m.progress.View()104 cellsAvail := max(0, m.width-lipgloss.Width(spin+prog+pkgCount))105106 pkgName := currentPkgNameStyle.Render(m.packages[m.index])107 info := lipgloss.NewStyle().MaxWidth(cellsAvail).Render("Installing " + pkgName)108109 cellsRemaining := max(0, m.width-lipgloss.Width(spin+info+prog+pkgCount))110 gap := strings.Repeat(" ", cellsRemaining)111112 return tea.NewView(spin + info + gap + prog + pkgCount)113}114115type installedPkgMsg string116117func downloadAndInstall(pkg string) tea.Cmd {118 // This is where you'd do i/o stuff to download and install packages. In119 // our case we're just pausing for a moment to simulate the process.120 d := time.Millisecond * time.Duration(rand.Intn(500)) //nolint:gosec121 return tea.Tick(d, func(t time.Time) tea.Msg {122 return installedPkgMsg(pkg)123 })124}125126func main() {127 if _, err := tea.NewProgram(newModel()).Run(); err != nil {128 fmt.Println("Error running program:", err)129 os.Exit(1)130 }131}