help.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dialog
  2. import (
  3. tea "github.com/charmbracelet/bubbletea/v2"
  4. "github.com/sst/opencode/internal/app"
  5. commandsComponent "github.com/sst/opencode/internal/components/commands"
  6. "github.com/sst/opencode/internal/components/modal"
  7. "github.com/sst/opencode/internal/layout"
  8. "github.com/sst/opencode/internal/theme"
  9. "github.com/sst/opencode/internal/viewport"
  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, but cap at reasonable width
  29. maxWidth := min(80, msg.Width-8)
  30. h.viewport = viewport.New(viewport.WithWidth(maxWidth-4), viewport.WithHeight(msg.Height-6))
  31. h.commandsComponent.SetSize(maxWidth-4, msg.Height-6)
  32. }
  33. // Update viewport content
  34. h.viewport.SetContent(h.commandsComponent.View())
  35. // Update viewport
  36. var vpCmd tea.Cmd
  37. h.viewport, vpCmd = h.viewport.Update(msg)
  38. cmds = append(cmds, vpCmd)
  39. return h, tea.Batch(cmds...)
  40. }
  41. func (h *helpDialog) View() string {
  42. t := theme.CurrentTheme()
  43. h.commandsComponent.SetBackgroundColor(t.BackgroundPanel())
  44. return h.viewport.View()
  45. }
  46. func (h *helpDialog) Render(background string) string {
  47. return h.modal.Render(h.View(), background)
  48. }
  49. func (h *helpDialog) Close() tea.Cmd {
  50. return nil
  51. }
  52. type HelpDialog interface {
  53. layout.Modal
  54. }
  55. func NewHelpDialog(app *app.App) HelpDialog {
  56. vp := viewport.New(viewport.WithHeight(12))
  57. return &helpDialog{
  58. app: app,
  59. commandsComponent: commandsComponent.New(app,
  60. commandsComponent.WithBackground(theme.CurrentTheme().BackgroundPanel()),
  61. commandsComponent.WithShowAll(true),
  62. commandsComponent.WithKeybinds(true),
  63. ),
  64. modal: modal.New(modal.WithTitle("Help"), modal.WithMaxWidth(80)),
  65. viewport: vp,
  66. }
  67. }