-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
150 lines (144 loc) · 4.06 KB
/
io.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
package main
import (
"bufio"
"fmt"
"strings"
"github.com/zzc-tongji/mydictionary/v4"
)
// input
func input(inputReader *bufio.Reader) (vocabularyAsk mydictionary.VocabularyAskStruct) {
var err error
// input
vocabularyAsk.Word, err = inputReader.ReadString('\n')
if err != nil {
fmt.Printf("%s\n\n", err.Error())
quit()
save()
}
vocabularyAsk.Word = strings.TrimSpace(vocabularyAsk.Word)
// quit
if strings.Compare(vocabularyAsk.Word, "*") == 0 {
vocabularyAsk.Word = ""
fmt.Printf("\n")
quitChannel <- 0
return
}
// check network
if strings.Compare(vocabularyAsk.Word, "&") == 0 {
vocabularyAsk.Word = "&"
fmt.Printf("\n")
return
}
// empty input
if strings.Compare(vocabularyAsk.Word, "") == 0 {
vocabularyAsk.Word = ""
fmt.Printf("\n")
return
}
// set word and option
if strings.Contains(vocabularyAsk.Word, "!") || strings.Contains(vocabularyAsk.Word, "!") {
vocabularyAsk.Advance = true
} else {
vocabularyAsk.Advance = false
}
if strings.Contains(vocabularyAsk.Word, "@") {
vocabularyAsk.Online = true
} else {
vocabularyAsk.Online = false
}
if strings.Contains(vocabularyAsk.Word, "#") {
vocabularyAsk.DoNotRecord = true
} else {
vocabularyAsk.DoNotRecord = false
}
vocabularyAsk.Word = strings.Replace(vocabularyAsk.Word, "!", "", -1)
vocabularyAsk.Word = strings.Replace(vocabularyAsk.Word, "!", "", -1)
vocabularyAsk.Word = strings.Replace(vocabularyAsk.Word, "@", "", -1)
vocabularyAsk.Word = strings.Replace(vocabularyAsk.Word, "#", "", -1)
vocabularyAsk.Word = strings.TrimSpace(vocabularyAsk.Word)
fmt.Printf("\n")
return
}
// output
func output(vocabularyAsk mydictionary.VocabularyAskStruct, vocabularyResult mydictionary.VocabularyResultStruct) {
const (
separator1 = "============================================================\n"
separator2 = "------------------------------------------------------------\n"
)
var content string
// word
content = separator1
content += convertVocabularyAsk(vocabularyAsk)
// basic
if vocabularyResult.Basic != nil {
content += separator2
content += "* BASIC\n"
for i := 0; i < len(vocabularyResult.Basic); i++ {
content += "\n"
content += convertVocabularyAnswer(vocabularyResult.Basic[i])
}
}
// advance
if vocabularyResult.Advance != nil {
content += separator2
content += "* ADVANCE\n"
for i := 0; i < len(vocabularyResult.Advance); i++ {
content += "\n"
content += convertVocabularyAnswer(vocabularyResult.Advance[i])
}
}
content += separator1
// new line
content += "\n"
fmt.Printf("%s", content)
}
// convert struct "VocabularyAsk" to string
func convertVocabularyAsk(vocabularyAsk mydictionary.VocabularyAskStruct) (result string) {
result = vocabularyAsk.Word
if vocabularyAsk.Advance || vocabularyAsk.Online || vocabularyAsk.DoNotRecord {
result += " ("
if vocabularyAsk.Advance {
result += "advance, "
}
if vocabularyAsk.Online {
result += "online, "
}
if vocabularyAsk.DoNotRecord {
result += "do not record, "
}
result = strings.TrimSuffix(result, ", ")
result += ")"
}
result += "\n"
return
}
// convert struct "VocabularyAnswer" to string
func convertVocabularyAnswer(vocabularyAnswer mydictionary.VocabularyAnswerStruct) (result string) {
result = vocabularyAnswer.Word
if strings.Compare(result, "") == 0 {
result = "{null}"
}
result += "\n"
if vocabularyAnswer.Location.TableType == mydictionary.Online {
result += fmt.Sprintf(" [%s]\n", vocabularyAnswer.SourceName)
} else {
result += fmt.Sprintf(" [%s: %d] (%d)\n", vocabularyAnswer.SourceName, vocabularyAnswer.SerialNumber, vocabularyAnswer.QueryCounter)
}
for i := 0; i < len(vocabularyAnswer.Definition); i++ {
result += " "
result += vocabularyAnswer.Definition[i]
result += "\n"
}
for i := 0; i < len(vocabularyAnswer.Note); i++ {
result += " # "
result += vocabularyAnswer.Note[i]
result += "\n"
}
if strings.Compare(vocabularyAnswer.Status, mydictionary.Basic) != 0 &&
strings.Compare(vocabularyAnswer.Status, mydictionary.Advance) != 0 {
result += " {"
result += vocabularyAnswer.Status
result += "}\n"
}
return
}