Re-factor MPD currentsong parsing
[khatus.git] / bin / khatus_controller
index 1ff6e83..ae4a8dc 100755 (executable)
     db["volume"] = $0
 }
 
-/^in:MPD_STATE/\
+/^in:MPD_SONG +file:/\
 {
     fields_shift()
-    db["mpd_state"]             = $1
-    db["mpd_curr_song_time"]    = $2
-    db["mpd_curr_song_percent"] = $3
+    fields_shift()
+    db["mpd_song_curr_file"] = $0
 }
 
-/^in:MPD_SONG/\
+/^in:MPD_SONG +Title:/\
 {
     fields_shift()
-    db["mpd_curr_song_name"] = $0
+    fields_shift()
+    db["mpd_song_curr_title"] = $0
+}
+/^in:MPD_SONG +Name:/\
+{
+    fields_shift()
+    fields_shift()
+    db["mpd_song_curr_name"] = $0
+}
+
+/^in:MPD_STATE /\
+{
+    fields_shift()
+    db["mpd_status_state"]   = $1
+    db["mpd_status_time"]    = $2
+    db["mpd_status_percent"] = $3
 }
 
 /^in:WEATHER/\
@@ -379,7 +393,7 @@ function make_status_net(    \
 }
 
 function make_status_mpd(    state, status) {
-    state = db["mpd_state"]
+    state = db["mpd_status_state"]
 
     if (state == "play") {
         status = make_status_mpd_state_known("▶")
@@ -394,13 +408,29 @@ function make_status_mpd(    state, status) {
     return sprintf("[%s]", status)
 }
 
-function make_status_mpd_state_known(symbol) {
+function make_status_mpd_state_known(symbol,    \
+    currently_playing, name, title, file\
+) {
+    name  = db["mpd_song_curr_name"]
+    title = db["mpd_song_curr_title:"]
+    file  = db["mpd_song_curr_file"]
+
+    if (name) {
+        currently_playing = name
+    } else if (title) {
+        currently_playing = title
+    } else if (file) {
+        last = split(file, parts, "/")
+        currently_playing = parts[last]
+    } else {
+        currently_playing = ""
+    }
     return sprintf(\
         "%s %s %s %s",
         symbol,
-        db["mpd_curr_song_time"],
-        db["mpd_curr_song_percent"],
-        substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
+        db["mpd_status_time"],
+        db["mpd_status_percent"],
+        substr(currently_playing, 1, opt_mpd_song_max_chars)\
     )
 }
 
This page took 0.043459 seconds and 4 git commands to generate.