From 4094d754f1d40a31fdae3fdd208fca6e93f64572 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Sun, 17 Jul 2022 18:54:23 -0400 Subject: [PATCH] REF don't use compile in modules --- drawing/common.lua | 89 ++++++++++++++++++++++++++++++++++++++++++ drawing/compile.lua | 88 ----------------------------------------- drawing/filesystem.lua | 12 +++--- drawing/graphics.lua | 21 +++++----- drawing/memory.lua | 10 ++--- drawing/network.lua | 6 +-- drawing/pacman.lua | 6 +-- drawing/power.lua | 8 ++-- drawing/processor.lua | 17 ++++---- drawing/readwrite.lua | 6 +-- drawing/system.lua | 5 +-- 11 files changed, 125 insertions(+), 143 deletions(-) diff --git a/drawing/common.lua b/drawing/common.lua index eeb6486..5ad7156 100644 --- a/drawing/common.lua +++ b/drawing/common.lua @@ -687,5 +687,94 @@ return function(config) ) end + + ---------------------------------------------------------------------------- + -- compile individual module + + local _combine_blocks = function(acc, new) + if new.active == true then + local n = new.f(acc.next_y) + table.insert(acc.objs, n.obj) + acc.w = math.max(acc.w, n.w) + acc.final_y = acc.next_y + n.h + acc.next_y = acc.final_y + new.offset + end + return acc + end + + local non_false = function(xs) + return pure.filter(function(x) return x ~= false end, xs) + end + + local mk_block = function(f, active, offset) + return {f = f, active = active, offset = offset} + end + + local active_blocks = function(blockspecs) + local bs = pure.filter(function(b) return b[2] end, blockspecs) + return pure.map(function(b) return mk_block(table.unpack(b)) end, bs) + end + + local flatten_sections = function(top, ...) + local f = function(acc, new) + if #new.blocks == 0 then + return acc + elseif #acc == 0 then + return new.blocks + else + return pure.flatten( + {acc, {mk_block(new.sep_fun, true, new.top)}, new.blocks} + ) + end + end + return pure.reduce(f, active_blocks(top), {...}) + end + + M.mk_section = function(top, sep_fun, ...) + return { + top = top, + sep_fun = sep_fun, + blocks = active_blocks({...}) + } + end + + M.mk_seperator = function(width, x, y) + local separator = M.make_separator(x, y, width) + return M.mk_acc_static(width, 0, pure.partial(line.draw, separator)) + end + + M.mk_acc = function(w, h, u, s, d) + return {w = w, h = h, obj = {u, s, d}} + end + + M.mk_acc_static = function(w, h, s) + return M.mk_acc(w, h, false, s, false) + end + + M.compile_module = function(header, point, width, top_blocks, ...) + local mk_header = function(y) + local obj = M.make_header(point.x, y, width, header) + return M.mk_acc_static( + width, + obj.bottom_y - y, + function(cr) M.draw_header(cr, obj) end + ) + end + local blocks = flatten_sections(top_blocks, ...) + local r = pure.reduce( + _combine_blocks, + {w = 0, next_y = point.y, final_y = point.y, objs = {}}, + {mk_block(mk_header, true, 0), table.unpack(blocks)} + ) + local us, ss, ds = table.unpack(pure.unzip(r.objs)) + return { + next_x = point.x + r.w, + next_y = r.final_y, + update = pure.sequence(table.unpack(non_false(us))), + static = pure.sequence(table.unpack(ss)), + dynamic = pure.sequence(table.unpack(non_false(ds))) + } + end + return M end diff --git a/drawing/compile.lua b/drawing/compile.lua index ef43dc2..a6d40d4 100644 --- a/drawing/compile.lua +++ b/drawing/compile.lua @@ -5,94 +5,6 @@ local geom = require 'geom' local fill_rect = require 'fill_rect' local line = require 'line' --------------------------------------------------------------------------------- --- compile individual module - -local _combine_blocks = function(acc, new) - if new.active == true then - local n = new.f(acc.next_y) - table.insert(acc.objs, n.obj) - acc.w = math.max(acc.w, n.w) - acc.final_y = acc.next_y + n.h - acc.next_y = acc.final_y + new.offset - end - return acc -end - -local non_false = function(xs) - return pure.filter(function(x) return x ~= false end, xs) -end - -local mk_block = function(f, active, offset) - return {f = f, active = active, offset = offset} -end - -local active_blocks = function(blockspecs) - local bs = pure.filter(function(b) return b[2] end, blockspecs) - return pure.map(function(b) return mk_block(table.unpack(b)) end, bs) -end - -local flatten_sections = function(top, ...) - local f = function(acc, new) - if #new.blocks == 0 then - return acc - elseif #acc == 0 then - return new.blocks - else - return pure.flatten( - {acc, {mk_block(new.sep_fun, true, new.top)}, new.blocks} - ) - end - end - return pure.reduce(f, active_blocks(top), {...}) -end - -M.mk_section = function(top, sep_fun, ...) - return { - top = top, - sep_fun = sep_fun, - blocks = active_blocks({...}) - } -end - -M.mk_seperator = function(common, width, x, y) - local separator = common.make_separator(x, y, width) - return M.mk_acc_static(width, 0, pure.partial(line.draw, separator)) -end - -M.mk_acc = function(w, h, u, s, d) - return {w = w, h = h, obj = {u, s, d}} -end - -M.mk_acc_static = function(w, h, s) - return M.mk_acc(w, h, false, s, false) -end - -M.compile_module = function(common, header, point, width, top_blocks, ...) - local mk_header = function(y) - local obj = common.make_header(point.x, y, width, header) - return M.mk_acc_static( - width, - obj.bottom_y - y, - function(cr) common.draw_header(cr, obj) end - ) - end - local blocks = flatten_sections(top_blocks, ...) - local r = pure.reduce( - _combine_blocks, - {w = 0, next_y = point.y, final_y = point.y, objs = {}}, - {mk_block(mk_header, true, 0), table.unpack(blocks)} - ) - local us, ss, ds = table.unpack(pure.unzip(r.objs)) - return { - next_x = point.x + r.w, - next_y = r.final_y, - update = pure.sequence(table.unpack(non_false(us))), - static = pure.sequence(table.unpack(ss)), - dynamic = pure.sequence(table.unpack(non_false(ds))) - } -end - -------------------------------------------------------------------------------- -- compile entire layout diff --git a/drawing/filesystem.lua b/drawing/filesystem.lua index 448dbfe..b241c6e 100644 --- a/drawing/filesystem.lua +++ b/drawing/filesystem.lua @@ -1,5 +1,4 @@ local i_o = require 'i_o' -local compile = require 'compile' local pure = require 'pure' local impure = require 'impure' @@ -19,7 +18,7 @@ return function(config, main_state, common, width, point) common.text_row_set(obj, (pid == '') and 'Error' or 'Running') end end - return compile.mk_acc( + return common.mk_acc( width, 0, update, @@ -28,7 +27,7 @@ return function(config, main_state, common, width, point) ) end - local mk_sep = pure.partial(compile.mk_seperator, common, width, point.x) + local mk_sep = pure.partial(common.mk_seperator, width, point.x) ----------------------------------------------------------------------------- -- filesystem bar chart @@ -58,7 +57,7 @@ return function(config, main_state, common, width, point) impure.ieach(read_fs, CONKY_CMDS) end end - return compile.mk_acc( + return common.mk_acc( width, (#config.fs_paths - 1) * SPACING, update, @@ -70,12 +69,11 @@ return function(config, main_state, common, width, point) ----------------------------------------------------------------------------- -- main functions - return compile.compile_module( - common, + return common.compile_module( 'FILE SYSTEMS', point, width, {{mk_smart, config.show_smart, SEPARATOR_SPACING}}, - compile.mk_section(SEPARATOR_SPACING, mk_sep, {mk_bars, true, 0}) + common.mk_section(SEPARATOR_SPACING, mk_sep, {mk_bars, true, 0}) ) end diff --git a/drawing/graphics.lua b/drawing/graphics.lua index cc395f1..b813c07 100644 --- a/drawing/graphics.lua +++ b/drawing/graphics.lua @@ -1,6 +1,5 @@ local pure = require 'pure' local i_o = require 'i_o' -local compile = require 'compile' return function(update_freq, config, common, width, point) local SEPARATOR_SPACING = 20 @@ -41,7 +40,7 @@ return function(update_freq, config, common, width, point) ) local static = pure.partial(common.tagged_percent_timeseries_draw_static, obj) local dynamic = pure.partial(common.tagged_percent_timeseries_draw_dynamic, obj) - return compile.mk_acc( + return common.mk_acc( width, PLOT_HEIGHT + PLOT_SEC_BREAK, update, @@ -69,12 +68,11 @@ return function(update_freq, config, common, width, point) end local static = pure.partial(common.text_row_draw_static, obj) local dynamic = pure.partial(common.text_row_draw_dynamic, obj) - return compile.mk_acc(width, 0, update, static, dynamic) + return common.mk_acc(width, 0, update, static, dynamic) end local mk_sep = pure.partial( - compile.mk_seperator, - common, + common.mk_seperator, width, point.x ) @@ -100,7 +98,7 @@ return function(update_freq, config, common, width, point) ) local static = pure.partial(common.threshold_text_row_draw_static, obj) local dynamic = pure.partial(common.threshold_text_row_draw_dynamic, obj) - return compile.mk_acc(width, 0, update, static, dynamic) + return common.mk_acc(width, 0, update, static, dynamic) end ----------------------------------------------------------------------------- @@ -125,7 +123,7 @@ return function(update_freq, config, common, width, point) end local static = pure.partial(common.text_rows_draw_static, obj) local dynamic = pure.partial(common.text_rows_draw_dynamic, obj) - return compile.mk_acc(width, TEXT_SPACING, update, static, dynamic) + return common.mk_acc(width, TEXT_SPACING, update, static, dynamic) end ----------------------------------------------------------------------------- @@ -217,23 +215,22 @@ return function(update_freq, config, common, width, point) ----------------------------------------------------------------------------- -- main drawing functions - local rbs = compile.compile_module( - common, + local rbs = common.compile_module( 'NVIDIA GRAPHICS', point, width, {{mk_status, true, SEPARATOR_SPACING}}, - compile.mk_section( + common.mk_section( SEPARATOR_SPACING, mk_sep, {mk_temp, config.show_temp, SEPARATOR_SPACING} ), - compile.mk_section( + common.mk_section( SEPARATOR_SPACING, mk_sep, {mk_clock, config.show_clock, SEPARATOR_SPACING} ), - compile.mk_section( + common.mk_section( SEPARATOR_SPACING, mk_sep, {mk_gpu_util, config.show_gpu_util, PLOT_SEC_BREAK}, diff --git a/drawing/memory.lua b/drawing/memory.lua index 263661f..bccda8f 100644 --- a/drawing/memory.lua +++ b/drawing/memory.lua @@ -2,7 +2,6 @@ local timeseries = require 'timeseries' local text_table = require 'text_table' local i_o = require 'i_o' local pure = require 'pure' -local compile = require 'compile' return function(update_freq, config, common, width, point) local DIAL_THICKNESS = 8 @@ -78,7 +77,7 @@ return function(update_freq, config, common, width, point) common.dial_draw_dynamic(swap, cr) common.text_rows_draw_dynamic(cache, cr) end - return compile.mk_acc(width, DIAL_DIAMETER, update, static, dynamic) + return common.mk_acc(width, DIAL_DIAMETER, update, static, dynamic) end ----------------------------------------------------------------------------- @@ -92,7 +91,7 @@ return function(update_freq, config, common, width, point) PLOT_HEIGHT, update_freq ) - return compile.mk_acc( + return common.mk_acc( width, PLOT_HEIGHT, function(s) timeseries.update(obj, s.mem.used_percent) end, @@ -130,7 +129,7 @@ return function(update_freq, config, common, width, point) text_table.set(obj, 3, r, i_o.conky(TABLE_CONKY[r].mem)) end end - return compile.mk_acc( + return common.mk_acc( width, TABLE_HEIGHT, update, @@ -179,8 +178,7 @@ return function(update_freq, config, common, width, point) ----------------------------------------------------------------------------- -- main functions - local rbs = compile.compile_module( - common, + local rbs = common.compile_module( 'MEMORY', point, width, diff --git a/drawing/network.lua b/drawing/network.lua index 23dbdf5..0711f4c 100644 --- a/drawing/network.lua +++ b/drawing/network.lua @@ -2,7 +2,6 @@ local format = require 'format' local pure = require 'pure' local i_o = require 'i_o' local sys = require 'sys' -local compile = require 'compile' return function(update_freq, common, width, point) local PLOT_SEC_BREAK = 20 @@ -51,7 +50,7 @@ return function(update_freq, common, width, point) update_freq, state[key] ) - return compile.mk_acc( + return common.mk_acc( width, PLOT_HEIGHT + PLOT_SEC_BREAK, function(s) common.update_rate_timeseries(obj, s[key]) end, @@ -66,8 +65,7 @@ return function(update_freq, common, width, point) ----------------------------------------------------------------------------- -- main drawing functions - local rbs = compile.compile_module( - common, + local rbs = common.compile_module( 'NETWORK', point, width, diff --git a/drawing/pacman.lua b/drawing/pacman.lua index fbee23d..b9e8b96 100644 --- a/drawing/pacman.lua +++ b/drawing/pacman.lua @@ -1,4 +1,3 @@ -local compile = require 'compile' local pure = require 'pure' return function(main_state, common, width, point) @@ -32,7 +31,7 @@ return function(main_state, common, width, point) end end end - return compile.mk_acc( + return common.mk_acc( width, TEXT_SPACING * 4, update, @@ -41,8 +40,7 @@ return function(main_state, common, width, point) ) end - return compile.compile_module( - common, + return common.compile_module( 'PACMAN', point, width, diff --git a/drawing/power.lua b/drawing/power.lua index bb9bf69..7bb8729 100644 --- a/drawing/power.lua +++ b/drawing/power.lua @@ -1,6 +1,5 @@ local format = require 'format' local pure = require 'pure' -local compile = require 'compile' local sys = require 'sys' return function(update_freq, config, common, width, point) @@ -40,7 +39,7 @@ return function(update_freq, config, common, width, point) update_freq, read_joules() ) - return compile.mk_acc( + return common.mk_acc( width, PLOT_HEIGHT + PLOT_SEC_BREAK, function(_) common.update_rate_timeseries(obj, read_joules()) end, @@ -89,7 +88,7 @@ return function(update_freq, config, common, width, point) 0, update_freq ) - return compile.mk_acc( + return common.mk_acc( width, PLOT_HEIGHT + PLOT_SEC_BREAK, function() @@ -106,8 +105,7 @@ return function(update_freq, config, common, width, point) ----------------------------------------------------------------------------- -- main functions - return compile.compile_module( - common, + return common.compile_module( 'POWER', point, width, diff --git a/drawing/processor.lua b/drawing/processor.lua index 9b3ce16..0097158 100644 --- a/drawing/processor.lua +++ b/drawing/processor.lua @@ -1,7 +1,6 @@ local compound_dial = require 'compound_dial' local text_table = require 'text_table' local i_o = require 'i_o' -local compile = require 'compile' local cpu = require 'sys' local pure = require 'pure' @@ -91,7 +90,7 @@ return function(update_freq, config, main_state, common, width, point) compound_dial.draw_dynamic(cores[i].loads, cr) end end - return compile.mk_acc( + return common.mk_acc( width, DIAL_OUTER_RADIUS * 2, update, @@ -123,7 +122,7 @@ return function(update_freq, config, main_state, common, width, point) end local static = pure.partial(common.text_rows_draw_static, cpu_status) local dynamic = pure.partial(common.text_rows_draw_dynamic, cpu_status) - return compile.mk_acc( + return common.mk_acc( width, TEXT_SPACING, update, @@ -136,8 +135,7 @@ return function(update_freq, config, main_state, common, width, point) -- frequency local mk_sep = pure.partial( - compile.mk_seperator, - common, + common.mk_seperator, width, point.x ) @@ -164,7 +162,7 @@ return function(update_freq, config, main_state, common, width, point) end local static = pure.partial(common.tagged_percent_timeseries_draw_static, total_load) local dynamic = pure.partial(common.tagged_percent_timeseries_draw_dynamic, total_load) - return compile.mk_acc( + return common.mk_acc( width, PLOT_HEIGHT + PLOT_SECTION_BREAK, update, @@ -203,7 +201,7 @@ return function(update_freq, config, main_state, common, width, point) end local static = pure.partial(text_table.draw_static, tbl) local dynamic = pure.partial(text_table.draw_dynamic, tbl) - return compile.mk_acc( + return common.mk_acc( width, TABLE_HEIGHT, update, @@ -215,8 +213,7 @@ return function(update_freq, config, main_state, common, width, point) ----------------------------------------------------------------------------- -- main functions - local rbs = compile.compile_module( - common, + local rbs = common.compile_module( 'PROCESSOR', point, width, @@ -224,7 +221,7 @@ return function(update_freq, config, main_state, common, width, point) {mk_cores, config.show_cores, TEXT_SPACING}, {mk_hwp_freq, config.show_stats, SEPARATOR_SPACING}, }, - compile.mk_section( + common.mk_section( SEPARATOR_SPACING, mk_sep, {mk_load_plot, config.show_plot, TABLE_SECTION_BREAK}, diff --git a/drawing/readwrite.lua b/drawing/readwrite.lua index 5202117..4153bff 100644 --- a/drawing/readwrite.lua +++ b/drawing/readwrite.lua @@ -1,6 +1,5 @@ local format = require 'format' local pure = require 'pure' -local compile = require 'compile' local sys = require 'sys' return function(update_freq, config, common, width, point) @@ -34,7 +33,7 @@ return function(update_freq, config, common, width, point) update_freq, state[key] ) - return compile.mk_acc( + return common.mk_acc( -- TODO construct this more sanely without referring to hardcoded vars width, PLOT_HEIGHT + PLOT_SEC_BREAK, @@ -50,8 +49,7 @@ return function(update_freq, config, common, width, point) ----------------------------------------------------------------------------- -- main drawing functions - local rbs = compile.compile_module( - common, + local rbs = common.compile_module( 'INPUT / OUTPUT', point, width, diff --git a/drawing/system.lua b/drawing/system.lua index 888990e..5a763f7 100644 --- a/drawing/system.lua +++ b/drawing/system.lua @@ -1,6 +1,5 @@ local i_o = require 'i_o' local pure = require 'pure' -local compile = require 'compile' return function(main_state, common, width, point) local TEXT_SPACING = 20 @@ -31,7 +30,7 @@ return function(main_state, common, width, point) end local static = pure.partial(common.text_rows_draw_static, obj) local dynamic = pure.partial(common.text_rows_draw_dynamic, obj) - return compile.mk_acc( + return common.mk_acc( width, TEXT_SPACING * 3, update, @@ -40,5 +39,5 @@ return function(main_state, common, width, point) ) end - return compile.compile_module(common, 'SYSTEM', point, width, {{mk_stats, true, 0}}) + return common.compile_module('SYSTEM', point, width, {{mk_stats, true, 0}}) end