mirror of
https://github.com/charmbracelet/gum
synced 2024-06-29 02:30:07 +02:00
feat: Adding timeout option to Filter command
This commit is contained in:
parent
ae1da5d329
commit
73e5a76f12
|
@ -90,6 +90,8 @@ func (o Options) Run() error {
|
||||||
limit: o.Limit,
|
limit: o.Limit,
|
||||||
reverse: o.Reverse,
|
reverse: o.Reverse,
|
||||||
fuzzy: o.Fuzzy,
|
fuzzy: o.Fuzzy,
|
||||||
|
timeout: o.Timeout,
|
||||||
|
hasTimeout: o.Timeout > 0,
|
||||||
sort: o.Sort,
|
sort: o.Sort,
|
||||||
}, options...)
|
}, options...)
|
||||||
|
|
||||||
|
@ -108,13 +110,7 @@ func (o Options) Run() error {
|
||||||
// than 1 or if flag --no-limit is passed, hence there is
|
// than 1 or if flag --no-limit is passed, hence there is
|
||||||
// no need to further checks
|
// no need to further checks
|
||||||
if len(m.selected) > 0 {
|
if len(m.selected) > 0 {
|
||||||
for k := range m.selected {
|
o.checkSelected(m, isTTY)
|
||||||
if isTTY {
|
|
||||||
fmt.Println(k)
|
|
||||||
} else {
|
|
||||||
fmt.Println(ansi.Strip(k))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if len(m.matches) > m.cursor && m.cursor >= 0 {
|
} else if len(m.matches) > m.cursor && m.cursor >= 0 {
|
||||||
if isTTY {
|
if isTTY {
|
||||||
fmt.Println(m.matches[m.cursor].Str)
|
fmt.Println(m.matches[m.cursor].Str)
|
||||||
|
@ -129,6 +125,16 @@ func (o Options) Run() error {
|
||||||
return nil
|
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.
|
// BeforeReset hook. Used to unclutter style flags.
|
||||||
func (o Options) BeforeReset(ctx *kong.Context) error {
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
||||||
style.HideFlags(ctx)
|
style.HideFlags(ctx)
|
||||||
|
|
|
@ -12,6 +12,9 @@ package filter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/gum/timeout"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
|
@ -46,9 +49,13 @@ type model struct {
|
||||||
reverse bool
|
reverse bool
|
||||||
fuzzy bool
|
fuzzy bool
|
||||||
sort 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 {
|
func (m model) View() string {
|
||||||
if m.quitting {
|
if m.quitting {
|
||||||
return ""
|
return ""
|
||||||
|
@ -149,6 +156,15 @@ func (m model) View() string {
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
switch msg := msg.(type) {
|
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:
|
case tea.WindowSizeMsg:
|
||||||
if m.height == 0 || m.height > msg.Height {
|
if m.height == 0 || m.height > msg.Height {
|
||||||
m.viewport.Height = msg.Height - lipgloss.Height(m.textinput.View())
|
m.viewport.Height = msg.Height - lipgloss.Height(m.textinput.View())
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package filter
|
package filter
|
||||||
|
|
||||||
import "github.com/charmbracelet/gum/style"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/gum/style"
|
||||||
|
)
|
||||||
|
|
||||||
// Options is the customization options for the filter command.
|
// Options is the customization options for the filter command.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
|
@ -27,4 +31,5 @@ type Options struct {
|
||||||
Reverse bool `help:"Display from the bottom of the screen" env:"GUM_FILTER_REVERSE"`
|
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:""`
|
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:""`
|
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"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue