From abae6fd80c802f347cd096484c788c135e5fa807 Mon Sep 17 00:00:00 2001 From: Dieter Eickstaedt Date: Thu, 29 Jun 2023 23:27:25 +0200 Subject: [PATCH] feat: Adding timeout option to Pager command (#381) --- pager/command.go | 2 ++ pager/options.go | 23 ++++++++++++++--------- pager/pager.go | 22 +++++++++++++++++++--- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/pager/command.go b/pager/command.go index b64a41e..69de89e 100644 --- a/pager/command.go +++ b/pager/command.go @@ -41,6 +41,8 @@ func (o Options) Run() error { softWrap: o.SoftWrap, matchStyle: o.MatchStyle.ToLipgloss(), matchHighlightStyle: o.MatchHighlightStyle.ToLipgloss(), + timeout: o.Timeout, + hasTimeout: o.Timeout > 0, } _, err := tea.NewProgram(model, tea.WithAltScreen()).Run() if err != nil { diff --git a/pager/options.go b/pager/options.go index 53b6c7c..f257cb0 100644 --- a/pager/options.go +++ b/pager/options.go @@ -1,16 +1,21 @@ package pager -import "github.com/charmbracelet/gum/style" +import ( + "time" + + "github.com/charmbracelet/gum/style" +) // Options are the options for the pager. type Options struct { //nolint:staticcheck - Style style.Styles `embed:"" help:"Style the pager" set:"defaultBorder=rounded" set:"defaultPadding=0 1" set:"defaultBorderForeground=212" envprefix:"GUM_PAGER_"` - HelpStyle style.Styles `embed:"" prefix:"help." help:"Style the help text" set:"defaultForeground=241" envprefix:"GUM_PAGER_HELP_"` - Content string `arg:"" optional:"" help:"Display content to scroll"` - ShowLineNumbers bool `help:"Show line numbers" default:"true"` - LineNumberStyle style.Styles `embed:"" prefix:"line-number." help:"Style the line numbers" set:"defaultForeground=237" envprefix:"GUM_PAGER_LINE_NUMBER_"` - SoftWrap bool `help:"Soft wrap lines" default:"false"` - MatchStyle style.Styles `embed:"" prefix:"match." help:"Style the matched text" set:"defaultForeground=212" set:"defaultBold=true" envprefix:"GUM_PAGER_MATCH_"` //nolint:staticcheck - MatchHighlightStyle style.Styles `embed:"" prefix:"match-highlight." help:"Style the matched highlight text" set:"defaultForeground=235" set:"defaultBackground=225" set:"defaultBold=true" envprefix:"GUM_PAGER_MATCH_HIGH_"` //nolint:staticcheck + Style style.Styles `embed:"" help:"Style the pager" set:"defaultBorder=rounded" set:"defaultPadding=0 1" set:"defaultBorderForeground=212" envprefix:"GUM_PAGER_"` + HelpStyle style.Styles `embed:"" prefix:"help." help:"Style the help text" set:"defaultForeground=241" envprefix:"GUM_PAGER_HELP_"` + Content string `arg:"" optional:"" help:"Display content to scroll"` + ShowLineNumbers bool `help:"Show line numbers" default:"true"` + LineNumberStyle style.Styles `embed:"" prefix:"line-number." help:"Style the line numbers" set:"defaultForeground=237" envprefix:"GUM_PAGER_LINE_NUMBER_"` + SoftWrap bool `help:"Soft wrap lines" default:"false"` + MatchStyle style.Styles `embed:"" prefix:"match." help:"Style the matched text" set:"defaultForeground=212" set:"defaultBold=true" envprefix:"GUM_PAGER_MATCH_"` //nolint:staticcheck + MatchHighlightStyle style.Styles `embed:"" prefix:"match-highlight." help:"Style the matched highlight text" set:"defaultForeground=235" set:"defaultBackground=225" set:"defaultBold=true" envprefix:"GUM_PAGER_MATCH_HIGH_"` //nolint:staticcheck + Timeout time.Duration `help:"Timeout until command exits" default:"0" env:"GUM_PAGER_TIMEOUT"` } diff --git a/pager/pager.go b/pager/pager.go index a3eed04..ac8c3a2 100644 --- a/pager/pager.go +++ b/pager/pager.go @@ -6,6 +6,9 @@ package pager import ( "fmt" "strings" + "time" + + "github.com/charmbracelet/gum/timeout" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" @@ -25,14 +28,23 @@ type model struct { matchStyle lipgloss.Style matchHighlightStyle lipgloss.Style maxWidth int + timeout time.Duration + hasTimeout bool } func (m model) Init() tea.Cmd { - return nil + return timeout.Init(m.timeout, nil) } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case timeout.TickTimeoutMsg: + if msg.TimeoutValue <= 0 { + return m, tea.Quit + } + m.timeout = msg.TimeoutValue + return m, timeout.Tick(msg.TimeoutValue, msg.Data) + case tea.WindowSizeMsg: m.ProcessText(msg) case tea.KeyMsg: @@ -130,13 +142,17 @@ func (m model) KeyHandler(key tea.KeyMsg) (model, func() tea.Msg) { } func (m model) View() string { - helpMsg := "\n ↑/↓: Navigate • q: Quit • /: Search " + var timeoutStr string + if m.hasTimeout { + timeoutStr = timeout.Str(m.timeout) + " " + } + helpMsg := "\n"+timeoutStr+" ↑/↓: Navigate • q: Quit • /: Search " if m.search.query != nil { helpMsg += "• n: Next Match " helpMsg += "• N: Prev Match " } if m.search.active { - return m.viewport.View() + "\n " + m.search.input.View() + return m.viewport.View() + "\n"+timeoutStr+ " "+ m.search.input.View() } return m.viewport.View() + m.helpStyle.Render(helpMsg)