-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.go
232 lines (199 loc) · 5.66 KB
/
display.go
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"fmt"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/p5quared/decanter/Autolab"
"golang.org/x/oauth2"
)
func displayUserInfo(user Autolab.UserResponse) {
var emph = lipgloss.NewStyle().
PaddingLeft(1).
Foreground(colorPrimary).
Bold(true)
var body = lipgloss.NewStyle()
// UB Autolab API doesn't appear to return anything else for now.
// Could just be because I'm a transfer and have less info though...
s1 := fmt.Sprintf("%s: %s %s", emph.Render("Name"), body.Render(user.FirstName), body.Render(user.LastName))
s2 := fmt.Sprintf("%s: %s", emph.Render("Email"), body.Render(user.Email))
fmt.Println()
fmt.Println(s1)
fmt.Println(s2)
}
func displayCourseList(courses []Autolab.CoursesResponse) {
var headerStyle = lipgloss.NewStyle().
Align(lipgloss.Left).
Bold(true).
Foreground(colorPrimary).
PaddingTop(1).
PaddingLeft(0)
fmt.Println(headerStyle.Render("Your Current Courses (s24):"))
var courseStyle = lipgloss.NewStyle().
Align(lipgloss.Left).
PaddingLeft(1)
// TODO: Push an align course.Name to right
for _, course := range courses {
s := fmt.Sprintf("%s (%s)", course.DisplayName, course.Name)
fmt.Println(courseStyle.Render(s))
}
var helpStyle = lipgloss.NewStyle()
helpStr := "[Course Name] (Course ID)"
fmt.Println(helpStyle.Render(helpStr))
}
func displayAssessmentList(d Decanter, courses []Autolab.CoursesResponse) {
var headerStyle = lipgloss.NewStyle().
Align(lipgloss.Left).
Bold(true).
Foreground(colorPrimary).
PaddingTop(1).
PaddingLeft(0)
fmt.Println(headerStyle.Render("Your Assessments:"))
var OddRowStyle = lipgloss.NewStyle().
Align(lipgloss.Left)
var EvenRowStyle = lipgloss.NewStyle().
Inherit(OddRowStyle)
t := table.New().
Headers("Course", "Assessment", "Assigned", "Due Date", "Closed").
Border(lipgloss.NormalBorder()).
StyleFunc(func(r, c int) lipgloss.Style {
switch {
case r == 0:
return lipgloss.NewStyle().Bold(true).Foreground(colorPrimary)
case r%2 == 0:
return EvenRowStyle
default:
return OddRowStyle
}
})
var assessmentsFetched int
for _, course := range courses {
assessments, _ := d.GetUserAssessments(course.Name)
for _, ass := range assessments {
assessmentsFetched++
// TODO: Align columns
assTime := func(time_raw string) string {
tt := Autolab.ParseTime(time_raw)
return fmt.Sprintf("%s %d-%d", tt.Weekday().String()[:3], tt.Month(), tt.Day())
}
t.Row(course.Name, ass.Name, assTime(ass.Assigned), assTime(ass.Due), assTime(ass.Closed))
}
}
fmt.Println(t.Render())
}
func displayTime(time_raw string) string {
t := Autolab.ParseTime(time_raw)
tSuffix := "AM"
if t.Hour() >= 12 {
tSuffix = "PM"
}
twelveHour := t.Hour()
if twelveHour > 12 {
twelveHour -= 12
}
return fmt.Sprintf("%s %d-%d (%d:%d%s)", t.Weekday(), t.Month(), t.Day(), twelveHour, t.Minute(), tSuffix)
}
func displaySubmission(submission Autolab.SubmissionsResponse) {
title := emph("Latest Submission")
fmt.Println(title)
fmt.Println(" Version: ", submission.Version)
fmt.Println(" Submitted: ", displayTime(submission.Submitted))
fmt.Println(" Filename: ", submission.Filename)
var OddRowStyle = lipgloss.NewStyle().
Align(lipgloss.Left)
var EvenRowStyle = lipgloss.NewStyle().
Inherit(OddRowStyle)
t := table.New().
Headers("Problem", "Score").
Border(lipgloss.NormalBorder()).
StyleFunc(func(r, c int) lipgloss.Style {
switch {
case r == 0:
return lipgloss.NewStyle().Bold(true).Foreground(colorPrimary)
case r%2 == 0:
return EvenRowStyle
default:
return OddRowStyle
}
})
scores := submission.Scores
for k, v := range scores {
t.Row(k, fmt.Sprintf("%.2f", v))
}
fmt.Println(t.Render())
}
// Complete device flow and cache token to disk
func (d Decanter) interactiveSetup() {
// 1. DeviceAuth
// 2. DeviceAccessCode
// 3. Exchange
// 4. Save
// NOTE: Error on anything is fatal for now.
var (
dResp oauth2.DeviceAuthResponse
dCode string
token oauth2.Token
err error
)
spinner.New().
Title("Initiating device flow...").
Style(spinStyle).
Action(func() {
dResp, err = d.auth.DeviceAuth()
}).Run()
if err != nil {
fmt.Println("Error starting device flow: ", err)
return
}
fmt.Println(finished("Starting device flow..."))
fmt.Printf("\n Visit %s and enter the code %s to authenticate.\n\n", url(dResp.VerificationURI), emph(dResp.UserCode))
spinner.New().
Title(" Polling server for access code...").
Type(spinner.Dots).
Style(spinStyle).
Action(func() {
dCode, err = d.auth.DeviceAccessCode(dResp)
}).Run()
if err != nil || dCode == "" {
fmt.Println("Error getting code: ", err)
return
}
fmt.Println(finished("Got code"))
spinner.New().
Title("Exchanging code for token...").
Style(spinStyle).
Action(func() {
token, err = d.auth.ExchangeCodeForToken(dCode)
}).Run()
if err != nil {
fmt.Println("Error exchanging code for token: ", err)
return
}
fmt.Println(finished("Exchanged code for token"))
spinner.New().
Title("Saving credentials...").
Style(spinStyle).
Action(func() {
err = d.ts.Save(token)
}).Run()
if err != nil {
fmt.Println("Error saving token: ", err)
return
}
fmt.Println(finished("Saved token"))
finStyle := lipgloss.NewStyle().
Foreground(colorPrimary).Bold(true).
PaddingTop(1).PaddingLeft(2)
fmt.Println(finStyle.Render("Decanter setup complete! Try `decanter list me`"))
}
func areYouSure(msg, affirm, neg string) (ans bool) {
huh.NewConfirm().
Title(msg).
Value(&ans).
Affirmative(affirm).
Negative(neg).
WithTheme(decanterFormStyle()).
Run()
return
}