Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(help): wrong full help sep rendering #554

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,23 @@ func (m Model) ShortHelpView(bindings []key.Binding) string {
continue
}

// Sep
var sep string
if totalWidth > 0 && i < len(bindings) {
sep = separator
}

// Item
str := sep +
m.Styles.ShortKey.Inline(true).Render(kb.Help().Key) + " " +
m.Styles.ShortDesc.Inline(true).Render(kb.Help().Desc)

w := lipgloss.Width(str)

// If adding this help item would go over the available width, stop
// drawing.
if m.Width > 0 && totalWidth+w > m.Width {
// Although if there's room for an ellipsis, print that.
tail := " " + m.Styles.Ellipsis.Inline(true).Render(m.Ellipsis)
tailWidth := lipgloss.Width(tail)

if totalWidth+tailWidth < m.Width {
// Tail
if tail, ok := m.shouldAddItem(totalWidth, w); !ok {
if tail != "" {
b.WriteString(tail)
}

break
}

Expand All @@ -170,21 +165,25 @@ func (m Model) FullHelpView(groups [][]key.Binding) string {
out []string

totalWidth int
sep = m.Styles.FullSeparator.Render(m.FullSeparator)
sepWidth = lipgloss.Width(sep)
separator = m.Styles.FullSeparator.Inline(true).Render(m.FullSeparator)
)

// Iterate over groups to build columns
for i, group := range groups {
if group == nil || !shouldRenderColumn(group) {
continue
}

var (
sep string
keys []string
descriptions []string
)

// Sep
if totalWidth > 0 && i < len(groups) {
sep = separator
}

// Separate keys and descriptions into different slices
for _, kb := range group {
if !kb.Enabled() {
Expand All @@ -194,33 +193,42 @@ func (m Model) FullHelpView(groups [][]key.Binding) string {
descriptions = append(descriptions, kb.Help().Desc)
}

// Column
col := lipgloss.JoinHorizontal(lipgloss.Top,
sep,
m.Styles.FullKey.Render(strings.Join(keys, "\n")),
m.Styles.FullKey.Render(" "),
" ",
m.Styles.FullDesc.Render(strings.Join(descriptions, "\n")),
)
w := lipgloss.Width(col)

// Column
totalWidth += lipgloss.Width(col)
if m.Width > 0 && totalWidth > m.Width {
// Tail
if tail, ok := m.shouldAddItem(totalWidth, w); !ok {
if tail != "" {
out = append(out, tail)
}
break
}

totalWidth += w
out = append(out, col)

// Separator
if i < len(group)-1 {
totalWidth += sepWidth
if m.Width > 0 && totalWidth > m.Width {
break
}
out = append(out, sep)
}
}

return lipgloss.JoinHorizontal(lipgloss.Top, out...)
}

func (m Model) shouldAddItem(totalWidth, width int) (tail string, ok bool) {
// If there's room for an ellipsis, print that.
if m.Width > 0 && totalWidth+width > m.Width {
tail = " " + m.Styles.Ellipsis.Inline(true).Render(m.Ellipsis)

if totalWidth+lipgloss.Width(tail) < m.Width {
return tail, false
}
}
return "", true
}

func shouldRenderColumn(b []key.Binding) (ok bool) {
for _, v := range b {
if v.Enabled() {
Expand Down
Loading