Skip to content

Commit

Permalink
add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Sep 20, 2021
1 parent 58bc613 commit fcd2709
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ github.com/charmbracelet/bubbles v0.9.0/go.mod h1:NWT/c+0rYEnYChz5qCyX4Lj6fDw9gG
github.com/charmbracelet/bubbletea v0.14.1/go.mod h1:b5lOf5mLjMg1tRn1HVla54guZB+jvsyV0yYAQja95zE=
github.com/charmbracelet/bubbletea v0.15.0 h1:7++QPke7CsjBs+tZl49x7KXTHsof+NUMhreAtwBXygE=
github.com/charmbracelet/bubbletea v0.15.0/go.mod h1:YTZSs2p3odhwYZdhqJheYHVUjU37c9OLgS85kw6NGQY=
github.com/charmbracelet/harmonica v0.1.0 h1:lFKeSd6OAckQ/CEzPVd2mqj+YMEubQ/3FM2IYY3xNm0=
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.3.0/go.mod h1:VkhdBS2eNAmRkTwRKLJCFhCOVkjntMusBDxv7TXahuk=
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=
Expand Down
2 changes: 1 addition & 1 deletion internal/meta/meta_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m MetaDummy) CleanTFState() {
}

func (m MetaDummy) Import(item ImportItem) error {
time.Sleep(500 * time.Millisecond)
time.Sleep(time.Second)
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/ui/aztfyclient/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aztfyclient

import (
"time"

"github.com/magodo/aztfy/internal/config"
"github.com/magodo/aztfy/internal/meta"

Expand Down Expand Up @@ -82,6 +84,9 @@ func ImportOneItem(c meta.Meta, item meta.ImportItem) tea.Cmd {
return func() tea.Msg {
if !item.Skip() {
item.ImportError = c.Import(item)
} else {
// This explicit minor delay is for the sake of a visual effect of the progress bar.
time.Sleep(100 * time.Millisecond)
}
return ImportOneItemDoneMsg{Item: item}
}
Expand Down
54 changes: 40 additions & 14 deletions internal/ui/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package progress
import (
"fmt"

prog "github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/magodo/aztfy/internal/meta"
"github.com/magodo/aztfy/internal/ui/aztfyclient"
common2 "github.com/magodo/aztfy/internal/ui/common"
"github.com/magodo/aztfy/internal/ui/common"
)

type result struct {
Expand All @@ -15,18 +16,20 @@ type result struct {
}

type Model struct {
c meta.Meta
l meta.ImportList
idx int
results []result
c meta.Meta
l meta.ImportList
idx int
results []result
progress prog.Model
}

func NewModel(c meta.Meta, l meta.ImportList) Model {
return Model{
c: c,
l: l,
idx: 0,
results: make([]result, common2.ProgressShowLastResults),
c: c,
l: l,
idx: 0,
results: make([]result, common.ProgressShowLastResults),
progress: prog.NewModel(prog.WithDefaultGradient()),
}
}

Expand All @@ -42,23 +45,44 @@ func (m Model) Init() tea.Cmd {

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.progress.Width = msg.Width - 4
return m, nil
// FrameMsg is sent when the progress bar wants to animate itself
case prog.FrameMsg:
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(prog.Model)
return m, cmd
case aztfyclient.ImportOneItemDoneMsg:
var cmds []tea.Cmd
var cmd tea.Cmd

// Update results
item := msg.Item
m.l[m.idx] = item
res := result{
item: msg.Item,
}
if item.ImportError != nil {
res.emoji = common2.WarningEmoji
res.emoji = common.WarningEmoji
} else {
res.emoji = common2.RandomHappyEmoji()
res.emoji = common.RandomHappyEmoji()
}
m.results = append(m.results[1:], res)

// Update progress bar
cmd = m.progress.SetPercent(float64(m.idx+1) / float64(len(m.l)))
cmds = append(cmds, cmd)

m.idx++
if m.iterationDone() {
return m, aztfyclient.FinishImport(m.l)
cmd = aztfyclient.FinishImport(m.l)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
return m, aztfyclient.ImportOneItem(m.c, m.l[m.idx])
cmd = aztfyclient.ImportOneItem(m.c, m.l[m.idx])
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
default:
return m, nil
}
Expand All @@ -80,7 +104,7 @@ func (m Model) View() string {
for _, res := range m.results {
emptyItem := meta.ImportItem{}
if res.item == emptyItem {
s += "........................\n"
s += "...\n"
} else {
switch {
case res.item.Skip():
Expand All @@ -93,6 +117,8 @@ func (m Model) View() string {
}
}

s += "\n\n" + m.progress.View()

return s
}

Expand Down
6 changes: 5 additions & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case aztfyclient.StartImportMsg:
m.status = statusImporting
m.progress = progress.NewModel(m.meta, msg.List)
return m, m.progress.Init()
return m, tea.Batch(
m.progress.Init(),
// Resize the progress bar
func() tea.Msg { return m.winsize },
)
case aztfyclient.ImportDoneMsg:
for idx, item := range msg.List {
if item.ImportError != nil {
Expand Down

0 comments on commit fcd2709

Please sign in to comment.