5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 04:49:20 +08:00

Improve prompt handling

This commit is contained in:
Lea Anthony 2019-03-06 19:11:03 +11:00
parent 56363d193d
commit bf001f5ad2
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405

View File

@ -1,25 +1,26 @@
package cmd package cmd
import ( import (
"bufio"
"fmt" "fmt"
"os"
"strconv" "strconv"
) )
// Prompt asks the user for a value // Prompt asks the user for a value
func Prompt(question string, defaultValue ...string) string { func Prompt(question string, defaultValue ...string) string {
var answer string var answer string
haveDefault := len(defaultValue) > 0 && defaultValue[0] != ""
if haveDefault { if len(defaultValue) > 0 {
question = fmt.Sprintf("%s (%s)", question, defaultValue[0]) answer = defaultValue[0]
question = fmt.Sprintf("%s (%s)", question, answer)
} }
fmt.Printf(question + ": ") fmt.Printf(question + ": ")
fmt.Scanln(&answer) scanner := bufio.NewScanner(os.Stdin)
if haveDefault { if scanner.Scan() {
if len(answer) == 0 { answer = scanner.Text()
answer = defaultValue[0]
}
} }
return answer return answer
} }