feat: Timeout for Filter Command (#382)

This commit is contained in:
Dieter Eickstaedt 2023-06-30 15:18:02 +02:00 committed by GitHub
parent f8caeef195
commit 6bf79aa899
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 14 deletions

View file

@ -43,6 +43,8 @@ func (o Options) Run() error {
aborted: false, aborted: false,
header: o.Header, header: o.Header,
headerStyle: o.HeaderStyle.ToLipgloss(), headerStyle: o.HeaderStyle.ToLipgloss(),
timeout: o.Timeout,
hasTimeout: o.Timeout > 0,
autoWidth: o.Width < 1, autoWidth: o.Width < 1,
}, tea.WithOutput(os.Stderr)) }, tea.WithOutput(os.Stderr))
tm, err := p.Run() tm, err := p.Run()

View file

@ -8,8 +8,11 @@
package input package input
import ( import (
"time"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/timeout"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
@ -20,14 +23,20 @@ type model struct {
textinput textinput.Model textinput textinput.Model
quitting bool quitting bool
aborted bool aborted bool
timeout time.Duration
hasTimeout bool
} }
func (m model) Init() tea.Cmd { return textinput.Blink } func (m model) Init() tea.Cmd {
return tea.Batch(
textinput.Blink,
timeout.Init(m.timeout, nil),
)
}
func (m model) View() string { func (m model) View() string {
if m.quitting { if m.quitting {
return "" return ""
} }
if m.header != "" { if m.header != "" {
header := m.headerStyle.Render(m.header) header := m.headerStyle.Render(m.header)
return lipgloss.JoinVertical(lipgloss.Left, header, m.textinput.View()) return lipgloss.JoinVertical(lipgloss.Left, header, m.textinput.View())
@ -38,6 +47,14 @@ 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) {
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.autoWidth { if m.autoWidth {
m.textinput.Width = msg.Width - lipgloss.Width(m.textinput.Prompt) - 1 m.textinput.Width = msg.Width - lipgloss.Width(m.textinput.Prompt) - 1

View file

@ -1,6 +1,10 @@
package input package input
import "github.com/charmbracelet/gum/style" import (
"time"
"github.com/charmbracelet/gum/style"
)
// Options are the customization options for the input. // Options are the customization options for the input.
type Options struct { type Options struct {
@ -15,4 +19,5 @@ type Options struct {
Password bool `help:"Mask input characters" default:"false"` Password bool `help:"Mask input characters" default:"false"`
Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"` Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"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:"0" env:"GUM_INPUT_TIMEOUT"`
} }