MCPcopy
hub / github.com/Jrohy/trojan / LoopInput

Function LoopInput

util/string.go:80–122  ·  view source on GitHub ↗

LoopInput 循环输入选择, 或者直接回车退出

(tip string, choices interface{}, singleRowPrint bool)

Source from the content-addressed store, hash-verified

78
79// LoopInput 循环输入选择, 或者直接回车退出
80func LoopInput(tip string, choices interface{}, singleRowPrint bool) int {
81 reflectValue := reflect.ValueOf(choices)
82 if reflectValue.Kind() != reflect.Slice && reflectValue.Kind() != reflect.Array {
83 fmt.Println("only support slice or array type!")
84 return -1
85 }
86 length := reflectValue.Len()
87 if reflectValue.Type().String() == "[]string" {
88 if singleRowPrint {
89 for i := 0; i < length; i++ {
90 fmt.Printf("%d.%s\n\n", i+1, reflectValue.Index(i).Interface())
91 }
92 } else {
93 for i := 0; i < length; i++ {
94 if i%2 == 0 {
95 fmt.Printf("%d.%-15s\t", i+1, reflectValue.Index(i).Interface())
96 } else {
97 fmt.Printf("%d.%-15s\n\n", i+1, reflectValue.Index(i).Interface())
98 }
99 }
100 }
101 }
102 for {
103 inputString := ""
104 if length < 10 {
105 inputString = getChar(tip)
106 } else {
107 fmt.Print(tip)
108 _, _ = fmt.Scanln(&inputString)
109 }
110 if inputString == "" {
111 return -1
112 } else if !IsInteger(inputString) {
113 fmt.Println("输入有误,请重新输入")
114 continue
115 }
116 number, _ := strconv.Atoi(inputString)
117 if number <= length && number > 0 {
118 return number
119 }
120 fmt.Println("输入数字越界,请重新输入")
121 }
122}
123
124// Input 读取终端用户输入
125func Input(tip string, defaultValue string) string {

Callers 13

WebMenuFunction · 0.92
InstallMenuFunction · 0.92
InstallTlsFunction · 0.92
InstallMysqlFunction · 0.92
ControlMenuFunction · 0.92
UserMenuFunction · 0.92
DelUserFunction · 0.92
SetUserQuotaFunction · 0.92
CleanDataFunction · 0.92
CancelExpireFunction · 0.92
SetupExpireFunction · 0.92
GenClientJsonFunction · 0.92

Calls 2

getCharFunction · 0.85
IsIntegerFunction · 0.85

Tested by

no test coverage detected