-
Notifications
You must be signed in to change notification settings - Fork 3
Example callback scripts
Daniel Gray edited this page Jan 27, 2017
·
8 revisions
by @DanielFGray
#!/usr/bin/env bash
# https://github.com/DanielFGray/yaxg/wiki/Example-callback-scripts
declare -r default_preview='mirage'
declare -r webm_preview='mpv --loop=inf --fullscreen --video-unscaled=downscale-big'
declare -r upload_script='tekup'
declare -r file="$1"
declare -A colors
colors[red]=$(tput setaf 1)
colors[blue]=$(tput setaf 4)
colors[reset]=$(tput sgr0)
in_term() {
[[ -t 0 || -p /dev/stdin ]]
}
has() {
local verbose
if [[ $1 = '-v' ]]; then
verbose=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( "$verbose" > 0 )) && err "$c not found"
return 1
fi
done
}
info() {
if in_term; then
printf '%s\n' "$*" >&2
else
notify-send 'yaxg' "$*"
fi
}
err() {
if in_term; then
printf "${colors[red]}%s${colors[reset]}\n" "$*" >&2
else
notify-send -u critical 'yaxg: error' "$*"
fi
}
die() {
[[ -n "$1" ]] && err "$1"
exit 1
}
ask() {
if in_term; then
read -r -n1 -p "$* " ans
printf '\n'
[[ ${ans^} = Y* ]]
else
zenity --title='yaxg' --question --text="$*" 2> /dev/null
fi
}
# optimize
if has optipng && [[ "$file" =~ png$ ]]; then
info "optimizing $file"
optipng -q "$file"
fi
# preview
if [[ "$file" =~ webm$ ]]; then
$webm_preview "$file" 2> /dev/null
else
$default_preview "$file" 2> /dev/null
fi
# upload
if [[ -f "$file" ]]; then
if ask "Upload with ${upload_script%% *}?"; then
response=$($upload_script "$file")
info "$response"
url=$(command grep -m1 -oP 'https://[[:alnum:]?=%/_.:,;~@!#$&()*+-]+' <<< "$response")
xclip -r <<< "$url"
fi
# ask "Delete $file?" && rm "$file"
fi