Skip to content

Commit ecde8c3

Browse files
committed
internal/prompt: skip fmt.Printf and use writer directly
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent b37d84f commit ecde8c3

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

internal/prompt/prompt.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package prompt
55
import (
66
"bufio"
77
"context"
8-
"fmt"
98
"io"
109
"os"
1110
"runtime"
@@ -50,7 +49,7 @@ func DisableInputEcho(ins *streams.In) (restore func() error, _ error) {
5049
// and propagate the error up the stack to prevent the background goroutine
5150
// from blocking indefinitely.
5251
func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string) (string, error) {
53-
_, _ = fmt.Fprint(out, message)
52+
_, _ = out.Write([]byte(message))
5453

5554
result := make(chan string)
5655
go func() {
@@ -62,7 +61,7 @@ func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string)
6261

6362
select {
6463
case <-ctx.Done():
65-
_, _ = fmt.Fprintln(out, "")
64+
_, _ = out.Write([]byte("\n"))
6665
return "", ErrTerminated
6766
case r := <-result:
6867
return r, nil
@@ -80,24 +79,24 @@ func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string)
8079
// returns an error, the caller should close the [io.Reader] used for the prompt
8180
// and propagate the error up the stack to prevent the background goroutine
8281
// from blocking indefinitely.
83-
func Confirm(ctx context.Context, ins io.Reader, outs io.Writer, message string) (bool, error) {
82+
func Confirm(ctx context.Context, in io.Reader, out io.Writer, message string) (bool, error) {
8483
if message == "" {
8584
message = "Are you sure you want to proceed?"
8685
}
8786
message += " [y/N] "
8887

89-
_, _ = fmt.Fprint(outs, message)
88+
_, _ = out.Write([]byte(message))
9089

9190
// On Windows, force the use of the regular OS stdin stream.
9291
if runtime.GOOS == "windows" {
93-
ins = streams.NewIn(os.Stdin)
92+
in = streams.NewIn(os.Stdin)
9493
}
9594

9695
result := make(chan bool)
9796

9897
go func() {
9998
var res bool
100-
scanner := bufio.NewScanner(ins)
99+
scanner := bufio.NewScanner(in)
101100
if scanner.Scan() {
102101
answer := strings.TrimSpace(scanner.Text())
103102
if strings.EqualFold(answer, "y") {
@@ -109,7 +108,7 @@ func Confirm(ctx context.Context, ins io.Reader, outs io.Writer, message string)
109108

110109
select {
111110
case <-ctx.Done():
112-
_, _ = fmt.Fprintln(outs, "")
111+
_, _ = out.Write([]byte("\n"))
113112
return false, ErrTerminated
114113
case r := <-result:
115114
return r, nil

0 commit comments

Comments
 (0)