Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions interact/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ func promptLines(in io.Reader) chan result {
promptReaders.ch = ch

go func(r io.Reader) {
defer close(ch)
s := bufio.NewScanner(r)
for s.Scan() {
ch <- result{answer: strings.TrimSpace(s.Text())}
}
ch <- result{err: s.Err()}
if err := s.Err(); err != nil {
ch <- result{err: err}
}
}(in)

return ch
Expand All @@ -64,7 +67,10 @@ func Prompt(ctx context.Context, query, defaultAnswer string) (string, error) {
_, _ = fmt.Fprintf(cutypes.Output, "%s [%s]: ", query, defaultAnswer)

select {
case r := <-promptLines(cutypes.Input):
case r, ok := <-promptLines(cutypes.Input):
if !ok {
return "", io.EOF
}
if r.err != nil {
return "", r.err
}
Expand Down
45 changes: 45 additions & 0 deletions interact/prompt_eof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package interact_test

import (
"context"
"errors"
"io"
"strings"
"testing"
"time"

"github.com/gookit/cliui"
"github.com/gookit/cliui/interact"
"github.com/gookit/goutil/x/assert"
)

func TestPromptReportsStreamEOF(t *testing.T) {
for _, tc := range []struct {
name, input string
answers []string
}{
{name: "empty stream"},
{name: "one line", input: "Ada\n", answers: []string{"Ada"}},
{name: "final unterminated line", input: "Ada", answers: []string{"Ada"}},
{name: "blank line uses default", input: "\n", answers: []string{"fallback"}},
{name: "several lines", input: "Ada\nGrace\n", answers: []string{"Ada", "Grace"}},
} {
t.Run(tc.name, func(t *testing.T) {
cliui.CustomIO(strings.NewReader(tc.input), io.Discard)
defer cliui.ResetIO()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
for _, want := range tc.answers {
got, err := interact.Prompt(ctx, "Name", "fallback")
assert.NoErr(t, err)
assert.Eq(t, want, got)
}
for i := 0; i < 3; i++ {
got, err := interact.Prompt(ctx, "Name", "fallback")
if !errors.Is(err, io.EOF) || got != "" {
t.Fatalf("Prompt after EOF #%d = %q,%v; want empty answer and EOF", i+1, got, err)
}
}
})
}
}