quit.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dialog
  2. import (
  3. "github.com/charmbracelet/bubbles/key"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/kujtimiihoxha/termai/internal/tui/components/core"
  7. "github.com/kujtimiihoxha/termai/internal/tui/layout"
  8. "github.com/kujtimiihoxha/termai/internal/tui/styles"
  9. "github.com/kujtimiihoxha/termai/internal/tui/util"
  10. "github.com/charmbracelet/huh"
  11. )
  12. const question = "Are you sure you want to quit?"
  13. var (
  14. width = lipgloss.Width(question) + 6
  15. height = 3
  16. )
  17. type QuitDialog interface {
  18. tea.Model
  19. layout.Sizeable
  20. layout.Bindings
  21. }
  22. type quitDialogCmp struct {
  23. form *huh.Form
  24. width int
  25. height int
  26. }
  27. func (q *quitDialogCmp) Init() tea.Cmd {
  28. return nil
  29. }
  30. func (q *quitDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  31. var cmds []tea.Cmd
  32. // Process the form
  33. form, cmd := q.form.Update(msg)
  34. if f, ok := form.(*huh.Form); ok {
  35. q.form = f
  36. cmds = append(cmds, cmd)
  37. }
  38. if q.form.State == huh.StateCompleted {
  39. v := q.form.GetBool("quit")
  40. if v {
  41. return q, tea.Quit
  42. }
  43. cmds = append(cmds, util.CmdHandler(core.DialogCloseMsg{}))
  44. }
  45. return q, tea.Batch(cmds...)
  46. }
  47. func (q *quitDialogCmp) View() string {
  48. return q.form.View()
  49. }
  50. func (q *quitDialogCmp) GetSize() (int, int) {
  51. return q.width, q.height
  52. }
  53. func (q *quitDialogCmp) SetSize(width int, height int) {
  54. q.width = width
  55. q.height = height
  56. }
  57. func (q *quitDialogCmp) BindingKeys() []key.Binding {
  58. return q.form.KeyBinds()
  59. }
  60. func newQuitDialogCmp() QuitDialog {
  61. confirm := huh.NewConfirm().
  62. Title(question).
  63. Affirmative("Yes!").
  64. Key("quit").
  65. Negative("No.")
  66. theme := styles.HuhTheme()
  67. theme.Focused.FocusedButton = theme.Focused.FocusedButton.Background(styles.Warning)
  68. theme.Blurred.FocusedButton = theme.Blurred.FocusedButton.Background(styles.Warning)
  69. form := huh.NewForm(huh.NewGroup(confirm)).
  70. WithWidth(width).
  71. WithHeight(height).
  72. WithShowHelp(false).
  73. WithTheme(theme).
  74. WithShowErrors(false)
  75. confirm.Focus()
  76. return &quitDialogCmp{
  77. form: form,
  78. width: width,
  79. }
  80. }
  81. func NewQuitDialogCmd() tea.Cmd {
  82. content := layout.NewSinglePane(
  83. newQuitDialogCmp().(*quitDialogCmp),
  84. layout.WithSignlePaneSize(width+2, height+2),
  85. layout.WithSinglePaneBordered(true),
  86. layout.WithSinglePaneFocusable(true),
  87. layout.WithSinglePaneActiveColor(styles.Warning),
  88. )
  89. content.Focus()
  90. return util.CmdHandler(core.DialogMsg{
  91. Content: content,
  92. })
  93. }