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
}
// 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