5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 14:10:25 +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
import (
"bufio"
"fmt"
"os"
"strconv"
)
// Prompt asks the user for a value
func Prompt(question string, defaultValue ...string) string {
var answer string
haveDefault := len(defaultValue) > 0 && defaultValue[0] != ""
if haveDefault {
question = fmt.Sprintf("%s (%s)", question, defaultValue[0])
if len(defaultValue) > 0 {
answer = defaultValue[0]
question = fmt.Sprintf("%s (%s)", question, answer)
}
fmt.Printf(question + ": ")
fmt.Scanln(&answer)
if haveDefault {
if len(answer) == 0 {
answer = defaultValue[0]
}
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
answer = scanner.Text()
}
return answer
}