feat(v0.4): add ConfirmYN prompt helper

This commit is contained in:
2026-04-21 17:05:36 -04:00
parent 42fce73824
commit cc7a8535a3
2 changed files with 77 additions and 0 deletions
+32
View File
@@ -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
}