From 79f3917a665d3c02ec3d4e2fca4a80ca33d148e8 Mon Sep 17 00:00:00 2001 From: swanadiva Date: Tue, 23 Jun 2026 15:45:13 +0700 Subject: [PATCH] fix: cap vertical padding + horizontal centering per line - 2 lines top padding instead of aggressive vertical centering - centerText() uses lipgloss.Width for accurate ANSI-aware centering - Each card line centered individually within terminal width - Card width capped at 74 chars --- pkg/tui/host_list_tab.go | 55 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pkg/tui/host_list_tab.go b/pkg/tui/host_list_tab.go index 14953e6..209eb1e 100644 --- a/pkg/tui/host_list_tab.go +++ b/pkg/tui/host_list_tab.go @@ -111,49 +111,50 @@ func (t *HostListTab) View() string { availW = 80 } - // Constrain content width so cards don't stretch too wide - contentW := availW - if contentW > 80 { - contentW = 80 + cardW := availW - 6 + if cardW > 74 { + cardW = 74 + } + if cardW < 30 { + cardW = 30 } var b strings.Builder - // Centered title - title := AppTitleStyle.Render(" HOSTKEEPER - SSH Manager ") - b.WriteString(lipgloss.Place(contentW, 1, lipgloss.Center, lipgloss.Center, title)) + // Capped vertical padding + b.WriteString(strings.Repeat("\n", 2)) + + // Title + b.WriteString(centerText(AppTitleStyle.Render(" HOSTKEEPER - SSH Manager "), availW)) b.WriteString("\n\n") if len(t.hosts) == 0 { - b.WriteString(lipgloss.Place(contentW, 1, lipgloss.Center, lipgloss.Center, - SubtitleStyle.Render("No hosts found. Add with Ctrl+N"))) + b.WriteString(centerText(SubtitleStyle.Render("No hosts found. Add with Ctrl+N"), availW)) + b.WriteString("\n") } else { - cardW := contentW - 4 - if cardW < 30 { - cardW = 30 - } for i, host := range t.hosts { entry := renderHostEntry(host, i == t.selectedIndex, cardW) - b.WriteString(entry) + // Center the card (which produces 4 lines) horizontally + for _, line := range strings.Split(entry, "\n") { + b.WriteString(centerText(line, availW)) + b.WriteString("\n") + } b.WriteString("\n") } } - // Center horizontally within the full terminal width - content := lipgloss.Place(availW, 1, lipgloss.Center, lipgloss.Top, b.String()) + return b.String() +} - // Add vertical padding to push content toward center of screen - availH := t.height - 5 - if availH < 0 { - availH = 0 +// centerText centers a line of text horizontally within the given width, +// respecting ANSI escape codes for accurate display width. +func centerText(s string, width int) string { + w := lipgloss.Width(s) + if w >= width { + return s } - contentLines := strings.Count(content, "\n") + 1 - if contentLines < availH { - topPad := (availH - contentLines) / 2 - content = strings.Repeat("\n", topPad) + content - } - - return content + pad := strings.Repeat(" ", (width-w)/2) + return pad + s } // Close is a no-op for host list tab