mirror of
https://github.com/Squibid/rph.git
synced 2025-10-20 03:44:04 +00:00
feat: inital commit
- added template subcmd which generates frc project templates
This commit is contained in:
76
cmd/template/downloadUi.go
Normal file
76
cmd/template/downloadUi.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/bubbles/progress"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
const (
|
||||
padding = 2
|
||||
maxWidth = 80
|
||||
)
|
||||
|
||||
type progressMsg float64
|
||||
type progressErrMsg struct{ err error }
|
||||
|
||||
func finalPause() tea.Cmd {
|
||||
return tea.Tick(time.Millisecond*750, func(_ time.Time) tea.Msg {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
type downloadModel struct {
|
||||
pw *progressWriter
|
||||
progress progress.Model
|
||||
err error
|
||||
}
|
||||
|
||||
func (m downloadModel) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m downloadModel) 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 = min(msg.Width - padding * 2 - 4, maxWidth)
|
||||
return m, nil
|
||||
|
||||
case progressErrMsg:
|
||||
m.err = msg.err
|
||||
return m, tea.Quit
|
||||
|
||||
case progressMsg:
|
||||
var cmds []tea.Cmd
|
||||
|
||||
if msg >= 1.0 {
|
||||
cmds = append(cmds, tea.Sequence(finalPause(), tea.Quit))
|
||||
}
|
||||
|
||||
cmds = append(cmds, m.progress.SetPercent(float64(msg)))
|
||||
return m, tea.Batch(cmds...)
|
||||
|
||||
// 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 downloadModel) View() string {
|
||||
if m.err != nil {
|
||||
return "Error downloading: " + m.err.Error() + "\n"
|
||||
}
|
||||
|
||||
pad := strings.Repeat(" ", padding)
|
||||
return "\n" + pad + "Downloading template.zip to " + m.pw.file.Name() + "\n\n" + pad + m.progress.View() + "\n\n" + pad + "Press any key to quit"
|
||||
}
|
Reference in New Issue
Block a user