11package leet
22
33import (
4- "encoding/json"
54 "fmt"
6- "io/ioutil"
75 "os"
8- "path"
96 "strings"
107
118 "github.com/olekukonko/tablewriter"
12- )
13-
14- var store = make (map [string ]Store )
15- var storeFile = path .Join ("questions" , "store.json" )
169
17- func init () {
18- updateCache ()
19- }
20-
21- func updateCache () (err error ) {
22- b , err := ioutil .ReadFile (storeFile )
23- if err != nil {
24- return
25- }
10+ "github.com/gladmo/leetcode/store"
11+ )
2612
27- err = json .Unmarshal (b , & store )
28- if err != nil {
13+ func InfoPrint (info store.Store , withDetail bool ) {
14+ if info .QuestionID == "" {
15+ fmt .Println ("问题还未下载到本地,请先使用 get 命令下载" )
2916 return
3017 }
31-
32- return
33- }
34-
35- type Store struct {
36- Title string `json:"title"`
37- TranslatedTitle string `json:"translated_title"`
38- QuestionID string `json:"question_id"`
39- Languages []string `json:"language"`
40- Tags []string `json:"tags"`
41-
42- Difficulty string `json:"difficulty"`
43- SaveDir []string `json:"save_dir"`
44- TitleSlug string `json:"title_slug"`
45- Question string `json:"question"`
46- }
47-
48- func (th Store ) Print (withDetail bool ) {
4918 table := tablewriter .NewWriter (os .Stdout )
5019 table .SetRowLine (true )
5120
52- table .Append ([]string {"标题" , th .Title })
53- table .Append ([]string {"问题ID" , th .QuestionID })
54- table .Append ([]string {"中文标题" , th .TranslatedTitle })
55- // table.Append([]string{"语言列表", strings.Join(th .Languages, ",")})
56- table .Append ([]string {"标签" , strings .Join (th .Tags , "," )})
57- table .Append ([]string {"难度" , th .Difficulty })
58- codeLine := fmt .Sprintf ("%s/README.md:2" , th .SaveDir [0 ])
21+ table .Append ([]string {"标题" , info .Title })
22+ table .Append ([]string {"问题ID" , info .QuestionID })
23+ table .Append ([]string {"中文标题" , info .TranslatedTitle })
24+ // table.Append([]string{"语言列表", strings.Join(info .Languages, ",")})
25+ table .Append ([]string {"标签" , strings .Join (info .Tags , "," )})
26+ table .Append ([]string {"难度" , info .Difficulty })
27+ codeLine := fmt .Sprintf ("%s/README.md:2" , info .SaveDir [0 ])
5928 table .Append ([]string {"题目描述" , codeLine })
6029 goFileDir := fmt .Sprintf (
6130 "%s/golang/solution/%s.go" ,
62- th .SaveDir [0 ],
63- th .TitleSlug )
31+ info .SaveDir [0 ],
32+ info .TitleSlug )
6433 goCodeLine := fmt .Sprintf (
6534 "%s:%d" ,
6635 goFileDir ,
@@ -71,33 +40,38 @@ func (th Store) Print(withDetail bool) {
7140 table .Render ()
7241
7342 if withDetail {
74- fmt .Println (th .Question )
43+ fmt .Println (info .Question )
7544 }
7645}
7746
78- func GetQuestionInfo (titleSlug string ) (info Store ) {
79- s , ok := store [titleSlug ]
80- if ! ok {
81- return
47+ func GetQuestionInfoByID (questionID string ) (info store.Store ) {
48+ var err error
49+ info , err = store .QuestionInfo (questionID )
50+ if err != nil {
51+ fmt .Println (err .Error ())
8252 }
83-
84- return s
53+ return
8554}
8655
87- func GetAllQuestionTitleSlug () (res []string ) {
88- for titleSlug := range store {
89- res = append (res , titleSlug )
56+ func GetQuestionInfo (titleSlug string ) (info store.Store ) {
57+ var err error
58+ info , err = store .QuestionInfo (titleSlug )
59+ if err != nil {
60+ fmt .Println (err .Error ())
9061 }
91-
9262 return
9363}
9464
95- func UpdateQuestionInfo (que Question , local Localization ) {
96- err := updateCache ()
65+ func GetAllQuestionTitleSlug () (res []string ) {
66+ var err error
67+ res , err = store .AllQuestionTitleSlug ()
9768 if err != nil {
9869 fmt .Println (err .Error ())
9970 }
71+ return
72+ }
10073
74+ func UpdateQuestionInfo (que Question , local Localization ) {
10175 var tags []string
10276 for _ , topicTag := range que .TopicTags {
10377 tags = append (tags , topicTag .TranslatedName )
@@ -108,7 +82,7 @@ func UpdateQuestionInfo(que Question, local Localization) {
10882 langs = append (langs , lan .LangSlug )
10983 }
11084
111- info := Store {
85+ info := store. Store {
11286 Title : que .Title ,
11387 TranslatedTitle : que .TranslatedTitle ,
11488 QuestionID : local .QuestionID ,
@@ -120,14 +94,7 @@ func UpdateQuestionInfo(que Question, local Localization) {
12094 Question : local .Question ,
12195 }
12296
123- store [info .TitleSlug ] = info
124- b , err := json .Marshal (store )
125- if err != nil {
126- fmt .Println (err .Error ())
127- return
128- }
129-
130- err = ioutil .WriteFile (storeFile , b , 0755 )
97+ err := store .UpdateQuestionInfo (info )
13198 if err != nil {
13299 fmt .Println (err .Error ())
133100 }
0 commit comments