help.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dialog
  2. import (
  3. "strings"
  4. "github.com/charmbracelet/bubbles/v2/key"
  5. tea "github.com/charmbracelet/bubbletea/v2"
  6. "github.com/charmbracelet/lipgloss/v2"
  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. bindings []key.Binding
  16. }
  17. // func (i bindingItem) Render(selected bool, width int) string {
  18. // t := theme.CurrentTheme()
  19. // baseStyle := styles.BaseStyle().
  20. // Width(width - 2).
  21. // Background(t.BackgroundElement())
  22. //
  23. // if selected {
  24. // baseStyle = baseStyle.
  25. // Background(t.Primary()).
  26. // Foreground(t.BackgroundElement()).
  27. // Bold(true)
  28. // } else {
  29. // baseStyle = baseStyle.
  30. // Foreground(t.Text())
  31. // }
  32. //
  33. // return baseStyle.Padding(0, 1).Render(i.binding.Help().Desc)
  34. // }
  35. func (h *helpDialog) Init() tea.Cmd {
  36. return nil
  37. }
  38. func (h *helpDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  39. switch msg := msg.(type) {
  40. case tea.WindowSizeMsg:
  41. h.width = msg.Width
  42. h.height = msg.Height
  43. }
  44. return h, nil
  45. }
  46. func (h *helpDialog) View() string {
  47. t := theme.CurrentTheme()
  48. keyStyle := lipgloss.NewStyle().
  49. Background(t.BackgroundElement()).
  50. Foreground(t.Text()).
  51. Bold(true)
  52. descStyle := lipgloss.NewStyle().
  53. Background(t.BackgroundElement()).
  54. Foreground(t.TextMuted())
  55. contentStyle := lipgloss.NewStyle().
  56. PaddingLeft(1).Background(t.BackgroundElement())
  57. lines := []string{}
  58. for _, b := range h.bindings {
  59. content := keyStyle.Render(b.Help().Key)
  60. content += descStyle.Render(" " + b.Help().Desc)
  61. for i, key := range b.Keys() {
  62. if i == 0 {
  63. keyString := " (" + strings.ToUpper(key) + ")"
  64. // space := max(h.width-lipgloss.Width(content)-lipgloss.Width(keyString), 0)
  65. // spacer := strings.Repeat(" ", space)
  66. // content += descStyle.Render(spacer)
  67. content += descStyle.Render(keyString)
  68. }
  69. }
  70. lines = append(lines, contentStyle.Render(content))
  71. }
  72. return strings.Join(lines, "\n")
  73. }
  74. func (h *helpDialog) Render(background string) string {
  75. return h.modal.Render(h.View(), background)
  76. }
  77. func (h *helpDialog) Close() tea.Cmd {
  78. return nil
  79. }
  80. type HelpDialog interface {
  81. layout.Modal
  82. }
  83. func NewHelpDialog(bindings ...key.Binding) HelpDialog {
  84. return &helpDialog{
  85. bindings: bindings,
  86. modal: modal.New(),
  87. }
  88. }