🐛 Fail gracefully when none found

This commit is contained in:
Dan Jones 2023-11-11 18:51:38 -06:00
commit 7a198a0273
7 changed files with 37 additions and 8 deletions

26
io/boolean/boolean.go Normal file
View file

@ -0,0 +1,26 @@
package boolean
import "github.com/rivo/tview"
func Choose(text string) bool {
choice := false
app := tview.NewApplication()
modal := tview.NewModal()
if text != "" {
modal.SetText(text)
}
modal.AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
choice = true
}
app.Stop()
})
if err := app.SetRoot(modal, false).EnableMouse(true).Run(); err != nil {
panic(err)
}
return choice
}

66
io/list/list.go Normal file
View 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(title string, 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, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
return items[index]
}

20
io/message/message.go Normal file
View file

@ -0,0 +1,20 @@
package message
import "github.com/rivo/tview"
func Message(text string) {
app := tview.NewApplication()
modal := tview.NewModal()
if text != "" {
modal.SetText(text)
}
modal.AddButtons([]string{"Ok"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
app.Stop()
})
if err := app.SetRoot(modal, false).EnableMouse(true).Run(); err != nil {
panic(err)
}
}