[cliplumber] Implement conditional feature loading

This commit is contained in:
lhark 2019-11-20 23:55:19 +00:00
parent f7b99e07d1
commit 24a61ea701

View file

@ -9,7 +9,9 @@ die() {
}
check_exists() {
which "$1" > /dev/null 2>&1 || die "$1 doesn't appear to be installed"
for c in $@; do
which "$c" > /dev/null 2>&1 || return 1
done
}
if ! which wl-paste > /dev/null && [ ! -z "$WAYLAND_DISPLAY" ]; then
@ -25,36 +27,43 @@ else
fi
urls="$(echo $clip | grep -o 'https\?://[a-zA-Z0-9~#%&_+=,.?/-]\+')"
opts="qr
audio search"
if [ -n "$urls" ]; then
opts="$(printf '%s\ndownload\nplay' "$opts")"
# Check the requirements for every option
opts=""
check_exists "$TERM_EMU" "mpv" "youtube-dl" &&
opts="$(printf "audio-search\n%s" "$opts")"
check_exists "dragon" &&
opts="$(printf "drag-n-drop\n%s" "$opts")"
# TODO: check requirements
if [ -n "$urls" ] && check_exists "youtube-dl"; then
check_exists "mpv" &&
opts="$(printf "play\n%s" "$opts")"
opts="$(printf "download\n%s" "$opts")"
fi
check_exists "qrencode" "feh" &&
opts="$(printf "qr\n%s" "$opts")"
choice="$(printf "%s" "$opts" | dmenu -p "$(printf "%s" "$clip" | cut -c 1-48)")"
case "$choice" in
"qr")
check_exists "qrencode"
check_exists "feh"
printf "%s" "$clip" | qrencode -o - | feh -. -Z --force-aliasing --geometry 400x400 -
;;
# Download supports optional folder target
"download"*)
check_exists "youtube-dl"
path="$HOME/mus/$(printf "%s" "$choice" | cut -d " " -f 2-)"
[ -d "$path" ] || mkdir -p "$path"
cd "$path" || exit
youtube-dl -x --audio-format mp3 --no-playlist -o "%(title)s.%(ext)s" "$clip" 2>&1 | grep 'ERROR:' | xargs -n1 -d "\n" notify-send
#notify-send "Error while downloading: $clip"
;;
"drag-n-drop"*)
dragon -x "$clip"
;;
"play")
check_exists "mpv"
mpv --ytdl-format='bestvideo[height<=?720]+bestaudio/best' "$clip" 2>&1 | grep 'ERROR:' | xargs -n1 -d "\n" notify-send
;;
"audio search")
check_exists "mpv"
check_exists "$TERM_EMU"
"audio-search")
"$TERM_EMU" --single-instance mpv --ytdl-format=bestaudio ytdl://ytsearch:"$clip"
;;
*)