✨ Add list input
This commit is contained in:
parent
871db79c19
commit
e0bc00d779
2 changed files with 90 additions and 9 deletions
66
input/list/list.go
Normal file
66
input/list/list.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package list
|
||||
|
||||
import "github.com/rivo/tview"
|
||||
|
||||
type Option interface {
|
||||
Title() string
|
||||
Text() string
|
||||
Rune() rune
|
||||
Selected() func()
|
||||
}
|
||||
|
||||
type opt struct {
|
||||
tit string
|
||||
t string
|
||||
r rune
|
||||
}
|
||||
|
||||
func (o opt) Title() string {
|
||||
return o.tit
|
||||
}
|
||||
|
||||
func (o opt) Text() string {
|
||||
return o.t
|
||||
}
|
||||
|
||||
func (o opt) Rune() rune {
|
||||
return o.r
|
||||
}
|
||||
|
||||
func (o opt) Selected() func() {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SimpleItem(title, text string, char rune) Option {
|
||||
return opt{title, text, char}
|
||||
}
|
||||
|
||||
func List(items []Option, cb func(*tview.List)) Option {
|
||||
if len(items) == 0 {
|
||||
return opt{}
|
||||
}
|
||||
|
||||
var index int
|
||||
|
||||
app := tview.NewApplication()
|
||||
list := tview.NewList()
|
||||
for _, item := range items {
|
||||
list.AddItem(item.Title(), item.Text(), item.Rune(), item.Selected())
|
||||
}
|
||||
list.Box.SetBorder(true) //.SetTitle(title)
|
||||
|
||||
if cb != nil {
|
||||
cb(list)
|
||||
}
|
||||
|
||||
list.SetSelectedFunc(func(idx int, _ string, _ string, _ rune) {
|
||||
index = idx
|
||||
app.Stop()
|
||||
})
|
||||
|
||||
if err := app.SetRoot(list, false).EnableMouse(true).Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return items[index]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue