Skip to content

Commit

Permalink
优化首页显示
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Feb 23, 2020
1 parent 84efe27 commit daf3090
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion trojan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GenClientJson() {
user = userList[0]
} else {
UserList()
choice := util.LoopInput("请选择要生成配置文件的用户序号: ", userList)
choice := util.LoopInput("请选择要生成配置文件的用户序号: ", userList, true)
if choice < 0 {
return
}
Expand Down
6 changes: 3 additions & 3 deletions trojan/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions trojan/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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("删除用户成功!")
}
Expand All @@ -49,7 +49,7 @@ func SetUserQuota() {
)
userList := *UserList()
mysql := core.GetMysql()
choice := util.LoopInput("请选择要限制流量的用户序号: ", userList)
choice := util.LoopInput("请选择要限制流量的用户序号: ", userList, true)
if choice == -1 {
return
}
Expand All @@ -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("清空流量成功!")
}
Expand Down
4 changes: 2 additions & 2 deletions util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit daf3090

Please sign in to comment.