help.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 h.viewport.Init()
  21. }
  22. func (h *helpDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  23. var cmds []tea.Cmd
  24. switch msg := msg.(type) {
  25. case tea.WindowSizeMsg:
  26. h.width = msg.Width
  27. h.height = msg.Height
  28. // Set viewport size with some padding for the modal
  29. h.viewport = viewport.New(viewport.WithWidth(msg.Width-4), viewport.WithHeight(msg.Height-6))
  30. h.commandsComponent.SetSize(msg.Width-4, msg.Height-6)
  31. }
  32. // Update viewport content
  33. h.viewport.SetContent(h.commandsComponent.View())
  34. // Update viewport
  35. var vpCmd tea.Cmd
  36. h.viewport, vpCmd = h.viewport.Update(msg)
  37. cmds = append(cmds, vpCmd)
  38. return h, tea.Batch(cmds...)
  39. }
  40. func (h *helpDialog) View() string {
  41. t := theme.CurrentTheme()
  42. h.commandsComponent.SetBackgroundColor(t.BackgroundElement())
  43. return h.viewport.View()
  44. }
  45. func (h *helpDialog) Render(background string) string {
  46. return h.modal.Render(h.View(), background)
  47. }
  48. func (h *helpDialog) Close() tea.Cmd {
  49. return nil
  50. }
  51. type HelpDialog interface {
  52. layout.Modal
  53. }
  54. func NewHelpDialog(app *app.App) HelpDialog {
  55. vp := viewport.New(viewport.WithHeight(12))
  56. return &helpDialog{
  57. app: app,
  58. commandsComponent: commandsComponent.New(app,
  59. commandsComponent.WithBackground(theme.CurrentTheme().BackgroundElement()),
  60. commandsComponent.WithShowAll(true),
  61. commandsComponent.WithKeybinds(true),
  62. ),
  63. modal: modal.New(modal.WithTitle("Help")),
  64. viewport: vp,
  65. }
  66. }