summaryrefslogtreecommitdiff
path: root/.config/vifm/scripts/vifmimg
blob: e45a9031536d0a739b7b886b1125025e0ca69990 (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
#!/usr/bin/env bash
readonly ID_PREVIEW="preview"

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

function font_preview() {
  # if no preview found, generate one
  if [ ! -f "/tmp${PWD}/$6.png" ]; then
    fontpreview -i "$6" -o "/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 pdf_preview() {
  # if no preview found, generate one
  if [ ! -f "/tmp${PWD}/$6.png" ]; then
    pdftoppm -png -singlefile "$6" "/tmp${PWD}/$6"
  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 audio_preview() {
  # if no preview found, generate one
  if [ ! -f "/tmp${PWD}/$6.png" ]; then
    ffmpeg -i "$6" "/tmp${PWD}/$6.png" -y &> /dev/null
  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 video_preview() {
  # if no preview found, generate one
  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 epub_preview() {
    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"
}

# clear preview image
function clear_preview() {
  declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
    > "$FIFO_UEBERZUG"
}

# Check all the dependencies that are needed to show all the file previews.
# The reason why we are sending a notification incase a dependency does not
# exist is because that is the easiest way to get the user's attention.
dependencies=(ffmpeg ffmpegthumbnailer fontpreview pdftoppm epub-thumbnailer)
for dependency in "${dependencies[@]}"; do
    type -p "$dependency" &>/dev/null || {
        notify-send "vifm" "Could not find '${dependency}', is it installed?"
        exit 1
    }
done


# Make sure ueberzug's fifo exists
if [ -e "$FIFO_UEBERZUG" ]; then
  # Make a temp dir for rendering if not exists
  [ ! -d "/tmp${PWD}/" ] && mkdir -p "/tmp${PWD}/"
  case "$1" in
    draw) draw "$@" ;;
    font) font_preview "$@" ;;
    pdf) pdf_preview "$@" ;;
    video) video_preview "$@" ;;
    clear) clear_preview ;;
    audio) audio_preview "$@" ;;
    epub) epub_preview "$@" ;;
  esac
fi