fix(cli): respect stdin overrides in prompts
All checks were successful
Security Scan / security (push) Successful in 20s
All checks were successful
Security Scan / security (push) Successful in 20s
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
c6fae794b3
commit
9c64f239a8
2 changed files with 27 additions and 6 deletions
|
|
@ -52,6 +52,29 @@ func TestMultiSelect_Good(t *testing.T) {
|
|||
assert.Equal(t, []string{"a", "c"}, vals)
|
||||
}
|
||||
|
||||
func TestConfirm_Good(t *testing.T) {
|
||||
SetStdin(strings.NewReader("y\n"))
|
||||
defer SetStdin(nil)
|
||||
|
||||
assert.True(t, Confirm("Proceed?"))
|
||||
}
|
||||
|
||||
func TestQuestion_Good(t *testing.T) {
|
||||
SetStdin(strings.NewReader("alice\n"))
|
||||
defer SetStdin(nil)
|
||||
|
||||
val := Question("Name:")
|
||||
assert.Equal(t, "alice", val)
|
||||
}
|
||||
|
||||
func TestChoose_Good_DefaultIndex(t *testing.T) {
|
||||
SetStdin(strings.NewReader("\n"))
|
||||
defer SetStdin(nil)
|
||||
|
||||
val := Choose("Pick", []string{"a", "b", "c"}, WithDefaultIndex[string](1))
|
||||
assert.Equal(t, "b", val)
|
||||
}
|
||||
|
||||
func TestSetStdin_Good_ResetNil(t *testing.T) {
|
||||
original := stdin
|
||||
t.Cleanup(func() { stdin = original })
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -97,7 +95,7 @@ func Confirm(prompt string, opts ...ConfirmOption) bool {
|
|||
suffix = fmt.Sprintf("%s(auto in %s) ", suffix, cfg.timeout.Round(time.Second))
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
reader := newReader()
|
||||
|
||||
for {
|
||||
fmt.Printf("%s %s", prompt, suffix)
|
||||
|
|
@ -215,7 +213,7 @@ func Question(prompt string, opts ...QuestionOption) string {
|
|||
opt(cfg)
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
reader := newReader()
|
||||
|
||||
for {
|
||||
// Build prompt with default
|
||||
|
|
@ -335,7 +333,7 @@ func Choose[T any](prompt string, items []T, opts ...ChooseOption[T]) T {
|
|||
fmt.Printf(" %s%d. %s\n", marker, i+1, cfg.displayFn(item))
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
reader := newReader()
|
||||
|
||||
for {
|
||||
fmt.Printf("Enter number [1-%d]: ", len(items))
|
||||
|
|
@ -396,7 +394,7 @@ func ChooseMulti[T any](prompt string, items []T, opts ...ChooseOption[T]) []T {
|
|||
fmt.Printf(" %d. %s\n", i+1, cfg.displayFn(item))
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
reader := newReader()
|
||||
|
||||
for {
|
||||
fmt.Printf("Enter numbers (e.g., 1 3 5 or 1-3) or empty for none: ")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue