table-select: have got a basic loading indicator built

This commit is contained in:
Leon Mika 2022-03-27 22:43:36 +11:00
parent 5d213c4ee8
commit 8b743351dd
3 changed files with 50 additions and 17 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/lmika/gopkgs/cli" "github.com/lmika/gopkgs/cli"
"log" "log"
"os" "os"
"time"
) )
func main() { func main() {
@ -135,9 +136,18 @@ func newTestModel(descr string) tea.Model {
OnKeyPressed: func(k string) tea.Cmd { OnKeyPressed: func(k string) tea.Cmd {
log.Println("got key press: " + k) log.Println("got key press: " + k)
if k == "enter" { if k == "enter" {
return tableselect.ShowTableSelect(func(n string) tea.Cmd { return tea.Batch(
tableselect.IndicateLoadingTables(),
tea.Sequentially(
func() tea.Msg {
<-time.After(2 * time.Second)
return nil
},
tableselect.ShowTableSelect(func(n string) tea.Cmd {
return statusandprompt.SetStatus("New table = " + n) return statusandprompt.SetStatus("New table = " + n)
}) }),
),
)
} else if k == "k" { } else if k == "k" {
return modal.PopMode return modal.PopMode
} }

View file

@ -2,6 +2,11 @@ package tableselect
import tea "github.com/charmbracelet/bubbletea" import tea "github.com/charmbracelet/bubbletea"
func IndicateLoadingTables() tea.Cmd {
return func() tea.Msg {
return indicateLoadingTablesMsg{}
}
}
func ShowTableSelect(onSelected func(n string) tea.Cmd) tea.Cmd { func ShowTableSelect(onSelected func(n string) tea.Cmd) tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
return showTableSelectMsg{ return showTableSelectMsg{
@ -10,6 +15,8 @@ func ShowTableSelect(onSelected func(n string) tea.Cmd) tea.Cmd {
} }
} }
type indicateLoadingTablesMsg struct{}
type showTableSelectMsg struct { type showTableSelectMsg struct {
onSelected func(n string) tea.Cmd onSelected func(n string) tea.Cmd
} }

View file

@ -3,54 +3,70 @@ package tableselect
import ( import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/modal" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/utils"
) )
type Model struct { type Model struct {
submodel tea.Model
pendingSelection *showTableSelectMsg pendingSelection *showTableSelectMsg
modal modal.Modal listController listController
isLoading bool
w, h int w, h int
} }
func New(submodel tea.Model) Model { func New(submodel tea.Model) Model {
return Model{modal: modal.New(submodel)} return Model{submodel: submodel}
} }
func (m Model) Init() tea.Cmd { func (m Model) Init() tea.Cmd {
return m.modal.Init() return m.submodel.Init()
} }
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cc utils.CmdCollector
switch msg := msg.(type) { switch msg := msg.(type) {
case showTableSelectMsg: case showTableSelectMsg:
m.isLoading = false
m.pendingSelection = &msg m.pendingSelection = &msg
m.modal.Push(newListController(m.w, m.h)) m.listController = newListController(m.w, m.h)
return m, nil
case indicateLoadingTablesMsg:
m.isLoading = true
return m, nil return m, nil
case tea.KeyMsg: case tea.KeyMsg:
if m.modal.Len() > 0 { if m.pendingSelection != nil {
switch msg.String() { switch msg.String() {
case "enter": case "enter":
listController := m.modal.Pop().(listController)
var sel showTableSelectMsg var sel showTableSelectMsg
sel, m.pendingSelection = *m.pendingSelection, nil sel, m.pendingSelection = *m.pendingSelection, nil
return m, sel.onSelected(listController.list.SelectedItem().(tableItem).name) return m, sel.onSelected(m.listController.list.SelectedItem().(tableItem).name)
default:
m.listController = cc.Collect(m.listController.Update(msg)).(listController)
return m, cc.Cmd()
} }
} }
} }
newModal, cmd := m.modal.Update(msg) m.submodel = cc.Collect(m.submodel.Update(msg))
m.modal = newModal.(modal.Modal) return m, cc.Cmd()
return m, cmd
} }
func (m Model) View() string { func (m Model) View() string {
return m.modal.View() if m.pendingSelection != nil {
return m.listController.View()
} else if m.isLoading {
return "Loading tables"
}
return m.submodel.View()
} }
func (m Model) Resize(w, h int) layout.ResizingModel { func (m Model) Resize(w, h int) layout.ResizingModel {
m.w, m.h = w, h m.w, m.h = w, h
m.modal = layout.Resize(m.modal, w, h).(modal.Modal) m.submodel = layout.Resize(m.submodel, w, h)
if m.pendingSelection != nil {
m.listController = m.listController.Resize(w, h).(listController)
}
return m return m
} }