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

Add GetLast and GetFirst method #479

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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: 25 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type List struct {
// The number of list items skipped at the top before the first item is drawn.
offset int

// Number of items drawn
itemsDrawn int

// An optional function which is called when the user has navigated to a list
// item.
changed func(index int, mainText, secondaryText string, shortcut rune)
Expand All @@ -67,6 +70,7 @@ type List struct {

// An optional function which is called when the user presses the Escape key.
done func()

}

// NewList returns a new form.
Expand Down Expand Up @@ -399,6 +403,7 @@ func (l *List) Draw(screen tcell.Screen) {
bottomLimit = totalHeight
}


// Do we show any shortcuts?
var showShortcuts bool
for _, item := range l.items {
Expand All @@ -423,6 +428,8 @@ func (l *List) Draw(screen tcell.Screen) {
}
}

l.itemsDrawn = 0

// Draw the list items.
for index, item := range l.items {
if index < l.offset {
Expand All @@ -440,6 +447,7 @@ func (l *List) Draw(screen tcell.Screen) {

// Main text.
Print(screen, item.MainText, x, y, width, AlignLeft, l.mainTextColor)
l.itemsDrawn++

// Background color of selected text.
if index == l.currentItem && (!l.selectedFocusOnly || l.HasFocus()) {
Expand Down Expand Up @@ -472,7 +480,24 @@ func (l *List) Draw(screen tcell.Screen) {
Print(screen, item.SecondaryText, x, y, width, AlignLeft, l.secondaryTextColor)
y++
}


}
}

// GetFirst returns the first index of item drawn
func (l *List) GetFirst() int {
return l.offset
}

// GetLast returns the last index of item drawn. If showSecondaryText set to true,
// GetLast returns the second last index of item drawn to prevent from scrolling
func (l *List) GetLast() int {
lastIndex := l.itemsDrawn - 1 + l.offset
if l.showSecondaryText {
return lastIndex - 1
}
return lastIndex
}

// InputHandler returns the handler for this primitive.
Expand Down