/
view.go
415 lines (351 loc) · 9.33 KB
/
view.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package triage
import (
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
"github.com/kr/text"
"github.com/tj/go-tea/input"
"github.com/aybabtme/rgbterm"
"github.com/dustin/go-humanize"
"github.com/kyokomi/emoji"
"github.com/tj/go-css/csshex"
"github.com/tj/go-tea"
"github.com/tj/go-tea/option"
"github.com/tj/go-tea/options"
"github.com/tj/go-tea/shortcut"
"github.com/tj/go-termd"
"github.com/tj/triage/internal/colors"
)
// defaultTheme is the default code syntax highlighting theme.
var defaultTheme = termd.SyntaxTheme{
"comment": termd.Style{
Color: "#323232",
},
"literal": termd.Style{
Color: "#555555",
},
"name": termd.Style{
Color: "#777777",
},
"name.function": termd.Style{
Color: "#444444",
},
"literal.string": termd.Style{
Color: "#333333",
},
}
// key is a shortcut key.
type key struct {
Key string
Help string
}
// View function.
func View(ctx context.Context, model tea.Model) string {
switch m := model.(Model); m.Page {
case PageNotifications:
return viewNotifications(ctx, m)
case PageNotification:
return viewNotification(ctx, m)
case PageComment:
return viewComment(ctx, m)
case PageLabels:
return viewLabels(ctx, m)
case PagePriorities:
return viewPriorities(ctx, m)
default:
panic("unhandled page")
}
}
// viewNotifications page.
func viewNotifications(ctx context.Context, m Model) string {
w := new(bytes.Buffer)
// loading
if m.Loading {
return loading(m)
}
// no notifications
if len(m.Notifications) == 0 {
return centered(m, "Looks like you're all done 😊")
}
// padding
defer padding(w)()
// search focused
if m.Searching {
fmt.Fprintf(w, " Searching: %s\r\n\r\n", input.View(m.SearchInput))
}
// search blurred
if !m.Searching && m.SearchInput.Value != "" {
fmt.Fprintf(w, " Searching: %s\r\n\r\n", m.SearchInput.Value)
}
// sort by updated time asc
sort.Slice(m.Notifications, func(i, j int) bool {
a := m.Notifications[i]
b := m.Notifications[j]
return a.GetUpdatedAt().After(b.GetUpdatedAt())
})
// filter
filtered := filterNotifications(m.Notifications, m.SearchInput.Value)
// notifications
for i, n := range filtered {
// title
if m.Selected == i {
fmt.Fprintf(w, " * %s\r\n", colors.Bold(n.Repository.GetFullName()))
} else {
fmt.Fprintf(w, " %s\r\n", colors.Bold(n.Repository.GetFullName()))
}
// marking as read
if m.MarkingAsRead && m.Selected == i {
fmt.Fprintf(w, " \033[32mMarking as read.\033[0m\r\n\r\n\r\n")
continue
}
// unsubscribing
if m.Unsubscribing && m.Selected == i {
fmt.Fprintf(w, " \033[32mUnsubscribing.\033[0m\r\n\r\n\r\n")
continue
}
// unwatching
if m.Unwatching && m.Selected == i {
fmt.Fprintf(w, " \033[32mUnwatching.\033[0m\r\n\r\n\r\n")
continue
}
// subject
fmt.Fprintf(w, " %s\r\n", n.Subject.GetTitle())
// updated time
fmt.Fprintf(w, " Updated %s (%s)\r\n", humanize.Time(n.GetUpdatedAt()), n.GetReason())
fmt.Fprintf(w, "\r\n")
}
fmt.Fprintf(w, "\r\n")
// viewport
var offset int
if m.Searching || m.SearchInput.Value != "" {
offset = 3
}
s := viewport(w.String(), m.NotificationsScrollY, m.Height, offset)
// menu
if m.Searching {
s = menu(s, m,
shortcut.Key{"Esc", "Abort"},
shortcut.Key{"Enter", "Save"})
} else {
s = menu(s, m,
shortcut.Key{"q", "Quit"},
shortcut.Key{"→", "View"},
shortcut.Key{"↑↓", "Scroll"},
shortcut.Key{"r", "Mark read"},
shortcut.Key{"u", "Unsubscribe"},
shortcut.Key{"U", "Unwatch"},
shortcut.Key{"R", "Refresh"},
shortcut.Key{"/", "Search"})
}
return s
}
// viewNotification page.
func viewNotification(ctx context.Context, m Model) string {
config := MustConfigFromContext(ctx)
theme := config.Theme.Code
w := new(bytes.Buffer)
n := m.Notification
issue := m.Issue
labels := m.Labels
comments := m.Comments
// padding
defer padding(w)()
// header
fmt.Fprintf(w, " %s\r\n", colors.Bold(n.Repository.GetFullName()))
fmt.Fprintf(w, " %s\r\n", n.Subject.GetTitle())
if issue == nil {
fmt.Fprintf(w, "\r\n")
} else {
fmt.Fprintf(w, " #%d opened %s by @%s\r\n", issue.GetNumber(), humanize.Time(issue.GetCreatedAt()), issue.GetUser().GetLogin())
}
// pending
switch {
case m.LoadingIssue:
fmt.Fprintf(w, "\r\n%s\r\n\r\n", hr())
fmt.Fprintf(w, " Loading\r\n")
return w.String()
case m.MarkingAsRead:
fmt.Fprintf(w, "\r\n%s\r\n\r\n", hr())
fmt.Fprintf(w, " Marking as read\r\n")
return w.String()
case m.Unsubscribing:
fmt.Fprintf(w, "\r\n%s\r\n\r\n", hr())
fmt.Fprintf(w, " Unsubscribing\r\n")
return w.String()
case m.Unwatching:
fmt.Fprintf(w, "\r\n%s\r\n\r\n", hr())
fmt.Fprintf(w, " Unwatching\r\n")
return w.String()
}
// labels
if len(labels) > 0 {
fmt.Fprintf(w, " ")
for _, l := range labels {
r, g, b, ok := csshex.Parse(l.GetColor())
if !ok {
continue
}
name := fmt.Sprintf(" %s ", l.GetName())
name = rgbterm.BgString(name, r, g, b)
name = rgbterm.FgString(name, 0, 0, 0)
emoji.Fprintf(w, "%s ", name)
}
fmt.Fprintf(w, "\r\n")
}
// body
fmt.Fprintf(w, "\r\n%s\r\n\r\n", hr())
if body := issue.GetBody(); body == "" {
fmt.Fprintf(w, " No description provided.\r\n")
} else {
fmt.Fprintf(w, "%s", text.Indent(markdownText(body, theme), " "))
}
// comments
fmt.Fprintf(w, "\r\n")
fmt.Fprintf(w, "%s\r\n", hr())
for i, c := range comments {
fmt.Fprintf(w, "\r\n")
fmt.Fprintf(w, " %s %s\r\n\r\n", colors.Bold("@"+c.GetUser().GetLogin()), humanize.Time(c.GetCreatedAt()))
fmt.Fprintf(w, "%s", text.Indent(markdownText(c.GetBody(), theme), " "))
if i < len(comments)-1 {
fmt.Fprintf(w, "%s\r\n", hr())
}
}
fmt.Fprintf(w, "\r\n")
// viewport
offset := 7
if len(labels) > 0 {
offset = 8
}
s := viewport(w.String(), m.NotificationScrollY, m.Height, offset)
// menu
s = menu(s, m,
shortcut.Key{"q", "Quit"},
shortcut.Key{"←", "Back"},
shortcut.Key{"↑↓", "Scroll"},
shortcut.Key{"r", "Mark read"},
shortcut.Key{"u", "Unsubscribe"},
shortcut.Key{"c", "Comment"},
shortcut.Key{"l", "Labels"},
shortcut.Key{"p", "Priority"},
shortcut.Key{"o", "Open"},
shortcut.Key{"R", "Refresh"})
return s
}
// viewLabels page.
func viewLabels(ctx context.Context, m Model) string {
w := new(bytes.Buffer)
// loading
if m.Loading || m.LoadingLabels {
return loading(m)
}
// padding
defer padding(w)()
fmt.Fprintf(w, " Press space to select labels:\r\n\r\n")
fmt.Fprintf(w, "%s", options.View(m.LabelOptions))
return menu(w.String(), m,
shortcut.Key{"Esc", "Abort"},
shortcut.Key{"Space", "Toggle"},
shortcut.Key{"Enter", "Save"})
}
// viewPriorities page.
func viewPriorities(ctx context.Context, m Model) string {
w := new(bytes.Buffer)
// padding
defer padding(w)()
fmt.Fprintf(w, " Select a priority:\r\n\r\n")
fmt.Fprintf(w, "%s", option.View(m.PriorityOptions))
return menu(w.String(), m,
shortcut.Key{"Esc", "Abort"},
shortcut.Key{"Space", "Toggle"},
shortcut.Key{"Enter", "Save"})
}
// viewComment page.
func viewComment(ctx context.Context, m Model) string {
w := new(bytes.Buffer)
// padding
defer padding(w)()
fmt.Fprintf(w, " Press enter to save your comment:\r\n\r\n")
fmt.Fprintf(w, " %s", input.View(m.CommentInput))
return menu(w.String(), m,
shortcut.Key{"Esc", "Abort"},
shortcut.Key{"Enter", "Save"})
}
// loading indicator.
func loading(m Model) string {
if m.Height == 0 {
return ""
}
return centered(m, "Loading")
}
// centered text.
func centered(m Model, s string) string {
y := strings.Repeat("\r\n", (m.Height/2)-1)
x := strings.Repeat(" ", (m.Width/2)-(len(s)/2))
return y + x + s
}
// menu view.
func menu(s string, m Model, keys ...shortcut.Key) string {
// TODO: refactor this stuff using a nicer box model
if m.Height == 0 {
return ""
}
lines := strings.Split(s, "\r\n")
for i := len(lines); i < m.Height; i++ {
lines = append(lines, "")
}
lines[len(lines)-2] = strings.Repeat(" ", m.Width)
lines[len(lines)-1] = shortcut.View(shortcut.Model{keys})
return strings.Join(lines, "\r\n")
}
// viewport returns a view into the lines of text, providing
// the scroll offset, height of the viewport, and offset
// which retains N lines behaving like a "sticky" header.
func viewport(s string, scroll, height, offset int) string {
lines := strings.Split(s, "\r\n")
// offset
leading := lines[:offset]
lines = lines[offset:]
// view
from := scroll
to := scroll + height - offset
lines = append(leading, bounded(lines, from, to)...)
return strings.Join(lines, "\r\n")
}
// bounded slice.
func bounded(s []string, from, to int) []string {
from = max(0, min(from, len(s)))
to = max(0, min(to, len(s)))
return s[from:to]
}
// markdownText helper.
func markdownText(s string, theme *termd.SyntaxTheme) string {
var md termd.Compiler
if theme == nil {
md.SyntaxHighlighter = defaultTheme
} else {
md.SyntaxHighlighter = *theme
}
// blackfriday's markdown parser only supports a single rune as the linebreak
s = strings.Replace(s, "\r\n", "\n", -1)
// tabs -> spaces
s = strings.Replace(s, "\t", " ", -1)
// compile and apply emoji support
s = md.Compile(s)
s = strings.Replace(s, "\n", "\r\n", -1)
s = emoji.Sprintf("%s", s)
return s
}
// padding util.
func padding(w io.Writer) func() {
fmt.Fprintf(w, "\r\n")
return func() {
fmt.Fprintf(w, "\r\n")
}
}
// hr is a horizontal rule.
func hr() string {
return fmt.Sprintf(" \033[38;5;102m%s\033[0m", strings.Repeat("─", 90))
}