hosts-go/internal/tui/view.go

40 lines
1,022 B
Go

package tui
import (
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
)
var (
listStyle = lipgloss.NewStyle().Padding(0, 1)
detailStyle = lipgloss.NewStyle().Padding(0, 1)
)
// View renders the two-pane layout.
func (m Model) View() string {
listView := m.list.View()
var detail strings.Builder
if len(m.hosts.Entries) > 0 {
entry := m.hosts.Entries[m.list.Index()]
status := "active"
if !entry.Active {
status = "inactive"
}
fmt.Fprintf(&detail, "IP: %s\n", entry.IP)
fmt.Fprintf(&detail, "Host: %s\n", entry.Hostname)
if len(entry.Aliases) > 0 {
fmt.Fprintf(&detail, "Aliases: %s\n", strings.Join(entry.Aliases, ", "))
}
if entry.Comment != "" {
fmt.Fprintf(&detail, "Comment: %s\n", entry.Comment)
}
fmt.Fprintf(&detail, "Status: %s", status)
}
left := listStyle.Width(m.width / 2).Height(m.height).Render(listView)
right := detailStyle.Width(m.width - m.width/2).Height(m.height).Render(detail.String())
return lipgloss.JoinHorizontal(lipgloss.Top, left, right)
}