Skip to content

Commit 677e47c

Browse files
Show XP in CLI success output (#188)
Show XP in CLI success output
1 parent f4510f9 commit 677e47c

6 files changed

Lines changed: 52 additions & 14 deletions

File tree

client/lessons.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,17 @@ type SubmissionDebugData struct {
199199
ResponseBody string
200200
}
201201

202+
type XPBreakdownItem struct {
203+
Name string
204+
Percent float64
205+
XP int
206+
}
207+
202208
type LessonSubmissionEvent struct {
203209
ResultSlug VerificationResultSlug
204210
StructuredErrCLI *StructuredErrCLI
211+
XPReward int
212+
XPBreakdown []XPBreakdownItem
205213
}
206214

207215
type StructuredErrCLI struct {

messages/messages.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ type ResolveTestMsg struct {
2121
}
2222

2323
type DoneStepMsg struct {
24-
Result api.VerificationResultSlug
25-
Failure *api.StructuredErrCLI
24+
Result api.VerificationResultSlug
25+
Failure *api.StructuredErrCLI
26+
XPReward int
27+
XPBreakdown []api.XPBreakdownItem
2628
}
2729

2830
type ResolveStepMsg struct {

render/models.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ type stepModel struct {
2222
}
2323

2424
type rootModel struct {
25-
steps []stepModel
26-
spinner spinner.Model
27-
result api.VerificationResultSlug
28-
failure *api.StructuredErrCLI
29-
isSubmit bool
30-
finalized bool
31-
clear bool
25+
steps []stepModel
26+
spinner spinner.Model
27+
result api.VerificationResultSlug
28+
failure *api.StructuredErrCLI
29+
xpReward int
30+
xpBreakdown []api.XPBreakdownItem
31+
isSubmit bool
32+
finalized bool
33+
clear bool
3234
}
3335

3436
func initModel(isSubmit bool) rootModel {

render/render.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
3636
case messages.DoneStepMsg:
3737
m.result = msg.Result
3838
m.failure = msg.Failure
39+
m.xpReward = msg.XPReward
40+
m.xpBreakdown = msg.XPBreakdown
3941
m.clear = true
4042
return m, tea.Quit
4143

@@ -121,8 +123,10 @@ func StartRenderer(data api.CLIData, isSubmit bool, ch chan tea.Msg) func(api.Le
121123

122124
return func(submissionEvent api.LessonSubmissionEvent) {
123125
ch <- messages.DoneStepMsg{
124-
Result: submissionEvent.ResultSlug,
125-
Failure: submissionEvent.StructuredErrCLI,
126+
Result: submissionEvent.ResultSlug,
127+
Failure: submissionEvent.StructuredErrCLI,
128+
XPReward: submissionEvent.XPReward,
129+
XPBreakdown: submissionEvent.XPBreakdown,
126130
}
127131
wg.Wait()
128132
}

render/view.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,30 @@ func (m rootModel) View() string {
151151
}
152152

153153
if m.result == api.VerificationResultSlugSuccess && m.isSubmit {
154-
str.WriteString("\n\n" + green.Render("All tests passed! 🎉") + "\n\n")
155-
str.WriteString(green.Render("Return to your browser to continue with the next lesson.") + "\n\n")
154+
str.WriteString("\n\n" + green.Render("All tests passed! 🎉") + "\n")
155+
if m.xpReward >= 0 {
156+
str.WriteString("\n")
157+
str.WriteString(green.Bold(true).Render(fmt.Sprintf("Gained +%d XP", m.xpReward)))
158+
str.WriteByte('\n')
159+
for _, item := range m.xpBreakdown {
160+
if item.XP == 0 {
161+
continue
162+
}
163+
sign := "+"
164+
xp := item.XP
165+
if xp < 0 {
166+
sign = "-"
167+
xp = -xp
168+
}
169+
if item.Percent > 0 {
170+
str.WriteString(gray.Render(fmt.Sprintf("%s%3d XP (%-4s %s)", sign, xp, fmt.Sprintf("%.0f%%", item.Percent*100), item.Name)))
171+
} else {
172+
str.WriteString(gray.Render(fmt.Sprintf("%s%3d XP (%s)", sign, xp, item.Name)))
173+
}
174+
str.WriteByte('\n')
175+
}
176+
}
177+
str.WriteString("\n" + green.Render("Return to your browser to continue with the next lesson.") + "\n\n")
156178
} else if m.result == api.VerificationResultSlugNoop {
157179
str.WriteString("\n\nTests failed! ❌")
158180
fmt.Fprintf(&str, "\n\nFailed Step: %v", m.failure.FailedStepIndex+1)

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.29.5
1+
v1.29.6

0 commit comments

Comments
 (0)