Skip to content

Commit

Permalink
feat(choose,filter): --input-delimiter --output-delimiter
Browse files Browse the repository at this point in the history
allows to change how content from stdin is used, and how results are
printed.

one could get around it piping into and from `tr`, but results aren't
quite right, especially when `tr '\n' ','` for example, as it'll add an
extra `,` in the end of the string.

This makes things a bit cleaner, hopefully.

closes #274
  • Loading branch information
caarlos0 committed Dec 13, 2024
1 parent 32786f7 commit 105998c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
12 changes: 6 additions & 6 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (o Options) Run() error {
if input == "" {
return errors.New("no options provided, see `gum choose --help`")
}
o.Options = strings.Split(input, "\n")
o.Options = strings.Split(input, o.InputDelimiter)
}

if o.SelectIfOne && len(o.Options) == 1 {
Expand Down Expand Up @@ -146,18 +146,18 @@ func (o Options) Run() error {
return m.items[i].order < m.items[j].order
})
}
var s strings.Builder

var out []string
for _, item := range m.items {
if item.selected {
s.WriteString(item.text)
s.WriteRune('\n')
out = append(out, item.text)
}
}

if term.IsTerminal(os.Stdout.Fd()) {
fmt.Print(s.String())
fmt.Println(strings.Join(out, o.OutputDelimiter))
} else {
fmt.Print(ansi.Strip(s.String()))
fmt.Println(ansi.Strip(strings.Join(out, o.OutputDelimiter)))
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions choose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Options struct {
UnselectedPrefix string `help:"Prefix to show on unselected items (hidden if limit is 1)" default:"• " env:"GUM_CHOOSE_UNSELECTED_PREFIX"`
Selected []string `help:"Options that should start as selected (selects all if given '*')" default:"" env:"GUM_CHOOSE_SELECTED"`
SelectIfOne bool `help:"Select the given option if there is only one" group:"Selection"`
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"`

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
9 changes: 5 additions & 4 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (o Options) Run() error {

if len(o.Options) == 0 {
if input, _ := stdin.ReadStrip(); input != "" {
o.Options = strings.Split(input, "\n")
o.Options = strings.Split(input, o.InputDelimiter)
} else {
o.Options = files.List()
}
Expand Down Expand Up @@ -142,11 +142,12 @@ func (o Options) Run() error {
}

func (o Options) checkSelected(m model, isTTY bool) {
out := []string{}
for k := range m.selected {
if isTTY {
fmt.Println(k)
} else {
fmt.Println(ansi.Strip(k))
k = ansi.Strip(k)
}
out = append(out, k)
}
fmt.Println(strings.Join(out, o.OutputDelimiter))
}
2 changes: 2 additions & 0 deletions filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Options struct {
Fuzzy bool `help:"Enable fuzzy matching; otherwise match from start of word" default:"true" env:"GUM_FILTER_FUZZY" negatable:""`
FuzzySort bool `help:"Sort fuzzy results by their scores" default:"true" env:"GUM_FILTER_FUZZY_SORT" negatable:""`
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"`

// 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

0 comments on commit 105998c

Please sign in to comment.