| 1 |
|
| 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) |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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) |
| 46 |
} |
| 47 |
return fmt.Errorf("Invalid: must be a number between %d and %d", low, high) |
| 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 |
|
| 71 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 107 |
|
| 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 |
| 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 |
|
| 130 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 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 |
} |