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
This commit is contained in:
swanadiva
2026-06-23 15:45:13 +07:00
parent 53db03814e
commit 79f3917a66
+28 -27
View File
@@ -111,49 +111,50 @@ func (t *HostListTab) View() string {
availW = 80 availW = 80
} }
// Constrain content width so cards don't stretch too wide cardW := availW - 6
contentW := availW if cardW > 74 {
if contentW > 80 { cardW = 74
contentW = 80 }
if cardW < 30 {
cardW = 30
} }
var b strings.Builder var b strings.Builder
// Centered title // Capped vertical padding
title := AppTitleStyle.Render(" HOSTKEEPER - SSH Manager ") b.WriteString(strings.Repeat("\n", 2))
b.WriteString(lipgloss.Place(contentW, 1, lipgloss.Center, lipgloss.Center, title))
// Title
b.WriteString(centerText(AppTitleStyle.Render(" HOSTKEEPER - SSH Manager "), availW))
b.WriteString("\n\n") b.WriteString("\n\n")
if len(t.hosts) == 0 { if len(t.hosts) == 0 {
b.WriteString(lipgloss.Place(contentW, 1, lipgloss.Center, lipgloss.Center, b.WriteString(centerText(SubtitleStyle.Render("No hosts found. Add with Ctrl+N"), availW))
SubtitleStyle.Render("No hosts found. Add with Ctrl+N"))) b.WriteString("\n")
} else { } else {
cardW := contentW - 4
if cardW < 30 {
cardW = 30
}
for i, host := range t.hosts { for i, host := range t.hosts {
entry := renderHostEntry(host, i == t.selectedIndex, cardW) 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") b.WriteString("\n")
} }
} }
// Center horizontally within the full terminal width return b.String()
content := lipgloss.Place(availW, 1, lipgloss.Center, lipgloss.Top, b.String()) }
// Add vertical padding to push content toward center of screen // centerText centers a line of text horizontally within the given width,
availH := t.height - 5 // respecting ANSI escape codes for accurate display width.
if availH < 0 { func centerText(s string, width int) string {
availH = 0 w := lipgloss.Width(s)
if w >= width {
return s
} }
contentLines := strings.Count(content, "\n") + 1 pad := strings.Repeat(" ", (width-w)/2)
if contentLines < availH { return pad + s
topPad := (availH - contentLines) / 2
content = strings.Repeat("\n", topPad) + content
}
return content
} }
// Close is a no-op for host list tab // Close is a no-op for host list tab