Skip to content

Commit 21fd81d

Browse files
committed
add quite mode, use unix like params style
1 parent 5b69022 commit 21fd81d

File tree

4 files changed

+50
-41
lines changed

4 files changed

+50
-41
lines changed

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ func main() {
2222
os.Exit(0)
2323
}
2424

25-
words, withVoice, withMore := parseArgs(os.Args)
26-
query(words, withVoice, withMore, len(words) > 1)
25+
words, withVoice, withMore, isQuiet := parseArgs(os.Args[1:])
26+
query(words, withVoice, withMore, isQuiet, len(words) > 1)
2727
}

query.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
voiceURL = "https://dict.youdao.com/dictvoice?audio=%s&type=2"
2222
)
2323

24-
func query(words []string, withVoice, withMore, isMulti bool) {
24+
func query(words []string, withVoice, withMore, isQuiet, isMulti bool) {
2525
var url string
2626
var doc *goquery.Document
2727
var voiceBody io.ReadCloser
@@ -38,13 +38,16 @@ func query(words []string, withVoice, withMore, isMulti bool) {
3838
}
3939

4040
//Init spinner
41-
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond)
42-
s.Prefix = "Querying... "
43-
if err := s.Color("green"); err != nil {
44-
color.Red("Failed to set color for spinner")
45-
os.Exit(1)
41+
var s *spinner.Spinner
42+
if !isQuiet {
43+
s = spinner.New(spinner.CharSets[35], 100*time.Millisecond)
44+
s.Prefix = "Querying... "
45+
if err := s.Color("green"); err != nil {
46+
color.Red("Failed to set color for spinner")
47+
os.Exit(1)
48+
}
49+
s.Start()
4650
}
47-
s.Start()
4851

4952
//Check proxy
5053
if proxy != "" {
@@ -90,7 +93,9 @@ func query(words []string, withVoice, withMore, isMulti bool) {
9093
}
9194
}
9295

93-
s.Stop()
96+
if !isQuiet {
97+
s.Stop()
98+
}
9499

95100
if isChinese {
96101
// Find the result

utils.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ func displayUsage() {
3434
color.Cyan(logo, Version)
3535
color.Cyan("Usage:")
3636
color.Cyan("ydict <word(s) to query> Query the word(s)")
37-
color.Cyan("ydict <word(s) to query> -v Query with speech")
38-
color.Cyan("ydict <word(s) to query> -m Query with more example sentences")
37+
color.Cyan("ydict -v <word(s) to query> Query with speech")
38+
color.Cyan("ydict -m <word(s) to query> Query with more example sentences")
39+
color.Cyan("ydict -q <word(s) to query> Query with quiet mode, don't show spinner")
3940
color.Cyan("ydict -h For help")
4041
}
4142

@@ -79,26 +80,31 @@ func loadEnv() {
7980
proxy = os.Getenv("SOCKS5")
8081
}
8182

82-
func parseArgs(args []string) ([]string, bool, bool) {
83-
//match argument: -v or -m
84-
var withVoice, withMore bool
85-
parameterStartIndex := findParamStartIndex(args)
86-
paramArray := args[parameterStartIndex:]
83+
func parseArgs(args []string) ([]string, bool, bool, bool) {
84+
//match argument: -v or -m or -q
85+
var withVoice, withMore, isQuiet bool
86+
wordStartIndex := findWordStartIndex(args)
87+
paramArray := args[:wordStartIndex]
8788
if elementInStringArray(paramArray, "-m") {
8889
withMore = true
8990
}
9091

9192
if elementInStringArray(paramArray, "-v") {
9293
withVoice = true
9394
}
94-
return args[1:parameterStartIndex], withVoice, withMore
95+
96+
if elementInStringArray(paramArray, "-q") {
97+
isQuiet = true
98+
}
99+
100+
return args[wordStartIndex:], withVoice, withMore, isQuiet
95101
}
96102

97-
func findParamStartIndex(args []string) int {
98-
// iter the args array, if an element is -m or -v,
103+
func findWordStartIndex(args []string) int {
104+
// iter the args array, if an element is -m or -v or -q,
99105
// then all of the latter elements must be parameter instead of words.
100106
for index, word := range args {
101-
if strings.HasPrefix(word, "-") && len(word) == 2 {
107+
if !strings.HasPrefix(word, "-") {
102108
return index
103109
}
104110
}

utils_test.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,28 @@ func TestParseArgs(t *testing.T) {
2727
// Only pass t into top-level Convey calls
2828
Convey("Init args arrays: withVoice & withOutVoice", t, func() {
2929
withoutAll := []string{"aa", "bb", "cc"}
30-
withAll := []string{"aa", "bb", "-v", "-m"}
31-
32-
withVoice := []string{"aa", "bb", "-v"}
33-
withMore := []string{"aa", "bb", "-m"}
30+
withAll := []string{"-v", "-m", "-q", "aa", "bb"}
3431

3532
Convey("Call parse func", func() {
36-
_, ret1, ret2 := parseArgs(withoutAll)
37-
_, ret3, ret4 := parseArgs(withAll)
38-
39-
_, ret5, ret6 := parseArgs(withVoice)
40-
_, ret7, ret8 := parseArgs(withMore)
33+
words01, ret01, ret02, ret03 := parseArgs(withoutAll)
34+
words11, ret11, ret12, ret13 := parseArgs(withAll)
4135

4236
Convey("result should be: true & false", func() {
43-
So(ret1, ShouldEqual, false)
44-
So(ret2, ShouldEqual, false)
45-
46-
So(ret3, ShouldEqual, true)
47-
So(ret4, ShouldEqual, true)
48-
49-
So(ret5, ShouldEqual, true)
50-
So(ret6, ShouldEqual, false)
51-
52-
So(ret7, ShouldEqual, false)
53-
So(ret8, ShouldEqual, true)
37+
So(words01, ShouldContain, "aa")
38+
So(words01, ShouldContain, "bb")
39+
So(words01, ShouldContain, "cc")
40+
So(ret01, ShouldEqual, false)
41+
So(ret02, ShouldEqual, false)
42+
So(ret03, ShouldEqual, false)
43+
44+
So(words11, ShouldContain, "aa")
45+
So(words11, ShouldContain, "bb")
46+
So(words11, ShouldNotContain, "-v")
47+
So(words11, ShouldNotContain, "-m")
48+
So(words11, ShouldNotContain, "-q")
49+
So(ret11, ShouldEqual, true)
50+
So(ret12, ShouldEqual, true)
51+
So(ret13, ShouldEqual, true)
5452
})
5553
})
5654
})

0 commit comments

Comments
 (0)