feat(v0.4): add ConfirmYN prompt helper
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ConfirmYN prints prompt to out, reads one line from in, and returns:
|
||||
// - true for "y"/"yes" (case-insensitive)
|
||||
// - false for "n"/"no" (case-insensitive)
|
||||
// - defaultYes for empty input
|
||||
// - !defaultYes for any other input (treat unrecognized as the
|
||||
// opposite of the default, i.e., the safer choice given the default)
|
||||
func ConfirmYN(in io.Reader, out io.Writer, prompt string, defaultYes bool) bool {
|
||||
fmt.Fprint(out, prompt)
|
||||
reader := bufio.NewReader(in)
|
||||
line, _ := reader.ReadString('\n')
|
||||
answer := strings.TrimSpace(strings.ToLower(line))
|
||||
|
||||
if answer == "" {
|
||||
return defaultYes
|
||||
}
|
||||
if answer == "y" || answer == "yes" {
|
||||
return true
|
||||
}
|
||||
if answer == "n" || answer == "no" {
|
||||
return false
|
||||
}
|
||||
return !defaultYes
|
||||
}
|
||||
Reference in New Issue
Block a user