aboutsummaryrefslogtreecommitdiffstats
path: root/lib/data.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/data.lua')
-rw-r--r--lib/data.lua18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/data.lua b/lib/data.lua
index 9f20d1f..13e5ebd 100644
--- a/lib/data.lua
+++ b/lib/data.lua
@@ -279,4 +279,22 @@ function M.du_parse(text)
return { total = total, items = items }
end
+-- Parse 'key value' lines into a table of strings.
+--
+-- Values stay strings and are converted by the caller. The sampler writes
+-- epochs as integers and the widget decides whether an age reads as hours or
+-- days; a parser that guessed would have to know that.
+--
+-- Only the first space separates, so a value keeps its own spaces:
+-- 'version Slackware 15.0+' yields 'Slackware 15.0+', not 'Slackware'.
+function M.kv_parse(text)
+ local out = {}
+ if type(text) ~= 'string' then return out end
+ for line in text:gmatch('[^\n]+') do
+ local k, v = line:match('^(%S+)%s+(.*)$')
+ if k and v ~= '' then out[k] = v end
+ end
+ return out
+end
+
return M