2022-07-26 21:43:26 +02:00
|
|
|
// Package confirm provides an interface to ask a user to confirm an action.
|
|
|
|
// The user is provided with an interface to choose an affirmative or negative
|
|
|
|
// answer, which is then reflected in the exit code for use in scripting.
|
|
|
|
//
|
|
|
|
// If the user selects the affirmative answer, the program exits with 0. If the
|
|
|
|
// user selects the negative answer, the program exits with 1.
|
|
|
|
//
|
|
|
|
// I.e. confirm if the user wants to delete a file
|
|
|
|
//
|
2022-08-05 00:45:19 +02:00
|
|
|
// $ gum confirm "Are you sure?" && rm file.txt
|
2022-07-26 21:43:26 +02:00
|
|
|
package confirm
|
|
|
|
|
|
|
|
import (
|
2022-08-08 19:57:55 +02:00
|
|
|
"time"
|
|
|
|
|
2023-06-30 15:20:33 +02:00
|
|
|
"github.com/charmbracelet/gum/timeout"
|
|
|
|
|
2022-07-26 21:43:26 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
prompt string
|
|
|
|
affirmative string
|
|
|
|
negative string
|
|
|
|
quitting bool
|
2022-12-13 21:46:43 +01:00
|
|
|
aborted bool
|
2022-08-08 19:57:55 +02:00
|
|
|
hasTimeout bool
|
|
|
|
timeout time.Duration
|
2022-07-29 17:48:37 +02:00
|
|
|
|
|
|
|
confirmation bool
|
2022-07-26 21:43:26 +02:00
|
|
|
|
2023-02-28 01:16:08 +01:00
|
|
|
defaultSelection bool
|
|
|
|
|
2022-07-26 21:43:26 +02:00
|
|
|
// styles
|
|
|
|
promptStyle lipgloss.Style
|
|
|
|
selectedStyle lipgloss.Style
|
|
|
|
unselectedStyle lipgloss.Style
|
|
|
|
}
|
|
|
|
|
2022-08-08 19:57:55 +02:00
|
|
|
func (m model) Init() tea.Cmd {
|
2023-06-30 15:20:33 +02:00
|
|
|
return timeout.Init(m.timeout, m.defaultSelection)
|
2022-08-08 19:57:55 +02:00
|
|
|
}
|
2022-07-26 21:43:26 +02:00
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
return m, nil
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
2023-01-13 18:08:06 +01:00
|
|
|
case "ctrl+c":
|
2023-06-30 15:20:33 +02:00
|
|
|
m.confirmation = false
|
2022-12-13 21:46:43 +01:00
|
|
|
m.aborted = true
|
2023-01-13 18:08:06 +01:00
|
|
|
fallthrough
|
|
|
|
case "esc":
|
|
|
|
m.confirmation = false
|
2022-12-13 21:46:43 +01:00
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
|
|
|
case "q", "n", "N":
|
2022-08-08 19:57:55 +02:00
|
|
|
m.confirmation = false
|
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
2022-07-29 17:48:37 +02:00
|
|
|
case "left", "h", "ctrl+p", "tab",
|
|
|
|
"right", "l", "ctrl+n", "shift+tab":
|
2022-10-06 19:15:53 +02:00
|
|
|
if m.negative == "" {
|
|
|
|
break
|
|
|
|
}
|
2022-07-29 17:48:37 +02:00
|
|
|
m.confirmation = !m.confirmation
|
2022-07-26 21:43:26 +02:00
|
|
|
case "enter":
|
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
2022-07-26 22:28:54 +02:00
|
|
|
case "y", "Y":
|
|
|
|
m.quitting = true
|
2022-07-29 17:48:37 +02:00
|
|
|
m.confirmation = true
|
2022-07-26 22:28:54 +02:00
|
|
|
return m, tea.Quit
|
2022-08-08 19:57:55 +02:00
|
|
|
}
|
2023-06-30 15:20:33 +02:00
|
|
|
case timeout.TickTimeoutMsg:
|
|
|
|
|
|
|
|
if msg.TimeoutValue <= 0 {
|
2022-07-26 21:43:26 +02:00
|
|
|
m.quitting = true
|
2023-02-28 01:16:08 +01:00
|
|
|
m.confirmation = m.defaultSelection
|
2022-07-26 21:43:26 +02:00
|
|
|
return m, tea.Quit
|
|
|
|
}
|
2023-06-30 15:20:33 +02:00
|
|
|
|
|
|
|
m.timeout = msg.TimeoutValue
|
|
|
|
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
|
2022-07-26 21:43:26 +02:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) View() string {
|
|
|
|
if m.quitting {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-06-30 15:20:33 +02:00
|
|
|
var aff, neg, timeoutStrYes, timeoutStrNo string
|
|
|
|
timeoutStrNo = ""
|
|
|
|
timeoutStrYes = ""
|
2022-08-08 19:57:55 +02:00
|
|
|
if m.hasTimeout {
|
2023-06-30 15:20:33 +02:00
|
|
|
if m.defaultSelection {
|
|
|
|
timeoutStrYes = timeout.Str(m.timeout)
|
|
|
|
} else {
|
|
|
|
timeoutStrNo = timeout.Str(m.timeout)
|
|
|
|
}
|
2023-02-28 01:16:08 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 17:48:37 +02:00
|
|
|
if m.confirmation {
|
2023-06-30 15:20:33 +02:00
|
|
|
aff = m.selectedStyle.Render(m.affirmative + timeoutStrYes)
|
|
|
|
neg = m.unselectedStyle.Render(m.negative + timeoutStrNo)
|
2022-07-26 21:43:26 +02:00
|
|
|
} else {
|
2023-06-30 15:20:33 +02:00
|
|
|
aff = m.unselectedStyle.Render(m.affirmative + timeoutStrYes)
|
|
|
|
neg = m.selectedStyle.Render(m.negative + timeoutStrNo)
|
2022-07-26 21:43:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:41:36 +02:00
|
|
|
// If the option is intentionally empty, do not show it.
|
|
|
|
if m.negative == "" {
|
|
|
|
neg = ""
|
|
|
|
}
|
|
|
|
|
2022-07-26 22:17:15 +02:00
|
|
|
return lipgloss.JoinVertical(lipgloss.Center, m.promptStyle.Render(m.prompt), lipgloss.JoinHorizontal(lipgloss.Left, aff, neg))
|
2022-07-26 21:43:26 +02:00
|
|
|
}
|