From 8aef69b77f69cf0ffc700378d11f07d5819ebfa2 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 15 Dec 2016 23:40:54 +1100 Subject: [PATCH] Song: Move dbus commands to a function --- neofetch | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/neofetch b/neofetch index 11986ed0..fbcc285b 100755 --- a/neofetch +++ b/neofetch @@ -1139,6 +1139,18 @@ get_song() { # This is absurdly long. player="$(ps x | awk '!(/awk|Helper|Cache/) && /mpd|cmus|mocp|spotify|Google Play|iTunes.app|rhythmbox|banshee|amarok|deadbeef|audacious|gnome-music|lollypop|clementine|pragha/ {printf $5 " " $6; exit}')" + get_song_dbus() { + # Multiple players use an almost identical dbus command to get the information. + # This function saves us including the same command throughtout the function. + song="$(\ + dbus-send --print-reply --dest=org.mpris.MediaPlayer2."${1}" /org/mpris/MediaPlayer2 \ + org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' |\ + awk -F 'string "' '/string|array/ {printf "%s",$2; next}{print ""}' |\ + awk -F '"' '/artist|title/ {printf $2 " - "}' + )" + song="${song% - }" + } + case "${player/*\/}" in "mpd"*) song="$(mpc current)" @@ -1159,18 +1171,7 @@ get_song() { "spotify"*) case "$os" in - "Linux") - # Thanks dbus - song="$(\ - dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 \ - org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' |\ - awk -F 'string "' '/string|array/ {printf "%s",$2; next}{print ""}' |\ - awk -F '"' '/artist|title/ {printf $2 " - "}' - )" - song="${song% - }" - song="${song/'('*}" - song="${song//'['*}" - ;; + "Linux") get_song_dbus "spotify" ;; "Mac OS X") song="$(osascript -e 'tell application "Spotify" to artist of current track as string & " - " & name of current track as string')" @@ -1219,22 +1220,9 @@ get_song() { song="$(audtool current-song)" ;; - "gnome-music"* | "lollypop"* | "clementine"*) - case "$player" in - "gnome-music"*) player="GnomeMusic" ;; - "lollypop"*) player="Lollypop" ;; - "clementine"*) player="clementine" ;; - esac - - # Hello dbus my old friend. - song="$(\ - dbus-send --print-reply --dest=org.mpris.MediaPlayer2."${player}" /org/mpris/MediaPlayer2 \ - org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' |\ - awk -F 'string "' '/string|array/ {printf "%s",$2; next}{print ""}' |\ - awk -F '"' '/artist|title/ {printf $2 " - "}' - )" - song="${song% - }" - ;; + "gnome-music"*) get_song_dbus "GnomeMusic" ;; + "lollypop"*) get_song_dbus "Lollypop" ;; + "clementine"*) get_song_dbus "clementine" ;; "pragha"*) artist="$(pragha -c | awk -F':' '/artist/ {print $2}')"