~anjan/sxmo-userscripts

b50d4c3e8da7a19c26752ce8468e07ce613d0cc2 — Stacy Harper 3 months ago 294d33f
Add musicpd user script

Signed-off-by: Stacy Harper <contact@stacyharper.net>
Signed-off-by: Anjandev Momi <anjan@momi.ca>
2 files changed, 186 insertions(+), 0 deletions(-)

M README.md
A musicpd.sh
M README.md => README.md +5 -0
@@ 38,6 38,11 @@ Please use [git send-email](https://git-send-email.io/) and send a patch to my [
- License: MIT
- Description: Use mblaze, dmenu, and msmtp to see emails

## musicpd.sh
- Author: Stacy Harper <contact@stacyharper.net>
- License: MIT
- Description: Dmenu mpd client to control music

## ticker-portfolio
- Author: Anjandev Momi <anjan@momi.ca>
- License: MIT

A musicpd.sh => musicpd.sh +181 -0
@@ 0,0 1,181 @@
#!/usr/bin/env sh
# shellcheck disable=SC2154

# shellcheck disable=SC1090
. "$(which sxmo_common.sh)"

prompt() {
	dmenu -i -c -l 13 "$@"
}

set -e

loadmarkers() {
	if ! MPC="$(mpc)"; then
		MARKERS="disconnected"
		return
	fi

	repeat="$(printf %s "$MPC" | grep -qF "repeat: on" && printf "r" || printf "" )"
	random="$(printf %s "$MPC" | grep -qF "random: on" && printf "z" || printf "" )"
	single="$(printf %s "$MPC" | grep -qF "single: on" && printf "s" || printf "" )"
	consume="$(printf %s "$MPC" | grep -qF "consume: on" && printf "c" || printf "" )"
	MARKERS="[$repeat$random$single$consume]"
}

playlistloop() {
	while true ;
	do
		CHOICE="$(
			mpc playlist |
			xargs -0 printf "$icon_ret Return\n%s" |
			prompt -p "Playlist"
		)"

		case "$CHOICE" in
			"$icon_ret Return")
				break
				;;
			*)
				echo "$CHOICE" \
					| awk -F " - " '{ print $2 }' \
					| xargs -I{} mpc searchplay "{}"
				;;
		esac
	done
}

browseloop() {
	path=""
	while true ;
	do
		[ "$path" = "." ] && path=
		CHOICE="$(
			mpc ls "$path" |
			xargs -0 printf "$icon_ret Return\n..\n*\n%s" |
			prompt -p "Browse"
		)"

		case "$CHOICE" in
			"$icon_ret Return")
				break
				;;
			"*")
				mpc add "$path"
				;;
			"..")
				path="$(dirname "$path")"
				;;
			*"."*)
				mpc add "$CHOICE"
				;;
			*)
				path="$CHOICE/"
				;;
		esac
	done
}

optionloop() {
	while true; do
		loadmarkers

		CHOICES="
			$icon_ret Return
			$icon_rol Repeat      ^ mpc repeat
			$icon_ror Single      ^ mpc single
			$icon_sfl Random      ^ mpc random
			$icon_shr Consume     ^ mpc consume
		"

		PICKED="$(
			echo "$CHOICES" |
			xargs -0 echo |
			sed '/^[[:space:]]*$/d' |
			awk '{$1=$1};1' |
			cut -d'^' -f1 |
			prompt -p "Options $MARKERS"
		)"

		case "$PICKED" in
			"$icon_ret Return")
				break
				;;
			*)
				CMD="$(
					printf '%b' "$CHOICES" |
					grep -m1 -F "$PICKED" |
					cut -d'^' -f2 |
					sed '/^[[:space:]]*$/d' |
					awk '{$1=$1};1'
				)"
				sh -c "$CMD"
				;;
		esac
	done
}

mpdtoggleline() {
	command -v mpd > /dev/null || return
	pgrep mpd > /dev/null \
		&& printf "%s Stop MPD ^ mpd --kill" "$icon_zzz" \
		|| printf "%s Start MPD ^ mpd" "$icon_pwr"
	printf " && sleep 1"
}

mainloop() {
	while true; do
		loadmarkers
		CHOICES="
			$icon_pau Toggle      ^ mpc toggle
			$icon_stp Stop        ^ mpc stop
			$icon_nxt Next        ^ mpc next
			$icon_prv Previous    ^ mpc prev
			$icon_lst Playlist
			$icon_dir Browse
			$icon_cfg Options
			$icon_spk Audio       ^ sxmo_appmenu.sh audioout
			$icon_bth Bluetooth   ^ sxmo_bluetoothmenu.sh
			$icon_rld Update      ^ mpc update
			$icon_cls Clear Queue ^ mpc clear
			$(pgrep mpd > /dev/null && printf "%s Stop MPD ^ mpd --kill" "$icon_zzz" || printf "%s Start MPD ^ mpd" "$icon_pwr") && sleep 1
			$icon_ret Close Menu
		"

		PICKED="$(
			echo "$CHOICES" |
			xargs -0 echo |
			sed '/^[[:space:]]*$/d' |
			awk '{$1=$1};1' |
			cut -d'^' -f1 |
			prompt -p "Music $MARKERS"
		)"

		case "$PICKED" in
			"$icon_ret Close Menu")
				break
				;;
			"$icon_lst Playlist")
				playlistloop
				;;
			"$icon_dir Browse")
				browseloop
				;;
			"$icon_cfg Options")
				optionloop
				;;
			*)
				CMD="$(
					printf '%b' "$CHOICES" |
					grep -m1 -F "$PICKED" |
					cut -d'^' -f2 |
					sed '/^[[:space:]]*$/d' |
					awk '{$1=$1};1'
				)"
				sh -c "$CMD"
				;;
		esac
	done
}

mainloop