@@ -3,10 +3,11 @@ package progress
3
3
import (
4
4
"fmt"
5
5
6
+ prog "github.com/charmbracelet/bubbles/progress"
6
7
tea "github.com/charmbracelet/bubbletea"
7
8
"github.com/magodo/aztfy/internal/meta"
8
9
"github.com/magodo/aztfy/internal/ui/aztfyclient"
9
- common2 "github.com/magodo/aztfy/internal/ui/common"
10
+ "github.com/magodo/aztfy/internal/ui/common"
10
11
)
11
12
12
13
type result struct {
@@ -15,18 +16,20 @@ type result struct {
15
16
}
16
17
17
18
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
22
24
}
23
25
24
26
func NewModel (c meta.Meta , l meta.ImportList ) Model {
25
27
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 ()),
30
33
}
31
34
}
32
35
@@ -42,23 +45,44 @@ func (m Model) Init() tea.Cmd {
42
45
43
46
func (m Model ) Update (msg tea.Msg ) (Model , tea.Cmd ) {
44
47
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
45
56
case aztfyclient.ImportOneItemDoneMsg :
57
+ var cmds []tea.Cmd
58
+ var cmd tea.Cmd
59
+
60
+ // Update results
46
61
item := msg .Item
47
62
m .l [m .idx ] = item
48
63
res := result {
49
64
item : msg .Item ,
50
65
}
51
66
if item .ImportError != nil {
52
- res .emoji = common2 .WarningEmoji
67
+ res .emoji = common .WarningEmoji
53
68
} else {
54
- res .emoji = common2 .RandomHappyEmoji ()
69
+ res .emoji = common .RandomHappyEmoji ()
55
70
}
56
71
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
+
57
77
m .idx ++
58
78
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 ... )
60
82
}
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 ... )
62
86
default :
63
87
return m , nil
64
88
}
@@ -80,7 +104,7 @@ func (m Model) View() string {
80
104
for _ , res := range m .results {
81
105
emptyItem := meta.ImportItem {}
82
106
if res .item == emptyItem {
83
- s += "........................ \n "
107
+ s += "...\n "
84
108
} else {
85
109
switch {
86
110
case res .item .Skip ():
@@ -93,6 +117,8 @@ func (m Model) View() string {
93
117
}
94
118
}
95
119
120
+ s += "\n \n " + m .progress .View ()
121
+
96
122
return s
97
123
}
98
124
0 commit comments