diff options
| author | Danilo M. <danix@danix.xyz> | 2026-09-17 19:06:06 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-09-17 19:06:06 +0200 |
| commit | f1be1bef9c3563fd78f811f54e5a64b1d993e4da (patch) | |
| tree | 7fc440ac31c01f1daba0645c4c13fbeb6cda24e6 | |
| parent | 8e6f60fc8f442351bdc948d2142914d3946ce34d (diff) | |
| download | conky-theme-udt-f1be1bef9c3563fd78f811f54e5a64b1d993e4da.tar.gz conky-theme-udt-f1be1bef9c3563fd78f811f54e5a64b1d993e4da.zip | |
feat: parse the sampler's key/value cache format
Only the first space separates, so a value keeps its own spaces. Values stay
strings: the sampler writes epochs and the widget decides whether an age
reads as hours or days.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
| -rw-r--r-- | lib/data.lua | 18 | ||||
| -rw-r--r-- | test/fixtures/slackware_cache | 5 | ||||
| -rw-r--r-- | test/test_data.lua | 21 |
3 files changed, 44 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 diff --git a/test/fixtures/slackware_cache b/test/fixtures/slackware_cache new file mode 100644 index 0000000..3f2b2ac --- /dev/null +++ b/test/fixtures/slackware_cache @@ -0,0 +1,5 @@ +version Slackware 15.0+ +packages 1234 +changelog 1758000000 +birth 1700000000 +kernel 6.12.1 diff --git a/test/test_data.lua b/test/test_data.lua index 1b6c580..fdf0e2f 100644 --- a/test/test_data.lua +++ b/test/test_data.lua @@ -218,4 +218,25 @@ r5:sample(1000, 100) assert(r5:sample(3048, 100) == 2048 / 5, 'an explicit fallback interval must be used, got ' .. tostring(r5:sample(3048, 100))) +-- === kv_parse ============================================================= +-- The slackware sampler writes 'key value' lines. The format is deliberately +-- dumber than the data: epochs stay integers rather than becoming formatted +-- dates, so the widget decides how to render an age and the shell never +-- reconstructs a date string. +local kv = data.kv_parse(read('test/fixtures/slackware_cache')) +assert(kv.version == 'Slackware 15.0+', 'version, got ' .. tostring(kv.version)) +-- The value keeps its internal spaces: only the FIRST space is the separator. +assert(kv.packages == '1234', 'packages, got ' .. tostring(kv.packages)) +assert(kv.changelog == '1758000000', 'changelog, got ' .. tostring(kv.changelog)) +assert(kv.kernel == '6.12.1', 'kernel, got ' .. tostring(kv.kernel)) + +-- A key the sampler did not write reads nil, which is how the widget tells +-- "the sampler ran but this fact was unavailable" from "no sampler". +assert(kv.nonexistent == nil, 'a missing key is nil') + +-- Malformed input must not raise: a Lua error in conky is a blank screen. +assert(type(data.kv_parse('')) == 'table', 'empty input gives a table') +assert(type(data.kv_parse(nil)) == 'table', 'nil gives a table') +assert(data.kv_parse('keyonly\n').keyonly == nil, 'a line with no value is skipped') + print('test_data: all assertions passed') |
