diff --git a/cmd/dynamo-browse/main.go b/cmd/dynamo-browse/main.go index ee61135..f43c724 100644 --- a/cmd/dynamo-browse/main.go +++ b/cmd/dynamo-browse/main.go @@ -23,7 +23,6 @@ import ( "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/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/modal" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/statusandprompt" @@ -81,8 +80,8 @@ func main() { var model tea.Model = statusandprompt.New( layout.NewVBox( layout.LastChildFixedAt(11), - frame.NewFrame("This is the header", true, dynamotableview.New(tableReadController)), - frame.NewFrame("This is another header", false, dynamoitemview.New()), + dynamotableview.New(tableReadController), + dynamoitemview.New(), ), "Hello world", ) diff --git a/internal/dynamo-browse/ui/teamodels/dynamoitemview/model.go b/internal/dynamo-browse/ui/teamodels/dynamoitemview/model.go index 6c962b7..03dfead 100644 --- a/internal/dynamo-browse/ui/teamodels/dynamoitemview/model.go +++ b/internal/dynamo-browse/ui/teamodels/dynamoitemview/model.go @@ -8,14 +8,17 @@ import ( "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" "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" ) type Model struct { - ready bool - viewport viewport.Model - w, h int + ready bool + frameTitle frame.FrameTitle + viewport viewport.Model + w, h int // model state currentResultSet *models.ResultSet @@ -24,7 +27,8 @@ type Model struct { func New() 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 { 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 { m.w, m.h = w, h if !m.ready { - m.viewport = viewport.New(w, h-1) + m.viewport = viewport.New(w, h-1-m.frameTitle.HeaderHeight()) m.ready = true } else { m.viewport.Width = w - m.viewport.Height = h + m.viewport.Height = h - m.frameTitle.HeaderHeight() } + m.frameTitle.Resize(w, h) return m } @@ -84,6 +89,6 @@ func (m *Model) updateViewportToSelectedMessage() { tabWriter.Flush() m.viewport.Width = m.w - m.viewport.Height = m.h + m.viewport.Height = m.h - m.frameTitle.HeaderHeight() m.viewport.SetContent(viewportContent.String()) } diff --git a/internal/dynamo-browse/ui/teamodels/dynamotableview/model.go b/internal/dynamo-browse/ui/teamodels/dynamotableview/model.go index 09b0d1b..b2db4f0 100644 --- a/internal/dynamo-browse/ui/teamodels/dynamotableview/model.go +++ b/internal/dynamo-browse/ui/teamodels/dynamotableview/model.go @@ -3,14 +3,17 @@ package dynamotableview import ( table "github.com/calyptia/go-bubble-table" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" "github.com/lmika/awstools/internal/dynamo-browse/controllers" "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/frame" "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/layout" ) type Model struct { tableReadControllers *controllers.TableReadController + frameTitle frame.FrameTitle table table.Model w, h int @@ -23,7 +26,13 @@ func New(tableReadControllers *controllers.TableReadController) Model { rows := make([]table.Row, 0) 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 { @@ -58,19 +67,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } 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 { 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 } func (m *Model) updateTable() { 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)) for i, r := range resultSet.Items { newRows[i] = itemTableRow{resultSet, r} diff --git a/internal/dynamo-browse/ui/teamodels/frame/frame.go b/internal/dynamo-browse/ui/teamodels/frame/frame.go index eaf7089..c9a3a95 100644 --- a/internal/dynamo-browse/ui/teamodels/frame/frame.go +++ b/internal/dynamo-browse/ui/teamodels/frame/frame.go @@ -1,66 +1,51 @@ package frame 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" + + "github.com/charmbracelet/lipgloss" + "github.com/lmika/awstools/internal/dynamo-browse/ui/teamodels/utils" ) var ( activeHeaderStyle = lipgloss.NewStyle(). - Bold(true). - Foreground(lipgloss.Color("#ffffff")). - Background(lipgloss.Color("#4479ff")) + Bold(true). + Foreground(lipgloss.Color("#ffffff")). + Background(lipgloss.Color("#4479ff")) inactiveHeaderStyle = lipgloss.NewStyle(). - Foreground(lipgloss.Color("#000000")). - Background(lipgloss.Color("#d1d1d1")) + Foreground(lipgloss.Color("#000000")). + Background(lipgloss.Color("#d1d1d1")) ) // Frame is a frame that appears in the -type Frame struct { +type FrameTitle struct { header string active bool - model layout.ResizingModel width int } -func NewFrame(header string, active bool, model layout.ResizingModel) Frame { - return Frame{header, active, model, 0} +func NewFrameTitle(header string, active bool) FrameTitle { + return FrameTitle{header, active, 0} } -func (f Frame) Init() tea.Cmd { - return f.model.Init() +func (f *FrameTitle) SetTitle(title string) { + f.header = title } -func (f Frame) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg.(type) { - 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 FrameTitle) View() string { + return f.headerView() } -func (f Frame) View() string { - return lipgloss.JoinVertical(lipgloss.Top, f.headerView(), f.model.View()) -} - -func (f Frame) Resize(w, h int) layout.ResizingModel { +func (f *FrameTitle) Resize(w, h int) { 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 if f.active { style = activeHeaderStyle