Add terminal image preview via chafa and release v0.1.3 updates

This commit is contained in:
vrubelroman 2026-04-24 15:35:11 +03:00
parent 6a518896b8
commit 6b23717572
4 changed files with 82 additions and 18 deletions

View file

@ -9,6 +9,7 @@ import (
_ "image/png"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
@ -65,6 +66,8 @@ type PreviewOptions struct {
HumanReadableSize bool
ThemeName string
UseNerdIcons bool
ImagePreviewWidth int
ImagePreviewHeight int
}
func BuildPreview(entry Entry, options PreviewOptions) Preview {
@ -119,12 +122,12 @@ func BuildPreview(entry Entry, options PreviewOptions) Preview {
preview.Kind = PreviewKindImage
preview.Metadata.ImageFormat = format
preview.Metadata.ImageSize = dimensions
preview.Body = fmt.Sprintf(
"Image preview is metadata-only for now.\n\nFormat: %s\nDimensions: %s\nPath: %s",
format,
dimensions,
entry.Path,
)
inline := renderImageInlinePreview(entry.Path, options.ImagePreviewWidth, options.ImagePreviewHeight)
if inline == "" {
preview.Body = "Image preview unavailable.\n\nInstall `chafa` for inline preview in info pane."
} else {
preview.Body = inline
}
preview.PlainBody = preview.Body
return preview
}
@ -377,6 +380,39 @@ func previewIcon(entry Entry, useNerdIcons bool) string {
}
}
func renderImageInlinePreview(path string, width int, height int) string {
if width < 20 {
width = 20
}
if height < 8 {
height = 8
}
if _, err := exec.LookPath("chafa"); err != nil {
return ""
}
cmd := exec.Command(
"chafa",
"--format=symbols",
"--symbols=vhalf",
"--animate=off",
"--fg-only",
"--size", fmt.Sprintf("%dx%d", width, height),
path,
)
out, err := cmd.Output()
if err != nil {
return ""
}
view := strings.TrimSpace(string(out))
if view == "" {
return ""
}
return view
}
func detectImage(data []byte) (string, string, bool) {
cfg, format, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil {