Skip to content

Commit

Permalink
feat(filter): --select-if-one returns if single match (#778)
Browse files Browse the repository at this point in the history
closes #311

Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 authored Dec 13, 2024
1 parent 966237b commit 0e501ea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
34 changes: 12 additions & 22 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
"github.com/charmbracelet/gum/internal/files"
"github.com/charmbracelet/gum/internal/stdin"
"github.com/charmbracelet/gum/internal/timeout"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/term"
"github.com/charmbracelet/gum/internal/tty"
"github.com/sahilm/fuzzy"
)

Expand Down Expand Up @@ -44,11 +43,6 @@ func (o Options) Run() error {
return errors.New("no options provided, see `gum filter --help`")
}

if o.SelectIfOne && len(o.Options) == 1 {
fmt.Println(o.Options[0])
return nil
}

ctx, cancel := timeout.Context(o.Timeout)
defer cancel()

Expand All @@ -74,11 +68,16 @@ func (o Options) Run() error {
matches = matchAll(o.Options)
}

km := defaultKeymap()

if o.NoLimit {
o.Limit = len(o.Options)
}

if o.SelectIfOne && len(matches) == 1 {
tty.Println(matches[0].Str)
return nil
}

km := defaultKeymap()
if o.NoLimit || o.Limit > 1 {
km.Toggle.SetEnabled(true)
km.ToggleAndPrevious.SetEnabled(true)
Expand Down Expand Up @@ -138,30 +137,21 @@ func (o Options) Run() error {
if !m.submitted {
return errors.New("nothing selected")
}
isTTY := term.IsTerminal(os.Stdout.Fd())

// allSelections contains values only if limit is greater
// than 1 or if flag --no-limit is passed, hence there is
// no need to further checks
if len(m.selected) > 0 {
o.checkSelected(m, isTTY)
o.checkSelected(m)
} else if len(m.matches) > m.cursor && m.cursor >= 0 {
if isTTY {
fmt.Println(m.matches[m.cursor].Str)
} else {
fmt.Println(ansi.Strip(m.matches[m.cursor].Str))
}
tty.Println(m.matches[m.cursor].Str)
}

return nil
}

func (o Options) checkSelected(m model, isTTY bool) {
func (o Options) checkSelected(m model) {
for k := range m.selected {
if isTTY {
fmt.Println(k)
} else {
fmt.Println(ansi.Strip(k))
}
tty.Println(k)
}
}
24 changes: 24 additions & 0 deletions internal/tty/tty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Package tty provides tty-aware printing.
package tty

import (
"fmt"
"os"
"sync"

"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/term"
)

var isTTY = sync.OnceValue(func() bool {
return term.IsTerminal(os.Stdout.Fd())
})

// Println handles println, striping ansi sequences if stdout is not a tty.
func Println(s string) {
if isTTY() {
fmt.Println(s)
return
}
fmt.Println(ansi.Strip(s))
}

0 comments on commit 0e501ea

Please sign in to comment.