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:
+28
-27
@@ -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
|
|
||||||
availH := t.height - 5
|
|
||||||
if availH < 0 {
|
|
||||||
availH = 0
|
|
||||||
}
|
|
||||||
contentLines := strings.Count(content, "\n") + 1
|
|
||||||
if contentLines < availH {
|
|
||||||
topPad := (availH - contentLines) / 2
|
|
||||||
content = strings.Repeat("\n", topPad) + content
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return content
|
// 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
|
||||||
|
}
|
||||||
|
pad := strings.Repeat(" ", (width-w)/2)
|
||||||
|
return pad + s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close is a no-op for host list tab
|
// Close is a no-op for host list tab
|
||||||
|
|||||||
Reference in New Issue
Block a user