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

feat: allow to set the height of the item #155

Merged
merged 3 commits into from May 24, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions list/defaultitem.go
Expand Up @@ -3,6 +3,7 @@ package list
import (
"fmt"
"io"
"strings"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -86,6 +87,7 @@ type DefaultDelegate struct {
UpdateFunc func(tea.Msg, *Model) tea.Cmd
ShortHelpFunc func() []key.Binding
FullHelpFunc func() [][]key.Binding
height int
spacing int
}

Expand All @@ -94,14 +96,22 @@ func NewDefaultDelegate() DefaultDelegate {
return DefaultDelegate{
ShowDescription: true,
Styles: NewDefaultItemStyles(),
height: 2,
spacing: 1,
}
}

// SetHeight sets delegate's preferred height.
func (d *DefaultDelegate) SetHeight(i int) {
d.height = i
}

// Height returns the delegate's preferred height.
// This has effect only if ShowDescription is true,
// otherwise height is always 1.
func (d DefaultDelegate) Height() int {
if d.ShowDescription {
return 2 //nolint:gomnd
return d.height
}
return 1
}
Expand Down Expand Up @@ -139,11 +149,22 @@ func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item) {
return
}

if d.ShowDescription {
lines := strings.Split(desc, "\n")
if len(lines) > d.height-1 {
desc = strings.Join(lines[0:d.height-1], "\n")
}
}

// Prevent text from exceeding list width
if m.width > 0 {
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
textwidth := uint(m.width - s.NormalTitle.GetPaddingLeft() - s.NormalTitle.GetPaddingRight())
title = truncate.StringWithTail(title, textwidth, ellipsis)
desc = truncate.StringWithTail(desc, textwidth, ellipsis)
lines := strings.Split(desc, "\n")
for i, line := range lines {
lines[i] = truncate.StringWithTail(line, textwidth, ellipsis)
}
desc = strings.Join(lines, "\n")
}

// Conditions
Expand Down