Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: yes|gum confirm #772

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion confirm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -52,5 +65,5 @@ func (o Options) Run() error {
return nil
}

return errors.New("not confirmed")
return errNotConfirmed
}
15 changes: 14 additions & 1 deletion internal/stdin/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand All @@ -10,10 +11,12 @@
"github.com/charmbracelet/x/ansi"
)

var ErrEmpty = errors.New("stdin is empty")

Check failure on line 14 in internal/stdin/stdin.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported var ErrEmpty should have comment or be unexported (revive)

Check failure on line 14 in internal/stdin/stdin.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported var ErrEmpty should have comment or be unexported (revive)

// 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)
Expand All @@ -39,6 +42,16 @@
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()
Expand Down
Loading