| 50 | } |
| 51 | |
| 52 | func (h Help) View() string { |
| 53 | keyStyle := lipgloss.NewStyle().Bold(true) |
| 54 | descStyle := lipgloss.NewStyle().Foreground(Colors.Border) |
| 55 | separator := descStyle.Render(" • ") |
| 56 | sepWidth := lipgloss.Width(separator) |
| 57 | |
| 58 | type helpItem struct { |
| 59 | str string |
| 60 | width int |
| 61 | index int |
| 62 | } |
| 63 | |
| 64 | var items []helpItem |
| 65 | for i, kb := range h.bindings { |
| 66 | help := kb.Help() |
| 67 | if help.Key == "" { |
| 68 | continue |
| 69 | } |
| 70 | rendered := keyStyle.Render(help.Key) + " " + descStyle.Render(help.Desc) |
| 71 | items = append(items, helpItem{str: rendered, width: lipgloss.Width(rendered), index: i}) |
| 72 | } |
| 73 | |
| 74 | if len(items) == 0 { |
| 75 | return "" |
| 76 | } |
| 77 | |
| 78 | maxWidth := h.width |
| 79 | var lines []string |
| 80 | var line strings.Builder |
| 81 | lineWidth := 0 |
| 82 | |
| 83 | for _, it := range items { |
| 84 | if lineWidth > 0 && maxWidth > 0 && lineWidth+sepWidth+it.width > maxWidth { |
| 85 | lines = append(lines, line.String()) |
| 86 | line.Reset() |
| 87 | lineWidth = 0 |
| 88 | } |
| 89 | if lineWidth > 0 { |
| 90 | line.WriteString(separator) |
| 91 | lineWidth += sepWidth |
| 92 | } |
| 93 | line.WriteString(mouse.Mark(helpTarget(it.index), it.str)) |
| 94 | lineWidth += it.width |
| 95 | } |
| 96 | if line.Len() > 0 { |
| 97 | lines = append(lines, line.String()) |
| 98 | } |
| 99 | |
| 100 | return strings.Join(lines, "\n") |
| 101 | } |
| 102 | |
| 103 | // Helpers |
| 104 | |