feat: Timeout for Confirm Command (#383)

* feat: Timeout for Confirm Command

* fix: comment

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
This commit is contained in:
Dieter Eickstaedt 2023-06-30 15:20:33 +02:00 committed by GitHub
parent 6bf79aa899
commit eef6431d7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 50 deletions

View file

@ -4,10 +4,11 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/charmbracelet/gum/internal/exit"
"github.com/alecthomas/kong" "github.com/alecthomas/kong"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/gum/style" "github.com/charmbracelet/gum/style"
) )

View file

@ -11,9 +11,10 @@
package confirm package confirm
import ( import (
"fmt"
"time" "time"
"github.com/charmbracelet/gum/timeout"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
@ -37,21 +38,8 @@ type model struct {
unselectedStyle lipgloss.Style unselectedStyle lipgloss.Style
} }
const tickInterval = time.Second
type tickMsg struct{}
func tick() tea.Cmd {
return tea.Tick(tickInterval, func(time.Time) tea.Msg {
return tickMsg{}
})
}
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
if m.timeout > 0 { return timeout.Init(m.timeout, m.defaultSelection)
return tick()
}
return nil
} }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -61,6 +49,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c": case "ctrl+c":
m.confirmation = false
m.aborted = true m.aborted = true
fallthrough fallthrough
case "esc": case "esc":
@ -85,14 +74,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.confirmation = true m.confirmation = true
return m, tea.Quit return m, tea.Quit
} }
case tickMsg: case timeout.TickTimeoutMsg:
if m.timeout <= 0 {
if msg.TimeoutValue <= 0 {
m.quitting = true m.quitting = true
m.confirmation = m.defaultSelection m.confirmation = m.defaultSelection
return m, tea.Quit return m, tea.Quit
} }
m.timeout -= tickInterval
return m, tick() m.timeout = msg.TimeoutValue
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
} }
return m, nil return m, nil
} }
@ -102,27 +93,23 @@ func (m model) View() string {
return "" return ""
} }
var aff, neg, timeout, affirmativeTimeout, negativeTimeout string var aff, neg, timeoutStrYes, timeoutStrNo string
timeoutStrNo = ""
timeoutStrYes = ""
if m.hasTimeout { if m.hasTimeout {
timeout = fmt.Sprintf(" (%d)", max(0, int(m.timeout.Seconds()))) if m.defaultSelection {
} timeoutStrYes = timeout.Str(m.timeout)
} else {
// set timer based on defaultSelection timeoutStrNo = timeout.Str(m.timeout)
if m.defaultSelection { }
affirmativeTimeout = m.affirmative + timeout
negativeTimeout = m.negative
} else {
affirmativeTimeout = m.affirmative
negativeTimeout = m.negative + timeout
} }
if m.confirmation { if m.confirmation {
aff = m.selectedStyle.Render(affirmativeTimeout) aff = m.selectedStyle.Render(m.affirmative + timeoutStrYes)
neg = m.unselectedStyle.Render(negativeTimeout) neg = m.unselectedStyle.Render(m.negative + timeoutStrNo)
} else { } else {
aff = m.unselectedStyle.Render(affirmativeTimeout) aff = m.unselectedStyle.Render(m.affirmative + timeoutStrYes)
neg = m.selectedStyle.Render(negativeTimeout) neg = m.selectedStyle.Render(m.negative + timeoutStrNo)
} }
// If the option is intentionally empty, do not show it. // If the option is intentionally empty, do not show it.
@ -132,10 +119,3 @@ func (m model) View() string {
return lipgloss.JoinVertical(lipgloss.Center, m.promptStyle.Render(m.prompt), lipgloss.JoinHorizontal(lipgloss.Left, aff, neg)) return lipgloss.JoinVertical(lipgloss.Center, m.promptStyle.Render(m.prompt), lipgloss.JoinHorizontal(lipgloss.Left, aff, neg))
} }
func max(a, b int) int {
if a > b {
return a
}
return b
}

View file

@ -8,14 +8,14 @@ import (
// Options is the customization options for the confirm command. // Options is the customization options for the confirm command.
type Options struct { type Options struct {
Affirmative string `help:"The title of the affirmative action" default:"Yes"` Default bool `help:"Default confirmation action" default:"true"`
Negative string `help:"The title of the negative action" default:"No"` Affirmative string `help:"The title of the affirmative action" default:"Yes"`
Default bool `help:"Default confirmation action" default:"true"` Negative string `help:"The title of the negative action" default:"No"`
Timeout time.Duration `help:"Timeout for confirmation" default:"0" env:"GUM_CONFIRM_TIMEOUT"` Prompt string `arg:"" help:"Prompt to display." default:"Are you sure?"`
Prompt string `arg:"" help:"Prompt to display." default:"Are you sure?"` PromptStyle style.Styles `embed:"" prefix:"prompt." help:"The style of the prompt" set:"defaultMargin=1 0 0 0" envprefix:"GUM_CONFIRM_PROMPT_"`
PromptStyle style.Styles `embed:"" prefix:"prompt." help:"The style of the prompt" set:"defaultMargin=1 0 0 0" envprefix:"GUM_CONFIRM_PROMPT_"`
//nolint:staticcheck //nolint:staticcheck
SelectedStyle style.Styles `embed:"" prefix:"selected." help:"The style of the selected action" set:"defaultBackground=212" set:"defaultForeground=230" set:"defaultPadding=0 3" set:"defaultMargin=1 1" envprefix:"GUM_CONFIRM_SELECTED_"` SelectedStyle style.Styles `embed:"" prefix:"selected." help:"The style of the selected action" set:"defaultBackground=212" set:"defaultForeground=230" set:"defaultPadding=0 3" set:"defaultMargin=1 1" envprefix:"GUM_CONFIRM_SELECTED_"`
//nolint:staticcheck //nolint:staticcheck
UnselectedStyle style.Styles `embed:"" prefix:"unselected." help:"The style of the unselected action" set:"defaultBackground=235" set:"defaultForeground=254" set:"defaultPadding=0 3" set:"defaultMargin=1 1" envprefix:"GUM_CONFIRM_UNSELECTED_"` UnselectedStyle style.Styles `embed:"" prefix:"unselected." help:"The style of the unselected action" set:"defaultBackground=235" set:"defaultForeground=254" set:"defaultPadding=0 3" set:"defaultMargin=1 1" envprefix:"GUM_CONFIRM_UNSELECTED_"`
Timeout time.Duration `help:"Timeout until confirm returns selected value or default if provided" default:"0" env:"GUM_CONFIRM_TIMEOUT"`
} }