-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathrender.go
More file actions
133 lines (118 loc) · 3.39 KB
/
Copy pathrender.go
File metadata and controls
133 lines (118 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package render
import (
"fmt"
"os"
"sync"
api "github.com/bootdotdev/bootdev/client"
"github.com/bootdotdev/bootdev/messages"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/spf13/viper"
)
var (
green lipgloss.Style
red lipgloss.Style
magenta lipgloss.Style
gray lipgloss.Style
white lipgloss.Style
borderBox = lipgloss.NewStyle().Border(lipgloss.RoundedBorder())
)
func (m rootModel) Init() tea.Cmd {
green = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.green")))
red = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.red")))
magenta = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.magenta")))
gray = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.gray")))
white = lipgloss.NewStyle().Foreground(lipgloss.Color("15"))
return m.spinner.Tick
}
func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case messages.DoneStepMsg:
m.result = msg.Result
m.failure = msg.Failure
m.xpReward = msg.XPReward
m.xpBreakdown = msg.XPBreakdown
m.clear = true
return m, tea.Quit
case messages.StartStepMsg:
step := fmt.Sprintf("Running: %s", msg.CMD)
if msg.TmdlQuery != nil {
step += fmt.Sprintf(" (TMDL query: '%s')", *msg.TmdlQuery)
}
if msg.CMD == "" {
step = fmt.Sprintf("%s %s", msg.Method, msg.URL)
}
m.steps = append(m.steps, stepModel{
step: step,
tests: []testModel{},
noPenaltyOnFail: msg.NoPenaltyOnFail,
})
return m, nil
case messages.SleepMsg:
if len(m.steps) > 0 {
lastStepIdx := len(m.steps) - 1
durationSec := float64(msg.DurationMs) / 1000.0
sleepText := ""
if durationSec >= 1.0 {
sleepText = fmt.Sprintf("Waiting %.1fs...", durationSec)
} else {
sleepText = fmt.Sprintf("Waiting %dms...", msg.DurationMs)
}
m.steps[lastStepIdx].sleepAfter = sleepText
}
return m, nil
case messages.ResolveStepMsg:
m.steps[msg.Index].passed = msg.Passed
m.steps[msg.Index].finished = true
if msg.Result != nil {
m.steps[msg.Index].result = msg.Result
}
return m, nil
case messages.StartTestMsg:
m.steps[len(m.steps)-1].tests = append(
m.steps[len(m.steps)-1].tests,
testModel{text: msg.Text},
)
return m, nil
case messages.ResolveTestMsg:
m.steps[msg.StepIndex].tests[msg.TestIndex].passed = msg.Passed
m.steps[msg.StepIndex].tests[msg.TestIndex].finished = true
return m, nil
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}
func StartRenderer(data api.CLIData, isSubmit bool, ch chan tea.Msg) func(api.LessonSubmissionEvent) {
var wg sync.WaitGroup
p := tea.NewProgram(initModel(isSubmit), tea.WithoutSignalHandler())
wg.Add(1)
go func() {
defer wg.Done()
if model, err := p.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
} else if r, ok := model.(rootModel); ok {
r.clear = false
r.finalized = true
output := termenv.NewOutput(os.Stdout)
output.WriteString(r.View())
}
}()
go func() {
for {
msg := <-ch
p.Send(msg)
}
}()
return func(submissionEvent api.LessonSubmissionEvent) {
ch <- messages.DoneStepMsg{
Result: submissionEvent.ResultSlug,
Failure: submissionEvent.StructuredErrCLI,
XPReward: submissionEvent.XPReward,
XPBreakdown: submissionEvent.XPBreakdown,
}
wg.Wait()
}
}