diff options
author | ChrHan <4262799+ChrHan@users.noreply.github.com> | 2023-02-28 07:16:08 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-27 19:16:08 -0500 |
commit | 46dee843db0cf33d27a688025c6976768d835102 (patch) | |
tree | 3857480d85f6e7ef70b3448c805ab33914adae92 | |
parent | e6de7749b139c3ff38285437d390f5f92e1689fb (diff) |
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>
-rw-r--r-- | confirm/command.go | 19 | ||||
-rw-r--r-- | confirm/confirm.go | 23 |
2 files changed, 27 insertions, 15 deletions
diff --git a/confirm/command.go b/confirm/command.go index 92a55ed..3597150 100644 --- a/confirm/command.go +++ b/confirm/command.go @@ -14,15 +14,16 @@ import ( // action with an affirmative or negative answer. func (o Options) Run() error { m, err := tea.NewProgram(model{ - affirmative: o.Affirmative, - negative: o.Negative, - confirmation: o.Default, - timeout: o.Timeout, - hasTimeout: o.Timeout > 0, - prompt: o.Prompt, - selectedStyle: o.SelectedStyle.ToLipgloss(), - unselectedStyle: o.UnselectedStyle.ToLipgloss(), - promptStyle: o.PromptStyle.ToLipgloss(), + affirmative: o.Affirmative, + negative: o.Negative, + confirmation: o.Default, + defaultSelection: o.Default, + timeout: o.Timeout, + hasTimeout: o.Timeout > 0, + prompt: o.Prompt, + selectedStyle: o.SelectedStyle.ToLipgloss(), + unselectedStyle: o.UnselectedStyle.ToLipgloss(), + promptStyle: o.PromptStyle.ToLipgloss(), }, tea.WithOutput(os.Stderr)).Run() if err != nil { diff --git a/confirm/confirm.go b/confirm/confirm.go index 7e6eeb1..250fc26 100644 --- a/confirm/confirm.go +++ b/confirm/confirm.go @@ -29,6 +29,8 @@ type model struct { confirmation bool + defaultSelection bool + // styles promptStyle lipgloss.Style selectedStyle lipgloss.Style @@ -86,7 +88,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tickMsg: if m.timeout <= 0 { m.quitting = true - m.confirmation = false + m.confirmation = m.defaultSelection return m, tea.Quit } m.timeout -= tickInterval @@ -100,18 +102,27 @@ func (m model) View() string { return "" } - var aff, neg, timeout string + var aff, neg, timeout, affirmativeTimeout, negativeTimeout string if m.hasTimeout { timeout = fmt.Sprintf(" (%d)", max(0, int(m.timeout.Seconds()))) } + // set timer based on defaultSelection + if m.defaultSelection { + affirmativeTimeout = m.affirmative + timeout + negativeTimeout = m.negative + } else { + affirmativeTimeout = m.affirmative + negativeTimeout = m.negative + timeout + } + if m.confirmation { - aff = m.selectedStyle.Render(m.affirmative) - neg = m.unselectedStyle.Render(m.negative + timeout) + aff = m.selectedStyle.Render(affirmativeTimeout) + neg = m.unselectedStyle.Render(negativeTimeout) } else { - aff = m.unselectedStyle.Render(m.affirmative) - neg = m.selectedStyle.Render(m.negative + timeout) + aff = m.unselectedStyle.Render(affirmativeTimeout) + neg = m.selectedStyle.Render(negativeTimeout) } // If the option is intentionally empty, do not show it. |