Canop / broot

A new way to see and navigate directory trees : https://dystroy.org/broot
MIT License
10.68k stars 234 forks source link

Preview transformers #898

Closed Canop closed 3 months ago

Canop commented 3 months ago

This allows defining file transformations that are applied to files before preview.

The default conf has 2 examples: previewing PDF and beautifying JSON:

preview_transformers: [
     # Use mutool to render any PDF file as an image
     # In this example we use placeholders for the input and output files
     {
         input_extensions: [ "pdf" ] // case doesn't matter
         output_extension: png
         mode: image
         command: [ "mutool", "draw", "-o", "{output-path}", "{input-path}" ]
     }

     # Use jq to beautify JSON
     # In this example, the command refers to neither the input nor the output,
     # so broot pipes them to the stdin and stdout of the jq process
     {
         input_extensions: [ "json" ] 
         output_extension: json
         mode: text
         command: [ "jq" ]
     }
]

PDF preview:

image

JSON beautification (the original file has no space nor new line):

image

Fix #494

Canop commented 3 months ago

BTW, I'd welcome suggestion of transformers for the website (and my own usage)

eddiehazel commented 3 months ago

both verified iterm/macos in principle, pdf image a little blurry but perhaps that is outside broot's hands?

Screenshot 2024-07-07 at 13 44 53

excited to see what else can be done with this feature! ( :

Canop commented 3 months ago

@eddiehazel The image is blurry here because your terminal doesn't support the Kitty protocol which broot uses to display high-res images.

eddiehazel commented 3 months ago

thanks, looking into it!

   # Use fold to wrap text
    # In this example, the command refers to neither the input nor the output,
    # so broot pipes them to the stdin and stdout of the jq process
    {
        input_extensions: [ "txt" ]
        output_extension: txt
        mode: text
        command: [ "fold", "-w 80", "-s" ]
    }
]

working well 💫

Canop commented 3 months ago

IMO wrapping should be done by broot so that the line numbers stay consistent (usually you want to jump to the right place when opening the file), but that's an interesting first step before I implement wrapping in broot.

eddiehazel commented 3 months ago

for sure, just playing around and it works for now ( :

so i need to switch terminal to get kitty support because everyone has their own protocol 😭

i thought to make a transformer for this with timg but i guess it's lower level

i will take a look at some alternatives

Canop commented 3 months ago

so i need to switch terminal to get kitty support because everyone has their own protocol 😭

i thought to make a transformer for this with timg but i guess it's lower level

This can't really be a transformer. The protocol of iterm2 could probably be implemented in broot the same way I implemented the kitty protocol but I can't do it myself as I don't own a Mac.

eddiehazel commented 3 months ago

seems there is a lot of inspiration here: https://github.com/lusingander/iterm2img

not sure if it's quite within my wheelhouse but i'll try size it up - wdyt?

definitely up for testing either way! 🤲

btw https://wezfurlong.org/wezterm/imgcat.html seems xplatform + iterm img protocol and it's rust 🦀 👀

Canop commented 3 months ago

not sure if it's quite within my wheelhouse but i'll try size it up

Even if you don't manage to finish, clarifying the problems and possible solutions would both be enlightening and useful, so you probably should. And you can come to miaou to discuss your findings.

But we're going off the topic of this PR, so I suggest you create an issue for item2 support in broot (or for high-res image support on mac if you prefer it less specific).

FrancescElies commented 3 months ago

I personally don't particularly care about this feature but gave it a try on a mac anyways, @Canop I hope this helps.

name version
nu 0.95.0
wezterm 20240203-110809-5046fc22
os macos

Not sure if this is an issue of broot or any other part of my setup but pdfs and images for whatever reason don't fully utilize the space available for the preview panel. pdf

grafik

image

grafik
Canop commented 3 months ago

pdfs and images for whatever reason don't fully utilize the space available for the preview panel.

All images ? Or just the ones coming from pdf transformation ?

FrancescElies commented 3 months ago

Nevermind, I think the images I downloaded where just simply too small.

grafik

The issue seems to be related to the pdf image transformation, maybe one needs to do something with mutool

Canop commented 3 months ago

You can increase the image width:

{
    input_extensions: [ "pdf" ] // case doesn't matter
    output_extension: png
    mode: image
    command: [ "mutool", "draw", "-w", "1000", "-o", "{output-path}", "{input-path}" ]
}
Canop commented 3 months ago

I could probably define some {preview-width} parameter...

But this is one of those things which aren't always determined so I'm not sure it would be good.