Files

33 lines
802 B
Go

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
}