speedwm-personal/modules/module_music
2022-10-27 17:51:19 +02:00

233 lines
8 KiB
Bash
Executable file

#!/bin/sh
# music module for status/stellar
# load config
. $HOME/.config/speedwm/statusrc
BINDIR="$(dirname $(command -v speedwm_status))/"
ARG1="$1"
CLICK() {
# values
ICON="$ITEM9_ICON"
BUTTON="$(cat /tmp/speedwm-button)"
if [ "$ITEM9_BACKEND" = "auto" ]; then
command -v mocp > /dev/null && ITEM9_BACKEND=mocp
command -v cmus > /dev/null && ITEM9_BACKEND=cmus
else
if [ "$ITEM9_BACKEND" = "cmus" ]; then
command -v cmus > /dev/null || ITEM9_BACKEND=mocp
elif [ "$ITEM9_BACKEND" = "mocp" ]; then
command -v mocp > /dev/null || ITEM9_BACKEND=cmus
fi
fi
# for cmus
CMUS() {
MUSIC_FILENAME="$(basename "$(cmus-remote -C status | grep file)")"
MUSIC_FILENAME_CUT="$(basename "$(cmus-remote -C status | grep file)"| sed 's|\(.*\)[.].*|\1|')"
MUSIC_FILEDIR="$(dirname "$(cmus-remote -C status | grep file | sed 's/file //g')")"
MUSIC_ARTIST="$(cmus-remote -C status | grep "tag artist" | sed "s/tag artist //g")"
MUSIC_SONG="$(cmus-remote -C status | grep "tag title" | sed "s/tag title //g")"
MUSIC_ALBUM="$(cmus-remote -C status | grep "tag album" | head -n 1 | sed "s/tag album //g")"
MUSIC_GENRE="$(cmus-remote -C status | grep "tag genre" | sed "s/tag genre //g")"
MUSIC_TIMEELAPSED="$(cmus-remote -Q | grep position | sed "s/position //g" | awk '{printf "%02d:%02d:%02d",$0/3600,$0%3600/60,$0%60}' | sed "s/00://")"
MUSIC_TIMETOTAL="$(cmus-remote -Q | grep duration | sed "s/duration //g" | awk '{printf "%02d:%02d:%02d",$0/3600,$0%3600/60,$0%60}' | sed "s/00://")"
MUSIC_REPEAT="$(cmus-remote -C status | grep 'repeat ' | sed "s/true/Yes/; s/false/No/; s/set repeat //")"
MUSIC_SHUFFLE="$(cmus-remote -C status | grep 'shuffle' | sed "s/on/Yes/; s/off/No/; s/set shuffle //")"
}
# for mocp
MOCP() {
MUSIC_FILENAME="$(basename "$(mocp -Q %file)")"
MUSIC_FILENAME_CUT="$(basename "$(mocp -Q %file)" | sed 's|\(.*\)[.].*|\1|')"
MUSIC_FILEDIR="$(dirname "$(mocp -Q %file)")"
MUSIC_ARTIST="$(mocp -Q %artist)"
MUSIC_SONG="$(mocp -Q %song)"
MUSIC_ALBUM="$(mocp -Q %album)"
MUSIC_TIMEELAPSED="$(mocp -Q %ct)"
MUSIC_TIMETOTAL="$(mocp -Q %tt)"
# todo: genre
}
# set variables with content
case "$ITEM9_BACKEND" in
"cmus") CMUS ;;
"mocp") MOCP ;;
esac
# terminal
case "$TERMINAL" in
"") TERMINAL=st
esac
# button
case "$BUTTON" in
"3") $TERMINAL -e $ITEM9_BACKEND ; exit ;;
esac
# no spacing if there's no icon
if [ "$ICON" != "" ]; then
ICONSPACING=" " # one character spacing
fi
# send the notification
SEND_NOTIF() {
if [ "$ITEM9_BACKEND" = "cmus" ]; then
FULL_MUSIC="$(printf "${ICON}${ICONSPACING}Artist: $MUSIC_ARTIST\nTitle: $MUSIC_SONG\nAlbum: $MUSIC_ALBUM\nGenre: $MUSIC_GENRE\nTime elapsed: $MUSIC_TIMEELAPSED\nTotal time: $MUSIC_TIMETOTAL\nFile: $MUSIC_FILENAME\nDirectory: $MUSIC_FILEDIR\nPath: $MUSIC_FILEDIR/$MUSIC_FILENAME\nRepeat: $MUSIC_REPEAT\nShuffle: $MUSIC_SHUFFLE")"
else
FULL_MUSIC="$(printf "${ICON}${ICONSPACING}Artist: $MUSIC_ARTIST\nTitle: $MUSIC_SONG\nAlbum: $MUSIC_ALBUM\nTime elapsed: $MUSIC_TIMEELAPSED\nTotal time: $MUSIC_TIMETOTAL\nFile: $MUSIC_FILENAME\nDirectory: $MUSIC_FILEDIR\nPath: $MUSIC_FILEDIR/$MUSIC_FILENAME")"
fi
notify-send "$FULL_MUSIC"
echo "Notification sent successfully!"
echo "INFO: $FULL_MUSIC"
exit 0
}
command -v notify-send > /dev/null && SEND_NOTIF
echo "FATAL: libnotify not installed, can't send notification."
exit 1
}
# information itself
PRINT() {
# Auto set backend if set to auto
if [ "$ITEM9_BACKEND" = "auto" ]; then
if [ -e "${BINDIR}cmus" ]; then
ITEM9_BACKEND="cmus"
elif [ -e "${BINDIR}mocp" ]; then
ITEM9_BACKEND="mocp"
fi
fi
if [ "$ENABLE_ITEM9" = "true" ]; then
if [ "$ITEM9_BACKEND" = "mocp" ]; then
if [ -e "${BINDIR}mocp" ]; then
if [ "$(echo $(mocp -Q %file))" != "" ]; then
# filename
if [ "$ITEM9_DISPLAY_FILE" = "true" ]; then
if [ "$(mocp -Q %file | head -n 1)" != "" ]; then
echo "$SEPARATOR $ITEM9_ICON $ITEM9_PRE_FILE$(basename "$(mocp -Q %file)" | sed 's|\(.*\)[.].*|\1|')$ITEM9_POST_FILE"
fi
else
# artist
if [ "$ITEM9_DISPLAY_ARTIST" = "true" ]; then
if [ "$(mocp -Q %artist | head -n 1)" != "" ]; then
MUSIC_ARTIST="$(mocp -Q %artist)"
fi
fi
# title
if [ "$ITEM9_DISPLAY_TITLE" = "true" ]; then
if [ "$(mocp -Q %song | head -n 1)" != "" ]; then
MUSIC_SONG="$(mocp -Q %song)"
fi
fi
# album
if [ "$ITEM9_DISPLAY_ALBUM" = "true" ]; then
if [ "$(mocp -Q %album | head -n 1)" != "" ]; then
MUSIC_ALBUM="$(mocp -Q %album)"
fi
fi
# time elapsed
if [ "$ITEM9_DISPLAY_TIMEELAPSED" = "true" ]; then
if [ "$(mocp -Q %tl | head -n 1)" != "" ]; then
MUSIC_TIMEELAPSED="$(mocp -Q %ct)"
fi
fi
# total time
if [ "$ITEM9_DISPLAY_TIMETOTAL" = "true" ]; then
if [ "$(mocp -Q %tt | head -n 1)" != "" ]; then
MUSIC_TIMETOTAL="$(mocp -Q %tt)"
fi
fi
data="$(printf "$ITEM9_FORMAT" | sed "s|@ab|$MUSIC_ALBUM|; s|@t|$MUSIC_SONG|; s|@g||; s|@a|$MUSIC_ARTIST|; s|@tt|$MUSIC_TIMETOTAL|; s|@te|$MUSIC_TIMEELAPSED|; s| ||g")"
# actually print it
echo "$SEPARATOR $ITEM9_ICON $data"
fi
fi
fi
fi
fi
if [ "$ENABLE_ITEM9" = "true" ]; then
if [ -e "${BINDIR}cmus-remote" ]; then
if [ "$(cmus-remote -C status | head -n 1 | awk '{ print $2 }')" = "playing" ]; then
if [ "$ITEM9_DISPLAY_FILE" = "true" ]; then
echo "$SEPARATOR $ITEM9_ICON $ITEM9_PRE_FILE$(basename "$(cmus-remote -C status | grep file)"| sed 's|\(.*\)[.].*|\1|')$ITEM9_POST_FILE"
else
# artist
if [ "$ITEM9_DISPLAY_ARTIST" = "true" ]; then
if [ "$(cmus-remote -C status | grep "tag artist")" != "" ]; then
MUSIC_ARTIST="$(cmus-remote -C status | grep "tag artist" | sed "s/tag artist //g")"
fi
fi
# title
if [ "$ITEM9_DISPLAY_TITLE" = "true" ]; then
if [ "$(cmus-remote -C status | grep "tag title")" != "" ]; then
MUSIC_SONG="$(cmus-remote -C status | grep "tag title" | sed "s/tag title //g")"
fi
fi
# album
if [ "$ITEM9_DISPLAY_ALBUM" = "true" ]; then
if [ "$(cmus-remote -C status | grep "tag album")" != "" ]; then
MUSIC_ALBUM="$(cmus-remote -C status | grep "tag album" | head -n 1 | sed "s/tag album //g")"
fi
fi
# genre
if [ "$ITEM9_DISPLAY_GENRE" = "true" ]; then
if [ "$(cmus-remote -C status | grep "tag genre")" != "" ]; then
MUSIC_GENRE="$(cmus-remote -C status | grep "tag genre" | sed "s/tag genre //g")"
fi
fi
# time elapsed
if [ "$ITEM9_DISPLAY_TIMEELAPSED" = "true" ]; then
if [ "$(cmus-remote -Q | grep position | sed "s/position //g")" != "" ]; then
MUSIC_TIMEELAPSED="$(cmus-remote -Q | grep position | sed "s/position //g" | awk '{printf "%02d:%02d:%02d",$0/3600,$0%3600/60,$0%60}' | sed "s/00://")"
fi
fi
# total time
if [ "$ITEM9_DISPLAY_TIMETOTAL" = "true" ]; then
if [ "$(cmus-remote -Q | grep duration | sed "s/duration //g")" != "" ]; then
MUSIC_TIMETOTAL="$(cmus-remote -Q | grep duration | sed "s/duration //g" | awk '{printf "%02d:%02d:%02d",$0/3600,$0%3600/60,$0%60}' | sed "s/00://")"
fi
fi
data="$(printf "$ITEM9_FORMAT" | sed "s|@ab|$MUSIC_ALBUM|; s|@t|$MUSIC_SONG|; s|@g|$MUSIC_GENRE|; s|@a|$MUSIC_ARTIST|; s|@tt|$MUSIC_TIMETOTAL|; s|@te|$MUSIC_TIMEELAPSED|; s| ||g")"
# actually print it
if [ "$data" != "" ]; then
echo "$SEPARATOR $ITEM9_ICON $data"
fi
fi
fi
fi
fi
}
# argument 1
case "$ARG1" in
"") CLICK ;;
"--print") PRINT > /tmp/module_music ;;
"--click") CLICK ;;
esac