diff --git a/helpbar/helpbar.go b/helpbar/helpbar.go index b83e9a6..481882a 100644 --- a/helpbar/helpbar.go +++ b/helpbar/helpbar.go @@ -1,10 +1,13 @@ package helpbar import ( + "strings" + "charm.land/bubbles/v2/help" "charm.land/bubbles/v2/key" tea "charm.land/bubbletea/v2" "charm.land/lipgloss/v2" + "github.com/charmbracelet/x/ansi" "github.com/anotherhadi/ilovetui/bubbles" ) @@ -59,10 +62,21 @@ func (m Model) View(contextual ...key.Binding) string { if len(bindings) == 0 || m.width <= 0 { return "" } + var view string if !m.ShowAll { - return m.help.ShortHelpView(bindings) + view = m.help.ShortHelpView(bindings) + } else { + view = m.help.FullHelpView(m.columns(bindings)) } - return m.help.FullHelpView(m.columns(bindings)) + return m.clampWidth(view) +} + +func (m Model) clampWidth(view string) string { + lines := strings.Split(view, "\n") + for i, line := range lines { + lines[i] = ansi.Truncate(line, m.width, "") + } + return strings.Join(lines, "\n") } func (m Model) Height(contextual ...key.Binding) int { diff --git a/style/border.go b/style/border.go index e57f73d..ca87b4f 100644 --- a/style/border.go +++ b/style/border.go @@ -4,6 +4,7 @@ import ( "strings" "charm.land/lipgloss/v2" + "github.com/charmbracelet/x/ansi" ) var borderTypes = map[string]lipgloss.Border{ @@ -41,12 +42,18 @@ func RenderWithTitle(border lipgloss.Style, title, content string, width, height box := border.BorderTop(false).Width(width).Height(boxH).Render(content) boxWidth := lipgloss.Width(strings.SplitN(box, "\n", 2)[0]) - titleW := lipgloss.Width(title) b, _, _, _, _ := border.GetBorder() topLeft, top, topRight := b.TopLeft, b.Top, b.TopRight + overhead := lipgloss.Width(topLeft) + lipgloss.Width(topRight) + 2 - fillW := boxWidth - titleW - lipgloss.Width(topLeft) - lipgloss.Width(topRight) - 2 + maxTitleW := max(boxWidth-overhead, 0) + if titleW := lipgloss.Width(title); titleW > maxTitleW { + title = ansi.Truncate(title, maxTitleW, "") + } + titleW := lipgloss.Width(title) + + fillW := boxWidth - titleW - overhead if fillW < 0 { fillW = 0 }