Skip to content

Commit 99e408e

Browse files
committed
feat: add --[no-]strip-ansi-codes option
1 parent 6d405c4 commit 99e408e

File tree

13 files changed

+62
-7
lines changed

13 files changed

+62
-7
lines changed

choose/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (o Options) Run() error {
2626
)
2727

2828
if len(o.Options) <= 0 {
29-
input, _ := stdin.ReadStrip()
29+
input, _ := stdin.ReadWithOptions(&o)
3030
if input == "" {
3131
return errors.New("no options provided, see `gum choose --help`")
3232
}

choose/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ type Options struct {
2929
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=99" envprefix:"GUM_CHOOSE_HEADER_"`
3030
ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
3131
SelectedItemStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_SELECTED_"`
32+
33+
StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_CHOOSE_STRIP_ANSI_CODES"`
34+
}
35+
36+
func (o *Options) DoStripANSICodes() bool {
37+
return o.StripANSICodes
3238
}

filter/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (o Options) Run() error {
3232
v := viewport.New(o.Width, o.Height)
3333

3434
if len(o.Options) == 0 {
35-
if input, _ := stdin.ReadStrip(); input != "" {
35+
if input, _ := stdin.ReadWithOptions(&o); input != "" {
3636
o.Options = strings.Split(input, o.InputDelimiter)
3737
} else {
3838
o.Options = files.List()

filter/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ type Options struct {
4343

4444
// Deprecated: use [FuzzySort]. This will be removed at some point.
4545
Sort bool `help:"Sort fuzzy results by their scores" default:"true" env:"GUM_FILTER_FUZZY_SORT" negatable:"" hidden:""`
46+
47+
StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_FILTER_STRIP_ANSI_CODES"`
48+
}
49+
50+
func (o *Options) DoStripANSICodes() bool {
51+
return o.StripANSICodes
4652
}

format/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (o Options) Run() error {
2424
if len(o.Template) > 0 {
2525
input = strings.Join(o.Template, "\n")
2626
} else {
27-
input, _ = stdin.ReadStrip()
27+
input, _ = stdin.ReadWithOptions(&o)
2828
}
2929

3030
switch o.Type {

format/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ type Options struct {
77
Language string `help:"Programming language to parse code" short:"l" default:"" env:"GUM_FORMAT_LANGUAGE"`
88

99
Type string `help:"Format to use (markdown,template,code,emoji)" enum:"markdown,template,code,emoji" short:"t" default:"markdown" env:"GUM_FORMAT_TYPE"`
10+
11+
StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_FORMAT_STRIP_ANSI_CODES"`
12+
}
13+
14+
func (o *Options) DoStripANSICodes() bool {
15+
return o.StripANSICodes
1016
}

input/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
// https://github.com/charmbracelet/bubbles/textinput
1818
func (o Options) Run() error {
1919
if o.Value == "" {
20-
if in, _ := stdin.ReadStrip(); in != "" {
20+
if in, _ := stdin.ReadWithOptions(&o); in != "" {
2121
o.Value = in
2222
}
2323
}
2424

2525
i := textinput.New()
2626
if o.Value != "" {
2727
i.SetValue(o.Value)
28-
} else if in, _ := stdin.ReadStrip(); in != "" {
28+
} else if in, _ := stdin.ReadWithOptions(&o); in != "" {
2929
i.SetValue(in)
3030
}
3131
i.Focus()

input/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ type Options struct {
2222
Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"`
2323
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_INPUT_HEADER_"`
2424
Timeout time.Duration `help:"Timeout until input aborts" default:"0s" env:"GUM_INPUT_TIMEOUT"`
25+
26+
StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_INPUT_STRIP_ANSI_CODES"`
27+
}
28+
29+
func (o *Options) DoStripANSICodes() bool {
30+
return o.StripANSICodes
2531
}

internal/stdin/options.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package stdin
2+
3+
// StdinOptions provide switches for ReadWithOptions, indicating how to read
4+
// input from stdin
5+
type StdinOptions interface {
6+
// DoStripANSICodes returns true if the ANSI codes should be stripped from the
7+
// input.
8+
DoStripANSICodes() bool
9+
}
10+
11+
// ReadWithOptions delegates to the module's Read functions based on the
12+
// provided options instance.
13+
func ReadWithOptions(opts StdinOptions) (string, error) {
14+
if opts.DoStripANSICodes() {
15+
return ReadStrip()
16+
} else {
17+
return Read()
18+
}
19+
}

style/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (o Options) Run() error {
2020
if len(o.Text) > 0 {
2121
text = strings.Join(o.Text, "\n")
2222
} else {
23-
text, _ = stdin.ReadStrip()
23+
text, _ = stdin.ReadWithOptions(&o)
2424
if text == "" {
2525
return errors.New("no input provided, see `gum style --help`")
2626
}

style/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ type Options struct {
55
Text []string `arg:"" optional:"" help:"Text to which to apply the style"`
66
Trim bool `help:"Trim whitespaces on every input line" default:"false"`
77
Style StylesNotHidden `embed:""`
8+
9+
StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_STYLE_STRIP_ANSI_CODES"`
10+
}
11+
12+
func (o *Options) DoStripANSICodes() bool {
13+
return o.StripANSICodes
814
}
915

1016
// Styles is a flag set of possible styles.

write/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// Run provides a shell script interface for the text area bubble.
1818
// https://github.com/charmbracelet/bubbles/textarea
1919
func (o Options) Run() error {
20-
in, _ := stdin.ReadStrip()
20+
in, _ := stdin.ReadWithOptions(&o)
2121
if in != "" && o.Value == "" {
2222
o.Value = strings.ReplaceAll(in, "\r", "")
2323
}

write/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ type Options struct {
3131
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_WRITE_HEADER_"`
3232
PlaceholderStyle style.Styles `embed:"" prefix:"placeholder." set:"defaultForeground=240" envprefix:"GUM_WRITE_PLACEHOLDER_"`
3333
PromptStyle style.Styles `embed:"" prefix:"prompt." set:"defaultForeground=7" envprefix:"GUM_WRITE_PROMPT_"`
34+
35+
StripANSICodes bool `help:"Strip ANSI codes from the input" negatable:"" default:"true" env:"GUM_WRITE_STRIP_ANSI_CODES"`
36+
}
37+
38+
func (o *Options) DoStripANSICodes() bool {
39+
return o.StripANSICodes
3440
}

0 commit comments

Comments
 (0)