summaryrefslogtreecommitdiff
path: root/.config/vifm/scripts/vifmimg
blob: 668a340bbc6f375e3923f983156cdfc7e9f25281 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
readonly ID_PREVIEW="preview"

#PLAY_GIF="yes"
# By enabling this option the GIF will be animated, by leaving it commented like it
# is now will make the gif previews behave the same way as video previews.

#AUTO_REMOVE="yes"
# By enabling this option the script will remove the preview file after it is drawn
# and by doing so the preview will always be up-to-date with the file.
# This however, requires more CPU and therefore affects the overall performance.

# The messy code below is for moving pages in pdf files in the vifm file preview by
# utilizing the < and > keys which will be bound to `vifmimg inc` or `vifmimg dec`.
PDF_PAGE_CONFIG="$HOME/.config/vifm/vifmimgpdfpage"
PDF_FILE_CONFIG="$HOME/.config/vifm/vifmimgpdffile"
PDF_PAGE=1
PDF_FILE=""
# Initialize the variables and required files
[[ -f "$PDF_PAGE_CONFIG" ]] && PDF_PAGE=$(cat $PDF_PAGE_CONFIG) || touch $PDF_PAGE_CONFIG
[[ -f "$PDF_FILE_CONFIG" ]] && PDF_FILE=$(cat $PDF_FILE_CONFIG) || touch $PDF_FILE_CONFIG


# Create temporary working directory if the directory structure doesn't exist
if [[ ! -d "/tmp$PWD/" ]]; then
    mkdir -p "/tmp$PWD/"
fi

function inc() {
	VAL="$(cat $PDF_PAGE_CONFIG)"
	echo "$(expr $VAL + 1)" > $PDF_PAGE_CONFIG
}

function dec() {
	VAL="$(cat $PDF_PAGE_CONFIG)"
	echo "$(expr $VAL - 1)" > $PDF_PAGE_CONFIG
	if [[ $VAL -le 0 ]]; then
		echo 0 > $PDF_PAGE_CONFIG
	fi
}

function previewclear() {
    declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
        > "$FIFO_UEBERZUG"
}

function fileclean() {
    if [[ -f "/tmp$PWD/$6.png" ]]; then
        rm -f "/tmp$PWD/$6.png"
    elif  [[ -d "/tmp$PWD/$6/" ]]; then
        rm -rf "/tmp$PWD/$6/"
    fi
}

function preview() {
    declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
        [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
        [path]="$PWD/$6") \
        > "$FIFO_UEBERZUG"
}

function previewvideo() {
    if [[ ! -f "/tmp$PWD/$6.png" ]]; then
        ffmpegthumbnailer -i "$PWD/$6" -o "/tmp$PWD/$6.png" -s 0 -q 10
    fi
    declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
        [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
        [path]="/tmp$PWD/$6.png") \
        > "$FIFO_UEBERZUG"
}

function previewepub() {
    if [[ ! -f "/tmp$PWD/$6.png" ]]; then
        epub-thumbnailer "$6" "/tmp$PWD/$6.png" 1024
    fi
    declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
        [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
        [path]="/tmp$PWD/$6.png") \
        > "$FIFO_UEBERZUG"
}

function previewgif() {
    if [[ ! -d "/tmp$PWD/$6/" ]]; then
        mkdir -p "/tmp$PWD/$6/"
        convert -coalesce "$PWD/$6" "/tmp$PWD/$6/$6.png"
    fi
    if [[ ! -z "$PLAY_GIF" ]]; then
        for frame in $(ls -1 /tmp$PWD/$6/$6*.png | sort -V); do
            declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
                [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
                [path]="$frame") \
                > "$FIFO_UEBERZUG"
            # Sleep between frames to make the animation smooth.
            sleep .07
        done
    else
            declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
                [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
                [path]="/tmp$PWD/$6/$6-0.png") \
                > "$FIFO_UEBERZUG"
    fi
}

function previewpdf() {
    if [[ ! "$6" == "$PDF_FILE" ]]; then
        PDF_PAGE=1
        echo 1 > $PDF_PAGE_CONFIG
        rm -f "/tmp$PWD/$6.png"
    fi

    if [[ ! "$PDF_PAGE" == "1" ]] && [[ -f "/tmp$PWD/$6.png" ]]; then
        rm -f "/tmp$PWD/$6.png"
    fi

    if [[ ! -f "/tmp$PWD/$6.png" ]]; then
        pdftoppm -png -f $PDF_PAGE -singlefile "$6" "/tmp$PWD/$6"
    fi
    echo "$6" > $PDF_FILE_CONFIG

    declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
        [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
        [path]="/tmp$PWD/$6.png") \
        > "$FIFO_UEBERZUG"
}


function previewmagick() {
    if [[ ! -f "/tmp$PWD/$6.png" ]]; then
        convert -thumbnail $(identify -format "%wx%h" "$6") "$PWD/$6" "/tmp$PWD/$6.png"
    fi
    declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
        [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
        [path]="/tmp$PWD/$6.png") \
        > "$FIFO_UEBERZUG"
}



function main() {
    case "$1" in
        "inc") inc "$@" ;;
        "dec") dec "$@" ;;
        "clear") previewclear "$@" ;;
        "clean") fileclean "$@" ;;
        "draw") preview "$@" ;;
        "videopreview") previewvideo "$@" ;;
        "epubpreview") previewepub "$@" ;;
        "gifpreview") previewgif "$@" ;;
        "pdfpreview") previewpdf "$@" ;;
        "magickpreview") previewmagick "$@" ;;
        "*") echo "Unknown command: '$@'" ;;
    esac
}
main "$@"