4 Commits
2 changed files with 7 additions and 6 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2025 Randall Winkhart Copyright (c) 2025-2026 Randall Winkhart
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
+6 -5
View File
@@ -11,7 +11,7 @@ import (
// Input prompts the user for input and returns the input as a string. // Input prompts the user for input and returns the input as a string.
func Input(prompt string) string { func Input(prompt string) string {
fmt.Print("\n" + prompt + " ") fmt.Print(prompt + " ")
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
userInput, _ := reader.ReadString('\n') userInput, _ := reader.ReadString('\n')
return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces
@@ -19,7 +19,7 @@ func Input(prompt string) string {
// InputHidden prompts the user for input and returns the input as a byte array, hiding the input from the terminal. // InputHidden prompts the user for input and returns the input as a byte array, hiding the input from the terminal.
func InputHidden(prompt string) []byte { func InputHidden(prompt string) []byte {
fmt.Print("\n" + prompt + " ") fmt.Print(prompt + " ")
byteInput, _ := term.ReadPassword(int(os.Stdin.Fd())) byteInput, _ := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println() fmt.Println()
return byteInput return byteInput
@@ -28,7 +28,7 @@ func InputHidden(prompt string) []byte {
// InputBinary prompts the user with a yes/no question and returns the response as a boolean. // InputBinary prompts the user with a yes/no question and returns the response as a boolean.
func InputBinary(prompt string) bool { func InputBinary(prompt string) bool {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
fmt.Print("\n" + prompt + " (y/N) ") fmt.Print(prompt + " (y/N) ")
char, _, _ := reader.ReadRune() char, _, _ := reader.ReadRune()
if char == 'y' || char == 'Y' { if char == 'y' || char == 'Y' {
return true return true
@@ -40,12 +40,13 @@ func InputBinary(prompt string) bool {
// A negative min/max value will disable the respective limit. // A negative min/max value will disable the respective limit.
func InputInt(prompt string, min, max int) int { func InputInt(prompt string, min, max int) int {
for { for {
fmt.Print("\n" + prompt + " ") fmt.Print(prompt + " ")
var userInput int var userInput int
_, err := fmt.Scanln(&userInput) _, err := fmt.Scanln(&userInput)
if err == nil && (userInput >= min || min < 0) && (userInput <= max || max < 0) { if err == nil && (userInput >= min || min < 0) && (userInput <= max || max < 0) {
return userInput return userInput
} }
fmt.Println()
} }
} }
@@ -54,5 +55,5 @@ func InputMenuGen(prompt string, options []string) int {
for i, option := range options { for i, option := range options {
fmt.Printf("%d. %s\n", i+1, option) fmt.Printf("%d. %s\n", i+1, option)
} }
return InputInt(prompt, 1, len(options)) return InputInt("\n"+prompt, 1, len(options))
} }