From ccc496f55fda872271a1ca27bed8fdf21b3d4070 Mon Sep 17 00:00:00 2001 From: Yonah Date: Thu, 19 Mar 2026 17:37:29 +0900 Subject: [PATCH] cmd/streamdata: y/n prompt helper This returns a boolean value with fallback. Signed-off-by: Yonah --- cmd/streamdata/prompt.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cmd/streamdata/prompt.go b/cmd/streamdata/prompt.go index 9d9bb16..c87d4cb 100644 --- a/cmd/streamdata/prompt.go +++ b/cmd/streamdata/prompt.go @@ -126,6 +126,40 @@ invalid: return v, true } +// promptBool prompts the user for a y/n response until a valid response or a +// single newline character is read. +func promptBool( + br *bufio.Reader, + p string, + fallback bool, +) (bool, bool) { + if fallback { + p += "[Y/n] " + } else { + p += "[y/N] " + } + +invalid: + s, ok := prompt(br, p) + if !ok { + return false, false + } + + switch strings.ToLower(strings.TrimSpace(s)) { + default: + goto invalid + + case "": + return fallback, true + + case "y": + return true, true + + case "n": + return false, true + } +} + // require panics with an error if ok is false, and returns v otherwise. func require[T any](v T, ok bool) T { if !ok {