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
defaultSelection bool
// styles
promptStyle lipgloss.Style
selectedStyle lipgloss.Style
@ -49,6 +51,7 @@ func (m model) Init() tea.Cmd {
if m.timeout > 0 {
return tick()
}
m.defaultSelection = m.confirmation
return nil
}
@ -99,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())))
}
if m.confirmation {
aff = m.selectedStyle.Render(m.affirmative + timeout)
neg = m.unselectedStyle.Render(m.negative)
// set timer based on defaultSelection
if m.defaultSelection {
affirmativeTimeout = m.affirmative + timeout
negativeTimeout = m.negative
} else {
aff = m.unselectedStyle.Render(m.affirmative)
neg = m.selectedStyle.Render(m.negative + timeout)
affirmativeTimeout = m.affirmative
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.