Checkpoint commit

Have got a basic table select model working.  Now will try to setup modal models to support prompts and confirmations
This commit is contained in:
Leon Mika 2022-03-27 08:48:34 +11:00
parent 33115c7c13
commit 6ac22aad1f
8 changed files with 205 additions and 27 deletions

View file

@ -34,10 +34,12 @@ type uiModel struct {
table table.Model
viewport viewport.Model
// TEMP
tableSelect tea.Model
tableWidth, tableHeight int
ready bool
//resultSet *models.ResultSet
ready bool
state controllers.State
message string
@ -62,6 +64,11 @@ func NewModel(dispatcher *dispatcher.Dispatcher, commandController *commandctrl.
message: "Press s to scan",
textInput: textInput,
// TEMP
tableSelect: newSizeWaitModel(func(w, h int) tea.Model {
return newTableSelectModel(w, h)
}),
dispatcher: dispatcher,
commandController: commandController,
tableReadController: tableReadController,
@ -72,7 +79,7 @@ func NewModel(dispatcher *dispatcher.Dispatcher, commandController *commandctrl.
}
func (m uiModel) Init() tea.Cmd {
m.invokeOperation(context.Background(), m.tableReadController.Scan())
//m.invokeOperation(context.Background(), m.tableReadController.Scan())
return nil
}
@ -215,11 +222,13 @@ func (m uiModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
updatedTable, tableMsgs := m.table.Update(msg)
updatedViewport, viewportMsgs := m.viewport.Update(msg)
updatedTableSelectModel, tableSelectMsgs := m.tableSelect.Update(msg)
m.table = updatedTable
m.viewport = updatedViewport
m.tableSelect = updatedTableSelectModel
return m, tea.Batch(textInputCommands, tableMsgs, viewportMsgs)
return m, tea.Batch(textInputCommands, tableMsgs, viewportMsgs, tableSelectMsgs)
}
func (m uiModel) invokeOperation(ctx context.Context, op uimodels.Operation) {
@ -233,27 +242,32 @@ func (m uiModel) invokeOperation(ctx context.Context, op uimodels.Operation) {
}
func (m uiModel) View() string {
if !m.ready {
return "Initializing"
}
// TEMP
return m.tableSelect.View()
/*
if !m.ready {
return "Initializing"
}
if m.pendingInput != nil {
return lipgloss.JoinVertical(lipgloss.Top,
m.headerView(),
m.table.View(),
m.splitterView(),
m.viewport.View(),
m.textInput.View(),
)
}
if m.pendingInput != nil {
return lipgloss.JoinVertical(lipgloss.Top,
m.headerView(),
m.table.View(),
m.splitterView(),
m.viewport.View(),
m.textInput.View(),
m.footerView(),
)
}
return lipgloss.JoinVertical(lipgloss.Top,
m.headerView(),
m.table.View(),
m.splitterView(),
m.viewport.View(),
m.footerView(),
)
*/
}
func (m uiModel) headerView() string {

View file

@ -0,0 +1,48 @@
package ui
import (
tea "github.com/charmbracelet/bubbletea"
"log"
)
// sizeWaitModel is a model which waits until the first screen size message comes through. It then creates the
// submodel and delegates calls to that model
type sizeWaitModel struct {
constr func(width, height int) tea.Model
model tea.Model
}
func newSizeWaitModel(constr func(width, height int) tea.Model) tea.Model {
return sizeWaitModel{constr: constr}
}
func (s sizeWaitModel) Init() tea.Cmd {
return nil
}
func (s sizeWaitModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m := msg.(type) {
case tea.WindowSizeMsg:
log.Println("got window size message")
if s.model == nil {
log.Println("creating model")
s.model = s.constr(m.Width, m.Height)
s.model.Init()
}
}
var submodelCmds tea.Cmd
if s.model != nil {
log.Println("starting update")
s.model, submodelCmds = s.model.Update(msg)
log.Println("ending update")
}
return s, submodelCmds
}
func (s sizeWaitModel) View() string {
if s.model == nil {
return ""
}
return s.model.View()
}

View file

@ -0,0 +1,95 @@
package ui
import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4)
)
type tableSelectModel struct {
list list.Model
}
func (t tableSelectModel) Init() tea.Cmd {
return nil
}
func (t tableSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
t.list.SetHeight(msg.Height)
t.list.SetWidth(msg.Width)
return t, nil
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl+c":
return t, tea.Quit
case "enter":
//i, ok := m.list.SelectedItem().(item)
//if ok {
// m.choice = string(i)
//}
return t, tea.Quit
}
}
var cmd tea.Cmd
t.list, cmd = t.list.Update(msg)
return t, cmd
}
func (t tableSelectModel) View() string {
return t.list.View()
}
func newTableSelectModel(w, h int) tableSelectModel {
tableItems := []tableItem{
{name: "alpha"},
{name: "beta"},
{name: "gamma"},
}
items := toListItems(tableItems)
delegate := list.NewDefaultDelegate()
delegate.ShowDescription = false
return tableSelectModel{
list: list.New(items, delegate, w, h),
}
}
type tableItem struct {
name string
}
func (ti tableItem) FilterValue() string {
return ""
}
func (ti tableItem) Title() string {
return ti.name
}
func (ti tableItem) Description() string {
return "abc"
}
func toListItems[T list.Item](xs []T) []list.Item {
ls := make([]list.Item, len(xs))
for i, x := range xs {
ls[i] = x
}
return ls
}