-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.go
205 lines (183 loc) · 4.27 KB
/
word.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"runtime"
"time"
"github.com/manifoldco/promptui"
)
const (
chooseCount = 4
)
var (
red = Color("\033[1;31m%s\033[0m")
green = Color("\033[1;32m%s\033[0m")
yellow = Color("\033[1;33m%s\033[0m")
purple = Color("\033[1;34m%s\033[0m")
magenta = Color("\033[1;35m%s\033[0m")
teal = Color("\033[1;36m%s\033[0m")
white = Color("\033[1;37m%s\033[0m")
num int
dataPath string
dataType string
lineMaxLen int
)
func main() {
flag.Parse()
wds := loadData(dataPath, dataType)
CallClear()
fmt.Println(promptui.Styler(promptui.BGWhite)(green("开始背单词: ")))
qts := wds.randQuestion(num)
cur := 0
l := len(qts)
wrongCount := l
for wrongCount > 0 {
if !qts[cur].answered {
prompt := promptui.Select{
Label: teal(qts[cur].content),
Items: qts[cur].chooses,
Size: 4,
HideSelected: true,
HideHelp: true,
}
choose, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
CallClear()
if choose != qts[cur].ansIdx {
fmt.Printf(red("Wrong!!! "))
ansIdx := shuffle(qts[cur].chooses, qts[cur].ansIdx)
qts[cur].ansIdx = ansIdx
} else {
fmt.Printf(green("Correct! "))
qts[cur].answered = true
wrongCount--
}
fmt.Printf("%v/%v\t%v : %v \n", purple(l-wrongCount), purple(l), teal(qts[cur].content), yellow(qts[cur].chooses[qts[cur].ansIdx]))
}
cur++
if cur == l {
cur = 0
}
}
}
func init() {
rand.Seed(time.Now().Unix())
flag.IntVar(&num, "n", 20, "the number of words to recite")
flag.IntVar(&lineMaxLen, "l", 180, "the max byte to show one line")
flag.StringVar(&dataPath, "f", "我的生词本.json", "the data file path")
flag.StringVar(&dataType, "t", "json", "the data file type")
clear = make(map[string]func()) //Initialize it
clear["linux"] = func() {
cmd := exec.Command("clear") //Linux example, its tested
cmd.Stdout = os.Stdout
cmd.Run()
}
clear["windows"] = func() {
cmd := exec.Command("cmd", "/c", "cls") //Windows example, its tested
cmd.Stdout = os.Stdout
cmd.Run()
}
}
// Color print color
func Color(colorString string) func(...interface{}) string {
sprint := func(args ...interface{}) string {
return fmt.Sprintf(colorString,
fmt.Sprint(args...))
}
return sprint
}
var clear map[string]func() //create a map for storing clear funcs
// CallClear clear terminal screen
func CallClear() {
value, ok := clear[runtime.GOOS] //runtime.GOOS -> linux, windows, darwin etc.
if ok { //if we defined a clear func for that platform:
value() //we execute it
} else { //unsupported platform
panic("Your platform is unsupported! I can't clear terminal screen :(")
}
}
type word struct {
Word string `json:"word"`
Chinese string `json:"chinese"`
}
type words struct {
wds []word
len int
}
type question struct {
content string
chooses []string
ansIdx int
answered bool
}
func loadData(file string, tp string) *words {
var wd words
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalln(err)
}
switch tp {
case "json":
err = json.Unmarshal(data, &wd.wds)
if err != nil {
log.Fatalln(err)
}
wd.len = len(wd.wds)
default:
log.Fatalln("Unknown data type")
}
return &wd
}
func shuffle(s []string, ansIdx int) int {
for i := len(s) - 1; i >= 0; i-- {
rnd := rand.Intn(i + 1)
if rnd == ansIdx {
ansIdx = i
} else if i == ansIdx {
ansIdx = rnd
}
s[rnd], s[i] = s[i], s[rnd]
}
return ansIdx
}
func toShort(s string) string {
if len(s) > lineMaxLen {
return s[:lineMaxLen] + "..."
}
return s
}
func (w *words) randChoose(idx int) ([]string, int) {
res := make([]string, 0, chooseCount)
res = append(res, toShort(w.wds[idx].Chinese))
rd := rand.Perm(w.len)[0 : chooseCount-1]
for _, v := range rd {
res = append(res, toShort(w.wds[v].Chinese))
}
rd = nil
ansIdx := shuffle(res, 0)
return res, ansIdx
}
func (w *words) randQuestion(cnt int) []question {
res := make([]question, 0, cnt)
rd := rand.Perm(w.len)[0:cnt]
var chooses []string
var ansIdx int
for _, v := range rd {
chooses, ansIdx = w.randChoose(v)
res = append(res, question{
content: w.wds[v].Word,
chooses: chooses,
ansIdx: ansIdx,
})
}
return res
}