From a8dd7c800e441cfd23a3956c67b8da411dcf9479 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Sun, 15 Dec 2024 19:07:16 -0500 Subject: [PATCH] feat: add --[no-]strip-ansi-codes option --- choose/command.go | 2 +- choose/options.go | 5 +++++ filter/command.go | 2 +- filter/options.go | 6 ++++++ format/command.go | 2 +- format/options.go | 6 ++++++ input/command.go | 4 ++-- input/options.go | 6 ++++++ internal/stdin/options.go | 19 +++++++++++++++++++ style/command.go | 2 +- style/options.go | 6 ++++++ write/command.go | 2 +- write/options.go | 6 ++++++ 13 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 internal/stdin/options.go diff --git a/choose/command.go b/choose/command.go index 92a57526d..c687d578d 100644 --- a/choose/command.go +++ b/choose/command.go @@ -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`") } diff --git a/choose/options.go b/choose/options.go index 4777ef0a7..0a53a6c98 100644 --- a/choose/options.go +++ b/choose/options.go @@ -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 } diff --git a/filter/command.go b/filter/command.go index 2c6b9b0ed..8514a20f2 100644 --- a/filter/command.go +++ b/filter/command.go @@ -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() diff --git a/filter/options.go b/filter/options.go index b439b372e..5b00daa89 100644 --- a/filter/options.go +++ b/filter/options.go @@ -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 } diff --git a/format/command.go b/format/command.go index 4cea2d9ff..80470eba4 100644 --- a/format/command.go +++ b/format/command.go @@ -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 { diff --git a/format/options.go b/format/options.go index 6f36dcdba..1a4c4cceb 100644 --- a/format/options.go +++ b/format/options.go @@ -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 } diff --git a/input/command.go b/input/command.go index 05dc1e357..21b79152a 100644 --- a/input/command.go +++ b/input/command.go @@ -17,7 +17,7 @@ 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 } } @@ -25,7 +25,7 @@ func (o Options) Run() error { 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() diff --git a/input/options.go b/input/options.go index 2df85e738..d070ca259 100644 --- a/input/options.go +++ b/input/options.go @@ -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 } diff --git a/internal/stdin/options.go b/internal/stdin/options.go new file mode 100644 index 000000000..a9d5879c7 --- /dev/null +++ b/internal/stdin/options.go @@ -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() + } +} diff --git a/style/command.go b/style/command.go index be04c8453..bb77e822d 100644 --- a/style/command.go +++ b/style/command.go @@ -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`") } diff --git a/style/options.go b/style/options.go index cd2251d97..2a53de0cd 100644 --- a/style/options.go +++ b/style/options.go @@ -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. diff --git a/write/command.go b/write/command.go index 34669cb8e..66c4d488e 100644 --- a/write/command.go +++ b/write/command.go @@ -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", "") } diff --git a/write/options.go b/write/options.go index 96ae7a876..191e32e47 100644 --- a/write/options.go +++ b/write/options.go @@ -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 }