diff --git a/internal/cli/handler/editHandler.go b/internal/cli/handler/editHandler.go index e8f12b5..75f11a3 100644 --- a/internal/cli/handler/editHandler.go +++ b/internal/cli/handler/editHandler.go @@ -26,9 +26,11 @@ func (h *EditHandler) Handle(args []string, connections []config.Connection) { for { fmt.Println() utils.Display(connections[indexToEdit]) - fieldToEdit, err := utils.PromptInput("Enter the field to edit (ip, user, port, keywords, default): ", false) + fmt.Println("Enter the field to edit (ip, user, port, keywords, default). Type 'quit' to exit anytime.") + fieldToEdit, err := utils.PromptInput("Field to edit: ", false) if err != nil { - fmt.Println("Error reading input:", err) + fmt.Println("") + fmt.Println(err) return } @@ -70,7 +72,7 @@ func (h *EditHandler) Handle(args []string, connections []config.Connection) { } connections[indexToEdit].Keywords = keywordList case "default": - newDefault, _ := utils.PromptInput("Set as default? (yes/no): ") + newDefault, _ := utils.PromptInput("Set as default? ([Y]es | [N]o): ") if strings.ToLower(newDefault) == "y" || strings.ToLower(newDefault) == "yes" { for i := range connections { connections[i].Default = false diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 50a011a..9260248 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -20,19 +20,24 @@ func PromptInput(question string, opts ...bool) (string, error) { quit = opts[0] } - fmt.Println(question) - reader := bufio.NewReader(os.Stdin) - input, err := reader.ReadString('\n') - if err != nil { - return "", err + fmt.Print(question) + + scanner := bufio.NewScanner(os.Stdin) + if scanner.Scan() { + input := strings.TrimSpace(scanner.Text()) + + if quit && strings.ToLower(input) == "quit" { + return "", fmt.Errorf("operation cancelled") + } + + return input, nil } - input = strings.TrimSpace(input) - if quit && strings.ToLower(input) == "quit" { - return "", fmt.Errorf("operation cancelled") + if err := scanner.Err(); err != nil { + return "", err } - return input, nil + return "", fmt.Errorf("operation cancelled") } func ConfirmAction(question string) (bool, error) {