/
course.go
81 lines (74 loc) · 2.27 KB
/
course.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
package course
import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/manifoldco/promptui/list"
"github.com/pathbird/pbauthor/internal/graphql"
"github.com/pkg/errors"
"strings"
)
var (
promptCourseTemplate = newSelectTemplate("Course", "🎓", ".Course.Name")
promptCodexCategoryTemplate = newSelectTemplate("Codex Category", "😻", ".Name")
)
func PromptCourse(courses []graphql.CourseEdge) (*graphql.Course, error) {
if len(courses) == 0 {
return nil, errors.New("no courses to select from")
}
searcher := newSubstrSearcher(func(i int) string {
return courses[i].Course.Name
})
prompt := promptui.Select{
Label: "Course",
Items: courses,
Templates: promptCourseTemplate,
Searcher: searcher,
}
n, _, err := prompt.Run()
if err != nil {
return nil, err
}
return &courses[n].Course, err
}
func PromptCodexCategory(cats []graphql.CodexCategory) (*graphql.CodexCategory, error) {
// Meow!
if len(cats) == 0 {
return nil, errors.New("course has no codex categories")
}
if len(cats) == 1 {
return &cats[0], nil
}
searcher := newSubstrSearcher(func(i int) string {
return cats[i].Name
})
prompt := promptui.Select{
Label: "Codex Category",
Items: cats,
Templates: promptCodexCategoryTemplate,
Searcher: searcher,
}
n, _, err := prompt.Run()
if err != nil {
return nil, err
}
return &cats[n], nil
}
// Construct a promptui.SelectTemplates.
// This helper function exists to have uniformity across different select prompts.
func newSelectTemplate(label string, emoji string, attr string) *promptui.SelectTemplates {
// Note: emoji's (in my terminal) render as two monospace characters wide
return &promptui.SelectTemplates{
Label: fmt.Sprintf(`%s {{ "%s:" | bold }}`, emoji, label),
Active: fmt.Sprintf(` > {{ %s | cyan | underline }}`, attr),
Inactive: fmt.Sprintf(" {{ %s }}", attr),
Selected: fmt.Sprintf("%s {{ %s | bold }}", emoji, attr),
}
}
// Create a new searcher function that simply checks if the query is a substring.
// The resolver argument should resolve the index of the item to a string.
func newSubstrSearcher(resolver func(int) string) list.Searcher {
return func(input string, index int) bool {
val := resolver(index)
return strings.Contains(strings.ToLower(val), strings.ToLower(input))
}
}