mirror of
https://github.com/charmbracelet/gum
synced 2024-05-26 02:12:24 +02:00
fix(prompt): fix default selection of prompt, timer and exit code (#148)
* fix(prompt): fix default selection of prompt, timer and exit code * chore(comment): remove unused comment * fix(confirm): ensure timer is located at fixed selection * chore(confirm): remove inefficient assignment * fix: default timeout selection --------- Co-authored-by: Maas Lalani <maas@lalani.dev>
This commit is contained in:
parent
e6de7749b1
commit
46dee843db
|
@ -14,15 +14,16 @@ import (
|
||||||
// action with an affirmative or negative answer.
|
// action with an affirmative or negative answer.
|
||||||
func (o Options) Run() error {
|
func (o Options) Run() error {
|
||||||
m, err := tea.NewProgram(model{
|
m, err := tea.NewProgram(model{
|
||||||
affirmative: o.Affirmative,
|
affirmative: o.Affirmative,
|
||||||
negative: o.Negative,
|
negative: o.Negative,
|
||||||
confirmation: o.Default,
|
confirmation: o.Default,
|
||||||
timeout: o.Timeout,
|
defaultSelection: o.Default,
|
||||||
hasTimeout: o.Timeout > 0,
|
timeout: o.Timeout,
|
||||||
prompt: o.Prompt,
|
hasTimeout: o.Timeout > 0,
|
||||||
selectedStyle: o.SelectedStyle.ToLipgloss(),
|
prompt: o.Prompt,
|
||||||
unselectedStyle: o.UnselectedStyle.ToLipgloss(),
|
selectedStyle: o.SelectedStyle.ToLipgloss(),
|
||||||
promptStyle: o.PromptStyle.ToLipgloss(),
|
unselectedStyle: o.UnselectedStyle.ToLipgloss(),
|
||||||
|
promptStyle: o.PromptStyle.ToLipgloss(),
|
||||||
}, tea.WithOutput(os.Stderr)).Run()
|
}, tea.WithOutput(os.Stderr)).Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -29,6 +29,8 @@ type model struct {
|
||||||
|
|
||||||
confirmation bool
|
confirmation bool
|
||||||
|
|
||||||
|
defaultSelection bool
|
||||||
|
|
||||||
// styles
|
// styles
|
||||||
promptStyle lipgloss.Style
|
promptStyle lipgloss.Style
|
||||||
selectedStyle lipgloss.Style
|
selectedStyle lipgloss.Style
|
||||||
|
@ -86,7 +88,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
case tickMsg:
|
case tickMsg:
|
||||||
if m.timeout <= 0 {
|
if m.timeout <= 0 {
|
||||||
m.quitting = true
|
m.quitting = true
|
||||||
m.confirmation = false
|
m.confirmation = m.defaultSelection
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
m.timeout -= tickInterval
|
m.timeout -= tickInterval
|
||||||
|
@ -100,18 +102,27 @@ func (m model) View() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var aff, neg, timeout string
|
var aff, neg, timeout, affirmativeTimeout, negativeTimeout string
|
||||||
|
|
||||||
if m.hasTimeout {
|
if m.hasTimeout {
|
||||||
timeout = fmt.Sprintf(" (%d)", max(0, int(m.timeout.Seconds())))
|
timeout = fmt.Sprintf(" (%d)", max(0, int(m.timeout.Seconds())))
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.confirmation {
|
// set timer based on defaultSelection
|
||||||
aff = m.selectedStyle.Render(m.affirmative)
|
if m.defaultSelection {
|
||||||
neg = m.unselectedStyle.Render(m.negative + timeout)
|
affirmativeTimeout = m.affirmative + timeout
|
||||||
|
negativeTimeout = m.negative
|
||||||
} else {
|
} else {
|
||||||
aff = m.unselectedStyle.Render(m.affirmative)
|
affirmativeTimeout = m.affirmative
|
||||||
neg = m.selectedStyle.Render(m.negative + timeout)
|
negativeTimeout = m.negative + timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.confirmation {
|
||||||
|
aff = m.selectedStyle.Render(affirmativeTimeout)
|
||||||
|
neg = m.unselectedStyle.Render(negativeTimeout)
|
||||||
|
} else {
|
||||||
|
aff = m.unselectedStyle.Render(affirmativeTimeout)
|
||||||
|
neg = m.selectedStyle.Render(negativeTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the option is intentionally empty, do not show it.
|
// If the option is intentionally empty, do not show it.
|
||||||
|
|
Loading…
Reference in a new issue