mirror of
https://github.com/Maks1mS/bubbles.git
synced 2024-12-24 14:44:38 +03:00
feat: allow to set the height of the item (#155)
* 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 <caarlos0@users.noreply.github.com> * fix: short-circuit if width <= 0 Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: height check Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
e1871db6d3
commit
cd2593cfb7
@ -3,6 +3,7 @@ package list
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
@ -86,6 +87,7 @@ type DefaultDelegate struct {
|
|||||||
UpdateFunc func(tea.Msg, *Model) tea.Cmd
|
UpdateFunc func(tea.Msg, *Model) tea.Cmd
|
||||||
ShortHelpFunc func() []key.Binding
|
ShortHelpFunc func() []key.Binding
|
||||||
FullHelpFunc func() [][]key.Binding
|
FullHelpFunc func() [][]key.Binding
|
||||||
|
height int
|
||||||
spacing int
|
spacing int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,14 +96,22 @@ func NewDefaultDelegate() DefaultDelegate {
|
|||||||
return DefaultDelegate{
|
return DefaultDelegate{
|
||||||
ShowDescription: true,
|
ShowDescription: true,
|
||||||
Styles: NewDefaultItemStyles(),
|
Styles: NewDefaultItemStyles(),
|
||||||
|
height: 2,
|
||||||
spacing: 1,
|
spacing: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetHeight sets delegate's preferred height.
|
||||||
|
func (d *DefaultDelegate) SetHeight(i int) {
|
||||||
|
d.height = i
|
||||||
|
}
|
||||||
|
|
||||||
// Height returns the delegate's preferred height.
|
// 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 {
|
func (d DefaultDelegate) Height() int {
|
||||||
if d.ShowDescription {
|
if d.ShowDescription {
|
||||||
return 2 //nolint:gomnd
|
return d.height
|
||||||
}
|
}
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@ -139,11 +149,23 @@ func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if m.width <= 0 {
|
||||||
|
// short-circuit
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Prevent text from exceeding list width
|
// Prevent text from exceeding list width
|
||||||
if m.width > 0 {
|
textwidth := uint(m.width - s.NormalTitle.GetPaddingLeft() - s.NormalTitle.GetPaddingRight())
|
||||||
textwidth := uint(m.width - s.NormalTitle.GetPaddingLeft() - s.NormalTitle.GetPaddingRight())
|
title = truncate.StringWithTail(title, textwidth, ellipsis)
|
||||||
title = truncate.StringWithTail(title, textwidth, ellipsis)
|
if d.ShowDescription {
|
||||||
desc = truncate.StringWithTail(desc, textwidth, ellipsis)
|
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")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conditions
|
// Conditions
|
||||||
|
Loading…
Reference in New Issue
Block a user