helm/shell/+mpd/bin/notify-song.sh

64 lines
1.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# A non-interactive script that creates notifications (libnotify on Linux,
# native notifications on MacOS), announcing the currently playing song. Used by
# ncmpcpp.
2017-04-26 20:26:01 -07:00
_color() { printf '<span fgcolor="%s">%s</span>' "$1" "$2"; }
if ! pgrep mpd >/dev/null; then
>&2 echo "mpd isn't running."
exit 1
fi
2017-04-26 20:26:01 -07:00
info=$(mpc current -f ";%artist%;%album%;%title%;%file%")
if [[ ${info:0:1} != ";" ]]; then
>&2 echo "nothing is playing."
2017-04-26 20:26:01 -07:00
exit 1
fi
2017-04-26 20:26:01 -07:00
IFS=';' read -r _ artist album title file <<< "$info"
2017-04-26 20:26:01 -07:00
artist="${artist:-$(_color "#757575" "No Artist")}"
album="${album:-$(_color "#757575" "No Album")}"
title="${title:-$(_color "#757575" "Untitled")}"
rating=
2017-04-26 20:26:01 -07:00
case $(grep "$file" ~/music/.db/playlists/*-star.m3u) in
*/1-star.m3u:*) starn=1 ;;
*/2-star.m3u:*) starn=2 ;;
*/3-star.m3u:*) starn=3 ;;
*/4-star.m3u:*) starn=4 ;;
*/5-star.m3u:*) starn=5 ;;
esac
if [[ $starn ]]; then
star_filled_icon="★"
star_empty_icon="☆"
if [[ $OSTYPE == linux* ]]; then
star_filled_icon=$(_color "#8fb1cd" "$star_filled_icon")
star_empty_icon=$(_color "#666666" "$star_empty_icon")
fi
2017-04-26 20:26:01 -07:00
for ((i=1;i<=5;i++)); do
if (( "$starn" >= "$i" )); then
rating="${rating}${star_filled_icon}"
else
rating="${rating}${star_empty_icon}"
fi
2017-04-26 20:26:01 -07:00
done
fi
case $OSTYPE in
darwin*)
[[ $rating ]] && rating="[$rating] "
killall terminal-notifier
terminal-notifier -title "$rating$title" -message "by $artist :: $album" -appIcon "/Applications/iTunes.app/Contents/Resources/iTunes.icns" &
;;
linux*)
[[ $rating ]] && rating="$rating\n"
notify-send --icon="media-playback-start" --app-name="ncmpcpp" "$rating$title" "${artist}\n<i>${album}</i>" &
;;
esac >/dev/null 2>&1
exit