fix(confirm): ensure timer is located at fixed selection

This commit is contained in:
Christoper Hans 2022-09-24 16:30:14 +07:00 committed by Maas Lalani
parent 8c99083115
commit 02591074e4
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000

View file

@ -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
@ -49,6 +51,7 @@ func (m model) Init() tea.Cmd {
if m.timeout > 0 { if m.timeout > 0 {
return tick() return tick()
} }
m.defaultSelection = m.confirmation
return nil return nil
} }
@ -99,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 + timeout) if m.defaultSelection {
neg = m.unselectedStyle.Render(m.negative) 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.