quit.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dialog
  2. import (
  3. "strings"
  4. "github.com/charmbracelet/bubbles/v2/key"
  5. tea "github.com/charmbracelet/bubbletea/v2"
  6. "github.com/charmbracelet/lipgloss/v2"
  7. "github.com/sst/opencode/internal/components/modal"
  8. "github.com/sst/opencode/internal/layout"
  9. "github.com/sst/opencode/internal/styles"
  10. "github.com/sst/opencode/internal/theme"
  11. "github.com/sst/opencode/internal/util"
  12. )
  13. const question = "Are you sure you want to quit?"
  14. // QuitDialog interface for the quit confirmation dialog
  15. type QuitDialog interface {
  16. layout.Modal
  17. IsQuitDialog() bool
  18. }
  19. type quitDialog struct {
  20. width int
  21. height int
  22. modal *modal.Modal
  23. selectedNo bool
  24. }
  25. type helpMapping struct {
  26. LeftRight key.Binding
  27. EnterSpace key.Binding
  28. Yes key.Binding
  29. No key.Binding
  30. }
  31. var helpKeys = helpMapping{
  32. LeftRight: key.NewBinding(
  33. key.WithKeys("left", "right", "h", "l", "tab"),
  34. key.WithHelp("←/→", "switch options"),
  35. ),
  36. EnterSpace: key.NewBinding(
  37. key.WithKeys("enter", " "),
  38. key.WithHelp("enter/space", "confirm"),
  39. ),
  40. Yes: key.NewBinding(
  41. key.WithKeys("y", "Y", "ctrl+c"),
  42. key.WithHelp("y/Y", "yes"),
  43. ),
  44. No: key.NewBinding(
  45. key.WithKeys("n", "N"),
  46. key.WithHelp("n/N", "no"),
  47. ),
  48. }
  49. func (q *quitDialog) Init() tea.Cmd {
  50. return nil
  51. }
  52. func (q *quitDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  53. switch msg := msg.(type) {
  54. case tea.WindowSizeMsg:
  55. q.width = msg.Width
  56. q.height = msg.Height
  57. case tea.KeyMsg:
  58. switch {
  59. case key.Matches(msg, helpKeys.LeftRight):
  60. q.selectedNo = !q.selectedNo
  61. return q, nil
  62. case key.Matches(msg, helpKeys.EnterSpace):
  63. if !q.selectedNo {
  64. return q, tea.Quit
  65. }
  66. return q, util.CmdHandler(modal.CloseModalMsg{})
  67. case key.Matches(msg, helpKeys.Yes):
  68. return q, tea.Quit
  69. case key.Matches(msg, helpKeys.No):
  70. return q, util.CmdHandler(modal.CloseModalMsg{})
  71. }
  72. }
  73. return q, nil
  74. }
  75. func (q *quitDialog) Render(background string) string {
  76. t := theme.CurrentTheme()
  77. baseStyle := styles.BaseStyle()
  78. yesStyle := baseStyle
  79. noStyle := baseStyle
  80. spacerStyle := baseStyle.Background(t.BackgroundElement())
  81. if q.selectedNo {
  82. noStyle = noStyle.Background(t.Primary()).Foreground(t.BackgroundElement())
  83. yesStyle = yesStyle.Background(t.BackgroundElement()).Foreground(t.Primary())
  84. } else {
  85. yesStyle = yesStyle.Background(t.Primary()).Foreground(t.BackgroundElement())
  86. noStyle = noStyle.Background(t.BackgroundElement()).Foreground(t.Primary())
  87. }
  88. yesButton := yesStyle.Padding(0, 1).Render("Yes")
  89. noButton := noStyle.Padding(0, 1).Render("No")
  90. buttons := lipgloss.JoinHorizontal(lipgloss.Left, yesButton, spacerStyle.Render(" "), noButton)
  91. width := lipgloss.Width(question)
  92. remainingWidth := width - lipgloss.Width(buttons)
  93. if remainingWidth > 0 {
  94. buttons = spacerStyle.Render(strings.Repeat(" ", remainingWidth)) + buttons
  95. }
  96. content := baseStyle.Render(
  97. lipgloss.JoinVertical(
  98. lipgloss.Center,
  99. question,
  100. "",
  101. buttons,
  102. ),
  103. )
  104. return q.modal.Render(content, background)
  105. }
  106. func (q *quitDialog) Close() tea.Cmd {
  107. return nil
  108. }
  109. func (q *quitDialog) IsQuitDialog() bool {
  110. return true
  111. }
  112. // NewQuitDialog creates a new quit confirmation dialog
  113. func NewQuitDialog() QuitDialog {
  114. return &quitDialog{
  115. selectedNo: true,
  116. modal: modal.New(),
  117. }
  118. }