@@ -15,12 +15,7 @@ import (
15
15
"github.com/spf13/viper"
16
16
)
17
17
18
- var green lipgloss.Style
19
- var red lipgloss.Style
20
- var gray lipgloss.Style
21
- var cmdBox = lipgloss .NewStyle ().Border (lipgloss .RoundedBorder ())
22
-
23
- type doneMsg struct {
18
+ type doneCmdMsg struct {
24
19
failure * api.StructuredErrCLICommand
25
20
}
26
21
@@ -34,21 +29,6 @@ type resolveCmdMsg struct {
34
29
results * api.CLICommandResult
35
30
}
36
31
37
- type startTestMsg struct {
38
- text string
39
- }
40
-
41
- type resolveTestMsg struct {
42
- index int
43
- passed * bool
44
- }
45
-
46
- type testModel struct {
47
- text string
48
- passed * bool
49
- finished bool
50
- }
51
-
52
32
type cmdModel struct {
53
33
command string
54
34
passed * bool
@@ -57,7 +37,7 @@ type cmdModel struct {
57
37
tests []testModel
58
38
}
59
39
60
- type rootModel struct {
40
+ type cmdRootModel struct {
61
41
cmds []cmdModel
62
42
spinner spinner.Model
63
43
failure * api.StructuredErrCLICommand
@@ -67,26 +47,26 @@ type rootModel struct {
67
47
clear bool
68
48
}
69
49
70
- func initialModel (isSubmit bool ) rootModel {
50
+ func initialModelCmd (isSubmit bool ) cmdRootModel {
71
51
s := spinner .New ()
72
52
s .Spinner = spinner .Dot
73
- return rootModel {
53
+ return cmdRootModel {
74
54
spinner : s ,
75
55
isSubmit : isSubmit ,
76
56
cmds : []cmdModel {},
77
57
}
78
58
}
79
59
80
- func (m rootModel ) Init () tea.Cmd {
60
+ func (m cmdRootModel ) Init () tea.Cmd {
81
61
green = lipgloss .NewStyle ().Foreground (lipgloss .Color (viper .GetString ("color.green" )))
82
62
red = lipgloss .NewStyle ().Foreground (lipgloss .Color (viper .GetString ("color.red" )))
83
63
gray = lipgloss .NewStyle ().Foreground (lipgloss .Color (viper .GetString ("color.gray" )))
84
64
return m .spinner .Tick
85
65
}
86
66
87
- func (m rootModel ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
67
+ func (m cmdRootModel ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
88
68
switch msg := msg .(type ) {
89
- case doneMsg :
69
+ case doneCmdMsg :
90
70
m .failure = msg .failure
91
71
if m .failure == nil && m .isSubmit {
92
72
m .success = true
@@ -123,49 +103,15 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
123
103
}
124
104
}
125
105
126
- func (m rootModel ) View () string {
106
+ func (m cmdRootModel ) View () string {
127
107
if m .clear {
128
108
return ""
129
109
}
130
110
s := m .spinner .View ()
131
111
var str string
132
112
for _ , cmd := range m .cmds {
133
- var cmdStr string
134
- if ! cmd .finished {
135
- cmdStr += fmt .Sprintf ("%s %s" , s , cmd .command )
136
- } else if ! m .isSubmit {
137
- cmdStr += cmd .command
138
- } else if cmd .passed == nil {
139
- cmdStr += gray .Render (fmt .Sprintf ("? %s" , cmd .command ))
140
- } else if * cmd .passed {
141
- cmdStr += green .Render (fmt .Sprintf ("✓ %s" , cmd .command ))
142
- } else {
143
- cmdStr += red .Render (fmt .Sprintf ("X %s" , cmd .command ))
144
- }
145
- box := cmdBox .Render (fmt .Sprintf (" %s " , cmdStr ))
146
- // monkey patching the border on the box lol
147
- sliced := strings .Split (box , "\n " )
148
- sliced [2 ] = strings .Replace (sliced [2 ], "─" , "┬" , 1 )
149
- str += strings .Join (sliced , "\n " )
150
- for _ , test := range cmd .tests {
151
- var testStr string
152
- if ! test .finished {
153
- testStr += fmt .Sprintf (" %s %s" , s , test .text )
154
- } else if test .passed == nil {
155
- testStr += gray .Render (fmt .Sprintf (" ? %s" , test .text ))
156
- } else if * test .passed {
157
- testStr += green .Render (fmt .Sprintf (" ✓ %s" , test .text ))
158
- } else {
159
- testStr += red .Render (fmt .Sprintf (" X %s" , test .text ))
160
- }
161
- edges := " ├─"
162
- for i := 0 ; i < lipgloss .Height (testStr )- 1 ; i ++ {
163
- edges += "\n │ "
164
- }
165
- testStr = lipgloss .JoinHorizontal (lipgloss .Top , edges , testStr )
166
- str = lipgloss .JoinVertical (lipgloss .Left , str , testStr )
167
- }
168
- str += "\n "
113
+ str += renderTestHeader (cmd .command , m .spinner , cmd .finished , m .isSubmit , cmd .passed )
114
+ str += renderTests (cmd .tests , s )
169
115
if cmd .results != nil && m .finalized {
170
116
// render the results
171
117
str += fmt .Sprintf ("\n > Command exit code: %d\n " , cmd .results .ExitCode )
@@ -185,7 +131,7 @@ func (m rootModel) View() string {
185
131
return str
186
132
}
187
133
188
- func prettyPrint (test api.CLICommandTestCase ) string {
134
+ func prettyPrintCmd (test api.CLICommandTestCase ) string {
189
135
if test .ExitCode != nil {
190
136
return fmt .Sprintf ("Expect exit code %d" , * test .ExitCode )
191
137
}
@@ -239,13 +185,13 @@ func commandRenderer(
239
185
) {
240
186
var wg sync.WaitGroup
241
187
ch := make (chan tea.Msg , 1 )
242
- p := tea .NewProgram (initialModel (isSubmit ), tea .WithoutSignalHandler ())
188
+ p := tea .NewProgram (initialModelCmd (isSubmit ), tea .WithoutSignalHandler ())
243
189
wg .Add (1 )
244
190
go func () {
245
191
defer wg .Done ()
246
192
if model , err := p .Run (); err != nil {
247
193
fmt .Fprintln (os .Stderr , err )
248
- } else if r , ok := model .(rootModel ); ok {
194
+ } else if r , ok := model .(cmdRootModel ); ok {
249
195
r .clear = false
250
196
r .finalized = true
251
197
output := termenv .NewOutput (os .Stdout )
@@ -265,7 +211,7 @@ func commandRenderer(
265
211
for i , cmd := range data .CLICommandData .Commands {
266
212
ch <- startCmdMsg {cmd : results [i ].FinalCommand }
267
213
for _ , test := range cmd .Tests {
268
- ch <- startTestMsg {text : prettyPrint (test )}
214
+ ch <- startTestMsg {text : prettyPrintCmd (test )}
269
215
}
270
216
time .Sleep (500 * time .Millisecond )
271
217
for j := range cmd .Tests {
@@ -294,7 +240,7 @@ func commandRenderer(
294
240
}
295
241
time .Sleep (500 * time .Millisecond )
296
242
297
- ch <- doneMsg {failure : failure }
243
+ ch <- doneCmdMsg {failure : failure }
298
244
}()
299
245
wg .Wait ()
300
246
}
0 commit comments