简体   繁体   中英

What gem or external program should I use with Ruby and Linux, to help to choose an image?

My Ruby script chooses a random image from a directory of photos, it then shows the chosen image to me, by simply calling "feh" image viewer. Currently I can't move on to a next photo, if needed: as the photo must suit somewhat to the music which was selected by the script earlier also randomly, and opened in a media player. Which gem or even external program can I use for this purpose, which, if called, enables to step to a next random image, and waiting for me pressing Y or ESC to accept, or N for a next random image.

A tricky solution I found with feh:

ret=%x{feh -z --info "echo '%F' 1>&2" /path/to/images/* 2>&1}
last_image=ret.split("\n").last
puts "chosen image: "+last_image

Explanation: -z randomizes the file list, --info accept a command line which return value is shown on the image as info, eg. exif data, size, etc. Currently it is a simple echo command echoing the actual file path, but redirecting its output to stderr from stdout. This way the info isn't shown on the current image, but is simply printed to the STDERR. At the end of the feh command line its stderr is redirected to stdout, so Ruby's %x command collects the printed lines. After exiting feh, the "ret" variable contains all the printed lines, which were echoed by the --info hook. The last one is the last image shown, which is the accepted image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM