diff options
Diffstat (limited to 'widgets/cache.lua')
| -rw-r--r-- | widgets/cache.lua | 73 |
1 files changed, 53 insertions, 20 deletions
diff --git a/widgets/cache.lua b/widgets/cache.lua index 0209f1a..40025eb 100644 --- a/widgets/cache.lua +++ b/widgets/cache.lua @@ -14,32 +14,68 @@ local M = {} function M.draw(cr, rect, colors) local inner = card.card(cr, rect, colors) local function clamp(v, lo, hi) return math.max(lo, math.min(hi, v)) end - local label_size = clamp(inner.w * 0.036, 10, 16) - local big_size = clamp(inner.w * 0.095, 22, 46) - - card.font(cr, card.FONT_MONO, label_size, false) - card.rgba(cr, colors.label) - card.text(cr, inner.x, inner.y + label_size, 'CACHE') + -- Fluid type. One base size S drives every element. The height budget makes + -- the content fill the cell; the measured width fit pulls S down only where + -- the header or a row would overrun the card. A tall cell gets larger type + -- instead of empty space, and nothing collides. + local LABEL_F, ROW_F = 0.30, 0.32 local c = data.du_parse(data.slurp(CACHE) or '') if not c then + local S = clamp(inner.h * 0.94 / (1.78 + 2 * 0.72), 10, 72) + S = math.min(S, card.fit_unit(cr, inner.w * 0.96, { + { { 'CACHE', card.FONT_MONO, LABEL_F } }, + { { 'no cache data', card.FONT_UI, 0.36 } }, + { { 'bin/cache-sample.sh', card.FONT_MONO, ROW_F } }, + }, 100)) + card.font(cr, card.FONT_MONO, S * LABEL_F, false) card.rgba(cr, colors.label) - card.font(cr, card.FONT_UI, label_size * 1.2, true) - card.text(cr, inner.x, inner.y + label_size * 3.4, 'no cache data') - card.font(cr, card.FONT_MONO, label_size, false) - card.text(cr, inner.x, inner.y + label_size * 5, 'bin/cache-sample.sh') + card.text(cr, inner.x, inner.y + S, 'CACHE') + card.font(cr, card.FONT_UI, S * 0.36, true) + card.text(cr, inner.x, inner.y + S + S * LABEL_F * 2.6, 'no cache data') + card.font(cr, card.FONT_MONO, S * ROW_F, false) + card.text(cr, inner.x, inner.y + S + S * LABEL_F * 2.6 + S * 0.72, + 'bin/cache-sample.sh') return end - card.font(cr, card.FONT_HEAVY, big_size, false) - card.rgba(cr, colors.body) - card.text(cr, inner.x, inner.y + label_size + big_size, c.total.label) + local items = c.items or {} + local names = {} + for i, e in ipairs(items) do + local name = e.name + if #name > 18 then name = name:sub(1, 17) .. '\u{2026}' end + names[i] = name + end - local ey = inner.y + label_size + big_size + label_size * 2 - local step = label_size * 2.6 + local row_n = #items + local S_h = inner.h * 0.94 / (1.78 + row_n * 0.64) + local groups = { + { { 'CACHE', card.FONT_MONO, LABEL_F }, { c.total.label, card.FONT_HEAVY, 1.0 } }, + } + for i = 1, row_n do + groups[#groups + 1] = { { names[i], card.FONT_MONO, ROW_F }, + { items[i].label, card.FONT_MONO, ROW_F } } + end + local S = clamp(math.min(S_h, card.fit_unit(cr, inner.w * 0.96, groups, 100)), 10, 72) + local label_size = S * LABEL_F + local row_size = S * ROW_F card.font(cr, card.FONT_MONO, label_size, false) - for i, e in ipairs(c.items) do + card.rgba(cr, colors.label) + card.text(cr, inner.x, inner.y + S, 'CACHE') + + -- The total shares the header line with the label, set on the same baseline + -- and right-aligned: the width carries the number instead of an otherwise + -- empty row beneath it. + card.font(cr, card.FONT_HEAVY, S, false) + card.rgba(cr, colors.body) + card.text_right(cr, inner.x + inner.w, inner.y + S, c.total.label) + + local ey = inner.y + S + label_size * 2.6 + local step = row_size * 2.0 + + card.font(cr, card.FONT_MONO, row_size, false) + for i, e in ipairs(items) do if ey + step > inner.y + inner.h then break end -- The biggest offender is the point of the card, so it carries colour: -- critical when it is over half the total, warning otherwise. The rest @@ -50,10 +86,7 @@ function M.draw(cr, rect, colors) colour = share > 0.5 and colors.critical or colors.warning end card.rgba(cr, colors.label) - -- Long names are truncated rather than allowed to collide with the size. - local name = e.name - if #name > 18 then name = name:sub(1, 17) .. '\u{2026}' end - card.text(cr, inner.x, ey, name) + card.text(cr, inner.x, ey, names[i]) card.rgba(cr, colour) card.text_right(cr, inner.x + inner.w, ey, e.label) ey = ey + step |
