diff --git a/input/command.go b/input/command.go index 4d3edb2..4094ddb 100644 --- a/input/command.go +++ b/input/command.go @@ -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() diff --git a/input/input.go b/input/input.go index 0ad2e54..9db2306 100644 --- a/input/input.go +++ b/input/input.go @@ -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" ) @@ -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()) @@ -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 diff --git a/input/options.go b/input/options.go index 77cc683..82ba508 100644 --- a/input/options.go +++ b/input/options.go @@ -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"` }