From 2faabd09251f8c1452b3bf4ebea328f433055014 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Sun, 24 Jul 2022 11:56:57 -0400 Subject: [PATCH] REF make ylabels generator functional --- src/modules/readwrite.lua | 1 - src/pure.lua | 4 ++++ src/widget/timeseries/ylabels.lua | 20 +++++++++----------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/modules/readwrite.lua b/src/modules/readwrite.lua index a275f09..2ffaed4 100644 --- a/src/modules/readwrite.lua +++ b/src/modules/readwrite.lua @@ -43,7 +43,6 @@ return function(update_freq, config, common, width, point) mod_state[key] ) return common.mk_acc( - -- TODO construct this more sanely without referring to hardcoded vars width, PLOT_HEIGHT + PLOT_SEC_BREAK, function() common.update_rate_timeseries(obj, mod_state[key]) end, diff --git a/src/pure.lua b/src/pure.lua index 32a73e3..c7a2866 100644 --- a/src/pure.lua +++ b/src/pure.lua @@ -219,6 +219,10 @@ M.iter_to_tableN = function(iter) return r end +M.curry_table = function(f) + return function(tbl) return f(table.unpack(tbl)) end +end + -------------------------------------------------------------------------------- -- functional functions diff --git a/src/widget/timeseries/ylabels.lua b/src/widget/timeseries/ylabels.lua index 06365b3..407b242 100644 --- a/src/widget/timeseries/ylabels.lua +++ b/src/widget/timeseries/ylabels.lua @@ -1,6 +1,7 @@ local M = {} local geom = require 'geom' +local pure = require 'pure' local text = require 'text' local ti = require 'text_internal' @@ -17,25 +18,22 @@ local make_y_label_text = function(point, chars, font) ) end --- TODO this function smells funny M.make = function(point, h, n, font, y_format, scale_factor) - local y_labels = {width = 0} local f = y_format(scale_factor) - for i = 1, n do + local to_label = function(i) local z = (i - 1) / (n - 1) - local l = make_y_label_text( + return make_y_label_text( geom.make_point(point.x, point.y + z * h), f((1 - z) * scale_factor), font ) - local w = ti.get_width(l.chars, font) - if w > y_labels.width then - y_labels.width = w - end - y_labels[i] = l end - y_labels.width = y_labels.width + Y_LABEL_PAD - return y_labels + local labels = pure.map_n(to_label, n) + local max_width = pure.compose( + pure.curry_table(math.max), + pure.partial(pure.map, function(l) return ti.get_width(l.chars, font) end) + ) + return { width = max_width(labels) + Y_LABEL_PAD, table.unpack(labels) } end --------------------------------------------------------------------------------