Page 1 of 2

Dealing with images in Ranger

Posted: Wed Nov 18, 2015 12:25 pm
by Snap
If you don't run the X, or your beard is untidy like a spiritual leader's beard you surely won't care about any of this. But if your beard is boring and regular like mine and use ranger just keep going.

if you select an image file in ranger (on its defaults) and hit r, you'll get this:

Code: Select all

0 | sxiv -- "$@"
1 | feh -- "$@"
These are the first two mime types for image viewers. Defined in rifle.conf like this:

Code: Select all

mime ^image, has sxiv,      X, flag f = sxiv -- "$@"
mime ^image, has feh,       X, flag f = feh -- "$@"
sxiv is in the repos. It does the job, though I personally don't quite like it.

The infamous feh is in the repos too. It's hated and loved by many. The problem it has for this use is that it fucking goes full resolution instead of full screen it can be really annoying. z3bra hates it (introducing meh):
I've spit on feh before (see hsetroot. I have effectively some problems with it. Feh is impossible to use. it displays the images in full size, no matter what the window size is. And that's freaking annoying... I just want my image viewer to display an image at the maximum size for the window, and to resize when I change the window size.

That is what meh does. And (almost) nothing more ! Meh take a list of images as arguments, and fits them in its window. Meh can also read a list from stdin. You can then cycle through images using h,j,k,l,left,down,up,right.

When you press <enter>, meh outputs the current image name to stdout. It allows cool stuffs like meh *.jpg | xargs rm to delete the images upon pressing <enter>.
meh... what a cool name, huh?

meh is lighter (less dependencies) than sxiv, it's frigging awesome and can be a good replacement for feh (but more on feh later). It's not in the repos, but it's easy to compile. I don't like dealing with lists, so to open an image it's just great (as z3bra stated), but not that good for slideshows or scrolling images IMHO.

To enable it just add this line into ~/.config/ranger/rifle.conf

Code: Select all

mime ^image, has meh,       X, flag f = meh -- "$@"
Ok. we can open images at window size and resize them at will. But, what if we want to do a slideshow or surfing images within a directory without needing to do lists? Easy: feh. Yes, feh. The old feh plus a small script and a couple of wrappers.

Here the script:

feh-browser

Code: Select all

#!/bin/bash
#nvoke it with the image path, e.g. "/path/to/script %f -F -Z".
#It should open that image in feh and let you browse through other images in the folder in the usual order, i.e. "feh /path/to/dir/*".
  
  # Change the input field separator to avoid problems with spaces.
  IFS='
  '
  # Get the target file, its name, directory, and the other files in the
  directory.
  _fpath="${1/%\/}"
  _fname="$(basename "$_fpath")"
  _dpath="$(dirname "$_fpath")"
  _flist=($(ls "$_dpath"))
  
  
  # Determine the index of the target file in the file list array.
  _i=0
  for _file in "${_flist[@]}"; do
    [ "$_file" == "$_fname" ] && break
    _i=$(($_i + 1))
  done
  
  # Remove the target file from the arguments, leaving any eventual arguments to
  # feh.
  shift
  
  # Change to the directory and launch feh with the target file as the first
  # argument, and the rest of the files in their normal order.
  cd "$_dpath"
  feh "$@"  "${_flist[@]:$_i}" "${_flist[@]:0:$_i}"
For GTK browsers this is (should be) enough to display the images at fullscreen instead of full scaled, but ranger keeps doing full scale. Still annoying. But we can use a couple of wrappers to make it happen.

To scroll or navigate all the images into a directory (with left/right arrow keys (up/down to zoom), left mouse clicks or mouse scrolling) use this one:

feh-b

Code: Select all

#!/bin/bash
/usr/local/bin/feh-browser %f -F -Z
For timed slideshows:

feh-s

Code: Select all

#!/bin/bash
/usr/local/bin/feh-browser %f -F -Z -x -q -Y -D 3 .
Set the delay at will in advance and then run it so now your hands will be free for wan... eeerm... slideshow pictures. the keystrokes and mouse actions still work as above, but they images will flow for you if your hands are busy.

My rifle:

Code: Select all

#-------------------------------------------
# Image Viewing:
#-------------------------------------------
mime ^image, has meh,       X, flag f = meh -- "$@"
mime ^image, has feh-b,     X, flag f = feh-b -- "$@"
mime ^image, has feh-s,     X, flag f = feh-s -- "$@"
mime ^image, has gimp,      X, flag f = gimp -- "$@"
ext xcf,                    X, flag f = gimp -- "$@"
enjoy!

Re: Dealing with images in Ranger

Posted: Wed Nov 18, 2015 12:37 pm
by GekkoP
Oh look at that. Thanks for sharing.

Re: Dealing with images in Ranger

Posted: Wed Nov 18, 2015 1:34 pm
by rhowaldt
Oh, snap! that's wonderful :) - thanks for researching this shit! meh looks nice, and good name too!

Re: Dealing with images in Ranger

Posted: Wed Nov 18, 2015 3:07 pm
by franksinistra
Thanks snappy! now i can get http://git.2f30.org/lel work in ranger

Re: Dealing with images in Ranger

Posted: Thu Nov 19, 2015 1:22 am
by Snap
Hey, thanks for pointing out lel, Mr. sinistra. Another nice name. Do you know if gif support means animated gifs or just plain gif? I'll keep an eye on it.

EDIT: sxvi does animated gifs nicely. Wish I knew another way with feh or meh (via imagemagick?) and forget about sxvi.

Re: Dealing with images in Ranger

Posted: Thu Nov 19, 2015 9:16 am
by wuxmedia
yeah nice work Snapper

Re: Dealing with images in Ranger

Posted: Thu Nov 19, 2015 8:45 pm
by franksinistra
Snap wrote:Hey, thanks for pointing out lel, Mr. sinistra. Another nice name. Do you know if gif support means animated gifs or just plain gif? I'll keep an eye on it.

EDIT: sxvi does animated gifs nicely. Wish I knew another way with feh or meh (via imagemagick?) and forget about sxvi.
to answer your question https://github.com/eXeC64/imv

Re: Dealing with images in Ranger

Posted: Fri Nov 20, 2015 9:04 am
by Snap
Fuck yeah! Thanks, fs.

Re: Dealing with images in Ranger

Posted: Fri Nov 20, 2015 10:33 am
by Snap
Found this about imv:
It doesn't work with ranger the same way other viewers do. imv -- "$@" but removing the -- seems to work. I'm not sure how it would make a difference but I'm no expert.
Haven't tried it yet whatsoever. About to compile and check.

Re: Dealing with images in Ranger

Posted: Sat Nov 21, 2015 8:59 pm
by dkeg
bonus - vim like keybindings

Re: Dealing with images in Ranger

Posted: Sun Nov 22, 2015 8:46 am
by Snap
Yup. Ranger made me install vim. Finally learning that shit.

Re: Dealing with images in Ranger

Posted: Sun Nov 22, 2015 2:24 pm
by dkeg
there's really no reason for ranger. Put this in your .bashrc and you're good to go

Code: Select all

# Automatically do an ls after each cd
cd() {
  if [ -n "$1" ]; then
builtin cd "$@" && ls --group-directories-first
  else
builtin cd ~ && ls --group-directories-first      
  fi                                              
}

Re: Dealing with images in Ranger

Posted: Mon Nov 23, 2015 9:57 am
by wuxmedia
^ not that I view images on servers, but yes generally

Code: Select all

cd /path/ ;ls

Re: Dealing with images in Ranger

Posted: Mon Nov 23, 2015 11:38 am
by Snap
Nice trick. Thanks, dkeg.

Re: Dealing with images in Ranger

Posted: Mon Nov 23, 2015 1:16 pm
by dkeg
It is either already in default bbq spins or was at some point. That's where I got it from, but I've been using the same .bashrc for years

Re: Dealing with images in Ranger

Posted: Mon Nov 23, 2015 2:57 pm
by Snap
Maybe it was in past spins. I've never saw it in any .bashrc in the spins I've tried. Thanks for rescuing it for the masses.

Re: Dealing with images in Ranger

Posted: Mon Nov 23, 2015 3:45 pm
by rhowaldt
yeah should be in default .bashrc usually. iirc i once made that...

Re: Dealing with images in Ranger

Posted: Fri Nov 27, 2015 5:20 pm
by pidsley
This works too:

Code: Select all

cd() {
	builtin cd $@ && ls
}
No need for the conditional code. It uses whatever alias you have already defined for "ls", so if you want directories first, alias ls:

Code: Select all

alias ls='ls -F --color=auto --group-directories-first'

Re: Dealing with images in Ranger

Posted: Fri Nov 27, 2015 6:56 pm
by wuxmedia
^ then you find out 'ls' is actually symlinked to a bash scripts: 'echo * | grep fmt -1'
and your mind begins to melt...

Re: Dealing with images in Ranger

Posted: Sat Nov 28, 2015 2:10 am
by franksinistra
add dircolors (if that's your thing) to make ls --color=auto colorful and interesting.