help.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package dialog
  2. import (
  3. "strings"
  4. tea "github.com/charmbracelet/bubbletea/v2"
  5. "github.com/charmbracelet/lipgloss/v2"
  6. "github.com/sst/opencode/internal/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. commands []commands.Command
  16. }
  17. func (h *helpDialog) Init() tea.Cmd {
  18. return nil
  19. }
  20. func (h *helpDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  21. switch msg := msg.(type) {
  22. case tea.WindowSizeMsg:
  23. h.width = msg.Width
  24. h.height = msg.Height
  25. }
  26. return h, nil
  27. }
  28. func (h *helpDialog) View() string {
  29. t := theme.CurrentTheme()
  30. keyStyle := lipgloss.NewStyle().
  31. Background(t.BackgroundElement()).
  32. Foreground(t.Text()).
  33. Bold(true)
  34. descStyle := lipgloss.NewStyle().
  35. Background(t.BackgroundElement()).
  36. Foreground(t.TextMuted())
  37. contentStyle := lipgloss.NewStyle().
  38. PaddingLeft(1).Background(t.BackgroundElement())
  39. lines := []string{}
  40. for _, b := range h.commands {
  41. // Only interested in slash commands
  42. if b.Trigger == "" {
  43. continue
  44. }
  45. content := keyStyle.Render("/" + b.Trigger)
  46. content += descStyle.Render(" " + b.Description)
  47. // for i, key := range b.Keybindings {
  48. // if i == 0 {
  49. // keyString := " (" + key.Key + ")"
  50. // space := max(h.width-lipgloss.Width(content)-lipgloss.Width(keyString), 0)
  51. // spacer := strings.Repeat(" ", space)
  52. // content += descStyle.Render(spacer)
  53. // content += descStyle.Render(keyString)
  54. // }
  55. // }
  56. lines = append(lines, contentStyle.Render(content))
  57. }
  58. return strings.Join(lines, "\n")
  59. }
  60. func (h *helpDialog) Render(background string) string {
  61. return h.modal.Render(h.View(), background)
  62. }
  63. func (h *helpDialog) Close() tea.Cmd {
  64. return nil
  65. }
  66. type HelpDialog interface {
  67. layout.Modal
  68. }
  69. func NewHelpDialog(commands []commands.Command) HelpDialog {
  70. return &helpDialog{
  71. commands: commands,
  72. modal: modal.New(modal.WithTitle("Help")),
  73. }
  74. }