mirror of
https://github.com/lunchcat/sif.git
synced 2026-01-11 04:33:40 -08:00
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
package util
|
|
|
|
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 InitProgressBar() {
|
|
m := model{
|
|
progress: progress.New(progress.WithDefaultGradient()),
|
|
}
|
|
|
|
if _, err := tea.NewProgram(m).Run(); err != nil {
|
|
fmt.Println("Oh no!", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
type tickMsg time.Time
|
|
|
|
type model struct {
|
|
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 tickMsg:
|
|
if m.progress.Percent() == 1.0 {
|
|
return m, tea.Quit
|
|
}
|
|
|
|
// Note that you can also use progress.Model.SetPercent to set the
|
|
// percentage value explicitly, too.
|
|
cmd := m.progress.IncrPercent(0.25)
|
|
return m, tea.Batch(tickCmd(), cmd)
|
|
|
|
// FrameMsg is sent when the progress bar wants to animate itself
|
|
case progress.FrameMsg:
|
|
progressModel, cmd := m.progress.Update(msg)
|
|
m.progress = progressModel.(progress.Model)
|
|
return m, cmd
|
|
|
|
default:
|
|
return m, nil
|
|
}
|
|
}
|
|
|
|
func (m model) View() string {
|
|
pad := strings.Repeat(" ", padding)
|
|
return "\n" +
|
|
pad + m.progress.View() + "\n\n" +
|
|
pad + helpStyle("Press any key to quit")
|
|
}
|
|
|
|
func tickCmd() tea.Cmd {
|
|
return tea.Tick(time.Second*1, func(t time.Time) tea.Msg {
|
|
return tickMsg(t)
|
|
})
|
|
}
|