add compact flag

Add an option to render the list compactly (without blank lines between `ListItem`s). This flag will be ignored if `showSecondaryText` is set to true.
This commit is contained in:
Ian C 2021-12-18 11:39:51 -08:00
parent c2f9074643
commit 28827c1146
1 changed files with 23 additions and 2 deletions

25
list.go
View File

@ -194,6 +194,10 @@ type List struct {
// The height of the list the last time it was drawn.
height int
// If true, will render each ListItem without blank lines in between. This flag
// is ignored if `showSecondaryText` is true.
compact bool
sync.RWMutex
}
@ -209,6 +213,7 @@ func NewList() *List {
selectedTextColor: Styles.PrimitiveBackgroundColor,
scrollBarColor: Styles.ScrollBarColor,
selectedBackgroundColor: Styles.PrimaryTextColor,
compact: false,
}
l.ContextMenu = NewContextMenu(l)
@ -519,6 +524,15 @@ func (l *List) SetDoneFunc(handler func()) {
l.done = handler
}
// SetCompactList sets the flag that determines whether a blank line is drawn between
// each ListItem. This flag will be ignored if `showSecondaryText` is true.
func (l *List) SetCompactList(compact bool) {
l.Lock()
defer l.Unlock()
l.compact = compact
}
// AddItem calls InsertItem() with an index of -1.
func (l *List) AddItem(item *ListItem) {
l.InsertItem(-1, item)
@ -799,7 +813,11 @@ func (l *List) updateOffset() {
}
} else {
if l.currentItem-l.itemOffset >= h {
l.itemOffset = l.currentItem + 1 - h
if l.compact {
l.itemOffset = l.currentItem - h
} else {
l.itemOffset = l.currentItem + 1 - h
}
}
}
@ -978,7 +996,10 @@ func (l *List) Draw(screen tcell.Screen) {
RenderScrollBar(screen, l.scrollBarVisibility, scrollBarX, y, scrollBarHeight, len(l.items), scrollBarCursor, index-l.itemOffset, l.hasFocus, l.scrollBarColor)
y++
// Compact List
if !l.compact {
y++
}
if y >= bottomLimit {
break