-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (99 loc) · 1.67 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
package main
import (
"bufio"
"fmt"
"gowallpaper/bing"
"log"
"os"
"strings"
"time"
)
type Cmd struct {
Name string
Desc string
Action func(string)
}
func main() {
newBing := bing.NewBing()
var Cmds []Cmd
Cmds = append(Cmds, Cmd{
"day",
"每天更新壁纸",
func(params string) {
newBing.Day()
},
})
Cmds = append(Cmds, Cmd{
"now",
"设置当天壁纸",
func(params string) {
newBing.Now()
},
})
Cmds = append(Cmds, Cmd{
"prev",
"设置前一天壁纸",
func(params string) {
newBing.Prev()
},
})
Cmds = append(Cmds, Cmd{
"next",
"设置后一天壁纸",
func(params string) {
newBing.Next()
},
})
Cmds = append(Cmds, Cmd{
"rand",
"间隔随机切换壁纸(如每分钟切换壁纸:rand 1m)",
func(params string) {
d, err := time.ParseDuration(params)
if err != nil {
log.Println("time.ParseDuration", err)
} else {
newBing.Rand(d)
}
},
})
Cmds = append(Cmds, Cmd{
"quit",
"退出",
func(params string) {
os.Exit(0)
},
})
fmt.Println("设置微软必应壁纸,输入如下命令:")
for _, cmd := range Cmds {
fmt.Println(cmd.Name, "\t-", cmd.Desc)
}
for {
fmt.Print("# ")
line := ReadLine()
line = strings.Trim(line, " ")
strList := strings.Split(line, " ")
if len(strList) == 0 {
continue
}
cmd := strList[0]
var params string
if len(strList) > 1 {
params = strList[1]
}
if cmd == "quit" || cmd == "exit" {
os.Exit(0)
return
}
for _, v := range Cmds {
if v.Name == cmd {
v.Action(params)
break
}
}
}
}
func ReadLine() string {
reader := bufio.NewReader(os.Stdin)
data, _, _ := reader.ReadLine()
return string(data)
}