-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtty_utils.go
121 lines (112 loc) · 2.75 KB
/
tty_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) Johan Stenstam, johan.stenstam@internetstiftelsen.se
*/
package tapir
import (
"bufio"
"strconv"
// "encoding/json"
"fmt"
"os"
"strings"
)
func TtyQuestion(query, oldval string, force bool) string {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [%s]: ", query, oldval)
text, _ := reader.ReadString('\n')
if text == "\n" {
fmt.Printf("[empty response, keeping previous value]\n")
if oldval != "" {
return oldval // all ok
} else if force {
fmt.Printf("[error: previous value was empty string, not allowed]\n")
continue
}
return oldval
} else {
// regardless of force we accept non-empty response
return strings.TrimSuffix(text, "\n")
}
}
}
func TtyIntQuestion(query string, oldval int, force bool) int {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [%d]: ", query, oldval)
text, _ := reader.ReadString('\n')
if text == "\n" {
fmt.Printf("[empty response, keeping previous value]\n")
if oldval != 0 {
return oldval // all ok
} else if force {
fmt.Printf("[error: previous value was empty string, not allowed]\n")
continue
}
return oldval
} else {
text = Chomp(text)
// regardless of force we accept non-empty response
val, err := strconv.Atoi(text)
if err != nil {
fmt.Printf("Error: %s is not an integer\n", text)
continue
}
return val
}
}
}
func TtyYesNo(query, defval string) string {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [%s]: ", query, defval)
text, _ := reader.ReadString('\n')
if text == "\n" {
if defval != "" {
fmt.Printf("[empty response, using default value]\n")
return defval // all ok
}
fmt.Printf("[error: default value is empty string, not allowed]\n")
continue
} else {
val := strings.ToLower(strings.TrimSuffix(text, "\n"))
if (val == "yes") || (val == "no") {
return val
}
fmt.Printf("Answer '%s' not accepted. Only yes or no.\n", val)
}
}
}
func TtyRadioButtonQ(query, defval string, choices []string) string {
var C []string
for _, c := range choices {
C = append(C, strings.ToLower(c))
}
allowed := func(str string) bool {
for _, c := range C {
if str == c {
return true
}
}
return false
}
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [%s]: ", query, defval)
text, _ := reader.ReadString('\n')
if text == "\n" {
if defval != "" {
fmt.Printf("[empty response, using default value]\n")
return defval // all ok
}
fmt.Printf("[error: default value is empty string, not allowed]\n")
continue
} else {
val := strings.ToLower(strings.TrimSuffix(text, "\n"))
if allowed(val) {
return val
}
fmt.Printf("Answer '%s' not accepted. Possible choices are: %v\n", val, choices)
}
}
}