Debian Codegraph @

internal/accessibility/accessibility.go

raw symbols SCIP
1 // Package accessibility provides accessible functions to capture user input.
2 package accessibility
3
4 import (
5 "bufio"
6 "cmp"
7 "errors"
8 "fmt"
9 "io"
10 "slices"
11 "strconv"
12 "strings"
13
14 "github.com/charmbracelet/x/term"
15 )
16
17 func atoi(s string) (int, error) {
18 if strings.TrimSpace(s) == "" {
19 return -1, nil
20 }
21 return strconv.Atoi(s) //nolint:wrapcheck
22 }
23
24 // PromptInt prompts a user for an integer between a certain range.
25 //
26 // Given invalid input (non-integers, integers outside of the range), the user
27 // will continue to be reprompted until a valid input is given, ensuring that
28 // the return value is always valid.
29 func PromptInt(
30 out io.Writer,
31 in io.Reader,
32 prompt string,
33 low, high int,
34 defaultValue *int,
35 ) int {
36 var choice int
37
38 validInt := func(s string) error {
39 if strings.TrimSpace(s) == "" && defaultValue != nil {
40 return nil
41 }
42 i, err := atoi(s)
43 if err != nil || i < low || i > high {
44 if low == high {
45 return fmt.Errorf("Invalid: must be %d", low) //nolint:staticcheck
46 }
47 return fmt.Errorf("Invalid: must be a number between %d and %d", low, high) //nolint:staticcheck
48 }
49 return nil
50 }
51
52 input := PromptString(
53 out,
54 in,
55 prompt,
56 ptrToStr(defaultValue, strconv.Itoa),
57 validInt,
58 )
59 choice, _ = strconv.Atoi(input)
60 return choice
61 }
62
63 func parseBool(s string) (bool, error) {
64 s = strings.ToLower(s)
65
66 if slices.Contains([]string{"y", "yes"}, s) {
67 return true, nil
68 }
69
70 // As a special case, we default to "" to no since the usage of this
71 // function suggests N is the default.
72 if slices.Contains([]string{"n", "no"}, s) {
73 return false, nil
74 }
75
76 return false, errors.New("invalid input. please try again")
77 }
78
79 // PromptBool prompts a user for a boolean value.
80 //
81 // Given invalid input (non-boolean), the user will continue to be reprompted
82 // until a valid input is given, ensuring that the return value is always valid.
83 func PromptBool(
84 out io.Writer,
85 in io.Reader,
86 prompt string,
87 defaultValue bool,
88 ) bool {
89 validBool := func(s string) error {
90 if strings.TrimSpace(s) == "" {
91 return nil
92 }
93 _, err := parseBool(s)
94 return err
95 }
96
97 input := PromptString(
98 out, in, prompt,
99 boolToStr(defaultValue),
100 validBool,
101 )
102 b, _ := parseBool(input)
103 return b
104 }
105
106 // PromptPassword allows to prompt for a password.
107 // In must be the fd of a tty.
108 func PromptPassword(
109 out io.Writer,
110 in uintptr,
111 prompt string,
112 validator func(input string) error,
113 ) (string, error) {
114 for {
115 _, _ = fmt.Fprint(out, prompt)
116 pwd, err := term.ReadPassword(in)
117 if err != nil {
118 return "", err //nolint:wrapcheck
119 }
120 _, _ = fmt.Fprintln(out)
121 if err := validator(string(pwd)); err != nil {
122 _, _ = fmt.Fprintln(out, err)
123 continue
124 }
125 return string(pwd), nil
126 }
127 }
128
129 // PromptString prompts a user for a string value and validates it against a
130 // validator function. It re-prompts the user until a valid input is given.
131 func PromptString(
132 out io.Writer,
133 in io.Reader,
134 prompt string,
135 defaultValue string,
136 validator func(input string) error,
137 ) string {
138 scanner := bufio.NewScanner(in)
139
140 var (
141 valid bool
142 input string
143 )
144
145 for !valid {
146 _, _ = fmt.Fprint(out, prompt)
147 if !scanner.Scan() {
148 // no way to bubble up errors or signal cancellation
149 // but the program is probably not continuing if
150 // stdin sent EOF
151 _, _ = fmt.Fprintln(out)
152 break
153 }
154 input = scanner.Text()
155
156 if err := validator(input); err != nil {
157 _, _ = fmt.Fprintln(out, err)
158 continue
159 }
160
161 break
162 }
163
164 return cmp.Or(strings.TrimSpace(input), defaultValue)
165 }
166
167 func ptrToStr[T any](t *T, fn func(t T) string) string {
168 if t == nil {
169 return ""
170 }
171 return fn(*t)
172 }
173
174 func boolToStr(b bool) string {
175 if b {
176 return "y"
177 }
178 return "N"
179 }