-
Notifications
You must be signed in to change notification settings - Fork 33
/
pick.go
145 lines (133 loc) · 3.18 KB
/
pick.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
package cmd
import (
"fmt"
"strings"
"github.com/AlecAivazis/survey/v2"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
"github.com/j178/leetgo/editor"
"github.com/j178/leetgo/lang"
"github.com/j178/leetgo/leetcode"
)
func askFilter(c leetcode.Client) (filter leetcode.QuestionFilter, err error) {
tags, err := c.GetQuestionTags()
if err != nil {
return
}
tagNames := make([]string, 0, len(tags))
tagNamesToSlug := make(map[string]string, len(tags))
for _, t := range tags {
tagNames = append(tagNames, t.Name)
tagNamesToSlug[t.Name] = t.Slug
}
qs := []*survey.Question{
{
Name: "Difficulty",
Prompt: &survey.Select{
Message: "Select a difficulty level",
Options: []string{"All", "Easy", "Medium", "Hard"},
},
Transform: func(ans interface{}) (newAns interface{}) {
opt := ans.(survey.OptionAnswer)
if opt.Index == 0 {
return survey.OptionAnswer{Value: ""}
}
opt.Value = strings.ToUpper(opt.Value)
return opt
},
},
{
Name: "Status",
Prompt: &survey.Select{
Message: "Select question status",
Options: []string{"All", "Not Started", "Tried", "Ac"},
},
Transform: func(ans interface{}) (newAns interface{}) {
opt := ans.(survey.OptionAnswer)
if opt.Index == 0 {
return survey.OptionAnswer{Value: ""}
}
opt.Value = strings.ReplaceAll(strings.ToUpper(opt.Value), " ", "_")
return opt
},
},
{
Name: "Tags",
Prompt: &survey.MultiSelect{
Message: "Select tags",
Options: tagNames,
},
Transform: func(ans interface{}) (newAns interface{}) {
opt := ans.([]survey.OptionAnswer)
if len(opt) == 0 {
return opt
}
if len(opt) == len(tagNames) {
return []survey.OptionAnswer{}
}
for i, o := range opt {
opt[i].Value = tagNamesToSlug[o.Value]
}
return opt
},
},
}
err = survey.Ask(qs, &filter, survey.WithRemoveSelectAll())
if err != nil {
return
}
return filter, nil
}
var skipEditor bool
func init() {
pickCmd.Flags().BoolVarP(&skipEditor, "skip-editor", "", false, "Skip opening the editor")
}
var pickCmd = &cobra.Command{
Use: "pick [qid]",
Short: "Generate a new question",
Example: `leetgo pick # show a list of questions to pick
leetgo pick today
leetgo pick 549
leetgo pick two-sum`,
Args: cobra.MaximumNArgs(1),
Aliases: []string{"p"},
ValidArgs: []string{"today", "yesterday"},
RunE: func(cmd *cobra.Command, args []string) error {
c := leetcode.NewClient(leetcode.ReadCredentials())
var q *leetcode.QuestionData
if len(args) > 0 {
qid := args[0]
qs, err := leetcode.ParseQID(qid, c)
if err != nil {
return err
}
if len(qs) > 1 {
return fmt.Errorf("`leetgo pick` cannot handle multiple contest questions, use `leetgo contest` instead")
}
q = qs[0]
} else {
filter, err := askFilter(c)
if err != nil {
return err
}
m := newTuiModel(filter, c)
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
return err
}
if m.Selected() == nil {
return nil
}
q = m.Selected()
}
result, err := lang.Generate(q)
if err != nil {
return err
}
if !skipEditor {
err = editor.Open(result)
return err
}
return nil
},
}