gokcehan / lf

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

Lf previewer doesn't show viu output #777

Open soniccat opened 2 years ago

soniccat commented 2 years ago

Hi, I'm trying to make viu 1.4.0 (https://github.com/atanunq/viu) work with lf (version 26) on MacOS (11.2.3) in iTerm2 (3.5.0beta4).

I use the same previewer code mentioned here https://github.com/gokcehan/lf/issues/735.

#!/bin/bash
if file --mime "$1" | grep "executable" >/dev/null
then
    file -b "$1" | fold -sw "$2"
    echo ""
    (/usr/local/bin/checksec --file "$1" | fold -sw "$2") 2>&1 | tail -n +2
else 
    case "$1" in
        *.tar*) tar tf "$1";;
        *.zip) unzip -l "$1";;
        *.rar) unrar l "$1";;
        *.7z) 7z l "$1";;
        *.pdf) pdftotext "$1" -;;
        *.jpg) viu "$1";;
        *.jpeg) viu "$1";;
        *.png) viu "$1";;
        *.doc) catdoc < "$1";;
        *.docx) docx2txt < "$1";;
        *) /bin/batcat --paging=never --terminal-width="$2" --color=always --theme=base16  "$1" | tail -n +3;;
    esac
fi

And I don't see an image in the lf previewer. It shows nothing for jpegs and pngs. But when I call viu directly I see an image in iTerm. Is there any workaround to be able to see viu output in lf previewer?

TobKed commented 2 years ago

Same for me. I was not able to set image preview on MacOS and terminal/Iterm2/Kitty. I tried many options, one of them is code taken from: github.com/rafi/.config I tried withimgcat, timg, chafa and catimg without success.

bugsbugsbux commented 2 years ago

set the TERM variable to something that works. for me xterm-kitty does not work but when i set TERM=tmux it works (tho not with kitty protocol :'()

TobKed commented 2 years ago

@marcusatiliusregulus tried with different values for TERM and not helped.

bugsbugsbux commented 2 years ago

@TobKed you could try directly with viu -b "$file". and make sure you get your variables right when you take code from others! it might be called "$f" or an index of your scripts argv (stupid me made this mistake,haha...)

pawel-szopinski commented 2 years ago

@marcusatiliusregulus adding -b flag makes the image very pixelated, it defeats the purpose of having previews if I cannot actually see what's on the picture.

And this seems to be a general problem with lf - it does not handle full resolution image previews. I've tried probably half a dozen of those previewers (all those mentioned by @TobKed as well), but they only work in their pixelated/blocky/ascii modes.

I've really grown to like lf and this is the only missing feature that causes me to go back to ranger from time to time.

idr4n commented 2 years ago

This preview script is working for me in both Kitty and WezTerm (in MacOS) to preview images and pdfs (not in iTerm though):

#!/usr/bin/env bash
file=$1
w=$2
h=$3
x=$4
y=$5
hfloat=$(expr $h*0.9 | bc)
hint=${hfloat/.*}
CACHE="$HOME/.cache/tmpimg"

if [[ "$( file -Lb --mime-type "$file")" =~ ^image ]]; then
  kitty +icat --silent --transfer-mode file --place "${w}x${h}@${x}x2" "$file"
  exit 1
fi

if [[ "$( file -Lb --mime-type "$file")" =~ ^application/pdf ]]; then
  pdftoppm -jpeg -f 1 -singlefile "$file" "$CACHE"
  kitty +icat --silent --transfer-mode file --place "${w}x$hint@${x}x2" "${CACHE}.jpg"
  rm "${CACHE}.jpg"
  exit 1
fi

cat "$file"

Make sure you have pdftoppm installed and in your path.

For the cleaner script I have:

#!/usr/bin/env bash

kitty +icat --clear --silent --transfer-mode file

I put these scripts together mainly from:

Neither viu nor WezTerm wezterm imgcat worked for me. I'm guessing that it is because it is necessary to specify the x and y position of the image, and these commands don't have that option, only width and height.

pawel-szopinski commented 2 years ago

Thanks, @idr4n! I installed kitty terminal and indeed previews work without any issues there (I am using ctpv to get previews). Unfortunately, I don't have enough knowledge to figure out why this is not the case in iTerm.

jtrees commented 2 years ago

@idr4n Thanks! Your script works well for me on Kitty.

However, it does not clear the previous image before rendering the next one, so going through a directory with images of different sizes and proportions results in weird overlay effects.

I noticed that kitty +icat --clear is a thing and tried adding it to the script as a call before the proper image display command but somehow that results in all hell breaking loose.

Any ideas on how to fix the issue I described?

EDIT: I just noticed the cleaner script. My mind skipped over it last time. Trying it now...

EDIT 2: Huh. The cleaner script has no effect. It looks like this on my system:

# ~/.config/lf/lfrc
set cleaner /foo/bar/lf_kitty_cleaner.sh
# /foo/bar/lf_kitty_cleaner.sh [executable file]

#!/bin/bash
kitty +icat --clear --silent --transfer-mode file
idr4n commented 2 years ago

@jtrees do you have the cleaner option set in your lfrc? e.g. set cleaner ~/.config/lf/lf_kitty_clean (or to whatever path that points to your cleaner script? (EDIT: Yes, you do, I missed that... then I wouldn't know why it is not cleaning the image for you, for me it works just fine)

These are my updated scripts just in case:

#!/usr/bin/env bash

CACHE="$HOME/.cache/tmpimg"
[ -f "${CACHE}.jpg" ] && rm "${CACHE}.jpg"
kitty +icat --clear --silent --transfer-mode file
#!/usr/bin/env bash
file=$1
w=$2
h=$3
x=$4
y=$5
hfloat=$(expr $h*0.9 | bc)
hint=${hfloat/.*}
CACHE="$HOME/.cache/tmpimg"

if [[ "$( file -Lb --mime-type "$file")" =~ ^image && $lf_preview == true ]]; then
  kitty +icat --silent --transfer-mode file --place "${w}x${h}@${x}x2" "$file"
  exit 1
fi

if [[ "$( file -Lb --mime-type "$file")" =~ ^application/pdf && $lf_preview == true ]]; then
  pdftoppm -jpeg -f 1 -singlefile "$file" "$CACHE"
  kitty +icat --silent --transfer-mode file --place "${w}x$hint@${x}x2" "${CACHE}.jpg"
  exit 1
fi

cat "$file"
idr4n commented 2 years ago

Thanks, @idr4n! I installed kitty terminal and indeed previews work without any issues there (I am using ctpv to get previews). Unfortunately, I don't have enough knowledge to figure out why this is not the case in iTerm.

@pawel-szopinski That script should only work in Kitty (and it does work in Wezterm as well!). My understanding is that iTerm has a different protocol and so kitty +icat does not work in there https://iterm2.com/documentation-images.html...

I tried iTerm's imgcat script, and although you can preview images inside the terminal with it, I tried using it in LF's preview and it didn't work. There should be similar reasons why viu, wezterm imgcat, etc., won't work either 🤷🏻.

kristoferus75 commented 1 year ago

i have also tested it with kitty the pdf preview doesnt work ?

davixx95 commented 1 year ago

I don't know if this is considered related enough to the thread at hand but I've been trying to debug this issue for the last couple of days to no avail so I'll post it here and make a new issue if deemed necessary.

I'm using similar scripts to the ones described in the wiki using the kitty protocol to preview images, I'll briefly post them here:

kitty +kitten icat --stdin no --transfer-mode memory --place "${w}x${h}@${x}x${y}" "$file" < /dev/null > /dev/tty
exit 1

kitty +kitten icat --clear --stdin no --transfer-mode memory < /dev/null > /dev/tty

they work like a wonder in kitty, but on wezterm they leave a visual artifact where the "Loading..." text is present. I've tried multiple avenues, pretty much all different kinds of command flags for the protocol and different alternatives, but I haven't managed to get a clear looking wezterm image preview in lf, I'll leave a screenshot to show the artifact I'm talking about.

image

Even with it being so minor it does hamper my experience, or maybe because it is so minor and silly yet seemingly unfixable, I'd just like to know what causes it (and possibly how to fix it). In my tests though (simply using sleeps as breakpoints of sorts) it became clear the protocol is not at fault, the image generation actually goes through fine, but lf seems to somehow make the already cleared text block out, or erase, the image successively once the preview script finishes.