help.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 removeDuplicateBindings(bindings []key.Binding) []key.Binding {
  47. // seen := make(map[string]struct{})
  48. // result := make([]key.Binding, 0, len(bindings))
  49. //
  50. // // Process bindings in reverse order
  51. // for i := len(bindings) - 1; i >= 0; i-- {
  52. // b := bindings[i]
  53. // k := strings.Join(b.Keys(), " ")
  54. // if _, ok := seen[k]; ok {
  55. // // duplicate, skip
  56. // continue
  57. // }
  58. // seen[k] = struct{}{}
  59. // // Add to the beginning of result to maintain original order
  60. // result = append([]key.Binding{b}, result...)
  61. // }
  62. //
  63. // return result
  64. // }
  65. func (h *helpDialog) View() string {
  66. t := theme.CurrentTheme()
  67. keyStyle := lipgloss.NewStyle().
  68. Background(t.BackgroundElement()).
  69. Foreground(t.Text()).
  70. Bold(true)
  71. descStyle := lipgloss.NewStyle().
  72. Background(t.BackgroundElement()).
  73. Foreground(t.TextMuted())
  74. lines := []string{}
  75. for _, b := range h.bindings {
  76. content := keyStyle.Render(b.Help().Key)
  77. content += descStyle.Render(" " + b.Help().Desc)
  78. for i, key := range b.Keys() {
  79. if i == 0 {
  80. keyString := " (" + strings.ToUpper(key) + ")"
  81. // space := max(h.width-lipgloss.Width(content)-lipgloss.Width(keyString), 0)
  82. // spacer := strings.Repeat(" ", space)
  83. // content += descStyle.Render(spacer)
  84. content += descStyle.Render(keyString)
  85. }
  86. }
  87. lines = append(lines, content)
  88. }
  89. return strings.Join(lines, "\n")
  90. }
  91. func (h *helpDialog) Render(background string) string {
  92. return h.modal.Render(h.View(), background)
  93. }
  94. func (h *helpDialog) Close() tea.Cmd {
  95. return nil
  96. }
  97. type HelpDialog interface {
  98. layout.Modal
  99. }
  100. func NewHelpDialog(bindings ...key.Binding) HelpDialog {
  101. return &helpDialog{
  102. bindings: bindings,
  103. modal: modal.New(),
  104. }
  105. }