diff --git a/front/input_UNIX.go b/front/input_utils.go similarity index 82% rename from front/input_UNIX.go rename to front/input_utils.go index 4f22a8a..860fabe 100644 --- a/front/input_UNIX.go +++ b/front/input_utils.go @@ -16,6 +16,15 @@ func Input(prompt string) string { return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces } +// Input prompts the user for input and +// returns the input as a rune. +func InputRune(prompt string) rune { + fmt.Print(prompt + " ") + reader := bufio.NewReader(os.Stdin) + userInput, _, _ := reader.ReadRune() + return userInput +} + // InputBinary prompts the user with a yes/no // question and returns the response as a boolean. func InputBinary(prompt string) bool { diff --git a/front/input_common.go b/front/input_wrappers.go similarity index 55% rename from front/input_common.go rename to front/input_wrappers.go index ca17444..43a449e 100644 --- a/front/input_common.go +++ b/front/input_wrappers.go @@ -3,6 +3,7 @@ package front import ( "fmt" "os" + "slices" "golang.org/x/term" ) @@ -16,6 +17,22 @@ func InputMenuGen(prompt string, options []string) int { return InputInt("\n"+prompt, 1, len(options)) } +// InputMenuGenWithRuneInputs prompts the user with a menu +// and returns the user's choice as a rune. +// Ensure runeInputs contains only unique runes and matches the +// length of options. +func InputMenuGenWithRuneInputs(prompt string, options []string, runeInputs []rune) rune { + for i, option := range options { + fmt.Printf("%c. %s\n", runeInputs[i], option) + } + for { + userInput := InputRune("\n" + prompt) + if slices.Contains(runeInputs, userInput) { + return userInput + } + } +} + // InputSecret prompts the user for input and returns the // input as a byte array, hiding the input from the terminal. func InputSecret(prompt string) []byte {