help.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package dialog
  2. import (
  3. "github.com/charmbracelet/bubbles/v2/viewport"
  4. tea "github.com/charmbracelet/bubbletea/v2"
  5. "github.com/sst/opencode/internal/app"
  6. commandsComponent "github.com/sst/opencode/internal/components/commands"
  7. "github.com/sst/opencode/internal/components/modal"
  8. "github.com/sst/opencode/internal/layout"
  9. "github.com/sst/opencode/internal/theme"
  10. )
  11. type helpDialog struct {
  12. width int
  13. height int
  14. modal *modal.Modal
  15. app *app.App
  16. commandsComponent commandsComponent.CommandsComponent
  17. viewport viewport.Model
  18. }
  19. func (h *helpDialog) Init() tea.Cmd {
  20. return tea.Batch(
  21. h.commandsComponent.Init(),
  22. h.viewport.Init(),
  23. )
  24. }
  25. func (h *helpDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  26. var cmds []tea.Cmd
  27. switch msg := msg.(type) {
  28. case tea.WindowSizeMsg:
  29. h.width = msg.Width
  30. h.height = msg.Height
  31. // Set viewport size with some padding for the modal
  32. h.viewport = viewport.New(viewport.WithWidth(msg.Width-4), viewport.WithHeight(msg.Height-6))
  33. h.commandsComponent.SetSize(msg.Width-4, msg.Height-6)
  34. }
  35. // Update commands component first to get the latest content
  36. _, cmdCmd := h.commandsComponent.Update(msg)
  37. cmds = append(cmds, cmdCmd)
  38. // Update viewport content
  39. h.viewport.SetContent(h.commandsComponent.View())
  40. // Update viewport
  41. var vpCmd tea.Cmd
  42. h.viewport, vpCmd = h.viewport.Update(msg)
  43. cmds = append(cmds, vpCmd)
  44. return h, tea.Batch(cmds...)
  45. }
  46. func (h *helpDialog) View() string {
  47. t := theme.CurrentTheme()
  48. h.commandsComponent.SetBackgroundColor(t.BackgroundElement())
  49. return h.viewport.View()
  50. }
  51. func (h *helpDialog) Render(background string) string {
  52. return h.modal.Render(h.View(), background)
  53. }
  54. func (h *helpDialog) Close() tea.Cmd {
  55. return nil
  56. }
  57. type HelpDialog interface {
  58. layout.Modal
  59. }
  60. func NewHelpDialog(app *app.App) HelpDialog {
  61. vp := viewport.New(viewport.WithHeight(12))
  62. return &helpDialog{
  63. app: app,
  64. commandsComponent: commandsComponent.New(app,
  65. commandsComponent.WithBackground(theme.CurrentTheme().BackgroundElement()),
  66. commandsComponent.WithShowAll(true),
  67. commandsComponent.WithKeybinds(true),
  68. ),
  69. modal: modal.New(modal.WithTitle("Help")),
  70. viewport: vp,
  71. }
  72. }