From 2f46f78d7f6712d4e07b61b33785a2d9380d4f0c Mon Sep 17 00:00:00 2001 From: Kaya-Sem Date: Mon, 19 Aug 2024 13:37:33 +0200 Subject: [PATCH] dynamic placement of detailview --- cmd/tables/baseTable.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/tables/baseTable.go b/cmd/tables/baseTable.go index c78a339..be830d6 100644 --- a/cmd/tables/baseTable.go +++ b/cmd/tables/baseTable.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "golang.org/x/term" ) type Data interface { @@ -50,9 +51,31 @@ func (m *Model[T]) View() string { if m.showMessage { return m.message } + + // Width terminal + width, _, _ := term.GetSize(int(os.Stdout.Fd())) + + dynamicHeight := len(m.data) + if dynamicHeight > 5 { + dynamicHeight = 5 + } + + if lipgloss.Width(m.table.View())+lipgloss.Width(m.selectedDetails) <= width { + // Horizontal layout + m.table.SetHeight(tableHeight) + } else { + // Vertical layout + m.table.SetHeight(dynamicHeight) + } + tableView := m.table.View() detailsView := DetailsBoxStyle.Render(m.selectedDetails) - return lipgloss.JoinHorizontal(lipgloss.Top, tableView, detailsView) + + if lipgloss.Width(tableView)+lipgloss.Width(detailsView) <= width { + return lipgloss.JoinHorizontal(lipgloss.Top, tableView, detailsView) + } else { + return lipgloss.JoinVertical(lipgloss.Left, tableView, detailsView) + } } func (m *Model[T]) updateSelectedDetails() { @@ -65,6 +88,7 @@ func (m *Model[T]) updateSelectedDetails() { } } +// TODO: do this with an interface instead func (m *Model[T]) getDetailedInfo(item T) string { switch v := any(item).(type) { case api.Connection: