]> git.rmz.io Git - dotfiles.git/commitdiff
awesome/mpc: add support for binary responses
authorSamir Benmendil <me@rmz.io>
Mon, 10 Aug 2020 18:31:44 +0000 (19:31 +0100)
committerSamir Benmendil <me@rmz.io>
Mon, 10 Aug 2020 21:05:30 +0000 (22:05 +0100)
Had to move the recursive `do_read` call to after the parsing of the
message as in binary form we need to first read the binary data before
carrying on and reading the next line.

awesome/widgets/mpc.lua

index d870f2f8f3cc374b63be48fd23f43a65a5a4450c..4da4a815e09a0b6b921c077e0e93623961aa57e2 100644 (file)
@@ -95,7 +95,6 @@ function mpc:_connect()
                 self._output, self._input = nil, nil
                 self:_error(err)
             else
-                do_read()
                 line = tostring(line)
                 if line == "OK" or line:match("^ACK ") then
                     local success = line == "OK"
@@ -112,9 +111,22 @@ function mpc:_connect()
                 else
                     local _, _, key, value = string.find(line, "([^:]+):%s(.+)")
                     if key then
-                        self._pending_reply[string.lower(key)] = value
+                        if key == "binary" then
+                            value = tonumber(value)
+                            local data = {}
+                            while value > 0 do
+                                local b = assert(obj:read_bytes(value))
+                                table.insert(data, b.data)
+                                value = value - #b
+                            end
+                            local w = obj:read_bytes(1)  -- read newline at end of binary
+                            self._pending_reply[string.lower(key)] = table.concat(data)
+                        else
+                            self._pending_reply[string.lower(key)] = value
+                        end
                     end
                 end
+                do_read()
             end
         end)
     end