From daf3090ed54a6ebe2ae0d634831e0bce9780e593 Mon Sep 17 00:00:00 2001 From: Jrohy Date: Sun, 23 Feb 2020 11:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A6=96=E9=A1=B5=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 10 +++++++++- trojan/client.go | 2 +- trojan/install.go | 6 +++--- trojan/trojan.go | 2 +- trojan/user.go | 8 ++++---- util/string.go | 4 ++-- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 4f4f7e8f..656f07ea 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,7 +58,15 @@ exit: fmt.Println(util.Cyan("欢迎使用trojan管理程序")) fmt.Println() menuList := []string{"trojan管理", "用户管理", "安装管理", "查看配置", "生成客户端配置文件"} - switch util.LoopInput("请选择: ", menuList) { + for i := 0; i < len(menuList); i++ { + if i%2 == 0 { + fmt.Printf("%d.%-15s\t", i+1, menuList[i]) + } else { + fmt.Printf("%d.%-15s\n\n", i+1, menuList[i]) + } + } + fmt.Println("\n") + switch util.LoopInput("请选择: ", menuList, false) { case 1: trojan.ControllMenu() case 2: diff --git a/trojan/client.go b/trojan/client.go index 7ea06e32..625dbb6c 100644 --- a/trojan/client.go +++ b/trojan/client.go @@ -23,7 +23,7 @@ func GenClientJson() { user = userList[0] } else { UserList() - choice := util.LoopInput("请选择要生成配置文件的用户序号: ", userList) + choice := util.LoopInput("请选择要生成配置文件的用户序号: ", userList, true) if choice < 0 { return } diff --git a/trojan/install.go b/trojan/install.go index 069187ad..91ec593f 100644 --- a/trojan/install.go +++ b/trojan/install.go @@ -19,7 +19,7 @@ var ( func InstallMenu() { fmt.Println() menu := []string{"更新trojan", "证书申请", "安装mysql"} - switch util.LoopInput("请选择: ", menu) { + switch util.LoopInput("请选择: ", menu, true) { case 1: InstallTrojan() case 2: @@ -50,7 +50,7 @@ func InstallTrojan() { func InstallTls() { domain := "" - choice := util.LoopInput("请选择使用证书方式: ", []string{"Let's Encrypt 证书", "自定义证书路径"}) + choice := util.LoopInput("请选择使用证书方式: ", []string{"Let's Encrypt 证书", "自定义证书路径"}, true) if choice == 1 { localIP := util.GetLocalIP() fmt.Printf("本机ip: %s\n", localIP) @@ -104,7 +104,7 @@ func InstallTls() { func InstallMysql() { var mysql core.Mysql - choice := util.LoopInput("请选择: ", []string{"安装docker版mysql", "输入自定义mysql连接"}) + choice := util.LoopInput("请选择: ", []string{"安装docker版mysql", "输入自定义mysql连接"}, true) if choice < 0 { return } else if choice == 1 { diff --git a/trojan/trojan.go b/trojan/trojan.go index 16c7524c..2fa0eb75 100644 --- a/trojan/trojan.go +++ b/trojan/trojan.go @@ -8,7 +8,7 @@ import ( func ControllMenu() { fmt.Println() menu := []string{"启动trojan", "停止trojan", "重启trojan", "查看trojan状态"} - switch util.LoopInput("请选择: ", menu) { + switch util.LoopInput("请选择: ", menu, true) { case 1: Start() case 2: diff --git a/trojan/user.go b/trojan/user.go index e93b7bbc..6ed549ab 100644 --- a/trojan/user.go +++ b/trojan/user.go @@ -10,7 +10,7 @@ import ( func UserMenu() { fmt.Println() menu := []string{"新增用户", "删除用户", "限制流量", "清空流量"} - switch util.LoopInput("请选择: ", menu) { + switch util.LoopInput("请选择: ", menu, true) { case 1: AddUser() case 2: @@ -36,7 +36,7 @@ func AddUser() { func DelUser() { userList := *UserList() mysql := core.GetMysql() - choice := util.LoopInput("请选择要删除的用户序号: ", userList) + choice := util.LoopInput("请选择要删除的用户序号: ", userList, true) if mysql.DeleteUser(userList[choice-1].ID) == nil { fmt.Println("删除用户成功!") } @@ -49,7 +49,7 @@ func SetUserQuota() { ) userList := *UserList() mysql := core.GetMysql() - choice := util.LoopInput("请选择要限制流量的用户序号: ", userList) + choice := util.LoopInput("请选择要限制流量的用户序号: ", userList, true) if choice == -1 { return } @@ -70,7 +70,7 @@ func SetUserQuota() { func CleanData() { userList := *UserList() mysql := core.GetMysql() - choice := util.LoopInput("请选择要清空流量的用户序号: ", userList) + choice := util.LoopInput("请选择要清空流量的用户序号: ", userList, true) if mysql.CleanData(userList[choice-1].ID) == nil { fmt.Println("清空流量成功!") } diff --git a/util/string.go b/util/string.go index 720fc0b1..9e3350f8 100644 --- a/util/string.go +++ b/util/string.go @@ -101,14 +101,14 @@ func getChar(str string) string { } } -func LoopInput(tip string, choices interface{}) int { +func LoopInput(tip string, choices interface{}, print bool) int { reflectValue := reflect.ValueOf(choices) if reflectValue.Kind() != reflect.Slice && reflectValue.Kind() != reflect.Array { fmt.Println("only support slice or array type!") return -1 } length := reflectValue.Len() - if reflectValue.Type().String() == "[]string" { + if print && reflectValue.Type().String() == "[]string" { for i := 0; i < length; i++ { fmt.Printf("%d.%s\n\n", i+1, reflectValue.Index(i).Interface()) }