Skip to content

Commit

Permalink
优化命令行显示
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Dec 15, 2020
1 parent ca88554 commit 12909cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
7 changes: 0 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ exit:
fmt.Println(util.Cyan("欢迎使用trojan管理程序"))
fmt.Println()
menuList := []string{"trojan管理", "用户管理", "安装管理", "web管理", "查看配置", "生成json"}
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])
}
}
switch util.LoopInput("请选择: ", menuList, false) {
case 1:
trojan.ControllMenu()
Expand Down
2 changes: 1 addition & 1 deletion trojan/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func UserMenu() {
fmt.Println()
menu := []string{"新增用户", "删除用户", "限制流量", "清空流量", "设置限期", "取消限期"}
switch util.LoopInput("请选择: ", menu, true) {
switch util.LoopInput("请选择: ", menu, false) {
case 1:
AddUser()
case 2:
Expand Down
18 changes: 14 additions & 4 deletions util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,26 @@ func getChar(str string) string {
}

// LoopInput 循环输入选择, 或者直接回车退出
func LoopInput(tip string, choices interface{}, print bool) int {
func LoopInput(tip string, choices interface{}, singleRowPrint 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 print && reflectValue.Type().String() == "[]string" {
for i := 0; i < length; i++ {
fmt.Printf("%d.%s\n\n", i+1, reflectValue.Index(i).Interface())
if reflectValue.Type().String() == "[]string" {
if singleRowPrint {
for i := 0; i < length; i++ {
fmt.Printf("%d.%s\n\n", i+1, reflectValue.Index(i).Interface())
}
} else {
for i := 0; i < length; i++ {
if i%2 == 0 {
fmt.Printf("%d.%-15s\t", i+1, reflectValue.Index(i).Interface())
} else {
fmt.Printf("%d.%-15s\n\n", i+1, reflectValue.Index(i).Interface())
}
}
}
}
for {
Expand Down

0 comments on commit 12909cd

Please sign in to comment.