2021-07-21 13:08:53 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
// A simple example that shows how to render a progress bar in a "pure"
|
|
|
|
// fashion. In this example we bump the progress by 25% every second,
|
|
|
|
// maintaining the progress state on our top level model using the progress bar
|
|
|
|
// model's ViewAs method only for rendering.
|
|
|
|
//
|
|
|
|
// The signature for ViewAs is:
|
|
|
|
//
|
|
|
|
// func (m Model) ViewAs(percent float64) string
|
|
|
|
//
|
|
|
|
// So it takes a float between 0 and 1, and renders the progress bar
|
|
|
|
// accordingly. When using the progress bar in this "pure" fashion and there's
|
|
|
|
// no need to call an Update method.
|
|
|
|
//
|
|
|
|
// The progress bar is also able to animate itself, however. For details see
|
|
|
|
// the progress-animated example.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/progress"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
padding = 2
|
|
|
|
maxWidth = 80
|
|
|
|
)
|
|
|
|
|
|
|
|
var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render
|
|
|
|
|
|
|
|
func main() {
|
2022-01-10 20:42:10 -05:00
|
|
|
prog := progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C"))
|
2021-07-21 13:08:53 -04:00
|
|
|
|
|
|
|
if err := tea.NewProgram(model{progress: prog}).Start(); err != nil {
|
|
|
|
fmt.Println("Oh no!", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tickMsg time.Time
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
percent float64
|
|
|
|
progress progress.Model
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:34:41 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
2021-07-21 13:08:53 -04:00
|
|
|
return tickCmd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
return m, tea.Quit
|
|
|
|
|
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
m.progress.Width = msg.Width - padding*2 - 4
|
|
|
|
if m.progress.Width > maxWidth {
|
|
|
|
m.progress.Width = maxWidth
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
|
|
|
|
case tickMsg:
|
|
|
|
m.percent += 0.25
|
|
|
|
if m.percent > 1.0 {
|
|
|
|
m.percent = 1.0
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
return m, tickCmd()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:34:41 -04:00
|
|
|
func (m model) View() string {
|
2021-07-21 13:08:53 -04:00
|
|
|
pad := strings.Repeat(" ", padding)
|
|
|
|
return "\n" +
|
2022-10-07 18:34:41 -04:00
|
|
|
pad + m.progress.ViewAs(m.percent) + "\n\n" +
|
2021-07-21 13:08:53 -04:00
|
|
|
pad + helpStyle("Press any key to quit")
|
|
|
|
}
|
|
|
|
|
|
|
|
func tickCmd() tea.Cmd {
|
|
|
|
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
|
|
|
|
return tickMsg(t)
|
|
|
|
})
|
|
|
|
}
|