-
Notifications
You must be signed in to change notification settings - Fork 3
/
confirm.go
107 lines (89 loc) · 2.84 KB
/
confirm.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
package disgo
import (
"fmt"
"strconv"
"strings"
)
var (
// DefaultConfirmationChoices is the default value
// for the choices that are given to
// the users in a confirmation prompt.
DefaultConfirmationChoices = []string{"y", "n"}
)
// ConfirmationParser is a function that parses an input and returns a
// confirmation value as well as an error, if the input can't be parsed.
type ConfirmationParser func(string) (bool, error)
// DefaultConfirmation is a confirmation parser that covers most cases
// for confirmation.
// It converts y/Y/yes/YES/t/T/true/True/1 to true.
// It converts n/N/no/NO/f/F/false/FALSE/0 to false.
func DefaultConfirmation(input string) (bool, error) {
switch input {
case "y", "Y", "yes", "YES":
return true, nil
case "n", "N", "no", "NO":
return false, nil
}
// ParseBool handles cases such as 0/1, true/false, t/f etc.
return strconv.ParseBool(input)
}
// Confirmation represents a confirmation prompt's configuration.
type Confirmation struct {
// The label that will be prompted to the user.
// Example: `Are you sure?`
Label string
// The choices that will be presented to the user.
// Example: `Y/n`. (A good practice is to uppercase
// the default value, if there is one).
Choices []string
// EnableDefaultValue tells the terminal whether or not
// there is a default value that will be used when the
// user doesn't input any data.
EnableDefaultValue bool
// DefaultValue is the default value that will be used when
// the user doesn't input any data, if EnableDefaultValue
// is set to true OR that the terminal is set to not
// interactive.
DefaultValue bool
// The parser that will be used to convert the user's input
// into a true/false value.
Parser ConfirmationParser
}
func (c Confirmation) parser() ConfirmationParser {
if c.Parser != nil {
return c.Parser
}
return DefaultConfirmation
}
func (c Confirmation) choices() string {
if c.Choices != nil {
return strings.Join(c.Choices, "/")
}
return strings.Join(DefaultConfirmationChoices, "/")
}
// Confirm prompts the user to confirm something.
func (t Terminal) Confirm(config Confirmation) (bool, error) {
// If terminal is not set to interactive,
// directly return the default value.
if !t.interactive {
return config.DefaultValue, nil
}
// Print the label and choices.
fmt.Fprintf(t.defaultOutput, "%s [%s] ", config.Label, config.choices())
// Wait for user input.
text, err := t.reader.ReadString('\n')
if err != nil {
return false, err
}
// If user just pressed enter directly, return default value.
if config.EnableDefaultValue && text == "\n" {
return config.DefaultValue, nil
}
// Parse user input.
return config.parser()(strings.TrimSpace(text))
}
// Confirm prompts the user to confirm something
// using the global terminal.
func Confirm(config Confirmation) (bool, error) {
return globalTerm.Confirm(config)
}