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:
parent
56363d193d
commit
bf001f5ad2
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user