Skip to content

Commit

Permalink
fix(pager): do not strip ansi sequences (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 authored Dec 9, 2024
1 parent 2e2b020 commit 2939e51
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (o Options) Run() error {
)

if len(o.Options) <= 0 {
input, _ := stdin.Read()
input, _ := stdin.ReadStrip()
if input == "" {
return errors.New("no options provided, see `gum choose --help`")
}
Expand Down
2 changes: 1 addition & 1 deletion filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (o Options) Run() error {
v := viewport.New(o.Width, o.Height)

if len(o.Options) == 0 {
if input, _ := stdin.Read(); input != "" {
if input, _ := stdin.ReadStrip(); input != "" {
o.Options = strings.Split(input, "\n")
} else {
o.Options = files.List()
Expand Down
2 changes: 1 addition & 1 deletion format/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (o Options) Run() error {
if len(o.Template) > 0 {
input = strings.Join(o.Template, "\n")
} else {
input, _ = stdin.Read()
input, _ = stdin.ReadStrip()
}

switch o.Type {
Expand Down
4 changes: 2 additions & 2 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
// https://github.com/charmbracelet/bubbles/textinput
func (o Options) Run() error {
if o.Value == "" {
if in, _ := stdin.Read(); in != "" {
if in, _ := stdin.ReadStrip(); in != "" {
o.Value = in
}
}

i := textinput.New()
if o.Value != "" {
i.SetValue(o.Value)
} else if in, _ := stdin.Read(); in != "" {
} else if in, _ := stdin.ReadStrip(); in != "" {
i.SetValue(in)
}
i.Focus()
Expand Down
8 changes: 7 additions & 1 deletion internal/stdin/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ func Read() (string, error) {
}
}

return strings.TrimSuffix(ansi.Strip(b.String()), "\n"), nil
return strings.TrimSuffix(b.String(), "\n"), nil
}

// ReadStrip reads input from an stdin pipe and strips ansi sequences.
func ReadStrip() (string, error) {
s, err := Read()
return ansi.Strip(s), err
}

// IsEmpty returns whether stdin is empty.
Expand Down
2 changes: 1 addition & 1 deletion style/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (o Options) Run() error {
if len(o.Text) > 0 {
text = strings.Join(o.Text, "\n")
} else {
text, _ = stdin.Read()
text, _ = stdin.ReadStrip()
if text == "" {
return errors.New("no input provided, see `gum style --help`")
}
Expand Down
2 changes: 1 addition & 1 deletion write/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// Run provides a shell script interface for the text area bubble.
// https://github.com/charmbracelet/bubbles/textarea
func (o Options) Run() error {
in, _ := stdin.Read()
in, _ := stdin.ReadStrip()
if in != "" && o.Value == "" {
o.Value = strings.ReplaceAll(in, "\r", "")
}
Expand Down

0 comments on commit 2939e51

Please sign in to comment.