vcom/internal/preview/dir.go
vrubelroman 5d36b76bbe feat(preview): pure Build over vfs with limits, tools timeout and fast highlighting (5B)
Build(ctx, req) never opens FIFOs, sockets or devices (Kind=Special;
U-P1-10), reads at most MaxBytes, keeps at most DirLimit directory entries
and MaxLines text lines, and honours ctx in reads, tools and highlighting.
pdftotext/pdfinfo/ffprobe run in their own process group with a 3 s timeout
and are killed with their children. A chroma formatter writes foreground
SGR only (no background, no regexp post-processing); the first
HighlightLines (400) lines are highlighted, so 64 KiB of Go takes ~20 ms
(was 190). Remote images are downloaded to an LRU cache only up to
max_remote_image_bytes (N-P1-16); a 32-entry result cache avoids rebuilds.
theme.ChromaStyle maps every built-in theme (U-P2); KindLabel fixes the
inverted label (U-P2).

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

39 lines
965 B
Go

package preview
import (
"context"
"sort"
"strings"
"vcom/internal/vfs"
)
// buildDir lists a directory, keeping at most DirLimit entries (dirs first,
// then by name). Hidden entries are dropped unless ShowHidden.
func buildDir(ctx context.Context, req Request, res Result) Result {
ents, err := req.Loc.FS.ReadDir(ctx, req.Loc.Path)
if err != nil {
res.Kind, res.Err = Error, err
return res
}
kept := ents[:0]
for _, e := range ents {
if req.ShowHidden || !strings.HasPrefix(e.Name, ".") {
kept = append(kept, e)
}
}
sort.Slice(kept, func(i, j int) bool {
di, dj := kept[i].NavigableDir(), kept[j].NavigableDir()
if di != dj {
return di
}
return strings.ToLower(kept[i].Name) < strings.ToLower(kept[j].Name)
})
res.Kind = Directory
if len(kept) > req.DirLimit {
kept, res.Truncated = kept[:req.DirLimit], true
}
res.Dir = make([]vfs.FileInfo, len(kept)) // do not pin the full listing
copy(res.Dir, kept)
return res
}