-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathview.go
More file actions
204 lines (185 loc) · 6.4 KB
/
Copy pathview.go
File metadata and controls
204 lines (185 loc) · 6.4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package render
import (
"fmt"
"strings"
"time"
"unicode/utf8"
api "github.com/bootdotdev/bootdev/client"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/lipgloss"
)
const safeStepIcon = "🛡︎"
func renderTestHeader(header string, spinner spinner.Model, isFinished bool, isSubmit bool, passed *bool, noPenaltyOnFail bool) string {
if noPenaltyOnFail {
header = fmt.Sprintf("%s %s", header, white.Render(safeStepIcon))
}
cmdStr := renderTest(header, spinner.View(), isFinished, &isSubmit, passed)
box := borderBox.Render(fmt.Sprintf(" %s ", cmdStr))
sliced := strings.Split(box, "\n")
sliced[2] = strings.Replace(sliced[2], "─", "┬", 1)
return strings.Join(sliced, "\n") + "\n"
}
func renderTests(tests []testModel, spinner string) string {
var str strings.Builder
var edges strings.Builder
for _, test := range tests {
testStr := renderTest(test.text, spinner, test.finished, nil, test.passed)
testStr = fmt.Sprintf(" %s", testStr)
height := lipgloss.Height(testStr)
edges.Reset()
edges.WriteString(" ├─")
for i := 1; i < height; i++ {
edges.WriteString("\n │ ")
}
str.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, edges.String(), testStr))
str.WriteByte('\n')
}
return str.String()
}
func renderTest(text string, spinner string, isFinished bool, isSubmit *bool, passed *bool) string {
testStr := ""
if !isFinished {
testStr += fmt.Sprintf("%s %s", spinner, text)
} else if isSubmit != nil && !*isSubmit {
testStr += text
} else if passed == nil {
testStr += gray.Render(fmt.Sprintf("? %s", text))
} else if *passed {
testStr += green.Render(fmt.Sprintf("✓ %s", text))
} else {
testStr += red.Render(fmt.Sprintf("X %s", text))
}
return testStr
}
func renderJqOutputs(outputs []api.CLICommandJqOutput) string {
if len(outputs) == 0 {
return ""
}
var str strings.Builder
str.WriteString("\n > jq output:\n\n")
for _, output := range outputs {
str.WriteString(gray.Render(fmt.Sprintf("Query: %s", output.Query)))
str.WriteByte('\n')
if output.Error != "" {
str.WriteString(gray.Render(fmt.Sprintf("Error: %s", output.Error)))
str.WriteByte('\n')
str.WriteByte('\n')
continue
}
if len(output.Results) == 0 {
str.WriteString(gray.Render("Results: [none]"))
str.WriteByte('\n')
str.WriteByte('\n')
continue
}
str.WriteString(gray.Render("Results:"))
str.WriteByte('\n')
for _, line := range output.Results {
str.WriteString(gray.Render(" - " + line))
str.WriteByte('\n')
}
str.WriteByte('\n')
}
return str.String()
}
func (m rootModel) View() string {
if m.clear {
return ""
}
s := m.spinner.View()
var str strings.Builder
for _, step := range m.steps {
str.WriteString(renderTestHeader(step.step, m.spinner, step.finished, m.isSubmit, step.passed, step.noPenaltyOnFail))
str.WriteString(renderTests(step.tests, s))
if step.sleepAfter != "" && step.finished {
sleepBox := borderBox.Render(fmt.Sprintf(" %s ", step.sleepAfter))
str.WriteString(sleepBox)
str.WriteByte('\n')
}
if step.result == nil || !m.finalized {
continue
}
if step.result.CLICommandResult != nil {
for _, test := range step.tests {
if strings.Contains(test.text, "exit code") {
fmt.Fprintf(&str, "\n > Command exit code: %d\n", step.result.CLICommandResult.ExitCode)
break
}
}
str.WriteString(" > Command stdout:\n\n")
sliced := strings.SplitSeq(step.result.CLICommandResult.Stdout, "\n")
i := 0
runeCount := 0
const maxLines, maxRunes = 32, 5120
for s := range sliced {
if i >= maxLines || runeCount >= maxRunes {
str.WriteString(gray.Render("... output visually truncated, full output captured"))
str.WriteByte('\n')
break
}
runeCount += utf8.RuneCountInString(s)
str.WriteString(gray.Render(s))
str.WriteByte('\n')
i++
}
str.WriteString(renderJqOutputs(step.result.CLICommandResult.JqOutputs))
availableVariables, expectsVariables := availableVariablesForCLIResult(*step.result.CLICommandResult)
if expectsVariables {
str.WriteString(renderVariableSection("Variables Available", availableVariables))
}
}
if step.result.HTTPRequestResult != nil {
str.WriteString(printHTTPRequestResult(*step.result.HTTPRequestResult))
}
}
if m.result == api.VerificationResultSlugSuccess && m.isSubmit {
str.WriteString("\n\n" + green.Render("All tests passed! 🎉") + "\n")
if m.xpReward >= 0 {
str.WriteString("\n")
str.WriteString(green.Bold(true).Render(fmt.Sprintf("Gained +%d XP", m.xpReward)))
str.WriteByte('\n')
for _, item := range m.xpBreakdown {
if item.XP == 0 {
continue
}
sign := "+"
xp := item.XP
if xp < 0 {
sign = "-"
xp = -xp
}
if item.Percent > 0 {
str.WriteString(gray.Render(fmt.Sprintf("%s%3d XP (%-4s %s)", sign, xp, fmt.Sprintf("%.0f%%", item.Percent*100), item.Name)))
} else {
str.WriteString(gray.Render(fmt.Sprintf("%s%3d XP (%s)", sign, xp, item.Name)))
}
str.WriteByte('\n')
}
}
str.WriteString("\n" + green.Render("Return to your browser to continue with the next lesson.") + "\n\n")
} else if m.result == api.VerificationResultSlugNoop {
str.WriteString("\n\nTests failed! ❌")
fmt.Fprintf(&str, "\n\nFailed Step: %v", m.failure.FailedStepIndex+1)
str.WriteString("\nError: " + m.failure.ErrorMessage + "\n")
str.WriteString("\n" + white.Render(safeStepIcon) + " This was a safe step.\n")
str.WriteString("You haven't passed, but you also haven't lost armor or Sharpshooter progress.\n\n")
} else if m.result == api.VerificationResultSlugFailure {
str.WriteString("\n\n" + red.Render("Tests failed! ❌"))
if m.failure != nil {
if m.failure.FailedStepIndex >= 0 && m.failure.FailedStepIndex < len(m.steps) {
fmt.Fprintf(&str, "%s", red.Render(fmt.Sprintf("\n\nFailed Command: %s", m.steps[m.failure.FailedStepIndex].step)))
}
fmt.Fprintf(&str, "%s", red.Render(fmt.Sprintf("\n\nFailed Step: %v", m.failure.FailedStepIndex+1)))
fmt.Fprintf(&str, "%s", red.Render("\nError: "+m.failure.ErrorMessage))
} else {
fmt.Fprintf(&str, "%s", red.Render("\n\nFailed Step: unknown"))
fmt.Fprintf(&str, "%s", red.Render("\nError: unknown"))
}
str.WriteString("\n\n")
currentDate := time.Now().Format("2006-01-02")
if strings.HasSuffix(currentDate, "04-01") {
str.WriteString(magenta.Render(fmt.Sprintf("This incident has been reported to your system administrator. [%s]\n", currentDate)))
}
}
return str.String()
}