quit.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package dialog
  2. import (
  3. "strings"
  4. "github.com/charmbracelet/bubbles/key"
  5. tea "github.com/charmbracelet/bubbletea"
  6. "github.com/charmbracelet/lipgloss"
  7. "github.com/opencode-ai/opencode/internal/tui/layout"
  8. "github.com/opencode-ai/opencode/internal/tui/styles"
  9. "github.com/opencode-ai/opencode/internal/tui/util"
  10. )
  11. const question = "Are you sure you want to quit?"
  12. type CloseQuitMsg struct{}
  13. type QuitDialog interface {
  14. tea.Model
  15. layout.Bindings
  16. }
  17. type quitDialogCmp struct {
  18. selectedNo bool
  19. }
  20. type helpMapping struct {
  21. LeftRight key.Binding
  22. EnterSpace key.Binding
  23. Yes key.Binding
  24. No key.Binding
  25. Tab key.Binding
  26. }
  27. var helpKeys = helpMapping{
  28. LeftRight: key.NewBinding(
  29. key.WithKeys("left", "right"),
  30. key.WithHelp("←/→", "switch options"),
  31. ),
  32. EnterSpace: key.NewBinding(
  33. key.WithKeys("enter", " "),
  34. key.WithHelp("enter/space", "confirm"),
  35. ),
  36. Yes: key.NewBinding(
  37. key.WithKeys("y", "Y"),
  38. key.WithHelp("y/Y", "yes"),
  39. ),
  40. No: key.NewBinding(
  41. key.WithKeys("n", "N"),
  42. key.WithHelp("n/N", "no"),
  43. ),
  44. Tab: key.NewBinding(
  45. key.WithKeys("tab"),
  46. key.WithHelp("tab", "switch options"),
  47. ),
  48. }
  49. func (q *quitDialogCmp) Init() tea.Cmd {
  50. return nil
  51. }
  52. func (q *quitDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  53. switch msg := msg.(type) {
  54. case tea.KeyMsg:
  55. switch {
  56. case key.Matches(msg, helpKeys.LeftRight) || key.Matches(msg, helpKeys.Tab):
  57. q.selectedNo = !q.selectedNo
  58. return q, nil
  59. case key.Matches(msg, helpKeys.EnterSpace):
  60. if !q.selectedNo {
  61. return q, tea.Quit
  62. }
  63. return q, util.CmdHandler(CloseQuitMsg{})
  64. case key.Matches(msg, helpKeys.Yes):
  65. return q, tea.Quit
  66. case key.Matches(msg, helpKeys.No):
  67. return q, util.CmdHandler(CloseQuitMsg{})
  68. }
  69. }
  70. return q, nil
  71. }
  72. func (q *quitDialogCmp) View() string {
  73. yesStyle := styles.BaseStyle
  74. noStyle := styles.BaseStyle
  75. spacerStyle := styles.BaseStyle.Background(styles.Background)
  76. if q.selectedNo {
  77. noStyle = noStyle.Background(styles.PrimaryColor).Foreground(styles.Background)
  78. yesStyle = yesStyle.Background(styles.Background).Foreground(styles.PrimaryColor)
  79. } else {
  80. yesStyle = yesStyle.Background(styles.PrimaryColor).Foreground(styles.Background)
  81. noStyle = noStyle.Background(styles.Background).Foreground(styles.PrimaryColor)
  82. }
  83. yesButton := yesStyle.Padding(0, 1).Render("Yes")
  84. noButton := noStyle.Padding(0, 1).Render("No")
  85. buttons := lipgloss.JoinHorizontal(lipgloss.Left, yesButton, spacerStyle.Render(" "), noButton)
  86. width := lipgloss.Width(question)
  87. remainingWidth := width - lipgloss.Width(buttons)
  88. if remainingWidth > 0 {
  89. buttons = spacerStyle.Render(strings.Repeat(" ", remainingWidth)) + buttons
  90. }
  91. content := styles.BaseStyle.Render(
  92. lipgloss.JoinVertical(
  93. lipgloss.Center,
  94. question,
  95. "",
  96. buttons,
  97. ),
  98. )
  99. return styles.BaseStyle.Padding(1, 2).
  100. Border(lipgloss.RoundedBorder()).
  101. BorderBackground(styles.Background).
  102. BorderForeground(styles.ForgroundDim).
  103. Width(lipgloss.Width(content) + 4).
  104. Render(content)
  105. }
  106. func (q *quitDialogCmp) BindingKeys() []key.Binding {
  107. return layout.KeyMapToSlice(helpKeys)
  108. }
  109. func NewQuitCmp() QuitDialog {
  110. return &quitDialogCmp{
  111. selectedNo: true,
  112. }
  113. }