-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (153 loc) Β· 3.55 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/fatih/color"
"io"
"net/http"
"os"
"strings"
)
const BaseUrl = "https://api.dictionaryapi.dev/api/v2/entries/en/"
type Phonetic struct {
Text string `json:"text"`
Audio string `json:"audio"`
}
type Definition struct {
Definition string `json:"definition"`
Example string `json:"example"`
Synonyms []string `json:"synonyms"`
Antonyms []string `json:"antonyms"`
}
type Meaning struct {
PartOfSpeech string `json:"partOfSpeech"`
Definitions []Definition `json:"definitions"`
}
type Word struct {
Word string `json:"word"`
Phonetic string `json:"phonetic"`
Phonetics []Phonetic `json:"phonetics"`
Origin string `json:"origin"`
Meanings []Meaning `json:"meanings"`
}
func Lookup(phrase string) ([]Word, error) {
url := BaseUrl + phrase
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if resp.StatusCode == 404 {
return nil, errors.New("Couldn't find word.")
}
return nil, errors.New("Something went wrong.")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var words []Word
err = json.Unmarshal(body, &words)
if err != nil {
return nil, err
}
return words, nil
}
func usage() {
fmt.Print(`dic
Usage: dic <query>`)
}
func printWord(word Word, num int) {
c := color.New(color.Bold).Add(color.FgGreen)
c.Printf("%d. %s", num+1, word.Word)
printPhonetics(word.Phonetics)
if len(word.Meanings) > 0 {
fmt.Println("")
for _, meaning := range word.Meanings {
printMeaning(meaning)
}
}
}
func printMeaning(meaning Meaning) {
c := color.New(color.FgYellow)
if len(meaning.PartOfSpeech) > 0 {
c.Printf("\n%s\n", meaning.PartOfSpeech)
}
contentAfterDefinition := false
for _, definition := range meaning.Definitions {
if contentAfterDefinition {
fmt.Println("")
}
c := color.New(color.Italic)
c.Print("def. ")
c = color.New(color.Bold).Add(color.FgBlue)
c.Printf("%s\n", definition.Definition)
if len(definition.Example) > 0 {
contentAfterDefinition = true
c := color.New(color.Italic)
c.Print("ex. ")
c.DisableColor()
c.Printf("%s\n", definition.Example)
}
c = color.New(color.Italic)
var presentSynonyms []string
for _, synonym := range definition.Synonyms {
if len(synonym) > 0 {
presentSynonyms = append(presentSynonyms, synonym)
}
}
if len(presentSynonyms) > 0 {
contentAfterDefinition = true
synonyms := strings.Join(presentSynonyms, ", ")
c.EnableColor()
c.Print("syn.")
c.DisableColor()
c.Printf(" %s\n", synonyms)
}
var presentAntonyms []string
for _, antonym := range definition.Antonyms {
if len(antonym) > 0 {
presentAntonyms = append(presentAntonyms, antonym)
}
}
if len(presentAntonyms) > 0 {
contentAfterDefinition = true
antonyms := strings.Join(presentAntonyms, ", ")
c.EnableColor()
c.Print("ant.")
c.DisableColor()
c.Printf(" %s\n", antonyms)
}
}
}
func printPhonetics(phonetics []Phonetic) {
var phoneticTexts []string
for _, phonetic := range phonetics {
if len(phonetic.Text) > 0 {
phoneticTexts = append(phoneticTexts, phonetic.Text)
}
}
if len(phoneticTexts) > 0 {
phoneticsString := strings.Join(phoneticTexts, ", ")
fmt.Printf(" [%s]", phoneticsString)
}
}
func main() {
if len(os.Args) == 1 {
usage()
os.Exit(0)
}
words, err := Lookup(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for i, word := range words {
if i > 0 {
fmt.Println("")
}
printWord(word, i)
}
}