86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package tui
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
"vcom/internal/preview"
|
||
"vcom/internal/tui/viewer"
|
||
"vcom/internal/vfs"
|
||
)
|
||
|
||
func (a *App) previewRequest(loc vfs.Location, fi vfs.FileInfo) preview.Request {
|
||
loc.Path = loc.FS.Paths().Join(loc.Path, fi.Name)
|
||
cfg := a.deps.Config
|
||
return preview.Request{
|
||
Loc: loc,
|
||
Info: fi,
|
||
MaxBytes: cfg.Preview.MaxPreviewBytes,
|
||
MaxLines: 3000,
|
||
HighlightLines: 400,
|
||
DirLimit: cfg.Preview.DirectoryPreviewLimit,
|
||
ShowHidden: cfg.Browser.ShowHidden,
|
||
Theme: a.theme,
|
||
MaxRemoteImageBytes: cfg.Preview.MaxRemoteImageBytes,
|
||
}
|
||
}
|
||
|
||
func contentFromPreview(fi vfs.FileInfo, res preview.Result) viewer.Content {
|
||
c := viewer.Content{Kind: viewer.Other, Title: fi.Name, Image: res.Image}
|
||
switch res.Kind {
|
||
case preview.Text:
|
||
c.Kind = viewer.Text
|
||
c.Lines, c.Plain = res.Lines, res.Plain
|
||
return c
|
||
case preview.Image:
|
||
c.Kind = viewer.Image
|
||
}
|
||
c.Lines = previewMetadata(res)
|
||
if res.Kind == preview.Directory {
|
||
for _, entry := range res.Dir {
|
||
name := entry.Name
|
||
if entry.NavigableDir() {
|
||
name += "/"
|
||
}
|
||
c.Lines = append(c.Lines, name)
|
||
}
|
||
}
|
||
if res.Image != nil && res.Image.TooLarge {
|
||
c.Lines = append(c.Lines, "Remote image is too large to download")
|
||
}
|
||
if res.Truncated {
|
||
c.Lines = append(c.Lines, "…")
|
||
}
|
||
return c
|
||
}
|
||
|
||
func previewMetadata(res preview.Result) []string {
|
||
m := res.Meta
|
||
var lines []string
|
||
if m.Label != "" {
|
||
line := m.Label
|
||
if m.Size >= 0 {
|
||
line += fmt.Sprintf(" · %d bytes", m.Size)
|
||
}
|
||
lines = append(lines, line)
|
||
}
|
||
if !m.ModTime.IsZero() {
|
||
lines = append(lines, m.ModTime.Format("2006-01-02 15:04:05"))
|
||
}
|
||
if m.LinkTarget != "" {
|
||
lines = append(lines, "→ "+m.LinkTarget)
|
||
}
|
||
if m.ImageFormat != "" {
|
||
lines = append(lines, fmt.Sprintf("%s · %d×%d", strings.ToUpper(m.ImageFormat), m.Width, m.Height))
|
||
}
|
||
if m.Dimensions != "" {
|
||
lines = append(lines, m.Dimensions)
|
||
}
|
||
if m.Duration != "" {
|
||
lines = append(lines, m.Duration)
|
||
}
|
||
if m.PageCount != "" {
|
||
lines = append(lines, m.PageCount+" pages")
|
||
}
|
||
return lines
|
||
}
|