-
Notifications
You must be signed in to change notification settings - Fork 33
/
info.go
167 lines (151 loc) · 4.19 KB
/
info.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
package cmd
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"github.com/charmbracelet/log"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
"github.com/j178/leetgo/leetcode"
)
type outputFormat string
func (e *outputFormat) String() string {
return string(*e)
}
func (e *outputFormat) Set(v string) error {
switch v {
case "json":
*e = outputFormat(v)
return nil
default:
return errors.New(`must be one of the supported formats: "json"`)
}
}
func (e *outputFormat) Type() string {
return "outputFormat"
}
var (
flagFull bool
flagFormat outputFormat = "default"
)
func init() {
infoCmd.Flags().BoolVar(&flagFull, "full", false, "show full question info")
infoCmd.Flags().Var(&flagFormat, "format", "show question info in specific format (json)")
}
// A simplified version of the leetcode.QuestionData struct
type question struct {
FrontendId string `json:"frontend_id"`
Title string `json:"title"`
Slug string `json:"slug"`
Difficulty string `json:"difficulty"`
Url string `json:"url"`
Tags []string `json:"tags"`
IsPaidOnly bool `json:"is_paid_only"`
TotalAccepted string `json:"total_accepted"`
TotalAcceptedRaw int `json:"total_accepted_raw"`
TotalSubmission string `json:"total_submission"`
TotalSubmissionRaw int `json:"total_submission_raw"`
ACRate string `json:"ac_rate"`
Content string `json:"content"`
Hints []string `json:"hints"`
}
var infoCmd = &cobra.Command{
Use: "info qid...",
Short: "Show question info",
Example: "leetgo info 145\nleetgo info two-sum",
Args: cobra.MinimumNArgs(1),
Aliases: []string{"i"},
ValidArgs: []string{"today", "last"},
RunE: func(cmd *cobra.Command, args []string) error {
c := leetcode.NewClient(leetcode.ReadCredentials())
var questions []question
for _, qid := range args {
qs, err := leetcode.ParseQID(qid, c)
if err != nil {
log.Error("failed to get question", "qid", qid, "err", err)
continue
}
for _, q := range qs {
_ = q.Fulfill()
content := ""
if flagFull {
content = q.GetFormattedContent()
}
questions = append(
questions, question{
FrontendId: q.QuestionFrontendId,
Title: q.GetTitle(),
Slug: q.TitleSlug,
Difficulty: q.Difficulty,
Url: q.Url(),
Tags: q.TagSlugs(),
IsPaidOnly: q.IsPaidOnly,
TotalAccepted: q.Stats.TotalAccepted,
TotalAcceptedRaw: q.Stats.TotalAcceptedRaw,
TotalSubmission: q.Stats.TotalSubmission,
TotalSubmissionRaw: q.Stats.TotalSubmissionRaw,
ACRate: q.Stats.ACRate,
Content: content,
Hints: q.Hints,
},
)
}
}
if len(questions) == 0 {
return errors.New("no questions found")
}
switch flagFormat {
default:
outputHuman(questions, cmd.OutOrStdout())
case "json":
outputJson(questions, cmd.OutOrStdout())
}
return nil
},
}
func outputHuman(qs []question, out io.Writer) {
w := table.NewWriter()
w.SetOutputMirror(out)
w.SetStyle(table.StyleColoredDark)
w.SetColumnConfigs(
[]table.ColumnConfig{
{
Number: 2,
WidthMax: 50,
},
},
)
for _, q := range qs {
w.AppendRow(
table.Row{fmt.Sprintf("%s. %s", q.FrontendId, q.Title)},
table.RowConfig{AutoMerge: true, AutoMergeAlign: text.AlignLeft},
)
w.AppendRow(table.Row{"Slug", q.Slug})
w.AppendRow(table.Row{"Difficulty", q.Difficulty})
w.AppendRow(table.Row{"URL", q.Url})
w.AppendRow(table.Row{"Tags", strings.Join(q.Tags, ", ")})
w.AppendRow(table.Row{"Paid Only", q.IsPaidOnly})
w.AppendRow(
table.Row{
"AC Rate",
fmt.Sprintf("%s/%s %s", q.TotalAccepted, q.TotalSubmission, q.ACRate),
},
)
if q.Content != "" {
w.AppendRow(table.Row{"Content", q.Content})
}
for _, h := range q.Hints {
w.AppendRow(table.Row{"Hint", h})
}
w.AppendSeparator()
}
w.Render()
}
func outputJson(qs []question, out io.Writer) {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
_ = enc.Encode(qs)
}