vcom/internal/tui/view.go
vrubelroman 40b8722d58 feat(tui): App skeleton with mode machine, async navigation and effects (6.1.5)
The new UI's root model: Screen x viewer focus x native selection x
filter edit x modal stack, one inputOwner, keys resolved through the
registry, an action table that features extend from register_*.go.
Directory reads and previews run in Cmds with tickets: Update never
blocks (N-P1-1), stale results are dropped (U-P1-13), failed navigation
keeps the panel and a vanished directory climbs up (U-P1-8). q closes
layers before asking to quit (U-P1-16), PgDn pages (U-P1-6), the theme
list previews live and saves with config.SetKey. Mouse mode is
reconciled after every Update; View only reads state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 09:11:39 +03:00

133 lines
3.1 KiB
Go

package tui
import (
"strconv"
"strings"
"github.com/charmbracelet/x/ansi"
"vcom/internal/tui/geom"
"vcom/internal/tui/panel"
"vcom/internal/vfs"
)
// View renders the screen. It only reads state (design §7.9): sizes were
// set in Update (relayout), effects are applied by reconcile.
func (a *App) View() string {
if a.w <= 0 || a.h <= 0 {
return ""
}
canvas := make([]string, a.h)
blank := strings.Repeat(" ", a.w)
for i := range canvas {
canvas[i] = blank
}
draw := func(r geom.Rect, s string) {
if r.Empty() || s == "" {
return
}
for i, line := range strings.Split(s, "\n") {
y := r.Y + i
if i >= r.H || y >= len(canvas) {
break
}
canvas[y] = place(canvas[y], ansi.Truncate(line, r.W, ""), r.X, a.w)
}
}
switch a.screen {
case ScreenView:
draw(a.layout.Viewer, a.renderViewer())
default:
for i, p := range a.panels {
r := a.layout.Panes[i]
if r.Empty() {
continue
}
draw(r, p.Render(panel.View{
Rect: r,
Active: i == a.active,
FilterLine: a.filterLine(i),
Title: a.paneTitle(p),
Styles: a.styles,
}))
}
if a.screen == ScreenInfo {
draw(a.layout.Preview, a.renderPreview())
}
}
draw(a.layout.Status, a.statusLine())
if !a.layout.Footer.Empty() {
draw(a.layout.Footer, a.footerLine())
}
out := strings.Join(canvas, "\n")
return a.modals.Render(out, a.layout.Screen, a.styles)
}
// place writes s into line at column x.
func place(line, s string, x, w int) string {
left := ansi.Truncate(line, x, "")
if n := ansi.StringWidth(left); n < x {
left += strings.Repeat(" ", x-n)
}
right := ansi.TruncateLeft(line, x+ansi.StringWidth(s), "")
out := left + s + right
if n := ansi.StringWidth(out); n < w {
out += strings.Repeat(" ", w-n)
}
return out
}
func (a *App) filterLine(i int) string {
if a.filter == nil || i != a.active {
return ""
}
return a.filter.View()
}
func (a *App) paneTitle(p *panel.Model) string {
if p.Source == panel.SourceHosts {
return "SSH hosts"
}
t := displayPath(p.Location())
if n := p.MarkCount(); n > 0 {
t += " [" + strconv.Itoa(n) + " marked]"
}
return t
}
func (a *App) renderPreview() string {
r := a.layout.Preview
if r.W < 4 || r.H < 3 {
return ""
}
inner := r.W - 2
title := a.viewer.Title()
if title == "" {
if e, ok := a.Active().Selected(); ok && !e.Parent {
title = e.Name
}
}
body := a.viewer.Render(inner, a.layout.PreviewBody.H, a.styles)
content := a.styles.PaneTitle.Render(pad(ansi.Truncate(title, inner, "…"), inner)) + "\n" + body
return a.styles.PaneBorder.Width(inner).Render(content)
}
func (a *App) renderViewer() string {
r := a.layout.Viewer
title := a.viewer.Title()
if title == "" {
title = "View"
}
head := a.styles.PaneTitleActive.Render(pad(ansi.Truncate(title, r.W, "…"), r.W))
return head + "\n" + a.viewer.Render(r.W, max(0, r.H-1), a.styles)
}
func pad(s string, w int) string {
if n := ansi.StringWidth(s); n < w {
return s + strings.Repeat(" ", w-n)
}
return s
}
// locationOf is a helper for features: the active panel's location.
func (a *App) locationOf(i int) vfs.Location { return a.panels[i].Location() }