feat: Adding timeout option to Filter command (#380)

This commit is contained in:
Dieter Eickstaedt 2023-06-29 23:29:46 +02:00 committed by GitHub
parent abae6fd80c
commit 7e71c4d664
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 32 deletions

View file

@ -90,6 +90,8 @@ func (o Options) Run() error {
limit: o.Limit,
reverse: o.Reverse,
fuzzy: o.Fuzzy,
timeout: o.Timeout,
hasTimeout: o.Timeout > 0,
sort: o.Sort,
}, options...)
@ -108,13 +110,7 @@ func (o Options) Run() error {
// than 1 or if flag --no-limit is passed, hence there is
// no need to further checks
if len(m.selected) > 0 {
for k := range m.selected {
if isTTY {
fmt.Println(k)
} else {
fmt.Println(ansi.Strip(k))
}
}
o.checkSelected(m, isTTY)
} else if len(m.matches) > m.cursor && m.cursor >= 0 {
if isTTY {
fmt.Println(m.matches[m.cursor].Str)
@ -129,6 +125,16 @@ func (o Options) Run() error {
return nil
}
func (o Options) checkSelected(m model, isTTY bool) {
for k := range m.selected {
if isTTY {
fmt.Println(k)
} else {
fmt.Println(ansi.Strip(k))
}
}
}
// BeforeReset hook. Used to unclutter style flags.
func (o Options) BeforeReset(ctx *kong.Context) error {
style.HideFlags(ctx)

View file

@ -12,6 +12,9 @@ package filter
import (
"strings"
"time"
"github.com/charmbracelet/gum/timeout"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
@ -46,9 +49,13 @@ type model struct {
reverse bool
fuzzy bool
sort bool
timeout time.Duration
hasTimeout bool
}
func (m model) Init() tea.Cmd { return nil }
func (m model) Init() tea.Cmd {
return timeout.Init(m.timeout, nil)
}
func (m model) View() string {
if m.quitting {
return ""
@ -149,6 +156,15 @@ func (m model) View() string {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 {
m.quitting = true
m.aborted = true
return m, tea.Quit
}
m.timeout = msg.TimeoutValue
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
case tea.WindowSizeMsg:
if m.height == 0 || m.height > msg.Height {
m.viewport.Height = msg.Height - lipgloss.Height(m.textinput.View())

View file

@ -1,6 +1,10 @@
package filter
import "github.com/charmbracelet/gum/style"
import (
"time"
"github.com/charmbracelet/gum/style"
)
// Options is the customization options for the filter command.
type Options struct {
@ -27,4 +31,5 @@ type Options struct {
Reverse bool `help:"Display from the bottom of the screen" env:"GUM_FILTER_REVERSE"`
Fuzzy bool `help:"Enable fuzzy matching" default:"true" env:"GUM_FILTER_FUZZY" negatable:""`
Sort bool `help:"Sort the results" default:"true" env:"GUM_FILTER_SORT" negatable:""`
Timeout time.Duration `help:"Timeout until filter command aborts" default:"0" env:"GUM_FILTER_TIMEOUT"`
}