From 0b53eef6f1690f89cce577a0453112fc514b49fb Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 12 Dec 2024 16:45:17 -0300 Subject: [PATCH 1/2] feat: yes|gum confirm Signed-off-by: Carlos Alexandro Becker --- confirm/command.go | 15 ++++++++++++++- internal/stdin/stdin.go | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/confirm/command.go b/confirm/command.go index ac151ad72..ec7ab6870 100644 --- a/confirm/command.go +++ b/confirm/command.go @@ -7,12 +7,25 @@ import ( "github.com/charmbracelet/bubbles/help" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" ) +var errNotConfirmed = errors.New("not confirmed") + // Run provides a shell script interface for prompting a user to confirm an // action with an affirmative or negative answer. func (o Options) Run() error { + line, err := stdin.ReadLine() + if err == nil { + switch line { + case "yes", "y": + return nil + default: + return errNotConfirmed + } + } + ctx, cancel := timeout.Context(o.Timeout) defer cancel() @@ -52,5 +65,5 @@ func (o Options) Run() error { return nil } - return errors.New("not confirmed") + return errNotConfirmed } diff --git a/internal/stdin/stdin.go b/internal/stdin/stdin.go index a5cebdb51..9032b30c1 100644 --- a/internal/stdin/stdin.go +++ b/internal/stdin/stdin.go @@ -2,6 +2,7 @@ package stdin import ( "bufio" + "errors" "fmt" "io" "os" @@ -10,10 +11,12 @@ import ( "github.com/charmbracelet/x/ansi" ) +var ErrEmpty = errors.New("stdin is empty") + // Read reads input from an stdin pipe. func Read() (string, error) { if IsEmpty() { - return "", fmt.Errorf("stdin is empty") + return "", ErrEmpty } reader := bufio.NewReader(os.Stdin) @@ -39,6 +42,16 @@ func ReadStrip() (string, error) { return ansi.Strip(s), err } +// ReadLine reads only one line and returns it. +func ReadLine() (string, error) { + if IsEmpty() { + return "", ErrEmpty + } + reader := bufio.NewReader(os.Stdin) + line, _, err := reader.ReadLine() + return strings.TrimSpace(string(line)), err +} + // IsEmpty returns whether stdin is empty. func IsEmpty() bool { stat, err := os.Stdin.Stat() From a4d325a16d4fd60adb7477ee183fe1b149dad392 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 17 Dec 2024 13:58:19 -0300 Subject: [PATCH 2/2] fix: rebase on main --- confirm/command.go | 2 +- internal/stdin/stdin.go | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/confirm/command.go b/confirm/command.go index ec7ab6870..103fbad16 100644 --- a/confirm/command.go +++ b/confirm/command.go @@ -16,7 +16,7 @@ var errNotConfirmed = errors.New("not confirmed") // Run provides a shell script interface for prompting a user to confirm an // action with an affirmative or negative answer. func (o Options) Run() error { - line, err := stdin.ReadLine() + line, err := stdin.Read(stdin.SingleLine(true)) if err == nil { switch line { case "yes", "y": diff --git a/internal/stdin/stdin.go b/internal/stdin/stdin.go index 5ddaa4dd9..42b016285 100644 --- a/internal/stdin/stdin.go +++ b/internal/stdin/stdin.go @@ -2,7 +2,6 @@ package stdin import ( "bufio" - "errors" "fmt" "io" "os" @@ -11,8 +10,6 @@ import ( "github.com/charmbracelet/x/ansi" ) -var ErrEmpty = errors.New("stdin is empty") - type options struct { ansiStrip bool singleLine bool @@ -38,7 +35,7 @@ func SingleLine(b bool) Option { // Read reads input from an stdin pipe. func Read(opts ...Option) (string, error) { if IsEmpty() { - return "", ErrEmpty + return "", fmt.Errorf("stdin is empty") } options := options{}