help.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/layout"
  8. "github.com/sst/opencode/internal/styles"
  9. "github.com/sst/opencode/internal/theme"
  10. )
  11. type helpComponent struct {
  12. width int
  13. height int
  14. keys []key.Binding
  15. }
  16. func (h *helpComponent) Init() tea.Cmd {
  17. return nil
  18. }
  19. func (h *helpComponent) SetBindings(k []key.Binding) {
  20. h.keys = k
  21. }
  22. func (h *helpComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  23. switch msg := msg.(type) {
  24. case tea.WindowSizeMsg:
  25. h.width = 90
  26. h.height = msg.Height
  27. }
  28. return h, nil
  29. }
  30. func removeDuplicateBindings(bindings []key.Binding) []key.Binding {
  31. seen := make(map[string]struct{})
  32. result := make([]key.Binding, 0, len(bindings))
  33. // Process bindings in reverse order
  34. for i := len(bindings) - 1; i >= 0; i-- {
  35. b := bindings[i]
  36. k := strings.Join(b.Keys(), " ")
  37. if _, ok := seen[k]; ok {
  38. // duplicate, skip
  39. continue
  40. }
  41. seen[k] = struct{}{}
  42. // Add to the beginning of result to maintain original order
  43. result = append([]key.Binding{b}, result...)
  44. }
  45. return result
  46. }
  47. func (h *helpComponent) render() string {
  48. t := theme.CurrentTheme()
  49. baseStyle := styles.BaseStyle()
  50. helpKeyStyle := styles.Bold().
  51. Background(t.Background()).
  52. Foreground(t.Text()).
  53. Padding(0, 1, 0, 0)
  54. helpDescStyle := styles.Regular().
  55. Background(t.Background()).
  56. Foreground(t.TextMuted())
  57. // Compile list of bindings to render
  58. bindings := removeDuplicateBindings(h.keys)
  59. // Enumerate through each group of bindings, populating a series of
  60. // pairs of columns, one for keys, one for descriptions
  61. var (
  62. pairs []string
  63. width int
  64. rows = 12 - 2
  65. )
  66. for i := 0; i < len(bindings); i += rows {
  67. var (
  68. keys []string
  69. descs []string
  70. )
  71. for j := i; j < min(i+rows, len(bindings)); j++ {
  72. keys = append(keys, helpKeyStyle.Render(bindings[j].Help().Key))
  73. descs = append(descs, helpDescStyle.Render(bindings[j].Help().Desc))
  74. }
  75. // Render pair of columns; beyond the first pair, render a three space
  76. // left margin, in order to visually separate the pairs.
  77. var cols []string
  78. if len(pairs) > 0 {
  79. cols = []string{baseStyle.Render(" ")}
  80. }
  81. maxDescWidth := 0
  82. for _, desc := range descs {
  83. if maxDescWidth < lipgloss.Width(desc) {
  84. maxDescWidth = lipgloss.Width(desc)
  85. }
  86. }
  87. for i := range descs {
  88. remainingWidth := maxDescWidth - lipgloss.Width(descs[i])
  89. if remainingWidth > 0 {
  90. descs[i] = descs[i] + baseStyle.Render(strings.Repeat(" ", remainingWidth))
  91. }
  92. }
  93. maxKeyWidth := 0
  94. for _, key := range keys {
  95. if maxKeyWidth < lipgloss.Width(key) {
  96. maxKeyWidth = lipgloss.Width(key)
  97. }
  98. }
  99. for i := range keys {
  100. remainingWidth := maxKeyWidth - lipgloss.Width(keys[i])
  101. if remainingWidth > 0 {
  102. keys[i] = keys[i] + baseStyle.Render(strings.Repeat(" ", remainingWidth))
  103. }
  104. }
  105. cols = append(cols,
  106. strings.Join(keys, "\n"),
  107. strings.Join(descs, "\n"),
  108. )
  109. pair := baseStyle.Render(lipgloss.JoinHorizontal(lipgloss.Top, cols...))
  110. // check whether it exceeds the maximum width avail (the width of the
  111. // terminal, subtracting 2 for the borders).
  112. width += lipgloss.Width(pair)
  113. if width > h.width-2 {
  114. break
  115. }
  116. pairs = append(pairs, pair)
  117. }
  118. // https://github.com/charmbracelet/lipgloss/v2/issues/209
  119. if len(pairs) > 1 {
  120. prefix := pairs[:len(pairs)-1]
  121. lastPair := pairs[len(pairs)-1]
  122. prefix = append(prefix, lipgloss.Place(
  123. lipgloss.Width(lastPair), // width
  124. lipgloss.Height(prefix[0]), // height
  125. lipgloss.Left, // x
  126. lipgloss.Top, // y
  127. lastPair, // content
  128. // lipgloss.WithWhitespaceBackground(t.Background()),
  129. ))
  130. content := baseStyle.Width(h.width).Render(
  131. lipgloss.JoinHorizontal(
  132. lipgloss.Top,
  133. prefix...,
  134. ),
  135. )
  136. return content
  137. }
  138. // Join pairs of columns and enclose in a border
  139. content := baseStyle.Width(h.width).Render(
  140. lipgloss.JoinHorizontal(
  141. lipgloss.Top,
  142. pairs...,
  143. ),
  144. )
  145. return content
  146. }
  147. func (h *helpComponent) View() string {
  148. t := theme.CurrentTheme()
  149. baseStyle := styles.BaseStyle()
  150. content := h.render()
  151. header := baseStyle.
  152. Bold(true).
  153. Width(lipgloss.Width(content)).
  154. Foreground(t.Primary()).
  155. Render("Keyboard Shortcuts")
  156. return baseStyle.Padding(1).
  157. Border(lipgloss.RoundedBorder()).
  158. BorderForeground(t.TextMuted()).
  159. Width(h.width).
  160. BorderBackground(t.Background()).
  161. Render(
  162. lipgloss.JoinVertical(lipgloss.Center,
  163. header,
  164. baseStyle.Render(strings.Repeat(" ", lipgloss.Width(header))),
  165. content,
  166. ),
  167. )
  168. }
  169. type HelpComponent interface {
  170. layout.ModelWithView
  171. SetBindings([]key.Binding)
  172. }
  173. func NewHelpCmp() HelpComponent {
  174. return &helpComponent{}
  175. }