internal/prompt: skip fmt.Printf and use writer directly

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-03-22 14:10:52 +01:00
parent b37d84fd10
commit ecde8c38a5
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -5,7 +5,6 @@ package prompt
import ( import (
"bufio" "bufio"
"context" "context"
"fmt"
"io" "io"
"os" "os"
"runtime" "runtime"
@ -50,7 +49,7 @@ func DisableInputEcho(ins *streams.In) (restore func() error, _ error) {
// and propagate the error up the stack to prevent the background goroutine // and propagate the error up the stack to prevent the background goroutine
// from blocking indefinitely. // from blocking indefinitely.
func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string) (string, error) { func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string) (string, error) {
_, _ = fmt.Fprint(out, message) _, _ = out.Write([]byte(message))
result := make(chan string) result := make(chan string)
go func() { go func() {
@ -62,7 +61,7 @@ func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string)
select { select {
case <-ctx.Done(): case <-ctx.Done():
_, _ = fmt.Fprintln(out, "") _, _ = out.Write([]byte("\n"))
return "", ErrTerminated return "", ErrTerminated
case r := <-result: case r := <-result:
return r, nil return r, nil
@ -80,24 +79,24 @@ func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string)
// returns an error, the caller should close the [io.Reader] used for the prompt // returns an error, the caller should close the [io.Reader] used for the prompt
// and propagate the error up the stack to prevent the background goroutine // and propagate the error up the stack to prevent the background goroutine
// from blocking indefinitely. // from blocking indefinitely.
func Confirm(ctx context.Context, ins io.Reader, outs io.Writer, message string) (bool, error) { func Confirm(ctx context.Context, in io.Reader, out io.Writer, message string) (bool, error) {
if message == "" { if message == "" {
message = "Are you sure you want to proceed?" message = "Are you sure you want to proceed?"
} }
message += " [y/N] " message += " [y/N] "
_, _ = fmt.Fprint(outs, message) _, _ = out.Write([]byte(message))
// On Windows, force the use of the regular OS stdin stream. // On Windows, force the use of the regular OS stdin stream.
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
ins = streams.NewIn(os.Stdin) in = streams.NewIn(os.Stdin)
} }
result := make(chan bool) result := make(chan bool)
go func() { go func() {
var res bool var res bool
scanner := bufio.NewScanner(ins) scanner := bufio.NewScanner(in)
if scanner.Scan() { if scanner.Scan() {
answer := strings.TrimSpace(scanner.Text()) answer := strings.TrimSpace(scanner.Text())
if strings.EqualFold(answer, "y") { if strings.EqualFold(answer, "y") {
@ -109,7 +108,7 @@ func Confirm(ctx context.Context, ins io.Reader, outs io.Writer, message string)
select { select {
case <-ctx.Done(): case <-ctx.Done():
_, _ = fmt.Fprintln(outs, "") _, _ = out.Write([]byte("\n"))
return false, ErrTerminated return false, ErrTerminated
case r := <-result: case r := <-result:
return r, nil return r, nil