-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.go
More file actions
98 lines (82 loc) · 2.39 KB
/
lib.go
File metadata and controls
98 lines (82 loc) · 2.39 KB
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
package main
import (
"fmt"
"strings"
"github.com/muesli/reflow/wordwrap"
)
type word struct {
Word string `json:"word,omitempty"`
Phonetic string `json:"phonetic,omitempty"`
Phonetics []phonetics `json:"phonetics,omitempty"`
Meanings []meaning `json:"meanings,omitempty"`
}
type phonetics struct {
Text string `json:"text,omitempty"`
}
type meaning struct {
PartOfSpeech string `json:"partOfSpeech,omitempty"`
Definitions []definition `json:"definitions,omitempty"`
Synonyms []string `json:"synonyms,omitempty"`
Antonyms []string `json:"antonyms,omitempty"`
}
type definition struct {
Definition string `json:"definition,omitempty"`
Synonyms []string `json:"synonyms,omitempty"`
Antonyms []string `json:"antonyms,omitempty"`
Example string `json:"example,omitempty"`
}
func (w word) render() {
res := titleMargin.Render(titleStyle.Render(w.Word) + w.renderPhonetic())
res += w.renderMeanings()
fmt.Println(res + "\n")
}
// the api is somewhat inconsistent for phonems so we have to search for the phonetic of a word, that is if it exists
func (w word) renderPhonetic() string {
if w.Phonetic != "" {
return " - " + phoneticStyle.Render(w.Phonetic)
}
for _, phonetic := range w.Phonetics {
if phonetic.Text != "" {
return " - " + phoneticStyle.Render(phonetic.Text)
}
}
return ""
}
func (w word) renderMeanings() (s string) {
for _, meaning := range w.Meanings {
s += "\n" + posStyle.Render(meaning.PartOfSpeech)
s += meaning.renderMeaningSynonymAndAntonym()
for i, definition := range meaning.Definitions {
n := fmt.Sprintf("%d. ", i+1)
s += "\n" + textStyle.Render(n+wordwrap.String(definition.Definition, 70))
s += definition.renderExample()
}
}
return
}
func (m meaning) renderMeaningSynonymAndAntonym() (s string) {
if !full && !related {
return
}
const MAX = 4
if len(m.Synonyms) > MAX {
s += " " + synonymStyle.Render(strings.Join(m.Synonyms[:MAX], ", "))
} else {
s += " " + synonymStyle.Render(strings.Join(m.Synonyms, ", "))
}
if len(m.Antonyms) > MAX {
s += " " + antonymStyle.Render(strings.Join(m.Antonyms[:MAX], ", "))
} else {
s += " " + antonymStyle.Render(strings.Join(m.Antonyms, ", "))
}
return
}
func (d definition) renderExample() (s string) {
if !full && !examples {
return
}
if d.Example != "" {
s = exampleStyle.Render(wordwrap.String("\n\""+d.Example+"\"", 70))
}
return
}