-- General collection of parsers for various linux utilities local parsers = {} parsers.upower = function(data) -- this shit was born in a fever dream, that's the only reasonable explanation i can give to this. local struct = {} local layer = 1 local leaf = struct local history = {} data:gsub("\n?\r?([%s]*)([^\n]*)",function(block,line) if block == "" then block = " " end block = block:len() if line:match("%S") then if block > layer then history[layer] = leaf local pos = #leaf local key = leaf[pos] table.remove(leaf,pos) leaf[key] = {} leaf = leaf[key] layer = block end if block < layer then leaf = history[block] layer = block end table.insert(leaf,line) end end) local function prettify(t) for k,v in ipairs(t) do if v:find(":") then t[v:match("^(.-):")] = v:match("^.-:%s*(.*)") t[k] = nil end end for k,v in pairs(t) do if type(v) == "table" then t[k] = prettify(v) end end return t end return prettify(struct) end return parsers