hpjansson / chafa

📺🗿 Terminal graphics for the 21st century.
https://hpjansson.org/chafa/
GNU Lesser General Public License v3.0
2.91k stars 62 forks source link

Sixel: image previewed inside Vifm doesn't span across all available space #77

Closed folliehiyuki closed 2 years ago

folliehiyuki commented 2 years ago

System information

OS: Alpine Linux (edge) chafa version: 1.8.0 / current master vifm version: 0.12 / current master terminal emulator: foot / kitty

Vifm

Here are some flags for the previewer inside Vifm that I use:

  %N        Do not detach viewer from terminal session (leaves `/dev/tty`
            available).

                                                               *vifm-%pc*
  %pc       marks the end of the main command and the beginning of the
            clear command for graphical preview, which is invoked on
            closing preview of a file.

                                                               *vifm-%pd*
  %pd       marks a preview command as one that directly communicates
            with the terminal.  Beware that this is for things like sixel
            which are self-contained sequences that depend only on current
            cursor position, using this with anything else is likely to
            mangle terminal state.

  The following dimensions and coordinates are in characters:
                                                               *vifm-%px*
  %px       x coordinate of top-left corner of preview area.
                                                               *vifm-%py*
  %py       y coordinate of top-left corner of preview area.
                                                               *vifm-%pw*
  %pw       width of preview area.
                                                               *vifm-%ph*
  %ph       height of preview area.
                                                               *vifm-%c*
  %c  %"c   the current file under the cursor.

Supported modifiers are:
    :p           - full path

Description

I'm trying to use sixel to preview images inside vifm. The command I use in vifmrc file:

fileviewer *.png,*.jpg
        \ chafa -f sixel -s %pwx%ph %c:p %pd 2>/dev/null

The output is something like this:

2022-02-18-111905_grim

Here is the output for kitty (left) and symbols (right) backends, with the following commands (%N flag is not in 0.12 branch):

fileviewer *.png,*.jpg
        \ chafa -f kitty -s %pwx%ph %c:p %pd %N 2>/dev/null

fileviewer *.png,*.jpg
        \ chafa -f symbols -s %pwx%ph %c:p 2>/dev/null

2022-02-18-111645_grim

Side note

I echoed the dimensions (%pw and %ph) then used the numbers to output the image with sixel in the command line and it works fine (in the examples above it should be 48x37)

hpjansson commented 2 years ago

Thanks for the report. I think Chafa is unable to determine the terminal's pixel dimensions, so it can't calculate how many pixels there are per cell. This would affect sixels and kitty, but not symbols. It probes stdout, stderr and stdin with the TIOCGWINSZ ioctl, then falls back to getting the controlling tty with ctermid() and if there is one, probes that the same way. If all of this fails, it falls back to assuming cells are 8x8 pixels, which is going to be wrong, but hey, at least there's something to look at.

So the issue is probably that Chafa is connected to vifm via pipes (which don't support TIOCGWINSZ) and is disassociated from the controlling terminal.

Did you try %N with sixels?

hpjansson commented 2 years ago

I misremembered the way I did this with Kitty. It supports cell dimensions natively, so it doesn't need to know the terminal's pixel size to display correctly. Therefore only sixel is affected, which lines up with what you're seeing.

Edit: Kitty images may look blurry, though, because we can't calculate the precise pixel density without access to the tty.

folliehiyuki commented 2 years ago

Silly me! Wonder why I didn't try %N with sixel before opening the issue. It works fine there.

For kitty images, the %N flag was introduced in vifm to solve that problem in the first place. It looks good with %N flag.

Thanks for the explanation, and sorry for all the fuss with this issue.

hpjansson commented 2 years ago

No problem, it's good to learn about these use cases even if there's no bug per se. Didn't know vifm supports graphics from subprocesses so well these days, I should do more testing with it.

folliehiyuki commented 2 years ago

At least whenever there is someone filling an issue about vifm in the future, you can just send them here and say everything works perfectly :)

Side note: vifm currently doesn't do async preview processes for graphics though, so scrolling in symbols mode is faster than in sixel and kitty. Last time I checked it was planned.

norbertkeri commented 1 year ago

EDIT: Got it working, the correct vifm config is:

fileviewer *.bmp,*.jpg,*.jpeg,*.png,*.xpm,*.webp,*.gif
        \ chafa -f kitty -s %pwx%ph %c:p %pd %N 2>/dev/null
        \ %pc
        \ kitty +icat --clear --silent %pd

@FollieHiyuki Sorry for pinging you on such an old thread, but this is the only relevant thread I can find relating to kitty chafa and vifm.

Do the images properly clear from the terminal for you? I end up with this, after I close the image preview:

1674992153

Notice all the overlapping images on the right. The command I'm trying is:

fileviewer *.bmp,*.jpg,*.jpeg,*.png,*.xpm,*.webp,*.gif
        \ chafa -f kitty -s %pwx%ph %c:p %pd %N 2>/dev/null
folliehiyuki commented 1 year ago

vifm has the option previewoptions=hardgraphicsclear so I didn't need the kitty +icat clear in the original post. I haven't used Kitty for quite a while so can't tell whether it is still the case now.