-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
96 lines (82 loc) · 1.96 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
package main
import (
"fmt"
"os"
"strings"
"github.com/Dreamacro/clash-ctl/commands"
"github.com/Dreamacro/clash-ctl/common"
"github.com/c-bata/go-prompt"
"github.com/jedib0t/go-pretty/v6/text"
)
func executor(in string) {
in = strings.TrimSpace(in)
blocks := strings.Split(in, " ")
switch blocks[0] {
case "exit":
fmt.Println("Bye!")
os.Exit(0)
case "server":
commands.HandleServerCommand(blocks[1:])
case "now", "use", "ping":
commands.HandleMiscCommand(blocks)
case "traffic", "connections":
commands.HandleCommonCommand(blocks)
}
}
func completer(in prompt.Document) []prompt.Suggest {
if in.TextBeforeCursor() == "" {
return []prompt.Suggest{}
}
args := strings.Split(in.TextBeforeCursor(), " ")
w := in.GetWordBeforeCursor()
// first class command
if len(args) <= 1 {
return prompt.FilterHasPrefix(
[]prompt.Suggest{
{Text: "server", Description: "manage remote clash server"},
{Text: "now", Description: "show selected clash server"},
{Text: "use", Description: "change selected clash server"},
{Text: "ping", Description: "check clash servers alive"},
{Text: "traffic", Description: "get clash traffic"},
{Text: "connections", Description: "get clash all connections"},
},
w,
true,
)
}
switch args[0] {
case "server":
return prompt.FilterHasPrefix(
[]prompt.Suggest{
{Text: "ls", Description: "list all server"},
{Text: "add", Description: "add new server"},
},
args[1],
true,
)
case "use":
cfg, err := common.ReadCfg()
if err != nil {
return []prompt.Suggest{}
}
suggests := []prompt.Suggest{}
for key := range cfg.Servers {
suggests = append(suggests, prompt.Suggest{Text: key})
}
return suggests
}
return []prompt.Suggest{}
}
func main() {
if err := common.Init(); err != nil {
fmt.Println(text.FgRed.Sprint(err.Error()))
return
}
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("clash-ctl"),
)
p.Run()
}