Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: --no-strip-ansi #784

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (o Options) Run() error {
)

if len(o.Options) <= 0 {
input, _ := stdin.ReadStrip()
input, _ := stdin.Read(stdin.StripANSI(o.StripANSI))
if input == "" {
return errors.New("no options provided, see `gum choose --help`")
}
Expand Down
1 change: 1 addition & 0 deletions choose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Options struct {
InputDelimiter string `help:"Option delimiter when reading from STDIN" default:"\n" env:"GUM_CHOOSE_INPUT_DELIMITER"`
OutputDelimiter string `help:"Option delimiter when writing to STDOUT" default:"\n" env:"GUM_CHOOSE_OUTPUT_DELIMITER"`
LabelDelimiter string `help:"Allows to set a delimiter, so options can be set as label:value" default:"" env:"GUM_CHOOSE_LABEL_DELIMITER"`
StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_CHOOSE_STRIP_ANSI"`

CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_CURSOR_"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=99" envprefix:"GUM_CHOOSE_HEADER_"`
Expand Down
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.Read(stdin.StripANSI(o.StripANSI)); input != "" {
o.Options = strings.Split(input, o.InputDelimiter)
} else {
o.Options = files.List()
Expand Down
1 change: 1 addition & 0 deletions filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Options struct {
Timeout time.Duration `help:"Timeout until filter command aborts" default:"0s" env:"GUM_FILTER_TIMEOUT"`
InputDelimiter string `help:"Option delimiter when reading from STDIN" default:"\n" env:"GUM_FILTER_INPUT_DELIMITER"`
OutputDelimiter string `help:"Option delimiter when writing to STDOUT" default:"\n" env:"GUM_FILTER_OUTPUT_DELIMITER"`
StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_FILTER_STRIP_ANSI"`

// 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:""`
Expand Down
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.Read(stdin.StripANSI(o.StripANSI))
}

switch o.Type {
Expand Down
2 changes: 2 additions & 0 deletions format/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ type Options struct {
Theme string `help:"Glamour theme to use for markdown formatting" default:"pink" env:"GUM_FORMAT_THEME"`
Language string `help:"Programming language to parse code" short:"l" default:"" env:"GUM_FORMAT_LANGUAGE"`

StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_FORMAT_STRIP_ANSI"`

Type string `help:"Format to use (markdown,template,code,emoji)" enum:"markdown,template,code,emoji" short:"t" default:"markdown" env:"GUM_FORMAT_TYPE"`
}
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.Read(stdin.StripANSI(o.StripANSI)); in != "" {
o.Value = in
}
}

i := textinput.New()
if o.Value != "" {
i.SetValue(o.Value)
} else if in, _ := stdin.ReadStrip(); in != "" {
} else if in, _ := stdin.Read(stdin.StripANSI(o.StripANSI)); in != "" {
i.SetValue(in)
}
i.Focus()
Expand Down
1 change: 1 addition & 0 deletions input/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ 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"`
StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_INPUT_STRIP_ANSI"`
}
54 changes: 45 additions & 9 deletions internal/stdin/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,54 @@ import (
"github.com/charmbracelet/x/ansi"
)

type options struct {
ansiStrip bool
singleLine bool
}

// Option is a read option.
type Option func(*options)

// StripANSI optionally strips ansi sequences.
func StripANSI(b bool) Option {
return func(o *options) {
o.ansiStrip = b
}
}

// SingleLine reads a single line.
func SingleLine(b bool) Option {
return func(o *options) {
o.singleLine = b
}
}

// Read reads input from an stdin pipe.
func Read() (string, error) {
func Read(opts ...Option) (string, error) {
if IsEmpty() {
return "", fmt.Errorf("stdin is empty")
}

options := options{}
for _, opt := range opts {
opt(&options)
}

reader := bufio.NewReader(os.Stdin)
var b strings.Builder

for {
if options.singleLine {
line, _, err := reader.ReadLine()
if err != nil {
return "", fmt.Errorf("failed to read line: %w", err)
}
_, err = b.Write(line)
if err != nil {
return "", fmt.Errorf("failed to write: %w", err)
}
}

for !options.singleLine {
r, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
Expand All @@ -30,13 +68,11 @@ func Read() (string, error) {
}
}

return strings.TrimSpace(b.String()), nil
}

// ReadStrip reads input from an stdin pipe and strips ansi sequences.
func ReadStrip() (string, error) {
s, err := Read()
return ansi.Strip(s), err
s := strings.TrimSpace(b.String())
if options.ansiStrip {
return ansi.Strip(s), nil
}
return s, nil
}

// IsEmpty returns whether stdin is empty.
Expand Down
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.Read(stdin.StripANSI(o.StripANSI))
if text == "" {
return errors.New("no input provided, see `gum style --help`")
}
Expand Down
7 changes: 4 additions & 3 deletions style/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package style

// Options is the customization options for the style command.
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:""`
Text []string `arg:"" optional:"" help:"Text to which to apply the style"`
Trim bool `help:"Trim whitespaces on every input line" default:"false"`
StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_STYLE_STRIP_ANSI"`
Style StylesNotHidden `embed:""`
}

// 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.Read(stdin.StripANSI(o.StripANSI))
if in != "" && o.Value == "" {
o.Value = strings.ReplaceAll(in, "\r", "")
}
Expand Down
1 change: 1 addition & 0 deletions write/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Options struct {
ShowHelp bool `help:"Show help key binds" negatable:"" default:"true" env:"GUM_WRITE_SHOW_HELP"`
CursorMode string `prefix:"cursor." name:"mode" help:"Cursor mode" default:"blink" enum:"blink,hide,static" env:"GUM_WRITE_CURSOR_MODE"`
Timeout time.Duration `help:"Timeout until choose returns selected element" default:"0s" env:"GUM_WRITE_TIMEOUT"`
StripANSI bool `help:"Strip ANSI sequences when reading from STDIN" default:"true" negatable:"" env:"GUM_WRITE_STRIP_ANSI"`

BaseStyle style.Styles `embed:"" prefix:"base." envprefix:"GUM_WRITE_BASE_"`
CursorLineNumberStyle style.Styles `embed:"" prefix:"cursor-line-number." set:"defaultForeground=7" envprefix:"GUM_WRITE_CURSOR_LINE_NUMBER_"`
Expand Down
Loading