gokcehan / lf

Terminal file manager
MIT License
7.69k stars 326 forks source link

Custom Previewer Renders Image Incorrectly #1394

Closed laluxx closed 1 year ago

laluxx commented 1 year ago

I've been working on creating a custom previewer for lf to handle font previews. The goal is to display a series of text samples of different sizes, rendered with a specified font. However, I've encountered two main issues:

  1. Image Rendering Position:

    • When using my custom previewer, images (including font previews) are rendered at the top-left corner of the lf window, instead of the designated preview pane on the right. This contrasts with the behavior observed when I use ctpv directly as the previewer in lf, where images are correctly rendered in the right pane.
  2. Background Transparency:

    • The generated image from my custom previewer has a transparent background. However, when it's displayed inside lf, the background becomes white. I want to maintain the original transparency of the image preview inside lf.

My Setup:

Questions:

How can I modify the custom previewer output (like with ctpv) so that lf can render it correctly in the right pane? (images are rendered in the top left corner for both font files and normal images)

How can I ensure the background transparency of the image remains intact when it's displayed in lf?

I would appreciate any insights or potential solutions to these problems. Thank you in advance!

ilyagr commented 1 year ago

Getting rid of the two >/dev/tty might help. lf needs to interrupt the output to put it in the correct place.

laluxx commented 1 year ago

Thank you for your suggestion. I've updated my previewer script as follows:

case "$1" in
    *.ttf|*.otf|*.woff|*.woff2)
        OUTPUT_PATH="/tmp/font_previews/font_preview_$(basename "$1").png"
        font-preview "$1" > /dev/null
        ctpv "$OUTPUT_PATH"
        ;;
    *)
        ctpv "$1"
        ;;
esac

Unfortunately, the behavior remains unchanged. The images are still being rendered at the top left of the lf interface. Moreover, the image background appears white even though the generated image is transparent.

What's even more puzzling is that this misplacement is happening even for non-font files, where I'm simply using the standard ctpv for previews. If I configure lf to directly use ctpv as its previewer, everything works as expected.

I'm not sure if there's something inherently wrong with the script logic or if it's an interaction issue with lf or ctpv. Any further guidance would be greatly appreciated.

MahouShoujoMivutilde commented 1 year ago

You're not giving the preview position to ctpv, that's why it draws the image in the wrong place.

Ctpv is meant to be used as previewer script, not inside the wrapper.

Meaning, it is meant to receive all of the previewer arguments, that is

Five arguments are passed to the file [talking about previewer script], (1) current file name, (2) width, (3) height, (4) horizontal position, and (5) vertical position of preview pane respectively.

(from the lf -doc)

Not just the file path that you're passing to it.

Consider running it as ctpv "$@" or ctpv $1 $2 $3 $4 $5 (if you want to modify some of the arguments).

laluxx commented 1 year ago

Thank you so much for your help. Passing the correct arguments to ctpv as you suggested fixed it. One last thing: the background of transparent images appears white. Any idea how to fix that?

Thanks again!

MahouShoujoMivutilde commented 1 year ago

That depends on what actually draws the preview image.

White background sounds like an ueberzug thing.

https://github.com/jstkdng/ueberzugpp/issues/8

I don't know how it can be fixed. I haven't tested it, maybe latest master is good.

Consider using either kitty terminal (has its own image drawing protocol, seems to be supported by ctpv) or build lf from latest master and use sixel (set sixel in lfrc) if your terminal supports it.

Both can somewhat do transparency ![21-08-2023-18_15_51](https://github.com/gokcehan/lf/assets/14999778/23cc1fca-d204-43b9-af5f-4274ee2c48ac) ![21-08-2023-18_14_42](https://github.com/gokcehan/lf/assets/14999778/53e820a8-a1e7-4a0e-8dfc-45c2e4ed0d1d)
laluxx commented 1 year ago

I am indeed using kitty as my terminal, and I've verified that both ctpv and kitty icat handle transparency well when used independently. The issue with the white background seems to appear exclusively when used within lf.

set sixel don't seem to fix the problem I am currently using ctpv for previews within lf. image

MahouShoujoMivutilde commented 1 year ago

set sixel don't seem to fix the problem

You also need to enable it in ctpv

"Add set chafasixel to ~/.config/ctpv/config" from readme

Kitty doesn't support sixel though.

The issue with the white background seems to appear exclusively when used within lf.

Idk, are you sure you don't have ueberzug / ueberzugpp installed, and it doesn't mess with environment variables?

Like, there is no wrapper for lf which could mess with things that ctpv is using to figure out what image drawing method to use, lf is launched directly...?

Because I am not sure what else could do it.

laluxx commented 1 year ago

Thank you for your assistance. Uninstalling ueberzug resolved the white background issue, and there was no need for set sixel. Everything is now working as expected. Really appreciate your help!