From: Samir Benmendil Date: Mon, 10 Aug 2020 18:31:44 +0000 (+0100) Subject: awesome/mpc: add support for binary responses X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/f2430c7c2d870fbdd8aa3b056a3acfcb09498b13?ds=inline awesome/mpc: add support for binary responses 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. --- diff --git a/awesome/widgets/mpc.lua b/awesome/widgets/mpc.lua index d870f2f..4da4a81 100644 --- a/awesome/widgets/mpc.lua +++ b/awesome/widgets/mpc.lua @@ -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