dotfiles

beau's configuration files
git clone https://git.beauhilton.com/dotfiles.git
Log | Files | Refs | README

vifmimg (4761B)


      1 #!/usr/bin/env bash
      2 readonly ID_PREVIEW="preview"
      3 
      4 #PLAY_GIF="yes"
      5 # By enabling this option the GIF will be animated, by leaving it commented like it
      6 # is now will make the gif previews behave the same way as video previews.
      7 
      8 #AUTO_REMOVE="yes"
      9 # By enabling this option the script will remove the preview file after it is drawn
     10 # and by doing so the preview will always be up-to-date with the file.
     11 # This however, requires more CPU and therefore affects the overall performance.
     12 
     13 # The messy code below is for moving pages in pdf files in the vifm file preview by
     14 # utilizing the < and > keys which will be bound to `vifmimg inc` or `vifmimg dec`.
     15 PDF_PAGE_CONFIG="$HOME/.config/vifm/vifmimgpdfpage"
     16 PDF_FILE_CONFIG="$HOME/.config/vifm/vifmimgpdffile"
     17 PDF_PAGE=1
     18 PDF_FILE=""
     19 # Initialize the variables and required files
     20 [[ -f "$PDF_PAGE_CONFIG" ]] && PDF_PAGE=$(cat $PDF_PAGE_CONFIG) || touch $PDF_PAGE_CONFIG
     21 [[ -f "$PDF_FILE_CONFIG" ]] && PDF_FILE=$(cat $PDF_FILE_CONFIG) || touch $PDF_FILE_CONFIG
     22 
     23 
     24 # Create temporary working directory if the directory structure doesn't exist
     25 if [[ ! -d "/tmp$PWD/" ]]; then
     26     mkdir -p "/tmp$PWD/"
     27 fi
     28 
     29 function inc() {
     30 	VAL="$(cat $PDF_PAGE_CONFIG)"
     31 	echo "$(expr $VAL + 1)" > $PDF_PAGE_CONFIG
     32 }
     33 
     34 function dec() {
     35 	VAL="$(cat $PDF_PAGE_CONFIG)"
     36 	echo "$(expr $VAL - 1)" > $PDF_PAGE_CONFIG
     37 	if [[ $VAL -le 0 ]]; then
     38 		echo 0 > $PDF_PAGE_CONFIG
     39 	fi
     40 }
     41 
     42 function previewclear() {
     43     declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
     44         > "$FIFO_UEBERZUG"
     45 }
     46 
     47 function fileclean() {
     48     if [[ -f "/tmp$PWD/$6.png" ]]; then
     49         rm -f "/tmp$PWD/$6.png"
     50     elif  [[ -d "/tmp$PWD/$6/" ]]; then
     51         rm -rf "/tmp$PWD/$6/"
     52     fi
     53 }
     54 
     55 function preview() {
     56     declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
     57         [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
     58         [path]="$PWD/$6") \
     59         > "$FIFO_UEBERZUG"
     60 }
     61 
     62 function previewvideo() {
     63     if [[ ! -f "/tmp$PWD/$6.png" ]]; then
     64         ffmpegthumbnailer -i "$PWD/$6" -o "/tmp$PWD/$6.png" -s 0 -q 10
     65     fi
     66     declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
     67         [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
     68         [path]="/tmp$PWD/$6.png") \
     69         > "$FIFO_UEBERZUG"
     70 }
     71 
     72 function previewepub() {
     73     if [[ ! -f "/tmp$PWD/$6.png" ]]; then
     74         epub-thumbnailer "$6" "/tmp$PWD/$6.png" 1024
     75     fi
     76     declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
     77         [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
     78         [path]="/tmp$PWD/$6.png") \
     79         > "$FIFO_UEBERZUG"
     80 }
     81 
     82 function previewgif() {
     83     if [[ ! -d "/tmp$PWD/$6/" ]]; then
     84         mkdir -p "/tmp$PWD/$6/"
     85         convert -coalesce "$PWD/$6" "/tmp$PWD/$6/$6.png"
     86     fi
     87     if [[ ! -z "$PLAY_GIF" ]]; then
     88         for frame in $(ls -1 /tmp$PWD/$6/$6*.png | sort -V); do
     89             declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
     90                 [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
     91                 [path]="$frame") \
     92                 > "$FIFO_UEBERZUG"
     93             # Sleep between frames to make the animation smooth.
     94             sleep .07
     95         done
     96     else
     97             declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
     98                 [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
     99                 [path]="/tmp$PWD/$6/$6-0.png") \
    100                 > "$FIFO_UEBERZUG"
    101     fi
    102 }
    103 
    104 function previewpdf() {
    105     if [[ ! "$6" == "$PDF_FILE" ]]; then
    106         PDF_PAGE=1
    107         echo 1 > $PDF_PAGE_CONFIG
    108         rm -f "/tmp$PWD/$6.png"
    109     fi
    110 
    111     if [[ ! "$PDF_PAGE" == "1" ]] && [[ -f "/tmp$PWD/$6.png" ]]; then
    112         rm -f "/tmp$PWD/$6.png"
    113     fi
    114 
    115     if [[ ! -f "/tmp$PWD/$6.png" ]]; then
    116         pdftoppm -png -f $PDF_PAGE -singlefile "$6" "/tmp$PWD/$6"
    117     fi
    118     echo "$6" > $PDF_FILE_CONFIG
    119 
    120     declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
    121         [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
    122         [path]="/tmp$PWD/$6.png") \
    123         > "$FIFO_UEBERZUG"
    124 }
    125 
    126 
    127 function previewmagick() {
    128     if [[ ! -f "/tmp$PWD/$6.png" ]]; then
    129         convert -thumbnail $(identify -format "%wx%h" "$6") "$PWD/$6" "/tmp$PWD/$6.png"
    130     fi
    131     declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
    132         [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
    133         [path]="/tmp$PWD/$6.png") \
    134         > "$FIFO_UEBERZUG"
    135 }
    136 
    137 
    138 
    139 function main() {
    140     case "$1" in
    141         "inc") inc "$@" ;;
    142         "dec") dec "$@" ;;
    143         "clear") previewclear "$@" ;;
    144         "clean") fileclean "$@" ;;
    145         "draw") preview "$@" ;;
    146         "videopreview") previewvideo "$@" ;;
    147         "epubpreview") previewepub "$@" ;;
    148         "gifpreview") previewgif "$@" ;;
    149         "pdfpreview") previewpdf "$@" ;;
    150         "magickpreview") previewmagick "$@" ;;
    151         "*") echo "Unknown command: '$@'" ;;
    152     esac
    153 }
    154 main "$@"