vcom/internal/tui/panel/render.go
vrubelroman bb7f98016b feat(tui): panel model with mounts, filter, marks, history and cached render (6.1.4)
A panel holds a mount stack, the sorted listing, cursor, marks, filter,
history and cursor memory. Navigation is applied as one transaction;
mounts are released when no snapshot uses them and re-acquired for a
mirror. Clicks map through the visible rows (U-P1-4), reload keeps each
panel's cursor and leaves it at the index of a deleted entry (U-P1-7),
symlinks to directories are navigable (U-P1-2). Rows are cached; a
20k-entry panel renders in ~0.3 ms.

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

213 lines
4.7 KiB
Go

package panel
import (
"fmt"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"vcom/internal/tui/geom"
"vcom/internal/tui/style"
)
// View describes how to draw the panel.
type View struct {
Rect geom.Rect
Active bool
// FilterLine is the filter row content while the filter is edited
// (with the text cursor); "" shows the committed filter, if any.
FilterLine string
Title string // path as the App wants it shown
Styles *style.Styles
}
func (v View) filterRow(p *Model) bool { return v.FilterLine != "" || p.filter != "" }
// ListRect is where the rows are inside the panel rectangle r: border,
// title row, optional filter row. Rendering and mouse hit-tests both use it.
func ListRect(r geom.Rect, filterRow bool) geom.Rect {
h := r.H - 3 // borders + title
if filterRow {
h--
}
return geom.Rect{X: r.X + 1, Y: r.Y + 2, W: max(0, r.W-2), H: max(0, h)}
}
// ListRectFor is ListRect for the panel's current state.
func (p *Model) ListRectFor(v View) geom.Rect { return ListRect(v.Rect, v.filterRow(p)) }
type rowKey struct {
gen, styles uint64
idx int
cursor, active bool
marked bool
width int
showSize, showTm bool
}
type rowCache struct {
m map[rowKey]string
}
func (c *rowCache) get(k rowKey, rows int, build func() string) string {
if s, ok := c.m[k]; ok {
return s
}
if c.m == nil || len(c.m) > 4*max(rows, 16) {
c.m = make(map[rowKey]string, 4*max(rows, 16))
}
s := build()
c.m[k] = s
return s
}
// Render draws the panel into v.Rect.
func (p *Model) Render(v View) string {
st := v.Styles
r := v.Rect
if r.W < 4 || r.H < 3 {
return ""
}
inner := r.W - 2
lr := p.ListRectFor(v) // the App keeps p.rows == lr.H via SetRows (View stays pure)
var b strings.Builder
title := v.Title
if p.Badge != "" {
title += " " + p.Badge
}
if p.Loading {
title += " …"
}
ts := st.PaneTitle
if v.Active {
ts = st.PaneTitleActive
}
b.WriteString(ts.Render(pad(ansi.Truncate(title, inner, "…"), inner)))
for row := 0; row < lr.H; row++ {
b.WriteByte('\n')
i := p.offset + row
if i >= len(p.visible) {
b.WriteString(strings.Repeat(" ", inner))
continue
}
e := p.entries[p.visible[i]]
k := rowKey{gen: p.gen, styles: st.Gen, idx: i, cursor: i == p.cursor, active: v.Active,
marked: p.marks[e.Name], width: inner, showSize: p.Opts.ShowSize, showTm: p.Opts.ShowTime}
b.WriteString(p.cache.get(k, lr.H, func() string { return p.renderRow(e, k, st) }))
}
if v.filterRow(p) {
b.WriteByte('\n')
line := v.FilterLine
if line == "" {
line = "/" + p.filter
}
b.WriteString(st.FilterLine.Render(pad(ansi.Truncate(line, inner, "…"), inner)))
}
border := st.PaneBorder
if v.Active {
border = st.PaneBorderActive
}
return border.Width(inner).Render(b.String())
}
func (p *Model) renderRow(e Entry, k rowKey, st *style.Styles) string {
w := k.width
sizeW, timeW := 0, 0
if k.showSize && w >= 30 {
sizeW = 8
}
if k.showTm && w >= 50 {
timeW = 16
}
nameW := w - 2 - sizeW - timeW
if sizeW > 0 {
nameW--
}
if timeW > 0 {
nameW--
}
mark := " "
if k.marked {
mark = "* "
}
name := e.Name
switch e.Cat {
case CatDir, CatLinkDir, CatParent:
name += "/"
}
if e.IsSymlink() {
name = "~" + name
}
line := mark + pad(ansi.Truncate(name, max(1, nameW), "…"), max(1, nameW))
if sizeW > 0 {
line += " " + fmt.Sprintf("%*s", sizeW, sizeText(e, p.Opts.HumanSize))
}
if timeW > 0 {
line += " " + timeText(e)
}
line = pad(ansi.Truncate(line, w, ""), w)
var s lipgloss.Style
switch {
case k.cursor && k.active:
return st.RowCursor.Render(line)
case k.cursor:
return st.RowCursorPassive.Render(line)
case k.marked:
s = st.RowMarked
case e.Cat == CatDir || e.Cat == CatLinkDir || e.Cat == CatParent:
s = st.Dir
case e.Cat == CatLink:
s = st.Link
case e.Cat == CatBroken || e.Cat == CatSpecial:
s = st.RowMuted
case e.Cat == CatExec:
s = st.Exec
default:
s = st.Row
}
return s.Render(line)
}
func sizeText(e Entry, human bool) string {
switch e.Cat {
case CatParent:
return "UP"
case CatDir, CatLinkDir:
return "DIR"
}
if !human {
return fmt.Sprint(e.Size)
}
return HumanSize(e.Size)
}
// HumanSize formats bytes as 1.2K / 34M.
func HumanSize(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%dB", n)
}
div, exp := int64(unit), 0
for x := n / unit; x >= unit && exp < 5; x /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%c", float64(n)/float64(div), "KMGTPE"[exp])
}
func timeText(e Entry) string {
if e.Parent || e.ModTime.IsZero() {
return strings.Repeat(" ", 16)
}
return e.ModTime.In(time.Local).Format("2006-01-02 15:04")
}
func pad(s string, w int) string {
if n := ansi.StringWidth(s); n < w {
return s + strings.Repeat(" ", w-n)
}
return s
}