2017-12-17 05:48:26 +08:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
2018-01-11 22:45:52 +08:00
|
|
|
runewidth "github.com/mattn/go-runewidth"
|
2017-12-17 05:48:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// listItem represents one item in a List.
|
|
|
|
type listItem struct {
|
|
|
|
MainText string // The main text of the list item.
|
|
|
|
SecondaryText string // A secondary text to be shown underneath the main text.
|
|
|
|
Shortcut rune // The key to select the list item directly, 0 if there is no shortcut.
|
2017-12-19 03:04:52 +08:00
|
|
|
Selected func() // The optional function which is called when the item is selected.
|
2017-12-17 05:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// List displays rows of items, each of which can be selected.
|
2018-01-07 23:39:06 +08:00
|
|
|
//
|
|
|
|
// See https://github.com/rivo/tview/wiki/List for an example.
|
2017-12-17 05:48:26 +08:00
|
|
|
type List struct {
|
|
|
|
*Box
|
|
|
|
|
|
|
|
// The items of the list.
|
|
|
|
items []*listItem
|
|
|
|
|
|
|
|
// The index of the currently selected item.
|
|
|
|
currentItem int
|
|
|
|
|
|
|
|
// Whether or not to show the secondary item texts.
|
|
|
|
showSecondaryText bool
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// The item main text color.
|
2017-12-17 05:48:26 +08:00
|
|
|
mainTextColor tcell.Color
|
|
|
|
|
|
|
|
// The item secondary text color.
|
|
|
|
secondaryTextColor tcell.Color
|
|
|
|
|
|
|
|
// The item shortcut text color.
|
|
|
|
shortcutColor tcell.Color
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// The text color for selected items.
|
|
|
|
selectedTextColor tcell.Color
|
|
|
|
|
|
|
|
// The background color for selected items.
|
|
|
|
selectedBackgroundColor tcell.Color
|
|
|
|
|
2018-01-02 00:17:20 +08:00
|
|
|
// An optional function which is called when the user has navigated to a list
|
|
|
|
// item.
|
|
|
|
changed func(index int, mainText, secondaryText string, shortcut rune)
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// An optional function which is called when a list item was selected. This
|
|
|
|
// function will be called even if the list item defines its own callback.
|
2017-12-17 05:48:26 +08:00
|
|
|
selected func(index int, mainText, secondaryText string, shortcut rune)
|
2018-01-02 00:17:20 +08:00
|
|
|
|
|
|
|
// An optional function which is called when the user presses the Escape key.
|
|
|
|
done func()
|
2017-12-17 05:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewList returns a new form.
|
|
|
|
func NewList() *List {
|
|
|
|
return &List{
|
2017-12-19 03:04:52 +08:00
|
|
|
Box: NewBox(),
|
|
|
|
showSecondaryText: true,
|
2018-01-10 16:43:32 +08:00
|
|
|
mainTextColor: Styles.PrimaryTextColor,
|
|
|
|
secondaryTextColor: Styles.TertiaryTextColor,
|
|
|
|
shortcutColor: Styles.SecondaryTextColor,
|
|
|
|
selectedTextColor: Styles.PrimitiveBackgroundColor,
|
|
|
|
selectedBackgroundColor: Styles.PrimaryTextColor,
|
2017-12-17 05:48:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// SetCurrentItem sets the currently selected item by its index.
|
|
|
|
func (l *List) SetCurrentItem(index int) *List {
|
|
|
|
l.currentItem = index
|
2018-01-02 00:17:20 +08:00
|
|
|
if l.currentItem < len(l.items) && l.changed != nil {
|
|
|
|
item := l.items[l.currentItem]
|
|
|
|
l.changed(l.currentItem, item.MainText, item.SecondaryText, item.Shortcut)
|
|
|
|
}
|
2017-12-19 03:04:52 +08:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMainTextColor sets the color of the items' main text.
|
|
|
|
func (l *List) SetMainTextColor(color tcell.Color) *List {
|
2017-12-17 05:48:26 +08:00
|
|
|
l.mainTextColor = color
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// SetSecondaryTextColor sets the color of the items' secondary text.
|
|
|
|
func (l *List) SetSecondaryTextColor(color tcell.Color) *List {
|
2017-12-17 05:48:26 +08:00
|
|
|
l.secondaryTextColor = color
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetShortcutColor sets the color of the items' shortcut.
|
|
|
|
func (l *List) SetShortcutColor(color tcell.Color) *List {
|
|
|
|
l.shortcutColor = color
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// SetSelectedTextColor sets the text color of selected items.
|
|
|
|
func (l *List) SetSelectedTextColor(color tcell.Color) *List {
|
|
|
|
l.selectedTextColor = color
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSelectedBackgroundColor sets the background color of selected items.
|
|
|
|
func (l *List) SetSelectedBackgroundColor(color tcell.Color) *List {
|
|
|
|
l.selectedBackgroundColor = color
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-12-17 05:48:26 +08:00
|
|
|
// ShowSecondaryText determines whether or not to show secondary item texts.
|
|
|
|
func (l *List) ShowSecondaryText(show bool) *List {
|
|
|
|
l.showSecondaryText = show
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2018-01-02 00:17:20 +08:00
|
|
|
// SetChangedFunc sets the function which is called when the user navigates to
|
|
|
|
// a list item. The function receives the item's index in the list of items
|
|
|
|
// (starting with 0), its main text, secondary text, and its shortcut rune.
|
|
|
|
//
|
|
|
|
// This function is also called when the first item is added or when
|
|
|
|
// SetCurrentItem() is called.
|
|
|
|
func (l *List) SetChangedFunc(handler func(int, string, string, rune)) *List {
|
|
|
|
l.changed = handler
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-12-17 05:48:26 +08:00
|
|
|
// SetSelectedFunc sets the function which is called when the user selects a
|
2017-12-30 05:27:10 +08:00
|
|
|
// list item by pressing Enter on the current selection. The function receives
|
|
|
|
// the item's index in the list of items (starting with 0), its main text,
|
|
|
|
// secondary text, and its shortcut rune.
|
2017-12-17 05:48:26 +08:00
|
|
|
func (l *List) SetSelectedFunc(handler func(int, string, string, rune)) *List {
|
|
|
|
l.selected = handler
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2018-01-02 00:17:20 +08:00
|
|
|
// SetDoneFunc sets a function which is called when the user presses the Escape
|
|
|
|
// key.
|
|
|
|
func (l *List) SetDoneFunc(handler func()) *List {
|
|
|
|
l.done = handler
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-12-17 05:48:26 +08:00
|
|
|
// AddItem adds a new item to the list. An item has a main text which will be
|
|
|
|
// highlighted when selected. It also has a secondary text which is shown
|
|
|
|
// underneath the main text (if it is set to visible) but which may remain
|
|
|
|
// empty.
|
|
|
|
//
|
|
|
|
// The shortcut is a key binding. If the specified rune is entered, the item
|
|
|
|
// is selected immediately. Set to 0 for no binding.
|
2017-12-19 03:04:52 +08:00
|
|
|
//
|
|
|
|
// The "selected" callback will be invoked when the user selects the item. You
|
|
|
|
// may provide nil if no such item is needed or if all events are handled
|
|
|
|
// through the selected callback set with SetSelectedFunc().
|
|
|
|
func (l *List) AddItem(mainText, secondaryText string, shortcut rune, selected func()) *List {
|
2017-12-17 05:48:26 +08:00
|
|
|
l.items = append(l.items, &listItem{
|
|
|
|
MainText: mainText,
|
|
|
|
SecondaryText: secondaryText,
|
|
|
|
Shortcut: shortcut,
|
2017-12-19 03:04:52 +08:00
|
|
|
Selected: selected,
|
2017-12-17 05:48:26 +08:00
|
|
|
})
|
2018-01-02 00:17:20 +08:00
|
|
|
if len(l.items) == 1 && l.changed != nil {
|
|
|
|
item := l.items[0]
|
|
|
|
l.changed(0, item.MainText, item.SecondaryText, item.Shortcut)
|
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2018-01-02 00:17:20 +08:00
|
|
|
// Clear removes all items from the list.
|
|
|
|
func (l *List) Clear() *List {
|
2017-12-19 03:04:52 +08:00
|
|
|
l.items = nil
|
|
|
|
l.currentItem = 0
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-12-17 05:48:26 +08:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (l *List) Draw(screen tcell.Screen) {
|
|
|
|
l.Box.Draw(screen)
|
|
|
|
|
|
|
|
// Determine the dimensions.
|
2017-12-22 01:08:53 +08:00
|
|
|
x, y, width, height := l.GetInnerRect()
|
|
|
|
bottomLimit := y + height
|
2017-12-17 05:48:26 +08:00
|
|
|
|
|
|
|
// Do we show any shortcuts?
|
|
|
|
var showShortcuts bool
|
|
|
|
for _, item := range l.items {
|
|
|
|
if item.Shortcut != 0 {
|
|
|
|
showShortcuts = true
|
|
|
|
x += 4
|
|
|
|
width -= 4
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the list items.
|
|
|
|
for index, item := range l.items {
|
|
|
|
if y >= bottomLimit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shortcuts.
|
|
|
|
if showShortcuts && item.Shortcut != 0 {
|
2017-12-26 08:07:30 +08:00
|
|
|
Print(screen, fmt.Sprintf("(%s)", string(item.Shortcut)), x-5, y, 4, AlignRight, l.shortcutColor)
|
2017-12-17 05:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Main text.
|
|
|
|
color := l.mainTextColor
|
2018-01-07 21:37:41 +08:00
|
|
|
if index == l.currentItem {
|
2018-01-11 22:45:52 +08:00
|
|
|
textLength := runewidth.StringWidth(item.MainText)
|
2017-12-19 03:04:52 +08:00
|
|
|
style := tcell.StyleDefault.Background(l.selectedBackgroundColor)
|
2017-12-17 05:48:26 +08:00
|
|
|
for bx := 0; bx < textLength && bx < width; bx++ {
|
|
|
|
screen.SetContent(x+bx, y, ' ', nil, style)
|
|
|
|
}
|
2017-12-19 03:04:52 +08:00
|
|
|
color = l.selectedTextColor
|
2017-12-17 05:48:26 +08:00
|
|
|
}
|
|
|
|
Print(screen, item.MainText, x, y, width, AlignLeft, color)
|
|
|
|
y++
|
|
|
|
|
|
|
|
if y >= bottomLimit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Secondary text.
|
|
|
|
if l.showSecondaryText {
|
|
|
|
Print(screen, item.SecondaryText, x, y, width, AlignLeft, l.secondaryTextColor)
|
|
|
|
y++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
2017-12-19 03:04:52 +08:00
|
|
|
func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
return func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-01-02 00:17:20 +08:00
|
|
|
previousItem := l.currentItem
|
|
|
|
|
2017-12-17 05:48:26 +08:00
|
|
|
switch key := event.Key(); key {
|
|
|
|
case tcell.KeyTab, tcell.KeyDown, tcell.KeyRight:
|
|
|
|
l.currentItem++
|
|
|
|
case tcell.KeyBacktab, tcell.KeyUp, tcell.KeyLeft:
|
|
|
|
l.currentItem--
|
|
|
|
case tcell.KeyHome:
|
|
|
|
l.currentItem = 0
|
|
|
|
case tcell.KeyEnd:
|
|
|
|
l.currentItem = len(l.items) - 1
|
|
|
|
case tcell.KeyPgDn:
|
|
|
|
l.currentItem += 5
|
|
|
|
case tcell.KeyPgUp:
|
|
|
|
l.currentItem -= 5
|
|
|
|
case tcell.KeyEnter:
|
2017-12-19 03:04:52 +08:00
|
|
|
item := l.items[l.currentItem]
|
|
|
|
if item.Selected != nil {
|
|
|
|
item.Selected()
|
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
if l.selected != nil {
|
|
|
|
l.selected(l.currentItem, item.MainText, item.SecondaryText, item.Shortcut)
|
|
|
|
}
|
2018-01-02 00:17:20 +08:00
|
|
|
case tcell.KeyEscape:
|
|
|
|
if l.done != nil {
|
|
|
|
l.done()
|
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
case tcell.KeyRune:
|
|
|
|
ch := event.Rune()
|
|
|
|
if ch != ' ' {
|
|
|
|
// It's not a space bar. Is it a shortcut?
|
|
|
|
var found bool
|
|
|
|
for index, item := range l.items {
|
|
|
|
if item.Shortcut == ch {
|
|
|
|
// We have a shortcut.
|
|
|
|
found = true
|
|
|
|
l.currentItem = index
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 03:04:52 +08:00
|
|
|
item := l.items[l.currentItem]
|
|
|
|
if item.Selected != nil {
|
|
|
|
item.Selected()
|
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
if l.selected != nil {
|
|
|
|
l.selected(l.currentItem, item.MainText, item.SecondaryText, item.Shortcut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.currentItem < 0 {
|
|
|
|
l.currentItem = len(l.items) - 1
|
|
|
|
} else if l.currentItem >= len(l.items) {
|
|
|
|
l.currentItem = 0
|
|
|
|
}
|
2018-01-02 00:17:20 +08:00
|
|
|
|
|
|
|
if l.currentItem != previousItem && l.currentItem < len(l.items) && l.changed != nil {
|
|
|
|
item := l.items[l.currentItem]
|
|
|
|
l.changed(l.currentItem, item.MainText, item.SecondaryText, item.Shortcut)
|
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
}
|
|
|
|
}
|