Merge branch '38-make-vifm-display-image-video-pdf-previews' into 'master'

Resolve "Make vifm display image/video/pdf previews"

Closes #38

See merge request marty-oehme/dotfiles!12
This commit is contained in:
Marty Oehme 2019-07-04 08:31:20 +00:00
commit 942d7798e4
4 changed files with 194 additions and 3 deletions

View file

@ -0,0 +1,7 @@
#!/bin/sh
# Check for existence of vifm command. If found, substitute vifm with it.
type vifm >/dev/null 2>&1 && alias vm=vifm
# Check for existence of vifmrun command. If found, substitute vifm with it.
type vifmrun >/dev/null 2>&1 && alias vifm=vifmrun

View file

@ -1,4 +1,4 @@
" vim: filetype=vifm : " vim: filetype=vifm : set foldmethod=marker foldlevel=0 nomodeline:
" Sample configuration file for vifm (last updated: 20 July, 2018) " Sample configuration file for vifm (last updated: 20 July, 2018)
" You can edit this file by hand. " You can edit this file by hand.
" The " character at the beginning of a line comments out the line. " The " character at the beginning of a line comments out the line.
@ -6,7 +6,8 @@
" The basic format for each item is shown with an example. " The basic format for each item is shown with an example.
" ------------------------------------------------------------------------------ " ------------------------------------------------------------------------------
" Options {{{
" ==============================================================================
" This is the actual command used to start vi. The default is vim. " This is the actual command used to start vi. The default is vim.
" If you would like to use another vi clone such as Elvis or Vile " If you would like to use another vi clone such as Elvis or Vile
" you will need to change this setting. " you will need to change this setting.
@ -101,6 +102,8 @@ set mintimeoutlen=50
" set the pre-key timeout really high, see https://git.io/fNm1d " set the pre-key timeout really high, see https://git.io/fNm1d
set timeoutlen=5000 set timeoutlen=5000
" }}}
" Marks {{{
" ------------------------------------------------------------------------------ " ------------------------------------------------------------------------------
" :mark mark /full/directory/path [filename] " :mark mark /full/directory/path [filename]
@ -111,6 +114,8 @@ mark p ~/projects/
mark n ~/Nextcloud/Notes/ mark n ~/Nextcloud/Notes/
mark t ~/.local/share/vifm/Trash/ mark t ~/.local/share/vifm/Trash/
" }}}
" Commands {{{
" ------------------------------------------------------------------------------ " ------------------------------------------------------------------------------
" :com[mand][!] command_name action " :com[mand][!] command_name action
@ -134,6 +139,8 @@ command! mkcd :mkdir %a | cd %a
" command! vgrep nvim "+grep %a" " command! vgrep nvim "+grep %a"
command! reload :write | restart command! reload :write | restart
" }}}
" Filetypes {{{
" ------------------------------------------------------------------------------ " ------------------------------------------------------------------------------
" The file type is for the default programs to be used with " The file type is for the default programs to be used with
@ -234,7 +241,9 @@ filextype *.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm
\ {View in shotwell} \ {View in shotwell}
\ feh, \ feh,
fileviewer *.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm fileviewer *.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm
\ convert -identify %f -verbose /dev/null \ vifmimg draw %px %py %pw %ph %c
\ %pc
\ vifmimg clear
" OpenRaster " OpenRaster
filextype *.ora filextype *.ora
@ -368,6 +377,8 @@ filextype */
" For Windows: " For Windows:
" filetype * start, explorer " filetype * start, explorer
" }}}
" vifminfo {{{
" ------------------------------------------------------------------------------ " ------------------------------------------------------------------------------
" What should be saved automatically between vifm runs " What should be saved automatically between vifm runs
@ -389,6 +400,8 @@ set vifminfo=dhistory,savedirs,chistory,state,tui,shistory,
" "
" filter! /^.*\.(lo|o|d|class|py[co])$|.*~$/ " filter! /^.*\.(lo|o|d|class|py[co])$|.*~$/
" }}}
" Mappings {{{
" ------------------------------------------------------------------------------ " ------------------------------------------------------------------------------
" make quitting simpler " make quitting simpler
@ -460,6 +473,8 @@ nnoremap te :execute ':tree! | echo ":tree"'<cr>
nnoremap t <nop> nnoremap t <nop>
nnoremap tt t nnoremap tt t
" }}}
"
" ------------------------------------------------------------------------------ " ------------------------------------------------------------------------------
" Various customization examples " Various customization examples

154
.local/bin/vifmimg Executable file
View file

@ -0,0 +1,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 "$@"

15
.local/bin/vifmrun Executable file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
export FIFO_UEBERZUG="/tmp/vifm-ueberzug-${PPID}"
function cleanup {
rm "$FIFO_UEBERZUG" 2>/dev/null
pkill -P $$ 2>/dev/null
}
pkill -P $$ 2>/dev/null
rm "$FIFO_UEBERZUG" 2>/dev/null
mkfifo "$FIFO_UEBERZUG" >/dev/null
trap cleanup EXIT 2>/dev/null
tail --follow "$FIFO_UEBERZUG" | ueberzug layer --silent --parser bash 2>&1 >/dev/null &
vifm
cleanup