feat: --no-strip-ansi (#784)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Carlos Alexandro Becker · committed by GitHub · parent 2e321f5 · Browse files · 13 files·+62 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
- Co-authored-by: Sridaran Thoniyil <sri7thon@gmail.com>
Diff stats
13 files+62 added−19 removed
- other:+62 added−19 removed
Provenance
- Commit
4cedf9f- Tree
6f8835a- Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
2e321f5
13 files changed+62 added−19 removed
modified:choose/command.go+1 added-1 removed
25 unmodified lines26 verySubduedStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#DDDADA", Dark: "#3C3C3C"})27 )28 29 if len(o.Options) <= 0 {30- input, _ := stdin.ReadStrip()30+ input, _ := stdin.Read(stdin.StripANSI(o.StripANSI))31 if input == "" {32 return errors.New("no options provided, see `gum choose --help`")33 }34 o.Options = strings.Split(input, o.InputDelimiter)141 unmodified linesmodified:choose/options.go+1 added
23 unmodified lines24 SelectIfOne bool `help:"Select the given option if there is only one" group:"Selection"`25 InputDelimiter string `help:"Option delimiter when reading from STDIN" default:"\n" env:"GUM_CHOOSE_INPUT_DELIMITER"`26 OutputDelimiter string `help:"Option delimiter when writing to STDOUT" default:"\n" env:"GUM_CHOOSE_OUTPUT_DELIMITER"`27 LabelDelimiter string `help:"Allows to set a delimiter, so options can be set as label:value" default:"" env:"GUM_CHOOSE_LABEL_DELIMITER"`28+ StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_CHOOSE_STRIP_ANSI"`29 30 CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_CURSOR_"`31 HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=99" envprefix:"GUM_CHOOSE_HEADER_"`32 ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`2 unmodified linesmodified:filter/command.go+1 added-1 removed
30 unmodified lines31 32 v := viewport.New(o.Width, o.Height)33 34 if len(o.Options) == 0 {35- if input, _ := stdin.ReadStrip(); input != "" {35+ if input, _ := stdin.Read(stdin.StripANSI(o.StripANSI)); input != "" {36 o.Options = strings.Split(input, o.InputDelimiter)37 } else {38 o.Options = files.List()39 }120 unmodified linesmodified:filter/options.go+1 added
38 unmodified lines39 FuzzySort bool `help:"Sort fuzzy results by their scores" default:"true" env:"GUM_FILTER_FUZZY_SORT" negatable:""`40 Timeout time.Duration `help:"Timeout until filter command aborts" default:"0s" env:"GUM_FILTER_TIMEOUT"`41 InputDelimiter string `help:"Option delimiter when reading from STDIN" default:"\n" env:"GUM_FILTER_INPUT_DELIMITER"`42 OutputDelimiter string `help:"Option delimiter when writing to STDOUT" default:"\n" env:"GUM_FILTER_OUTPUT_DELIMITER"`43+ StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_FILTER_STRIP_ANSI"`44 45 // Deprecated: use [FuzzySort]. This will be removed at some point.46 Sort bool `help:"Sort fuzzy results by their scores" default:"true" env:"GUM_FILTER_FUZZY_SORT" negatable:"" hidden:""`47 }modified:format/command.go+1 added-1 removed
22 unmodified lines23 var err error24 if len(o.Template) > 0 {25 input = strings.Join(o.Template, "\n")26 } else {27- input, _ = stdin.ReadStrip()27+ input, _ = stdin.Read(stdin.StripANSI(o.StripANSI))28 }29 30 switch o.Type {31 case "code":15 unmodified linesmodified:format/options.go+2 added
4 unmodified lines5 Template []string `arg:"" optional:"" help:"Template string to format (can also be provided via stdin)"`6 Theme string `help:"Glamour theme to use for markdown formatting" default:"pink" env:"GUM_FORMAT_THEME"`7 Language string `help:"Programming language to parse code" short:"l" default:"" env:"GUM_FORMAT_LANGUAGE"`8 9+ StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_FORMAT_STRIP_ANSI"`10+11 Type string `help:"Format to use (markdown,template,code,emoji)" enum:"markdown,template,code,emoji" short:"t" default:"markdown" env:"GUM_FORMAT_TYPE"`12 }modified:input/command.go+2 added-2 removed
15 unmodified lines16 // Run provides a shell script interface for the text input bubble.17 // https://github.com/charmbracelet/bubbles/textinput18 func (o Options) Run() error {19 if o.Value == "" {20- if in, _ := stdin.ReadStrip(); in != "" {20+ if in, _ := stdin.Read(stdin.StripANSI(o.StripANSI)); in != "" {21 o.Value = in22 }23 }24 25 i := textinput.New()26 if o.Value != "" {27 i.SetValue(o.Value)28- } else if in, _ := stdin.ReadStrip(); in != "" {28+ } else if in, _ := stdin.Read(stdin.StripANSI(o.StripANSI)); in != "" {29 i.SetValue(in)30 }31 i.Focus()32 i.Prompt = o.Prompt44 unmodified linesmodified:input/options.go+1 added
20 unmodified lines21 ShowHelp bool `help:"Show help keybinds" default:"true" negatable:"" env:"GUM_INPUT_SHOW_HELP"`22 Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"`23 HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_INPUT_HEADER_"`24 Timeout time.Duration `help:"Timeout until input aborts" default:"0s" env:"GUM_INPUT_TIMEOUT"`25+ StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_INPUT_STRIP_ANSI"`26 }modified:internal/stdin/stdin.go+45 added-9 removed
8 unmodified lines9 10 "github.com/charmbracelet/x/ansi"11 )12 13+type options struct {14+ ansiStrip bool15+ singleLine bool16+}17+18+// Option is a read option.19+type Option func(*options)20+21+// StripANSI optionally strips ansi sequences.22+func StripANSI(b bool) Option {23+ return func(o *options) {24+ o.ansiStrip = b25+ }26+}27+28+// SingleLine reads a single line.29+func SingleLine(b bool) Option {30+ return func(o *options) {31+ o.singleLine = b32+ }33+}34+35 // Read reads input from an stdin pipe.14-func Read() (string, error) {36+func Read(opts ...Option) (string, error) {37 if IsEmpty() {38 return "", fmt.Errorf("stdin is empty")39 }40 41+ options := options{}42+ for _, opt := range opts {43+ opt(&options)44+ }45+46 reader := bufio.NewReader(os.Stdin)47 var b strings.Builder48 22- for {49+ if options.singleLine {50+ line, _, err := reader.ReadLine()51+ if err != nil {52+ return "", fmt.Errorf("failed to read line: %w", err)53+ }54+ _, err = b.Write(line)55+ if err != nil {56+ return "", fmt.Errorf("failed to write: %w", err)57+ }58+ }59+60+ for !options.singleLine {61 r, _, err := reader.ReadRune()62 if err != nil && err == io.EOF {63 break64 }2 unmodified lines67 return "", fmt.Errorf("failed to write rune: %w", err)68 }69 }70 33- return strings.TrimSpace(b.String()), nil71+ s := strings.TrimSpace(b.String())72+ if options.ansiStrip {73+ return ansi.Strip(s), nil74+ }75+ return s, nil76 }77 36-// ReadStrip reads input from an stdin pipe and strips ansi sequences.37-func ReadStrip() (string, error) {38- s, err := Read()39- return ansi.Strip(s), err40-}41-78 // IsEmpty returns whether stdin is empty.79 func IsEmpty() bool {80 stat, err := os.Stdin.Stat()81 if err != nil {9 unmodified linesmodified:style/command.go+1 added-1 removed
18 unmodified lines19 var text string20 if len(o.Text) > 0 {21 text = strings.Join(o.Text, "\n")22 } else {23- text, _ = stdin.ReadStrip()23+ text, _ = stdin.Read(stdin.StripANSI(o.StripANSI))24 if text == "" {25 return errors.New("no input provided, see `gum style --help`")26 }27 }10 unmodified linesmodified:style/options.go+4 added-3 removed
1 package style2 3 // Options is the customization options for the style command.4 type Options struct {5- Text []string `arg:"" optional:"" help:"Text to which to apply the style"`6- Trim bool `help:"Trim whitespaces on every input line" default:"false"`7- Style StylesNotHidden `embed:""`5+ Text []string `arg:"" optional:"" help:"Text to which to apply the style"`6+ Trim bool `help:"Trim whitespaces on every input line" default:"false"`7+ StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_STYLE_STRIP_ANSI"`8+ Style StylesNotHidden `embed:""`9 }10 11 // Styles is a flag set of possible styles.12 //58 unmodified linesmodified:write/command.go+1 added-1 removed
15 unmodified lines16 17 // Run provides a shell script interface for the text area bubble.18 // https://github.com/charmbracelet/bubbles/textarea19 func (o Options) Run() error {20- in, _ := stdin.ReadStrip()20+ in, _ := stdin.Read(stdin.StripANSI(o.StripANSI))21 if in != "" && o.Value == "" {22 o.Value = strings.ReplaceAll(in, "\r", "")23 }24 58 unmodified linesmodified:write/options.go+1 added
19 unmodified lines20 MaxLines int `help:"Maximum number of lines (0 for no limit)" default:"0"`21 ShowHelp bool `help:"Show help key binds" negatable:"" default:"true" env:"GUM_WRITE_SHOW_HELP"`22 CursorMode string `prefix:"cursor." name:"mode" help:"Cursor mode" default:"blink" enum:"blink,hide,static" env:"GUM_WRITE_CURSOR_MODE"`23 Timeout time.Duration `help:"Timeout until choose returns selected element" default:"0s" env:"GUM_WRITE_TIMEOUT"`24+ StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_WRITE_STRIP_ANSI"`25 26 BaseStyle style.Styles `embed:"" prefix:"base." envprefix:"GUM_WRITE_BASE_"`27 CursorLineNumberStyle style.Styles `embed:"" prefix:"cursor-line-number." set:"defaultForeground=7" envprefix:"GUM_WRITE_CURSOR_LINE_NUMBER_"`28 CursorLineStyle style.Styles `embed:"" prefix:"cursor-line." envprefix:"GUM_WRITE_CURSOR_LINE_"`7 unmodified lines