aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/imgview
blob: daa08452d2f3d37623878c28efc1bd74a7396ae9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env sh

# Description: Open images in hovered  directory and thumbnails
#              open hovered image in sxiv or viu and browse other images in the directory
# Dependencies: imv (https://github.com/eXeC64/imv) or,
#               sxiv (https://github.com/muennich/sxiv) or,
#               viu (https://github.com/atanunq/viu), less
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana

abspath() {
    case "$1" in
        /*) printf "%s\n" "$1";;
        *)  printf "%s\n" "$PWD/$1";;
    esac
}

listimages() {
    find -L "$(dirname "$target")" -maxdepth 1 -type f -iregex \
      '.*\(jpe?g\|bmp\|webp\|png\|gif\)$' -print0 | sort -z
}

view_dir() {
    target="$(abspath "$2")"
    count="$(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)"

    if [ -n "$count" ]; then
        if [ "$1" = 'sxiv' ]; then
            listimages | xargs -0 "$1" -an "$count" --
        else
            listimages | xargs -0 "$1" -n "$count" --
        fi
    else
        shift
        "$1" -- "$@" # fallback
    fi
}

if [ -z "$1" ] || ! [ -s "$1" ]; then
    printf "empty file"
    read -r _
    exit 1
fi

if uname | grep -q "Darwin"; then
    if [ -f "$1" ]; then
        open "$1" >/dev/null 2>&1 &
    fi
# `imvr` is often callable as `imv` on Linux distros
# You might need to change the reference below
elif which imvr >/dev/null 2>&1; then
    if [ -f "$1" ]; then
        view_dir imvr "$1" >/dev/null 2>&1 &
    elif [ -d "$1" ] || [ -h "$1" ]; then
        imvr "$1" >/dev/null 2>&1 &
    fi
elif which sxiv >/dev/null 2>&1; then
    if [ -f "$1" ]; then
        view_dir sxiv "$1" >/dev/null 2>&1 &
    elif [ -d "$1" ] || [ -h "$1" ]; then
        sxiv -aqt "$1" >/dev/null 2>&1 &
    fi
elif which viu >/dev/null 2>&1; then
    viu -n "$1" | less -R
else
    printf "Please install imv/sxiv/viu and check their callable names match the plugin source"
    read -r _
    exit 2
fi