Skip to content

Commit

Permalink
feat: Timeout for Filter Command (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
deicon authored Jun 30, 2023
1 parent f8caeef commit 6bf79aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
2 changes: 2 additions & 0 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (o Options) Run() error {
aborted: false,
header: o.Header,
headerStyle: o.HeaderStyle.ToLipgloss(),
timeout: o.Timeout,
hasTimeout: o.Timeout > 0,
autoWidth: o.Width < 1,
}, tea.WithOutput(os.Stderr))
tm, err := p.Run()
Expand Down
21 changes: 19 additions & 2 deletions input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
package input

import (
"time"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/timeout"
"github.com/charmbracelet/lipgloss"
)

Expand All @@ -20,14 +23,20 @@ type model struct {
textinput textinput.Model
quitting 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 {
if m.quitting {
return ""
}

if m.header != "" {
header := m.headerStyle.Render(m.header)
return lipgloss.JoinVertical(lipgloss.Left, header, m.textinput.View())
Expand All @@ -38,6 +47,14 @@ func (m model) View() string {

func (m model) Update(msg tea.Msg) (tea.Model, 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.autoWidth {
m.textinput.Width = msg.Width - lipgloss.Width(m.textinput.Prompt) - 1
Expand Down
29 changes: 17 additions & 12 deletions input/options.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package input

import "github.com/charmbracelet/gum/style"
import (
"time"

"github.com/charmbracelet/gum/style"
)

// Options are the customization options for the input.
type Options struct {
Placeholder string `help:"Placeholder value" default:"Type something..." env:"GUM_INPUT_PLACEHOLDER"`
Prompt string `help:"Prompt to display" default:"> " env:"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_"`
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:""`
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"`
Password bool `help:"Mask input characters" default:"false"`
Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_INPUT_HEADER_"`
Placeholder string `help:"Placeholder value" default:"Type something..." env:"GUM_INPUT_PLACEHOLDER"`
Prompt string `help:"Prompt to display" default:"> " env:"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_"`
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:""`
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"`
Password bool `help:"Mask input characters" default:"false"`
Header string `help:"Header value" default:"" env:"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"`
}

0 comments on commit 6bf79aa

Please sign in to comment.