From e8b9aeb14922390ee7eac37a54b1ff514affcaf7 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 23 May 2022 22:43:35 -0300 Subject: [PATCH 1/3] feat: allow to set the height of the item The user might want to show more than 2 lines, and, right now, if they try to do so, things break. Signed-off-by: Carlos A Becker --- list/defaultitem.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/list/defaultitem.go b/list/defaultitem.go index 04dfe7b9..ab9114ad 100644 --- a/list/defaultitem.go +++ b/list/defaultitem.go @@ -3,6 +3,7 @@ package list import ( "fmt" "io" + "strings" "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" @@ -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 } @@ -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 } @@ -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 { 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 From 67f1e06f1fbaa63640b7ddd680a9845c2c8f093e Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Tue, 24 May 2022 11:03:26 -0300 Subject: [PATCH 2/3] fix: short-circuit if width <= 0 Signed-off-by: Carlos A Becker --- list/defaultitem.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/list/defaultitem.go b/list/defaultitem.go index ab9114ad..788e6616 100644 --- a/list/defaultitem.go +++ b/list/defaultitem.go @@ -149,20 +149,21 @@ 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") - } + if m.width <= 0 { + // short-circuit + return } // Prevent text from exceeding list width - if m.width > 0 { - textwidth := uint(m.width - s.NormalTitle.GetPaddingLeft() - s.NormalTitle.GetPaddingRight()) - title = truncate.StringWithTail(title, textwidth, ellipsis) - lines := strings.Split(desc, "\n") - for i, line := range lines { - lines[i] = truncate.StringWithTail(line, textwidth, ellipsis) + textwidth := uint(m.width - s.NormalTitle.GetPaddingLeft() - s.NormalTitle.GetPaddingRight()) + title = truncate.StringWithTail(title, textwidth, ellipsis) + if d.ShowDescription { + var lines []string + for i, line := range strings.Split(desc, "\n") { + if i > d.height-1 { + break + } + lines = append(lines, truncate.StringWithTail(line, textwidth, ellipsis)) } desc = strings.Join(lines, "\n") } From 128d4264d000e41ef312ed4459f2b338edbba108 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Tue, 24 May 2022 12:01:56 -0300 Subject: [PATCH 3/3] fix: height check Signed-off-by: Carlos A Becker --- list/defaultitem.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/list/defaultitem.go b/list/defaultitem.go index 788e6616..74e4ee22 100644 --- a/list/defaultitem.go +++ b/list/defaultitem.go @@ -160,7 +160,7 @@ func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item) { if d.ShowDescription { var lines []string for i, line := range strings.Split(desc, "\n") { - if i > d.height-1 { + if i >= d.height-1 { break } lines = append(lines, truncate.StringWithTail(line, textwidth, ellipsis))