Rejigged the frames a little

This commit is contained in:
Leon Mika 2022-03-27 21:58:41 +00:00 committed by GitHub
parent c3d19d5891
commit 6ab8a3ef44
4 changed files with 53 additions and 51 deletions

View file

@ -23,7 +23,6 @@ import (
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/dynamoitemview" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/dynamoitemview"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/dynamotableview" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/dynamotableview"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/frame"
"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/modal"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/statusandprompt" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/statusandprompt"
@ -81,8 +80,8 @@ func main() {
var model tea.Model = statusandprompt.New( var model tea.Model = statusandprompt.New(
layout.NewVBox( layout.NewVBox(
layout.LastChildFixedAt(11), layout.LastChildFixedAt(11),
frame.NewFrame("This is the header", true, dynamotableview.New(tableReadController)), dynamotableview.New(tableReadController),
frame.NewFrame("This is another header", false, dynamoitemview.New()), dynamoitemview.New(),
), ),
"Hello world", "Hello world",
) )

View file

@ -8,14 +8,17 @@ import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lmika/awstools/internal/dynamo-browse/models" "github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/frame"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout"
) )
type Model struct { type Model struct {
ready bool ready bool
viewport viewport.Model frameTitle frame.FrameTitle
w, h int viewport viewport.Model
w, h int
// model state // model state
currentResultSet *models.ResultSet currentResultSet *models.ResultSet
@ -24,7 +27,8 @@ type Model struct {
func New() Model { func New() Model {
return Model{ return Model{
viewport: viewport.New(100, 100), frameTitle: frame.NewFrameTitle("Item", false),
viewport: viewport.New(100, 100),
} }
} }
@ -47,18 +51,19 @@ func (m Model) View() string {
if !m.ready { if !m.ready {
return "" return ""
} }
return m.viewport.View() return lipgloss.JoinVertical(lipgloss.Top, m.frameTitle.View(), m.viewport.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
if !m.ready { if !m.ready {
m.viewport = viewport.New(w, h-1) m.viewport = viewport.New(w, h-1-m.frameTitle.HeaderHeight())
m.ready = true m.ready = true
} else { } else {
m.viewport.Width = w m.viewport.Width = w
m.viewport.Height = h m.viewport.Height = h - m.frameTitle.HeaderHeight()
} }
m.frameTitle.Resize(w, h)
return m return m
} }
@ -84,6 +89,6 @@ func (m *Model) updateViewportToSelectedMessage() {
tabWriter.Flush() tabWriter.Flush()
m.viewport.Width = m.w m.viewport.Width = m.w
m.viewport.Height = m.h m.viewport.Height = m.h - m.frameTitle.HeaderHeight()
m.viewport.SetContent(viewportContent.String()) m.viewport.SetContent(viewportContent.String())
} }

View file

@ -3,14 +3,17 @@ package dynamotableview
import ( import (
table "github.com/calyptia/go-bubble-table" table "github.com/calyptia/go-bubble-table"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lmika/awstools/internal/dynamo-browse/controllers" "github.com/lmika/awstools/internal/dynamo-browse/controllers"
"github.com/lmika/awstools/internal/dynamo-browse/models" "github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/dynamoitemview" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/dynamoitemview"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/frame"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout"
) )
type Model struct { type Model struct {
tableReadControllers *controllers.TableReadController tableReadControllers *controllers.TableReadController
frameTitle frame.FrameTitle
table table.Model table table.Model
w, h int w, h int
@ -23,7 +26,13 @@ func New(tableReadControllers *controllers.TableReadController) Model {
rows := make([]table.Row, 0) rows := make([]table.Row, 0)
tbl.SetRows(rows) tbl.SetRows(rows)
return Model{tableReadControllers: tableReadControllers, table: tbl} frameTitle := frame.NewFrameTitle("No table", true)
return Model{
tableReadControllers: tableReadControllers,
frameTitle: frameTitle,
table: tbl,
}
} }
func (m Model) Init() tea.Cmd { func (m Model) Init() tea.Cmd {
@ -58,19 +67,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m Model) View() string { func (m Model) View() string {
return m.table.View() return lipgloss.JoinVertical(lipgloss.Top, m.frameTitle.View(), m.table.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.table.SetSize(w, h) tblHeight := h - m.frameTitle.HeaderHeight()
m.table.SetSize(w, tblHeight)
m.frameTitle.Resize(w, h)
return m return m
} }
func (m *Model) updateTable() { func (m *Model) updateTable() {
resultSet := m.resultSet resultSet := m.resultSet
newTbl := table.New(resultSet.Columns, m.w, m.h) m.frameTitle.SetTitle("Table: " + resultSet.TableInfo.Name)
newTbl := table.New(resultSet.Columns, m.w, m.h-m.frameTitle.HeaderHeight())
newRows := make([]table.Row, len(resultSet.Items)) newRows := make([]table.Row, len(resultSet.Items))
for i, r := range resultSet.Items { for i, r := range resultSet.Items {
newRows[i] = itemTableRow{resultSet, r} newRows[i] = itemTableRow{resultSet, r}

View file

@ -1,66 +1,51 @@
package frame package frame
import ( import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/utils"
"strings" "strings"
"github.com/charmbracelet/lipgloss"
"github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/utils"
) )
var ( var (
activeHeaderStyle = lipgloss.NewStyle(). activeHeaderStyle = lipgloss.NewStyle().
Bold(true). Bold(true).
Foreground(lipgloss.Color("#ffffff")). Foreground(lipgloss.Color("#ffffff")).
Background(lipgloss.Color("#4479ff")) Background(lipgloss.Color("#4479ff"))
inactiveHeaderStyle = lipgloss.NewStyle(). inactiveHeaderStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#000000")). Foreground(lipgloss.Color("#000000")).
Background(lipgloss.Color("#d1d1d1")) Background(lipgloss.Color("#d1d1d1"))
) )
// Frame is a frame that appears in the // Frame is a frame that appears in the
type Frame struct { type FrameTitle struct {
header string header string
active bool active bool
model layout.ResizingModel
width int width int
} }
func NewFrame(header string, active bool, model layout.ResizingModel) Frame { func NewFrameTitle(header string, active bool) FrameTitle {
return Frame{header, active, model, 0} return FrameTitle{header, active, 0}
} }
func (f Frame) Init() tea.Cmd { func (f *FrameTitle) SetTitle(title string) {
return f.model.Init() f.header = title
} }
func (f Frame) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (f FrameTitle) View() string {
switch msg.(type) { return f.headerView()
case tea.KeyMsg:
// If frame is not active, do not receive key messages
if !f.active {
return f, nil
}
}
newModel, cmd := f.model.Update(msg)
f.model = newModel.(layout.ResizingModel)
return f, cmd
} }
func (f Frame) View() string { func (f *FrameTitle) Resize(w, h int) {
return lipgloss.JoinVertical(lipgloss.Top, f.headerView(), f.model.View())
}
func (f Frame) Resize(w, h int) layout.ResizingModel {
f.width = w f.width = w
headerHeight := lipgloss.Height(f.headerView())
f.model = f.model.Resize(w, h-headerHeight)
return f
} }
func (f Frame) headerView() string { func (f FrameTitle) HeaderHeight() int {
return lipgloss.Height(f.headerView())
}
func (f FrameTitle) headerView() string {
style := inactiveHeaderStyle style := inactiveHeaderStyle
if f.active { if f.active {
style = activeHeaderStyle style = activeHeaderStyle