table-select: fixed sizing bug

This commit is contained in:
Leon Mika 2022-03-28 21:07:11 +11:00
parent 826a28664a
commit 6f323fa4cf
8 changed files with 31 additions and 19 deletions

View file

@ -9,7 +9,6 @@ import (
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
@ -34,9 +33,7 @@ func main() {
ctx := context.Background()
// TEMP
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion("ap-southeast-2"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("abc", "123", "")))
cfg, err := config.LoadDefaultConfig(ctx)
// END TEMP
if err != nil {

View file

@ -21,7 +21,7 @@ func NewModel(rc *controllers.TableReadController) Model {
div := dynamoitemview.New()
m := statusandprompt.New(
layout.NewVBox(layout.LastChildFixedAt(11), dtv, div),
layout.NewVBox(layout.LastChildFixedAt(17), dtv, div),
"Hello world",
)
root := layout.FullScreen(tableselect.New(m))

View file

@ -57,7 +57,8 @@ func (m Model) View() string {
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.frameTitle.HeaderHeight())
m.viewport = viewport.New(w, h-m.frameTitle.HeaderHeight())
m.viewport.SetContent("")
m.ready = true
} else {
m.viewport.Width = w

View file

@ -12,6 +12,10 @@ type equalSize struct {
}
func (l equalSize) childSize(idx, cnt, available int) int {
if cnt == 1 {
return available
}
childrenHeight := available / cnt
lastChildRem := available % cnt
if idx == cnt-1 {

View file

@ -7,7 +7,7 @@ type tableItem struct {
}
func (ti tableItem) FilterValue() string {
return ti.name
return ""
}
func (ti tableItem) Title() string {

View file

@ -27,6 +27,7 @@ func newListController(tableNames []string, w, h int) listController {
delegate.ShowDescription = false
list := list.New(items, delegate, w, h)
list.SetShowTitle(false)
return listController{list: list}
}

View file

@ -3,21 +3,25 @@ package tableselect
import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lmika/awstools/internal/dynamo-browse/controllers"
"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/utils"
)
type Model struct {
frameTitle frame.FrameTitle
listController listController
submodel tea.Model
pendingSelection *controllers.PromptForTableMsg
listController listController
isLoading bool
w, h int
}
func New(submodel tea.Model) Model {
return Model{submodel: submodel}
frameTitle := frame.NewFrameTitle("Select table", false)
return Model{frameTitle: frameTitle, submodel: submodel}
}
func (m Model) Init() tea.Cmd {
@ -30,7 +34,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case controllers.PromptForTableMsg:
m.isLoading = false
m.pendingSelection = &msg
m.listController = newListController(msg.Tables, m.w, m.h)
m.listController = newListController(msg.Tables, m.w, m.h-m.frameTitle.HeaderHeight())
return m, nil
case indicateLoadingTablesMsg:
m.isLoading = true
@ -58,19 +62,25 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m Model) View() string {
if m.pendingSelection != nil {
return m.listController.View()
return lipgloss.JoinVertical(lipgloss.Top, m.frameTitle.View(), m.listController.View())
} else if m.isLoading {
return "Loading tables"
return lipgloss.JoinVertical(lipgloss.Top, m.frameTitle.View(), "Loading tables")
}
return m.submodel.View()
}
func (m Model) shouldShow() bool {
return m.pendingSelection != nil || m.isLoading
}
func (m Model) Resize(w, h int) layout.ResizingModel {
m.w, m.h = w, h
m.submodel = layout.Resize(m.submodel, w, h)
m.frameTitle.Resize(w, h)
if m.pendingSelection != nil {
m.listController = m.listController.Resize(w, h).(listController)
m.listController = m.listController.Resize(w, h-m.frameTitle.HeaderHeight()).(listController)
}
return m
}

View file

@ -2,15 +2,14 @@ package main
import (
"context"
"github.com/brianvoe/gofakeit/v6"
"github.com/google/uuid"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/brianvoe/gofakeit/v6"
"github.com/google/uuid"
"github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/lmika/awstools/internal/dynamo-browse/providers/dynamo"
"github.com/lmika/awstools/internal/dynamo-browse/services/tables"
@ -22,9 +21,7 @@ func main() {
tableName := "awstools-test"
totalItems := 300
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion("ap-southeast-2"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("abc", "123", "")))
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
cli.Fatalf("cannot load AWS config: %v", err)
}
@ -64,6 +61,8 @@ func main() {
dynamoProvider := dynamo.NewProvider(dynamoClient)
tableService := tables.NewService(dynamoProvider)
_, _ = tableService, tableInfo
for i := 0; i < totalItems; i++ {
key := uuid.New().String()
if err := tableService.Put(ctx, tableInfo, models.Item{