init.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/opencode-ai/opencode/internal/tui/styles"
  7. "github.com/opencode-ai/opencode/internal/tui/util"
  8. )
  9. // InitDialogCmp is a component that asks the user if they want to initialize the project.
  10. type InitDialogCmp struct {
  11. width, height int
  12. selected int
  13. keys initDialogKeyMap
  14. }
  15. // NewInitDialogCmp creates a new InitDialogCmp.
  16. func NewInitDialogCmp() InitDialogCmp {
  17. return InitDialogCmp{
  18. selected: 0,
  19. keys: initDialogKeyMap{},
  20. }
  21. }
  22. type initDialogKeyMap struct {
  23. Tab key.Binding
  24. Left key.Binding
  25. Right key.Binding
  26. Enter key.Binding
  27. Escape key.Binding
  28. Y key.Binding
  29. N key.Binding
  30. }
  31. // ShortHelp implements key.Map.
  32. func (k initDialogKeyMap) ShortHelp() []key.Binding {
  33. return []key.Binding{
  34. key.NewBinding(
  35. key.WithKeys("tab", "left", "right"),
  36. key.WithHelp("tab/←/→", "toggle selection"),
  37. ),
  38. key.NewBinding(
  39. key.WithKeys("enter"),
  40. key.WithHelp("enter", "confirm"),
  41. ),
  42. key.NewBinding(
  43. key.WithKeys("esc", "q"),
  44. key.WithHelp("esc/q", "cancel"),
  45. ),
  46. key.NewBinding(
  47. key.WithKeys("y", "n"),
  48. key.WithHelp("y/n", "yes/no"),
  49. ),
  50. }
  51. }
  52. // FullHelp implements key.Map.
  53. func (k initDialogKeyMap) FullHelp() [][]key.Binding {
  54. return [][]key.Binding{k.ShortHelp()}
  55. }
  56. // Init implements tea.Model.
  57. func (m InitDialogCmp) Init() tea.Cmd {
  58. return nil
  59. }
  60. // Update implements tea.Model.
  61. func (m InitDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  62. switch msg := msg.(type) {
  63. case tea.KeyMsg:
  64. switch {
  65. case key.Matches(msg, key.NewBinding(key.WithKeys("esc"))):
  66. return m, util.CmdHandler(CloseInitDialogMsg{Initialize: false})
  67. case key.Matches(msg, key.NewBinding(key.WithKeys("tab", "left", "right", "h", "l"))):
  68. m.selected = (m.selected + 1) % 2
  69. return m, nil
  70. case key.Matches(msg, key.NewBinding(key.WithKeys("enter"))):
  71. return m, util.CmdHandler(CloseInitDialogMsg{Initialize: m.selected == 0})
  72. case key.Matches(msg, key.NewBinding(key.WithKeys("y"))):
  73. return m, util.CmdHandler(CloseInitDialogMsg{Initialize: true})
  74. case key.Matches(msg, key.NewBinding(key.WithKeys("n"))):
  75. return m, util.CmdHandler(CloseInitDialogMsg{Initialize: false})
  76. }
  77. case tea.WindowSizeMsg:
  78. m.width = msg.Width
  79. m.height = msg.Height
  80. }
  81. return m, nil
  82. }
  83. // View implements tea.Model.
  84. func (m InitDialogCmp) View() string {
  85. // Calculate width needed for content
  86. maxWidth := 60 // Width for explanation text
  87. title := styles.BaseStyle.
  88. Foreground(styles.PrimaryColor).
  89. Bold(true).
  90. Width(maxWidth).
  91. Padding(0, 1).
  92. Render("Initialize Project")
  93. explanation := styles.BaseStyle.
  94. Foreground(styles.Forground).
  95. Width(maxWidth).
  96. Padding(0, 1).
  97. Render("Initialization generates a new OpenCode.md file that contains information about your codebase, this file serves as memory for each project, you can freely add to it to help the agents be better at their job.")
  98. question := styles.BaseStyle.
  99. Foreground(styles.Forground).
  100. Width(maxWidth).
  101. Padding(1, 1).
  102. Render("Would you like to initialize this project?")
  103. maxWidth = min(maxWidth, m.width-10)
  104. yesStyle := styles.BaseStyle
  105. noStyle := styles.BaseStyle
  106. if m.selected == 0 {
  107. yesStyle = yesStyle.
  108. Background(styles.PrimaryColor).
  109. Foreground(styles.Background).
  110. Bold(true)
  111. noStyle = noStyle.
  112. Background(styles.Background).
  113. Foreground(styles.PrimaryColor)
  114. } else {
  115. noStyle = noStyle.
  116. Background(styles.PrimaryColor).
  117. Foreground(styles.Background).
  118. Bold(true)
  119. yesStyle = yesStyle.
  120. Background(styles.Background).
  121. Foreground(styles.PrimaryColor)
  122. }
  123. yes := yesStyle.Padding(0, 3).Render("Yes")
  124. no := noStyle.Padding(0, 3).Render("No")
  125. buttons := lipgloss.JoinHorizontal(lipgloss.Center, yes, styles.BaseStyle.Render(" "), no)
  126. buttons = styles.BaseStyle.
  127. Width(maxWidth).
  128. Padding(1, 0).
  129. Render(buttons)
  130. content := lipgloss.JoinVertical(
  131. lipgloss.Left,
  132. title,
  133. styles.BaseStyle.Width(maxWidth).Render(""),
  134. explanation,
  135. question,
  136. buttons,
  137. styles.BaseStyle.Width(maxWidth).Render(""),
  138. )
  139. return styles.BaseStyle.Padding(1, 2).
  140. Border(lipgloss.RoundedBorder()).
  141. BorderBackground(styles.Background).
  142. BorderForeground(styles.ForgroundDim).
  143. Width(lipgloss.Width(content) + 4).
  144. Render(content)
  145. }
  146. // SetSize sets the size of the component.
  147. func (m *InitDialogCmp) SetSize(width, height int) {
  148. m.width = width
  149. m.height = height
  150. }
  151. // Bindings implements layout.Bindings.
  152. func (m InitDialogCmp) Bindings() []key.Binding {
  153. return m.keys.ShortHelp()
  154. }
  155. // CloseInitDialogMsg is a message that is sent when the init dialog is closed.
  156. type CloseInitDialogMsg struct {
  157. Initialize bool
  158. }
  159. // ShowInitDialogMsg is a message that is sent to show the init dialog.
  160. type ShowInitDialogMsg struct {
  161. Show bool
  162. }