LoopInput 循环输入选择, 或者直接回车退出
(tip string, choices interface{}, singleRowPrint bool)
| 78 | |
| 79 | // LoopInput 循环输入选择, 或者直接回车退出 |
| 80 | func 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 读取终端用户输入 |
| 125 | func Input(tip string, defaultValue string) string { |
no test coverage detected