ptgdp: Switch from a getopts arg to parsing out program arguments for playlists
This is a breaking change It's also actually the right way to do things and it comes with multiple playlist support
This commit is contained in:
		
							
								
								
									
										45
									
								
								ptgdp
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								ptgdp
									
									
									
									
									
								
							@@ -18,6 +18,7 @@ _xdguserdirs="${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
 | 
			
		||||
_musicdir="${XDG_MUSIC_DIR:-$HOME/Music}"
 | 
			
		||||
_ptgdpmusicdir="$_musicdir/${PTGDP_MUSIC_DIR:-PTGDP Songs}"
 | 
			
		||||
declare -a _queue
 | 
			
		||||
declare -a _playlists
 | 
			
		||||
 | 
			
		||||
# Helper functions
 | 
			
		||||
log() {
 | 
			
		||||
@@ -177,8 +178,15 @@ execqueue() {
 | 
			
		||||
		file=${file##$_musicdir/}
 | 
			
		||||
		mpc add "$file" > /dev/null 2>&1 || error "Could not add file: \"$file\""
 | 
			
		||||
	done
 | 
			
		||||
}
 | 
			
		||||
execplaylists() {
 | 
			
		||||
	# Plays all playlists in _playlists
 | 
			
		||||
	for list in "${_playlists[@]}"; do
 | 
			
		||||
		playlist "$list" || error "Failed to play playlist: \"$list\""
 | 
			
		||||
	done
 | 
			
		||||
	if [ -n "$_optautoplay" ]; then
 | 
			
		||||
		mpc play > /dev/null 2>&1
 | 
			
		||||
		log "Started playback"
 | 
			
		||||
		unset _optautoplay
 | 
			
		||||
	fi
 | 
			
		||||
}
 | 
			
		||||
@@ -232,13 +240,9 @@ clearcache() {
 | 
			
		||||
}
 | 
			
		||||
helptext() {
 | 
			
		||||
	cat << EOF
 | 
			
		||||
Usage: $_name [OPTION]
 | 
			
		||||
Use youtube-dl and mpc to queue up a playlist given a file of only search
 | 
			
		||||
queries. The first result found is the one that will be downloaded. Downloaded
 | 
			
		||||
files are cached in your Music folder under "PTGDP Songs" for offline use.
 | 
			
		||||
  -f <file>		The playlist file to load. The file should be plaintext
 | 
			
		||||
			containing a YouTube search query on each line.
 | 
			
		||||
			Comments are supported and prepended with #
 | 
			
		||||
Usage: $_name [OPTION]... [FILE]...
 | 
			
		||||
Use youtube-dl and a music player to queue up or download a number of songs
 | 
			
		||||
given plaintext FILEs with only search queries.
 | 
			
		||||
 | 
			
		||||
  -c			Clears the cache (which can become quite large)
 | 
			
		||||
  -d			Download only; don't queue anything up
 | 
			
		||||
@@ -303,7 +307,8 @@ rofimenu() {
 | 
			
		||||
	choice="$(rofi -dmenu -i -p "$prompt" <<< "$playlists" 2>/dev/null)"
 | 
			
		||||
	[ -z "$choice" ] && error "User aborted at selection" 62
 | 
			
		||||
	if [ -z "$_optrofisong" ]; then
 | 
			
		||||
		playlist "$_optrofi"/"$choice".gdp
 | 
			
		||||
		_playlists+=("$_optrofi"/"$choice".gdp)
 | 
			
		||||
		execplaylists
 | 
			
		||||
	else
 | 
			
		||||
		rofiplaysong "$_optrofi"/"$choice".gdp
 | 
			
		||||
	fi
 | 
			
		||||
@@ -401,7 +406,7 @@ main() {
 | 
			
		||||
	mkdir -p "$_ptgdpmusicdir"
 | 
			
		||||
	trap trapexit EXIT
 | 
			
		||||
 | 
			
		||||
	# Actual program stuff
 | 
			
		||||
	# Get options
 | 
			
		||||
	while getopts ":cdDf:pr:R:sh" opt; do
 | 
			
		||||
		case $opt in
 | 
			
		||||
			c)
 | 
			
		||||
@@ -414,9 +419,6 @@ main() {
 | 
			
		||||
			D)
 | 
			
		||||
				_optdryrun=1
 | 
			
		||||
				;;
 | 
			
		||||
			f)
 | 
			
		||||
				_optfile="$OPTARG"
 | 
			
		||||
				;;
 | 
			
		||||
			p)
 | 
			
		||||
				_optautoplay=1
 | 
			
		||||
				;;
 | 
			
		||||
@@ -442,12 +444,25 @@ main() {
 | 
			
		||||
				;;
 | 
			
		||||
		esac
 | 
			
		||||
	done
 | 
			
		||||
	# Get program arguments (playlists)
 | 
			
		||||
	shift $((OPTIND - 1))
 | 
			
		||||
	for list in "$@"; do
 | 
			
		||||
		if ! [ -f "$list" ]; then
 | 
			
		||||
			error "Failed to find playlist: $list"
 | 
			
		||||
			continue
 | 
			
		||||
		fi
 | 
			
		||||
		_playlists+=("$list")
 | 
			
		||||
	done
 | 
			
		||||
	[ -z "$_optdownloadonly" ] && [ -z "$_optdryrun" ] && ! validatedeps mpc && error "$_return is required outside of dry- and download-only runs"
 | 
			
		||||
	[ -n "$_optfile" ] && [ -n "$_optrofi" ] && error "Flags -f and -r conflict" 2
 | 
			
		||||
	[ -n "$_playlists" ] && [ -n "$_optrofi" ] && error "Flag -r cannot be used with playlist arguments" 2
 | 
			
		||||
	[ -n "$_optdownloadonly" ] && [ -n "$_optautoplay" ] && error "Flags -d and -p conflict" 2
 | 
			
		||||
	if [ -n "$_optrofi" ]; then rofimenu; exit $?; fi
 | 
			
		||||
	if [ -n "$_optfile" ]; then playlist "$_optfile"; exit $?; fi
 | 
			
		||||
	error "Nothing to do" 0
 | 
			
		||||
	if [ -n "$_playlists" ]; then
 | 
			
		||||
		execplaylists
 | 
			
		||||
		exit $?
 | 
			
		||||
	else
 | 
			
		||||
		error "Nothing to do" 1
 | 
			
		||||
	fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
main "$@"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user