Skip to content

Commit

Permalink
WIP: no more progress bars, use spinner instead
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGedd committed Mar 20, 2024
1 parent 175b014 commit cfdbf69
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 40 deletions.
23 changes: 15 additions & 8 deletions src/pkg/bundle/tui/deploy/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand All @@ -29,15 +28,23 @@ func (m *Model) handleNewPackage(pkgName string, currentPkgIdx int) tea.Cmd {
newPkg.resetProgress = true
}

// finish creating newPkg and start the spinner
newPkg.progress = progress.New(progress.WithDefaultGradient())
// finish creating newPkg and start the spinners
m.pkgIdx = currentPkgIdx
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
newPkg.spinner = s

// create spinner to track deployment progress
deploySpinner := spinner.New()
deploySpinner.Spinner = spinner.Dot
deploySpinner.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
newPkg.deploySpinner = deploySpinner

// for remote packages, create spinner to track verification progress
verifySpinner := spinner.New()
verifySpinner.Spinner = spinner.Dot
verifySpinner.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
newPkg.verifySpinner = verifySpinner

m.packages = append(m.packages, newPkg)
return m.packages[m.pkgIdx].spinner.Tick
return tea.Batch(m.packages[m.pkgIdx].deploySpinner.Tick, m.packages[m.pkgIdx].verifySpinner.Tick)
}

func (m *Model) handleDeploy() tea.Cmd {
Expand Down
49 changes: 29 additions & 20 deletions src/pkg/bundle/tui/deploy/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -48,13 +47,14 @@ type bndlClientShim interface {

// pkgState contains the state of the pkg as its deploying
type pkgState struct {
name string
numComponents int
componentStatuses []bool
spinner spinner.Model
complete bool
resetProgress bool
progress progress.Model
name string
numComponents int
percLayersVerified float64
componentStatuses []bool
deploySpinner spinner.Model
complete bool
resetProgress bool
verifySpinner spinner.Model
}

type Model struct {
Expand Down Expand Up @@ -130,13 +130,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

default:
switch msg := msg.(type) {
// FrameMsg is sent when the progress bar wants to animate itself
case progress.FrameMsg:
if len(m.packages) > m.pkgIdx {
progressModel, cmd := m.packages[m.pkgIdx].progress.Update(msg)
m.packages[m.pkgIdx].progress = progressModel.(progress.Model)
return m, cmd
}

// handle changes in window size
case tea.WindowSizeMsg:
termWidth = msg.Width
Expand All @@ -151,10 +145,14 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.logViewport, _ = m.logViewport.Update(msg)
m.yamlViewport, _ = m.yamlViewport.Update(msg)

// handle spinner
// spin the spinners
case spinner.TickMsg:
var cmd tea.Cmd
m.packages[m.pkgIdx].spinner, cmd = m.packages[m.pkgIdx].spinner.Update(msg)
if msg.ID == m.packages[m.pkgIdx].deploySpinner.ID() {
m.packages[m.pkgIdx].deploySpinner, cmd = m.packages[m.pkgIdx].deploySpinner.Update(msg)
} else if msg.ID == m.packages[m.pkgIdx].verifySpinner.ID() {
m.packages[m.pkgIdx].verifySpinner, cmd = m.packages[m.pkgIdx].verifySpinner.Update(msg)
}
return m, cmd

// handle ticks
Expand Down Expand Up @@ -191,6 +189,19 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

//if !m.confirmed {
// switch msg.(type) {
// case tea.KeyUp, tea.KeyCtrlK:
// m.yamlViewport.ScrollBy(-1) // Scroll up by one line
// case tea.KeyDown, tea.KeyCtrlJ:
// m.yamlViewport.ScrollBy(1) // Scroll down by one line
// case tea.KeyPgUp:
// m.yamlViewport.ScrollBy(-m.yamlViewport.Height / 2) // Scroll up by half a page
// case tea.KeyPgDown:
// m.yamlViewport.ScrollBy(m.yamlViewport.Height / 2) // Scroll down by half a page
// }
//}

// handle deploy
case deployOp:
cmd := m.handleDeploy()
Expand All @@ -215,10 +226,8 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.totalPkgs = totalPkgs
}
case verified:
// update progress bar
if perc, err := strconv.ParseFloat(strings.Split(msg, ":")[1], 64); err == nil {
cmd := m.packages[m.pkgIdx].progress.SetPercent(perc)
return m, cmd
m.packages[m.pkgIdx].percLayersVerified = perc
}
case complete:
m.packages[m.pkgIdx].complete = true
Expand Down
43 changes: 32 additions & 11 deletions src/pkg/bundle/tui/deploy/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ var (
)

func (m *Model) logView() string {
headerMsg := fmt.Sprintf("Package %s deploy logs", m.packages[m.pkgIdx].name)
return lipgloss.NewStyle().Padding(0, 3).Render(
fmt.Sprintf("%s\n%s\n%s\n\n", m.logHeaderView(), m.logViewport.View(), m.logFooterView()),
fmt.Sprintf("%s\n%s\n%s\n\n", m.logHeaderView(headerMsg), m.logViewport.View(), m.logFooterView()),
)
}

func (m *Model) logHeaderView() string {
title := titleStyle.Render(fmt.Sprintf("Package %s deploy logs", m.packages[m.pkgIdx].name))
func (m *Model) logHeaderView(msg string) string {
title := titleStyle.Render(msg)
headerLine := strings.Repeat("─", max(0, m.logViewport.Width-lipgloss.Width(title)))
return lipgloss.JoinHorizontal(lipgloss.Center, title, headerLine)
}
Expand All @@ -68,20 +69,29 @@ func (m *Model) deployView() string {
}
}

// todo: sometimes this says it's deploying 0/0 components, fix this
text := lipgloss.NewStyle().
Align(lipgloss.Left).
Padding(0, 3).
Render(fmt.Sprintf("%s Package %s deploying (%d / %d components)", p.spinner.View(), p.name, min(numComponentsSuccess+1, p.numComponents), p.numComponents))

var text string
if p.percLayersVerified > 0 {
perc := lightGrayText.Render(fmt.Sprintf("%d%%", int32(p.percLayersVerified)))
text = lipgloss.NewStyle().
Align(lipgloss.Left).
Padding(0, 3).
Render(fmt.Sprintf("%s Verifying pkg %s (%s)", p.verifySpinner.View(), p.name, perc))
}
if p.numComponents != 0 {
// todo: sometimes this says it's deploying 0/0 components, fix this
text = lipgloss.NewStyle().
Align(lipgloss.Left).
Padding(0, 3).
Render(fmt.Sprintf("%s Package %s deploying (%d / %d components)", p.deploySpinner.View(), p.name, min(numComponentsSuccess+1, p.numComponents), p.numComponents))
}
if p.complete {
text = lipgloss.NewStyle().
Align(lipgloss.Left).
Padding(0, 3).
Render(fmt.Sprintf("✅ Package %s deployed", p.name))
}

view = lipgloss.JoinVertical(lipgloss.Left, view, text+"\n", p.progress.View())
view = lipgloss.JoinVertical(lipgloss.Left, view, text+"\n")
}

return view
Expand All @@ -94,8 +104,19 @@ func (m *Model) preDeployView() string {
prettyYAML := paddingStyle.Render(colorPrintYAML(m.bundleYAML))
m.yamlViewport.SetContent(prettyYAML)

headerMsg := "Use mouse wheel to scroll"
//return lipgloss.NewStyle().Padding(0, 3).Render(
// fmt.Sprintf("%s\n%s\n%s\n\n", m.logHeaderView(headerMsg), m.logViewport.View(), m.logFooterView()),
//)

// Concatenate header, highlighted YAML, and prompt
return fmt.Sprintf("\n%s\n\n%s\n\n%s\n\n%s", header, line, m.yamlViewport.View(), prompt)
return fmt.Sprintf("\n%s\n\n%s\n%s\n%s\n\n%s",
header,
lipgloss.NewStyle().Padding(0, 3).Render(m.logHeaderView(headerMsg)),
lipgloss.NewStyle().Padding(0, 3).Render(m.yamlViewport.View()),
lipgloss.NewStyle().Padding(0, 3).Render(m.logFooterView()),
prompt,
)
}

func tickCmd() tea.Cmd {
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/sources/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ func (r *RemoteBundle) downloadPkgFromRemoteBundle() ([]ocispec.Descriptor, erro
}
progressBar.Add(1)
numLayersVerified++
deploy.Program.Send(fmt.Sprintf("verified:%v", numLayersVerified/float64(len(pkgManifest.Layers))))
if ok {
percVerified := numLayersVerified / float64(len(pkgManifest.Layers)) * 100
deploy.Program.Send(fmt.Sprintf("verified:%v", percVerified))
estimatedBytes += layer.Size
layersInBundle = append(layersInBundle, layer)
digest := layer.Digest.Encoded()
Expand Down

0 comments on commit cfdbf69

Please sign in to comment.