Skip to content

Commit fcd2709

Browse files
committed
add progress bar
1 parent 58bc613 commit fcd2709

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ github.com/charmbracelet/bubbles v0.9.0/go.mod h1:NWT/c+0rYEnYChz5qCyX4Lj6fDw9gG
9999
github.com/charmbracelet/bubbletea v0.14.1/go.mod h1:b5lOf5mLjMg1tRn1HVla54guZB+jvsyV0yYAQja95zE=
100100
github.com/charmbracelet/bubbletea v0.15.0 h1:7++QPke7CsjBs+tZl49x7KXTHsof+NUMhreAtwBXygE=
101101
github.com/charmbracelet/bubbletea v0.15.0/go.mod h1:YTZSs2p3odhwYZdhqJheYHVUjU37c9OLgS85kw6NGQY=
102+
github.com/charmbracelet/harmonica v0.1.0 h1:lFKeSd6OAckQ/CEzPVd2mqj+YMEubQ/3FM2IYY3xNm0=
102103
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
103104
github.com/charmbracelet/lipgloss v0.3.0/go.mod h1:VkhdBS2eNAmRkTwRKLJCFhCOVkjntMusBDxv7TXahuk=
104105
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=

internal/meta/meta_dummy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (m MetaDummy) CleanTFState() {
4949
}
5050

5151
func (m MetaDummy) Import(item ImportItem) error {
52-
time.Sleep(500 * time.Millisecond)
52+
time.Sleep(time.Second)
5353
return nil
5454
}
5555

internal/ui/aztfyclient/client.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package aztfyclient
22

33
import (
4+
"time"
5+
46
"github.com/magodo/aztfy/internal/config"
57
"github.com/magodo/aztfy/internal/meta"
68

@@ -82,6 +84,9 @@ func ImportOneItem(c meta.Meta, item meta.ImportItem) tea.Cmd {
8284
return func() tea.Msg {
8385
if !item.Skip() {
8486
item.ImportError = c.Import(item)
87+
} else {
88+
// This explicit minor delay is for the sake of a visual effect of the progress bar.
89+
time.Sleep(100 * time.Millisecond)
8590
}
8691
return ImportOneItemDoneMsg{Item: item}
8792
}

internal/ui/progress/progress.go

+40-14
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package progress
33
import (
44
"fmt"
55

6+
prog "github.com/charmbracelet/bubbles/progress"
67
tea "github.com/charmbracelet/bubbletea"
78
"github.com/magodo/aztfy/internal/meta"
89
"github.com/magodo/aztfy/internal/ui/aztfyclient"
9-
common2 "github.com/magodo/aztfy/internal/ui/common"
10+
"github.com/magodo/aztfy/internal/ui/common"
1011
)
1112

1213
type result struct {
@@ -15,18 +16,20 @@ type result struct {
1516
}
1617

1718
type Model struct {
18-
c meta.Meta
19-
l meta.ImportList
20-
idx int
21-
results []result
19+
c meta.Meta
20+
l meta.ImportList
21+
idx int
22+
results []result
23+
progress prog.Model
2224
}
2325

2426
func NewModel(c meta.Meta, l meta.ImportList) Model {
2527
return Model{
26-
c: c,
27-
l: l,
28-
idx: 0,
29-
results: make([]result, common2.ProgressShowLastResults),
28+
c: c,
29+
l: l,
30+
idx: 0,
31+
results: make([]result, common.ProgressShowLastResults),
32+
progress: prog.NewModel(prog.WithDefaultGradient()),
3033
}
3134
}
3235

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

4346
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
4447
switch msg := msg.(type) {
48+
case tea.WindowSizeMsg:
49+
m.progress.Width = msg.Width - 4
50+
return m, nil
51+
// FrameMsg is sent when the progress bar wants to animate itself
52+
case prog.FrameMsg:
53+
progressModel, cmd := m.progress.Update(msg)
54+
m.progress = progressModel.(prog.Model)
55+
return m, cmd
4556
case aztfyclient.ImportOneItemDoneMsg:
57+
var cmds []tea.Cmd
58+
var cmd tea.Cmd
59+
60+
// Update results
4661
item := msg.Item
4762
m.l[m.idx] = item
4863
res := result{
4964
item: msg.Item,
5065
}
5166
if item.ImportError != nil {
52-
res.emoji = common2.WarningEmoji
67+
res.emoji = common.WarningEmoji
5368
} else {
54-
res.emoji = common2.RandomHappyEmoji()
69+
res.emoji = common.RandomHappyEmoji()
5570
}
5671
m.results = append(m.results[1:], res)
72+
73+
// Update progress bar
74+
cmd = m.progress.SetPercent(float64(m.idx+1) / float64(len(m.l)))
75+
cmds = append(cmds, cmd)
76+
5777
m.idx++
5878
if m.iterationDone() {
59-
return m, aztfyclient.FinishImport(m.l)
79+
cmd = aztfyclient.FinishImport(m.l)
80+
cmds = append(cmds, cmd)
81+
return m, tea.Batch(cmds...)
6082
}
61-
return m, aztfyclient.ImportOneItem(m.c, m.l[m.idx])
83+
cmd = aztfyclient.ImportOneItem(m.c, m.l[m.idx])
84+
cmds = append(cmds, cmd)
85+
return m, tea.Batch(cmds...)
6286
default:
6387
return m, nil
6488
}
@@ -80,7 +104,7 @@ func (m Model) View() string {
80104
for _, res := range m.results {
81105
emptyItem := meta.ImportItem{}
82106
if res.item == emptyItem {
83-
s += "........................\n"
107+
s += "...\n"
84108
} else {
85109
switch {
86110
case res.item.Skip():
@@ -93,6 +117,8 @@ func (m Model) View() string {
93117
}
94118
}
95119

120+
s += "\n\n" + m.progress.View()
121+
96122
return s
97123
}
98124

internal/ui/ui.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
146146
case aztfyclient.StartImportMsg:
147147
m.status = statusImporting
148148
m.progress = progress.NewModel(m.meta, msg.List)
149-
return m, m.progress.Init()
149+
return m, tea.Batch(
150+
m.progress.Init(),
151+
// Resize the progress bar
152+
func() tea.Msg { return m.winsize },
153+
)
150154
case aztfyclient.ImportDoneMsg:
151155
for idx, item := range msg.List {
152156
if item.ImportError != nil {

0 commit comments

Comments
 (0)