Skip to content

Commit

Permalink
feat: add --[no-]strip-ansi-codes option
Browse files Browse the repository at this point in the history
  • Loading branch information
srithon committed Dec 16, 2024
1 parent 6d405c4 commit a8dd7c8
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (o Options) Run() error {
)

if len(o.Options) <= 0 {
input, _ := stdin.ReadStrip()
input, _ := stdin.ReadWithOptions(&o)
if input == "" {
return errors.New("no options provided, see `gum choose --help`")
}
Expand Down
5 changes: 5 additions & 0 deletions choose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ type Options struct {
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=99" envprefix:"GUM_CHOOSE_HEADER_"`
ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
SelectedItemStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_SELECTED_"`
StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_CHOOSE_STRIP_ANSI_CODES"`
}

func (o *Options) DoStripANSICodes() bool {
return o.StripANSICodes
}
2 changes: 1 addition & 1 deletion filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (o Options) Run() error {
v := viewport.New(o.Width, o.Height)

if len(o.Options) == 0 {
if input, _ := stdin.ReadStrip(); input != "" {
if input, _ := stdin.ReadWithOptions(&o); input != "" {
o.Options = strings.Split(input, o.InputDelimiter)
} else {
o.Options = files.List()
Expand Down
6 changes: 6 additions & 0 deletions filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ type Options struct {

// Deprecated: use [FuzzySort]. This will be removed at some point.
Sort bool `help:"Sort fuzzy results by their scores" default:"true" env:"GUM_FILTER_FUZZY_SORT" negatable:"" hidden:""`

StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_FILTER_STRIP_ANSI_CODES"`
}

func (o *Options) DoStripANSICodes() bool {
return o.StripANSICodes
}
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.ReadStrip()
input, _ = stdin.ReadWithOptions(&o)
}

switch o.Type {
Expand Down
6 changes: 6 additions & 0 deletions format/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ type Options struct {
Language string `help:"Programming language to parse code" short:"l" default:"" env:"GUM_FORMAT_LANGUAGE"`

Type string `help:"Format to use (markdown,template,code,emoji)" enum:"markdown,template,code,emoji" short:"t" default:"markdown" env:"GUM_FORMAT_TYPE"`

StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_FORMAT_STRIP_ANSI_CODES"`
}

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

i := textinput.New()
if o.Value != "" {
i.SetValue(o.Value)
} else if in, _ := stdin.ReadStrip(); in != "" {
} else if in, _ := stdin.ReadWithOptions(&o); in != "" {
i.SetValue(in)
}
i.Focus()
Expand Down
6 changes: 6 additions & 0 deletions input/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ type Options struct {
Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_INPUT_HEADER_"`
Timeout time.Duration `help:"Timeout until input aborts" default:"0s" env:"GUM_INPUT_TIMEOUT"`

StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_INPUT_STRIP_ANSI_CODES"`
}

func (o *Options) DoStripANSICodes() bool {
return o.StripANSICodes
}
19 changes: 19 additions & 0 deletions internal/stdin/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package stdin

// StdinOptions provide switches for ReadWithOptions, indicating how to read
// input from stdin
type StdinOptions interface {
// DoStripANSICodes returns true if the ANSI codes should be stripped from the
// input.
DoStripANSICodes() bool
}

// ReadWithOptions delegates to the module's Read functions based on the
// provided options instance.
func ReadWithOptions(opts StdinOptions) (string, error) {
if opts.DoStripANSICodes() {
return ReadStrip()
} else {
return Read()
}
}
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.ReadStrip()
text, _ = stdin.ReadWithOptions(&o)
if text == "" {
return errors.New("no input provided, see `gum style --help`")
}
Expand Down
6 changes: 6 additions & 0 deletions style/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ type Options struct {
Text []string `arg:"" optional:"" help:"Text to which to apply the style"`
Trim bool `help:"Trim whitespaces on every input line" default:"false"`
Style StylesNotHidden `embed:""`

StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_STYLE_STRIP_ANSI_CODES"`
}

func (o *Options) DoStripANSICodes() bool {
return o.StripANSICodes
}

// Styles is a flag set of possible styles.
Expand Down
2 changes: 1 addition & 1 deletion write/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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.ReadStrip()
in, _ := stdin.ReadWithOptions(&o)
if in != "" && o.Value == "" {
o.Value = strings.ReplaceAll(in, "\r", "")
}
Expand Down
6 changes: 6 additions & 0 deletions write/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ type Options struct {
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_WRITE_HEADER_"`
PlaceholderStyle style.Styles `embed:"" prefix:"placeholder." set:"defaultForeground=240" envprefix:"GUM_WRITE_PLACEHOLDER_"`
PromptStyle style.Styles `embed:"" prefix:"prompt." set:"defaultForeground=7" envprefix:"GUM_WRITE_PROMPT_"`

StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_WRITE_STRIP_ANSI_CODES"`
}

func (o *Options) DoStripANSICodes() bool {
return o.StripANSICodes
}

0 comments on commit a8dd7c8

Please sign in to comment.