78 lines
1.4 KiB
Go
78 lines
1.4 KiB
Go
package main
|
|
|
|
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() {
|
|
prog := progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C"))
|
|
|
|
if _, err := tea.NewProgram(model{progress: prog}).Run(); err != nil {
|
|
fmt.Println("Oh no!", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
type tickMsg time.Time
|
|
|
|
type model struct {
|
|
percent float64
|
|
progress progress.Model
|
|
}
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
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
|
|
}
|
|
}
|
|
|
|
func (m model) View() string {
|
|
pad := strings.Repeat(" ", padding)
|
|
return "\n" +
|
|
pad + m.progress.ViewAs(m.percent) + "\n\n" +
|
|
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)
|
|
})
|
|
}
|