4
4
"fmt"
5
5
"strings"
6
6
7
+ "github.com/charmbracelet/bubbles/spinner"
7
8
tea "github.com/charmbracelet/bubbletea"
8
9
"github.com/charmbracelet/lipgloss"
9
10
@@ -13,9 +14,15 @@ import (
13
14
"github.com/dlvhdr/gh-dash/v4/utils"
14
15
)
15
16
17
+ type SectionState struct {
18
+ Count int
19
+ IsLoading bool
20
+ spinner spinner.Model
21
+ }
22
+
16
23
type Model struct {
17
24
sectionsConfigs []config.SectionConfig
18
- sectionCounts []* int
25
+ sectionCounts []SectionState
19
26
CurrSectionId int
20
27
}
21
28
@@ -26,16 +33,32 @@ func NewModel(ctx *context.ProgramContext) Model {
26
33
}
27
34
28
35
func (m Model ) Update (msg tea.Msg ) (Model , tea.Cmd ) {
29
- return m , nil
36
+ cmds := make ([]tea.Cmd , 0 )
37
+ switch msg := msg .(type ) {
38
+ case spinner.TickMsg :
39
+ for i , s := range m .sectionCounts {
40
+ if s .IsLoading {
41
+ var cmd tea.Cmd
42
+ m .sectionCounts [i ].spinner , cmd = s .spinner .Update (msg )
43
+ cmds = append (cmds , cmd )
44
+ }
45
+ }
46
+ }
47
+
48
+ return m , tea .Batch (cmds ... )
30
49
}
31
50
32
51
func (m Model ) View (ctx * context.ProgramContext ) string {
33
52
sectionTitles := make ([]string , 0 , len (m .sectionsConfigs ))
34
53
for i , section := range m .sectionsConfigs {
35
54
title := section .Title
36
55
// handle search section
37
- if i > 0 && m .sectionCounts [i ] != nil && ctx .Config .Theme .Ui .SectionsShowCount {
38
- title = fmt .Sprintf ("%s (%s)" , title , utils .ShortNumber (* m .sectionCounts [i ]))
56
+ if i > 0 {
57
+ if m .sectionCounts [i ].IsLoading {
58
+ title = fmt .Sprintf ("%s %s" , title , m .sectionCounts [i ].spinner .View ())
59
+ } else {
60
+ title = fmt .Sprintf ("%s (%s)" , title , utils .ShortNumber (m .sectionCounts [i ].Count ))
61
+ }
39
62
}
40
63
sectionTitles = append (sectionTitles , title )
41
64
}
@@ -68,11 +91,29 @@ func (m *Model) SetCurrSectionId(id int) {
68
91
69
92
func (m * Model ) UpdateSectionsConfigs (ctx * context.ProgramContext ) {
70
93
m .sectionsConfigs = ctx .GetViewSectionsConfig ()
94
+ m .sectionCounts = make ([]SectionState , len (m .sectionsConfigs ))
95
+ for i := range m .sectionsConfigs {
96
+ m .sectionCounts [i ] = SectionState {
97
+ Count : 0 ,
98
+ IsLoading : false ,
99
+ spinner : spinner .New (spinner .WithSpinner (spinner .Dot ), spinner .WithStyle (lipgloss .NewStyle ().Foreground (ctx .Theme .FaintText ).PaddingLeft (2 ))),
100
+ }
101
+ }
71
102
}
72
103
73
104
func (m * Model ) UpdateSectionCounts (sections []section.Section ) {
74
- m .sectionCounts = make ([]* int , len (sections ))
75
105
for i , s := range sections {
76
- m .sectionCounts [i ] = s .GetTotalCount ()
106
+ m .sectionCounts [i ].Count = s .GetTotalCount ()
107
+ m .sectionCounts [i ].IsLoading = s .GetIsLoading ()
77
108
}
78
109
}
110
+
111
+ func (m * Model ) SetAllLoading () []tea.Cmd {
112
+ cmds := make ([]tea.Cmd , 0 )
113
+ for i := range m .sectionCounts {
114
+ m .sectionCounts [i ].IsLoading = true
115
+ cmds = append (cmds , m .sectionCounts [i ].spinner .Tick )
116
+ }
117
+
118
+ return cmds
119
+ }
0 commit comments