Skip to content
Myr

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

13 files+62 added−19 removed

modified:choose/command.go+1 added-1 removed
25 unmodified lines
26 verySubduedStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#DDDADA", Dark: "#3C3C3C"})27 )2829 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 lines
modified:choose/options.go+1 added
23 unmodified lines
24 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"`2930 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 lines
modified:filter/command.go+1 added-1 removed
30 unmodified lines
3132 v := viewport.New(o.Width, o.Height)3334 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 lines
modified:filter/options.go+1 added
38 unmodified lines
39 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"`4445 // 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 lines
23 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 }2930 switch o.Type {31 case "code":
15 unmodified lines
modified:format/options.go+2 added
4 unmodified lines
5 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"`89 StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_FORMAT_STRIP_ANSI"`1011 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 lines
16// Run provides a shell script interface for the text input bubble.17// https://github.com/charmbracelet/bubbles/textinput18func (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 }2425 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.Prompt
44 unmodified lines
modified:input/options.go+1 added
20 unmodified lines
21 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 lines
910 "github.com/charmbracelet/x/ansi"11)1213type options struct {14 ansiStrip bool15 singleLine bool16}1718// Option is a read option.19type Option func(*options)2021// StripANSI optionally strips ansi sequences.22func StripANSI(b bool) Option {23 return func(o *options) {24 o.ansiStrip = b25 }26}2728// SingleLine reads a single line.29func SingleLine(b bool) Option {30 return func(o *options) {31 o.singleLine = b32 }33}3435// Read reads input from an stdin pipe.14func Read() (string, error) {36func Read(opts ...Option) (string, error) {37 if IsEmpty() {38 return "", fmt.Errorf("stdin is empty")39 }4041 options := options{}42 for _, opt := range opts {43 opt(&options)44 }4546 reader := bufio.NewReader(os.Stdin)47 var b strings.Builder4822 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 }5960 for !options.singleLine {61 r, _, err := reader.ReadRune()62 if err != nil && err == io.EOF {63 break64 }
2 unmodified lines
67 return "", fmt.Errorf("failed to write rune: %w", err)68 }69 }7033 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}7736// ReadStrip reads input from an stdin pipe and strips ansi sequences.37func ReadStrip() (string, error) {38 s, err := Read()39 return ansi.Strip(s), err40}4178// IsEmpty returns whether stdin is empty.79func IsEmpty() bool {80 stat, err := os.Stdin.Stat()81 if err != nil {
9 unmodified lines
modified:style/command.go+1 added-1 removed
18 unmodified lines
19 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 lines
modified:style/options.go+4 added-3 removed
1package style23// Options is the customization options for the style command.4type 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}1011// Styles is a flag set of possible styles.12//
58 unmodified lines
modified:write/command.go+1 added-1 removed
15 unmodified lines
1617// Run provides a shell script interface for the text area bubble.18// https://github.com/charmbracelet/bubbles/textarea19func (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 lines
modified:write/options.go+1 added
19 unmodified lines
20 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"`2526 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