You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sxivviewer 1.6KB

5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/bin/sh
  2. # Compatible with ranger 1.6.*
  3. #
  4. # This script searches image files in a directory, opens them all with sxiv and
  5. # sets the first argument to the first image displayed by sxiv.
  6. #
  7. # This is supposed to be used in rifle.conf as a workaround for the fact that
  8. # sxiv takes no file name arguments for the first image, just the number. Copy
  9. # this file somewhere into your $PATH and add this at the top of rifle.conf:
  10. #
  11. # mime ^image, has sxiv, X, flag f = path/to/this/script -- "$@"
  12. #
  13. # Implementation notes: this script is quite slow because of POSIX limitations
  14. # and portability concerns. First calling the shell function 'abspath' is
  15. # quicker than calling 'realpath' because it would fork a whole process, which
  16. # is slow. Second, we need to append a file list to sxiv, which can only be done
  17. # properly in two ways: arrays (which are not POSIX) or \0 sperated
  18. # strings. Unfortunately, assigning \0 to a variable is not POSIX either (will
  19. # not work in dash and others), so we cannot store the result of listfiles to a
  20. # variable.
  21. if [ $# -eq 0 ]; then
  22. echo "Usage: ${0##*/} PICTURES"
  23. exit
  24. fi
  25. [ "$1" == '--' ] && shift
  26. abspath () {
  27. case "$1" in
  28. /*) printf "%s\n" "$1";;
  29. *) printf "%s\n" "$PWD/$1";;
  30. esac
  31. }
  32. listfiles () {
  33. find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \
  34. '.*\(jpe?g\|bmp\|png\|gif\)$' -print0 | sort -z
  35. }
  36. target="$(abspath "$1")"
  37. count="$(listfiles | grep -m 1 -ZznF "$target" | cut -d: -f1)"
  38. if [ -n "$count" ]; then
  39. listfiles | xargs -0 sxiv -n "$count" --
  40. else
  41. sxiv -- "$@" # fallback
  42. fi