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

@ -2,6 +2,11 @@ package tableselect
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 {
return func() tea.Msg {
return showTableSelectMsg{
@ -10,6 +15,8 @@ func ShowTableSelect(onSelected func(n string) tea.Cmd) tea.Cmd {
}
}
type indicateLoadingTablesMsg struct{}
type showTableSelectMsg struct {
onSelected func(n string) tea.Cmd
}

View file

@ -3,54 +3,70 @@ package tableselect
import (
tea "github.com/charmbracelet/bubbletea"
"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 {
submodel tea.Model
pendingSelection *showTableSelectMsg
modal modal.Modal
listController listController
isLoading bool
w, h int
}
func New(submodel tea.Model) Model {
return Model{modal: modal.New(submodel)}
return Model{submodel: submodel}
}
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) {
var cc utils.CmdCollector
switch msg := msg.(type) {
case showTableSelectMsg:
m.isLoading = false
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
case tea.KeyMsg:
if m.modal.Len() > 0 {
if m.pendingSelection != nil {
switch msg.String() {
case "enter":
listController := m.modal.Pop().(listController)
var sel showTableSelectMsg
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.modal = newModal.(modal.Modal)
return m, cmd
m.submodel = cc.Collect(m.submodel.Update(msg))
return m, cc.Cmd()
}
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 {
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
}