Skip to content

mission control for agent skills

Fix responsive TUI layout

Alvin Unreala99e68e40acd
diff --git a/internal/tui/tui.go b/internal/tui/tui.go
index bc3219e..be674b1 100644
--- a/internal/tui/tui.go
+++ b/internal/tui/tui.go
@@ -42,6 +42,29 @@ type appModel struct {
viewport viewport.Model
}
+type paneLayout struct {
+ OuterWidth int
+ OuterHeight int
+ StyleWidth int
+ StyleHeight int
+ ContentWidth int
+ ContentHeight int
+}
+
+type appLayout struct {
+ Small bool
+ Width int
+ Height int
+ Left paneLayout
+ List paneLayout
+ Detail paneLayout
+}
+
+const (
+ minLayoutWidth = 40
+ minLayoutHeight = 7
+)
+
var (
titleStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("212"))
borderStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Padding(0, 1)
@@ -157,12 +180,18 @@ func (m appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m *appModel) syncViewport() {
- _, _, detailOuter := paneOuterWidths(max(1, m.width))
- detailInner := max(1, detailOuter-4)
- innerHeight := max(1, max(8, m.height-2)-2)
- m.viewport.Width = detailInner
- m.viewport.Height = innerHeight
- m.viewport.SetContent(m.detailText(detailInner))
+ layout := newAppLayout(m.width, m.height)
+ if layout.Small {
+ m.viewport.Width = 0
+ m.viewport.Height = 0
+ m.viewport.SetContent("")
+ m.viewport.SetYOffset(0)
+ return
+ }
+ m.viewport.Width = layout.Detail.ContentWidth
+ m.viewport.Height = layout.Detail.ContentHeight
+ m.viewport.SetContent(m.detailText(layout.Detail.ContentWidth))
+ m.clampViewportOffset()
}
func (m *appModel) clampSelection() {
@@ -181,33 +210,31 @@ func (m *appModel) clampSelection() {
func (m appModel) View() string {
if m.err != nil {
- return errorStyle.Render(fmt.Sprintf("LazySkills error: %s\n\nPress q to quit.", compat.SanitizeMetadata(m.err.Error())))
- }
- if m.width == 0 {
- m.width = 100
- }
- if m.height == 0 {
- m.height = 32
- }
-
- outerHeight := max(8, m.height-2)
- innerHeight := max(1, outerHeight-2)
- leftOuter, listOuter, detailOuter := paneOuterWidths(m.width)
- leftInner := max(1, leftOuter-4)
- listInner := max(1, listOuter-4)
- detailInner := max(1, detailOuter-4)
-
- leftStyle := borderStyle.Width(leftInner).MaxWidth(leftInner).Height(innerHeight).MaxHeight(innerHeight)
- listStyle := borderStyle.Width(listInner).MaxWidth(listInner).Height(innerHeight).MaxHeight(innerHeight)
- detailStyle := borderStyle.Width(detailInner).MaxWidth(detailInner).Height(innerHeight).MaxHeight(innerHeight)
- left := leftStyle.Render(fitLines(m.filterPane(), innerHeight))
- list := listStyle.Render(fitLines(m.listPane(innerHeight), innerHeight))
- detail := detailStyle.Render(m.detailPane(innerHeight, detailInner))
- footer := m.footer()
+ return fitToScreen(errorStyle.Render(fmt.Sprintf("LazySkills error: %s\n\nPress q to quit.", compat.SanitizeMetadata(m.err.Error()))), viewWidth(m.width), viewHeight(m.height))
+ }
+ layout := newAppLayout(m.width, m.height)
+ if layout.Small {
+ return smallTerminalView(layout.Width, layout.Height)
+ }
+
+ // Keep View pure for callers: sync a local copy so render-time fallback
+ // sizing does not mutate the model stored by Bubble Tea.
+ viewModel := m
+ viewModel.width = layout.Width
+ viewModel.height = layout.Height
+ viewModel.syncViewport()
+
+ leftStyle := paneStyle(layout.Left)
+ listStyle := paneStyle(layout.List)
+ detailStyle := paneStyle(layout.Detail)
+ left := leftStyle.Render(fitLines(viewModel.filterPane(layout.Left.ContentWidth), layout.Left.ContentHeight))
+ list := listStyle.Render(fitLines(viewModel.listPane(layout.List.ContentHeight, layout.List.ContentWidth), layout.List.ContentHeight))
+ detail := detailStyle.Render(viewModel.detailPane())
+ footer := fitToScreen(viewModel.footer(), layout.Width, 1)
return lipgloss.JoinHorizontal(lipgloss.Top, left, list, detail) + "\n" + footer
}
-func (m appModel) filterPane() string {
+func (m appModel) filterPane(width int) string {
counts := map[model.Scope]int{}
issues := 0
for _, sk := range m.result.Skills {
@@ -230,7 +257,7 @@ func (m appModel) filterPane() string {
if len(m.result.HealthIssues) > 0 {
lines = append(lines, "", errorStyle.Render("Scan health"))
for _, issue := range m.result.HealthIssues {
- lines = append(lines, truncate(fmt.Sprintf("- %s: %s", compat.SanitizeMetadata(issue.Type), compat.SanitizeMetadata(issue.Message)), 36))
+ lines = append(lines, truncate(fmt.Sprintf("- %s: %s", compat.SanitizeMetadata(issue.Type), compat.SanitizeMetadata(issue.Message)), width))
}
}
if m.search != "" || m.searching {
@@ -253,7 +280,7 @@ func filterLine(label string, active bool) string {
return " " + label
}
-func (m appModel) listPane(height int) string {
+func (m appModel) listPane(height, width int) string {
items := m.filteredSkills()
lines := []string{titleStyle.Render("Skills")}
if len(items) == 0 {
@@ -279,20 +306,15 @@ func (m appModel) listPane(height int) string {
label += fmt.Sprintf(" !%d", len(view.HealthIssues))
}
if i == m.selected {
- lines = append(lines, selectedStyle.Render(truncate(label, 48)))
+ lines = append(lines, selectedStyle.Render(truncate(label, width)))
} else {
- lines = append(lines, truncate(label, 48))
+ lines = append(lines, truncate(label, width))
}
}
return strings.Join(lines, "\n")
}
-func (m appModel) detailPane(height, width int) string {
- if m.viewport.Width != width || m.viewport.Height != height || m.viewport.View() == "" {
- m.viewport.Width = width
- m.viewport.Height = height
- m.viewport.SetContent(m.detailText(width))
- }
+func (m appModel) detailPane() string {
return m.viewport.View()
}
@@ -341,7 +363,7 @@ func (m appModel) detailLines(width int) []string {
}
if m.commands {
lines = append(lines, "")
- lines = append(lines, m.commandPreview(items[m.selected], width-4)...)
+ lines = append(lines, m.commandPreview(items[m.selected], max(1, width-4))...)
return lines
}
if view.Preview != "" {
@@ -534,6 +556,76 @@ func fitToScreen(s string, width, height int) string {
return strings.Join(lines, "\n")
}
+func viewWidth(width int) int {
+ if width > 0 {
+ return width
+ }
+ return 100
+}
+
+func viewHeight(height int) int {
+ if height > 0 {
+ return height
+ }
+ return 32
+}
+
+func newAppLayout(width, height int) appLayout {
+ width = viewWidth(width)
+ height = viewHeight(height)
+ layout := appLayout{Width: width, Height: height}
+ if width < minLayoutWidth || height < minLayoutHeight {
+ layout.Small = true
+ return layout
+ }
+
+ leftOuter, listOuter, detailOuter := paneOuterWidths(width)
+ paneHeight := height - 1 // reserve one row for the footer
+ layout.Left = newPaneLayout(leftOuter, paneHeight)
+ layout.List = newPaneLayout(listOuter, paneHeight)
+ layout.Detail = newPaneLayout(detailOuter, paneHeight)
+ return layout
+}
+
+func newPaneLayout(outerWidth, outerHeight int) paneLayout {
+ contentWidth := max(1, outerWidth-borderStyle.GetHorizontalFrameSize())
+ contentHeight := max(1, outerHeight-borderStyle.GetVerticalFrameSize())
+ return paneLayout{
+ OuterWidth: outerWidth,
+ OuterHeight: outerHeight,
+ StyleWidth: contentWidth + borderStyle.GetHorizontalPadding(),
+ StyleHeight: contentHeight + borderStyle.GetVerticalPadding(),
+ ContentWidth: contentWidth,
+ ContentHeight: contentHeight,
+ }
+}
+
+func paneStyle(p paneLayout) lipgloss.Style {
+ return borderStyle.
+ Width(p.StyleWidth).
+ Height(p.StyleHeight).
+ MaxWidth(p.OuterWidth).
+ MaxHeight(p.OuterHeight)
+}
+
+func smallTerminalView(width, height int) string {
+ message := "Terminal too small. Please resize."
+ if height >= 2 && width >= 22 {
+ message = "Terminal too small.\nPlease resize."
+ }
+ return fitToScreen(message, width, height)
+}
+
+func (m *appModel) clampViewportOffset() {
+ maxOffset := max(0, m.viewport.TotalLineCount()-m.viewport.Height)
+ if m.viewport.YOffset > maxOffset {
+ m.viewport.SetYOffset(maxOffset)
+ }
+ if m.viewport.YOffset < 0 {
+ m.viewport.SetYOffset(0)
+ }
+}
+
func min(a, b int) int {
if a < b {
return a
diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go
index 4fa0a43..494a498 100644
--- a/internal/tui/tui_test.go
+++ b/internal/tui/tui_test.go
@@ -2,6 +2,7 @@ package tui
import (
"errors"
+ "fmt"
"strings"
"testing"
@@ -124,9 +125,10 @@ func TestCommandPreviewToggle(t *testing.T) {
func TestDetailPaneClipsLongPreview(t *testing.T) {
preview := strings.Repeat("line\n", 80)
m := appModel{width: 100, height: 20, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: "desc", Scope: model.ScopeProject, Preview: preview}}}}
- out := m.detailPane(8, 40)
+ m.syncViewport()
+ out := m.detailPane()
lines := strings.Split(out, "\n")
- if len(lines) > 8 {
+ if len(lines) > m.viewport.Height {
t.Fatalf("detail pane overflowed: got %d lines\n%s", len(lines), out)
}
}
@@ -150,13 +152,68 @@ func TestDetailScrollKeysMoveViewport(t *testing.T) {
func TestFullViewFitsTerminalDimensionsWithLongPreview(t *testing.T) {
preview := strings.Repeat("a very long line that should be clipped and not wrap the entire screen\n", 120)
m := appModel{width: 100, height: 24, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: strings.Repeat("description ", 30), Scope: model.ScopeProject, Preview: preview}}}}
+ assertViewFits(t, m, 100, 24)
+}
+
+func TestSmallTerminalFallbackFitsTinyHeights(t *testing.T) {
+ for _, height := range []int{4, 5, 6} {
+ t.Run(fmt.Sprintf("height_%d", height), func(t *testing.T) {
+ m := appModel{width: 80, height: height, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: "desc", Scope: model.ScopeProject}}}}
+ out := m.View()
+ assertRenderedSize(t, out, 80, height)
+ if strings.Contains(out, "╭") || strings.Contains(out, "╰") {
+ t.Fatalf("tiny terminal should render fallback, not cards: %q", out)
+ }
+ })
+ }
+}
+
+func TestResponsiveViewFitsCommonTerminalSizes(t *testing.T) {
+ preview := strings.Repeat("a very long line that should reflow into the detail viewport without breaking borders\n", 100)
+ sizes := []struct {
+ width int
+ height int
+ }{
+ {40, 7},
+ {60, 12},
+ {80, 20},
+ {100, 24},
+ {120, 40},
+ }
+ for _, size := range sizes {
+ t.Run(fmt.Sprintf("%dx%d", size.width, size.height), func(t *testing.T) {
+ m := appModel{width: size.width, height: size.height, result: model.ScanResult{Skills: []*model.Skill{{Name: "Long", Description: strings.Repeat("description ", 12), Scope: model.ScopeProject, Preview: preview}}}}
+ assertViewFits(t, m, size.width, size.height)
+ })
+ }
+}
+
+func TestNormalViewPreservesBottomBorders(t *testing.T) {
+ m := appModel{width: 100, height: 24, result: model.ScanResult{Skills: []*model.Skill{{Name: "Build", Description: "desc", Scope: model.ScopeProject}}}}
+ out := m.View()
+ assertRenderedSize(t, out, 100, 24)
+ if got := strings.Count(out, "╰"); got != 3 {
+ t.Fatalf("expected three complete bottom-left borders, got %d\n%s", got, out)
+ }
+ if got := strings.Count(out, "╯"); got != 3 {
+ t.Fatalf("expected three complete bottom-right borders, got %d\n%s", got, out)
+ }
+}
+
+func assertViewFits(t *testing.T, m appModel, width, height int) {
+ t.Helper()
out := m.View()
- if got := lipgloss.Height(out); got > 24 {
- t.Fatalf("view height overflowed: got %d want <= 24\n%s", got, out)
+ assertRenderedSize(t, out, width, height)
+}
+
+func assertRenderedSize(t *testing.T, out string, width, height int) {
+ t.Helper()
+ if got := lipgloss.Height(out); got > height {
+ t.Fatalf("view height overflowed: got %d want <= %d\n%s", got, height, out)
}
for i, line := range strings.Split(out, "\n") {
- if width := lipgloss.Width(line); width > 100 {
- t.Fatalf("line %d width overflowed: got %d want <= 100\n%s", i, width, line)
+ if got := lipgloss.Width(line); got > width {
+ t.Fatalf("line %d width overflowed: got %d want <= %d\n%s", i, got, width, line)
}
}
}