diff --git a/svw b/svw index fe3ea33..aaedd71 100644 --- a/svw +++ b/svw @@ -1,34 +1,101 @@ #!/usr/bin/env sh -printf "%b\n" "" | xclip -i -selection clipboard - stop_file="/tmp/stop-select-videos-to-watch" -if [ ! -e $stop_file ]; then - touch $stop_file -else - notify-send "Error" "Already selecting" + +cleanup() { + rm "$videos_file" 2>/dev/null +} +trap cleanup INT TERM + +usage() { + echo "Usage: $0 [-1] [-h]" 1>&2 + echo "" 1>&2 + printf "This is a program made to copy multimedia URLs into system clibboard +and play them through \$VIDEOPLAYER (if it is not set, mpv).\n" 1>&2 + printf "\t-1 Makes videos be played on a single instance of \$VIDEOPLAYER +instead of each one individually.\n" 1>&2 + printf "\t-t Toggle svw.\n" 1>&2 + printf "\t-s Stop svw from looking for clipboard changes.\n" 1>&2 + printf "\t-h (or any other flag) prints this help menu.\n" 1>&2 exit 1 -fi +} -# While doesnt say to stop, continue -while [ -f "$stop_file" ] -do - clipnotify & - echo "$!" > "$stop_file" - wait - link="$(xclip -o -selection clipboard)" - if [ -n "$link" ] && [ -z "$(printf "%b\n" "$links" | grep "$link")" ]; then - links="$links\n$link" - echo "$link" +svw_toggle() { + if [ ! -e "$stop_file" ]; then + notify-send "Looking" "Select Video to Watch" + svw + else + notify-send "Stop Looking" + svw_stop fi +} + +svw_stop() { + xargs kill < "$stop_file" + rm "$stop_file" +} + +svw() { + printf "%b\n" "" | xclip -i -selection clipboard + + if [ ! -e $stop_file ]; then + touch $stop_file + else + notify-send "Error" "Already selecting" + exit 1 + fi + + # While doesnt say to stop, continue + while [ -f "$stop_file" ] + do + clipnotify & + echo "$!" > "$stop_file" + wait + link="$(xclip -o -selection clipboard)" + if [ -n "$link" ] && [ -z "$(printf "%b\n" "$links" | grep "$link")" ] + then + links="$links\n$link" + echo "$link" + fi + done + + # Get first link and download + videos_file="/tmp/selected-videos-to-watch$(date +%s)" + + if [ -n "$VIDEOPLAYER" ]; then + player=$VIDEOPLAYER + else + player="mpv" + fi + + [ -n "$links" ] && + if [ -n "$single" ]; then + printf "%b\n" "$links" | tail -n +2 | xargs "$player" + else + printf "%b\n" "$links" | tail -n +2 | xargs -L1 "$player" + fi + + rm "$videos_file" 2>/dev/null +} + +while getopts ":1ts" option; do + case "${option}" in + 1) + single="1" + ;; + t) + command_to_run="svw_toggle" + ;; + s) + command_to_run="svw_stop" + ;; + *) + command_to_run="usage" + ;; + esac done +shift $((OPTIND-1)) -# Get first link and download -videos_file="/tmp/selected-videos-to-watch$(date +%s)" +[ -z "$command_to_run" ] && command_to_run="svw" -[ -n "$links" ] && printf "%b\n" "$links" | tail -n +2 > "$videos_file" - -# Make mpv start playing videos from the file -[ ! -f $stop_file ] && dash $TSCRIPTS/play-videos-from-file.sh $videos_file - -rm "$videos_file" +$command_to_run