From 24a61ea701a8d7b911dac07c5ce7ea6339a8dee9 Mon Sep 17 00:00:00 2001 From: lhark Date: Wed, 20 Nov 2019 23:55:19 +0000 Subject: [PATCH] [cliplumber] Implement conditional feature loading --- bin/cliplumber | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/bin/cliplumber b/bin/cliplumber index 3fa3cdd..091301c 100755 --- a/bin/cliplumber +++ b/bin/cliplumber @@ -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" ;; *)