]> git.llucax.com Git - personal/ion3-config.git/commitdiff
Bugfix: don't assume the two MPD responses will come as one in statusd_mpd.
authorLeandro Lucarella <llucax@gmail.com>
Fri, 2 Nov 2007 20:49:54 +0000 (17:49 -0300)
committerLeandro Lucarella <llucax@gmail.com>
Fri, 2 Nov 2007 20:49:54 +0000 (17:49 -0300)
statusd_mpd.lua

index 0bf006b5c935fad351080a4477f29c8c9d633809..9d3cee4ab1cbe74f4e24293902a8b83d0f785bab 100644 (file)
@@ -98,66 +98,77 @@ local function start_execution()
                 data = coroutine.yield()
             end
 
-            local info = {}
-            local hint = "normal"
+            -- process only if we have 2 OK responses
+            local start, end_ = string.find(data, "\nOK")
+            if start and string.find(data, "\nOK", end_) then
 
-            for attrib, val in string.gfind(data, "([^\n]-): ([^\n]*)") do
+                local info = {}
+                local hint = "normal"
 
-                if attrib == "time" then
-                    _, _, info.pos, info.len = string.find(val, "(%d+):(%d+)")
-                    if settings.important_time == -1
-                                    or info.pos+0 <= settings.important_time
+                for attrib, val in string.gfind(data, "([^\n]-): ([^\n]*)") do
+
+                    if attrib == "time" then
+                        _, _, info.pos, info.len = string.find(val,
+                                                                "(%d+):(%d+)")
+                        if settings.important_time == -1
+                                or info.pos+0 <= settings.important_time
                                         and settings.important_time ~= 0 then
-                        hint = "important"
-                    end
-                    info.pos = string.format("%d:%02d", math.floor(info.pos / 60),
-                            math.mod(info.pos, 60))
-                    info.len = string.format("%d:%02d", math.floor(info.len / 60),
-                            math.mod(info.len, 60))
-                elseif attrib == "state" then
-                    info.state = val
-                    if info.state ~= last_state then
-                        hint = "important"
-                    end
-                    last_state = info.state
-                elseif attrib == "volume" then
-                    info.volume = val .. "%"
-                    if info.volume ~= last_volume then
-                        hint = "important"
+                            hint = "important"
+                        end
+                        info.pos = string.format("%d:%02d",
+                                                    math.floor(info.pos / 60),
+                                                    math.mod(info.pos, 60))
+                        info.len = string.format("%d:%02d",
+                                                    math.floor(info.len / 60),
+                                                    math.mod(info.len, 60))
+                    elseif attrib == "state" then
+                        info.state = val
+                        if info.state ~= last_state then
+                            hint = "important"
+                        end
+                        last_state = info.state
+                    elseif attrib == "volume" then
+                        info.volume = val .. "%"
+                        if info.volume ~= last_volume then
+                            hint = "important"
+                        end
+                        last_volume = info.volume
+                    elseif attrib == "Artist" then
+                        info.artist = val
+                    elseif attrib == "Title" then
+                        info.title = val
+                    elseif attrib == "Album" then
+                        info.album = val
+                    elseif attrib == "Track" then
+                        info.num = val
+                    elseif attrib == "Date" then
+                        info.year = val
                     end
-                    last_volume = info.volume
-                elseif attrib == "Artist" then
-                    info.artist = val
-                elseif attrib == "Title" then
-                    info.title = val
-                elseif attrib == "Album" then
-                    info.album = val
-                elseif attrib == "Track" then
-                    info.num = val
-                elseif attrib == "Date" then
-                    info.year = val
                 end
-            end
 
-            -- done querying; now build the string
-            statusd.inform("mpd_hint", hint)
-            if info.state == "play" then
-                local mpd_st = settings.template
-                -- fill in %values
-                mpd_st = string.gsub(mpd_st, "%%([%w%_]+)",
-                        function (x) return(info[x]  or "") end)
-                mpd_st = string.gsub(mpd_st, "%%%%", "%%")
-                statusd.inform("mpd", mpd_st)
-            elseif info.state == "pause" then
-                statusd.inform("mpd", "Paused")
-            else
-                statusd.inform("mpd", "No song playing")
-            end
+                -- done querying; now build the string
+                statusd.inform("mpd_hint", hint)
+                if info.state == "play" then
+                    local mpd_st = settings.template
+                    -- fill in %values
+                    mpd_st = string.gsub(mpd_st, "%%([%w%_]+)",
+                            function (x) return(info[x]  or "") end)
+                    mpd_st = string.gsub(mpd_st, "%%%%", "%%")
+                    statusd.inform("mpd", mpd_st)
+                elseif info.state == "pause" then
+                    statusd.inform("mpd", "Paused")
+                else
+                    statusd.inform("mpd", "No song playing")
+                end
 
-            -- Wait for bgread to signal that more data
-            -- is available or the program has exited
+                -- Wait for bgread to signal that more data
+                -- is available or the program has exited
 
-            data = coroutine.yield()
+                data = coroutine.yield()
+            else
+                -- There was no enough data to interpret a complete status
+                data = data .. coroutine.yield()
+            end
         end
 
         -- Program has exited.