mirror of
https://github.com/charmbracelet/gum
synced 2024-05-20 15:16:42 +02:00
feat: Timeout for Filter Command (#382)
This commit is contained in:
parent
f8caeef195
commit
6bf79aa899
|
@ -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()
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -1,18 +1,23 @@
|
||||||
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 {
|
||||||
Placeholder string `help:"Placeholder value" default:"Type something..." env:"GUM_INPUT_PLACEHOLDER"`
|
Placeholder string `help:"Placeholder value" default:"Type something..." env:"GUM_INPUT_PLACEHOLDER"`
|
||||||
Prompt string `help:"Prompt to display" default:"> " env:"GUM_INPUT_PROMPT"`
|
Prompt string `help:"Prompt to display" default:"> " env:"GUM_INPUT_PROMPT"`
|
||||||
PromptStyle style.Styles `embed:"" prefix:"prompt." envprefix:"GUM_INPUT_PROMPT_"`
|
PromptStyle style.Styles `embed:"" prefix:"prompt." envprefix:"GUM_INPUT_PROMPT_"`
|
||||||
CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_INPUT_CURSOR_"`
|
CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_INPUT_CURSOR_"`
|
||||||
CursorMode string `prefix:"cursor." name:"mode" help:"Cursor mode" default:"blink" enum:"blink,hide,static" env:"GUM_INPUT_CURSOR_MODE"`
|
CursorMode string `prefix:"cursor." name:"mode" help:"Cursor mode" default:"blink" enum:"blink,hide,static" env:"GUM_INPUT_CURSOR_MODE"`
|
||||||
Value string `help:"Initial value (can also be passed via stdin)" default:""`
|
Value string `help:"Initial value (can also be passed via stdin)" default:""`
|
||||||
CharLimit int `help:"Maximum value length (0 for no limit)" default:"400"`
|
CharLimit int `help:"Maximum value length (0 for no limit)" default:"400"`
|
||||||
Width int `help:"Input width (0 for terminal width)" default:"40" env:"GUM_INPUT_WIDTH"`
|
Width int `help:"Input width (0 for terminal width)" default:"40" env:"GUM_INPUT_WIDTH"`
|
||||||
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"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue