REF make modules return startup closures

This commit is contained in:
Nathan Dwarshuis 2021-07-17 00:17:22 -04:00
parent 1fb91d03be
commit 5875eeff98
10 changed files with 205 additions and 216 deletions

View File

@ -1,5 +1,3 @@
local M = {}
local Line = require 'Line'
local Util = require 'Util'
local Common = require 'Common'
@ -71,17 +69,19 @@ local update = function(cr)
end
end
M.draw_static = function(cr)
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.text_row_draw_static(smart, cr)
Line.draw(separator, cr)
Common.compound_bar_draw_static(fs, cr)
end
M.draw_dynamic = function(cr, trigger)
local draw_dynamic = function(cr, trigger)
if trigger == 0 then update(cr) end
Common.text_row_draw_dynamic(smart, cr)
Common.compound_bar_draw_dynamic(fs, cr)
end
return M
return function()
return {static = draw_static, dynamic = draw_dynamic}
end

View File

@ -1,5 +1,3 @@
local M = {}
local Text = require 'Text'
local Line = require 'Line'
local Util = require 'Util'
@ -191,7 +189,7 @@ _GPU_UTIL_Y_ = nil
_MEM_UTIL_Y_ = nil
_VID_UTIL_Y_ = nil
M.draw_static = function(cr)
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.text_row_draw_static(status, cr)
@ -208,7 +206,7 @@ M.draw_static = function(cr)
Common.percent_plot_draw_static(vid_util, cr)
end
M.draw_dynamic = function(cr)
local draw_dynamic = function(cr)
update(cr)
Common.text_row_draw_dynamic(status, cr)
@ -219,4 +217,6 @@ M.draw_dynamic = function(cr)
Common.percent_plot_draw_dynamic(vid_util, cr)
end
return M
return function()
return {static = draw_static, dynamic = draw_dynamic}
end

View File

@ -1,5 +1,3 @@
local M = {}
local Arc = require 'Arc'
local Dial = require 'Dial'
local LabelPlot = require 'LabelPlot'
@ -172,7 +170,7 @@ _TEXT_LEFT_X_ = nil
_RIGHT_X_ = nil
_PLOT_Y_ = nil
M.draw_static = function(cr)
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.text_ring_draw_static(text_ring, cr)
@ -185,7 +183,7 @@ M.draw_static = function(cr)
Table.draw_static(tbl, cr)
end
M.draw_dynamic = function(cr)
local draw_dynamic = function(cr)
update(cr)
Dial.draw_dynamic(dial, cr)
@ -200,4 +198,6 @@ M.draw_dynamic = function(cr)
Table.draw_dynamic(tbl, cr)
end
return M
return function(update_freq)
return {dynamic = draw_dynamic, static = draw_static}
end

View File

@ -1,5 +1,3 @@
local M = {}
local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
@ -57,7 +55,17 @@ local get_bits = function(path)
return Util.read_file(path, nil, '*n') * 8
end
local update = function(cr, update_frequency)
_PLOT_SEC_BREAK_ = nil
_PLOT_HEIGHT_ = nil
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(dnload, cr)
Common.annotated_scale_plot_draw_static(upload, cr)
end
return function(update_freq)
local _update = function(cr)
local dspeed, uspeed = 0, 0
local rx_delta, tx_delta
@ -92,30 +100,19 @@ local update = function(cr, update_frequency)
interface_counters.prev_tx_byte_cnt = tx_byte_cnt
-- mask overflow
if rx_delta > 0 then dspeed = dspeed + rx_delta * update_frequency end
if tx_delta > 0 then uspeed = uspeed + tx_delta * update_frequency end
if rx_delta > 0 then dspeed = dspeed + rx_delta * update_freq end
if tx_delta > 0 then uspeed = uspeed + tx_delta * update_freq end
end
Common.annotated_scale_plot_set(dnload, cr, dspeed)
Common.annotated_scale_plot_set(upload, cr, uspeed)
end
_PLOT_SEC_BREAK_ = nil
_PLOT_HEIGHT_ = nil
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(dnload, cr)
Common.annotated_scale_plot_draw_static(upload, cr)
end
local draw_dynamic = function(cr, update_frequency)
update(cr, update_frequency)
local draw_dynamic = function(cr)
_update(cr)
Common.annotated_scale_plot_draw_dynamic(dnload, cr)
Common.annotated_scale_plot_draw_dynamic(upload, cr)
end
M.draw_static = draw_static
M.draw_dynamic = draw_dynamic
return M
return {static = draw_static, dynamic = draw_dynamic}
end

View File

@ -1,5 +1,3 @@
local M = {}
local Common = require 'Common'
local Geometry = require 'Geometry'
@ -40,14 +38,16 @@ local update = function(cr, pacman_stats)
end
end
M.draw_static = function(cr)
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.text_rows_draw_static(rows, cr)
end
M.draw_dynamic = function(cr, pacman_stats)
local draw_dynamic = function(cr, pacman_stats)
update(cr, pacman_stats)
Common.text_rows_draw_dynamic(rows, cr)
end
return M
return function()
return {static = draw_static, dynamic = draw_dynamic}
end

View File

@ -1,5 +1,3 @@
local M = {}
local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
@ -83,35 +81,6 @@ local DRAM_PATH = '/sys/class/powercap/intel-rapl:0:2/energy_uj'
local prev_pkg0_uj_cnt = Util.read_file(PKG0_PATH, nil, '*n')
local prev_dram_uj_cnt = Util.read_file(DRAM_PATH, nil, '*n')
local update = function(cr, update_frequency, is_using_ac)
local pkg0_uj_cnt = Util.read_file(PKG0_PATH, nil, '*n')
local dram_uj_cnt = Util.read_file(DRAM_PATH, nil, '*n')
local pkg0_power = calculate_power(prev_pkg0_uj_cnt, pkg0_uj_cnt, update_frequency)
-- Common.annotated_scale_plot_set(pkg0, cr, Util.precision_round_to_string(pkg0_power, 3), pkg0_power)
Common.annotated_scale_plot_set(pkg0, cr, pkg0_power)
local dram_power = calculate_power(prev_dram_uj_cnt, dram_uj_cnt, update_frequency)
-- Common.annotated_scale_plot_set(dram, cr, Util.precision_round_to_string(dram_power, 3), dram_power)
Common.annotated_scale_plot_set(dram, cr, dram_power)
prev_pkg0_uj_cnt = pkg0_uj_cnt
prev_dram_uj_cnt = dram_uj_cnt
if is_using_ac then
-- Common.annotated_scale_plot_set(battery_draw, cr, 'A/C', 0)
Common.annotated_scale_plot_set(battery_draw, cr, 0)
else
local current = Util.read_file('/sys/class/power_supply/BAT0/current_now', nil, '*n')
local voltage = Util.read_file('/sys/class/power_supply/BAT0/voltage_now', nil, '*n')
local power = current * voltage * 0.000000000001
-- local t = Util.precision_round_to_string(power, 3)..' W'
-- Common.annotated_scale_plot_set(battery_draw, cr, t, power)
Common.annotated_scale_plot_set(battery_draw, cr, power)
end
end
_MODULE_Y_ = nil
_TEXT_SPACING_ = nil
@ -119,18 +88,46 @@ _PLOT_SEC_BREAK_ = nil
_PLOT_HEIGHT_ = nil
_CORE_Y_ = nil
M.draw_static = function(cr)
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(pkg0, cr)
Common.annotated_scale_plot_draw_static(dram, cr)
Common.annotated_scale_plot_draw_static(battery_draw, cr)
end
M.draw_dynamic = function(cr, update_frequency, is_using_ac)
update(cr, update_frequency, is_using_ac)
return function(update_freq)
local _update = function(cr, is_using_ac)
local pkg0_uj_cnt = Util.read_file(PKG0_PATH, nil, '*n')
local dram_uj_cnt = Util.read_file(DRAM_PATH, nil, '*n')
local pkg0_power = calculate_power(prev_pkg0_uj_cnt, pkg0_uj_cnt, update_freq)
Common.annotated_scale_plot_set(pkg0, cr, pkg0_power)
local dram_power = calculate_power(prev_dram_uj_cnt, dram_uj_cnt, update_freq)
Common.annotated_scale_plot_set(dram, cr, dram_power)
prev_pkg0_uj_cnt = pkg0_uj_cnt
prev_dram_uj_cnt = dram_uj_cnt
if is_using_ac then
Common.annotated_scale_plot_set(battery_draw, cr, 0)
else
local current = Util.read_file('/sys/class/power_supply/BAT0/current_now', nil, '*n')
local voltage = Util.read_file('/sys/class/power_supply/BAT0/voltage_now', nil, '*n')
local power = current * voltage * 0.000000000001
Common.annotated_scale_plot_set(battery_draw, cr, power)
end
end
local draw_dynamic = function(cr, is_using_ac)
_update(cr, is_using_ac)
Common.annotated_scale_plot_draw_dynamic(pkg0, cr)
Common.annotated_scale_plot_draw_dynamic(dram, cr)
Common.annotated_scale_plot_draw_dynamic(battery_draw, cr)
end
return M
return {static = draw_static, dynamic = draw_dynamic}
end

View File

@ -1,5 +1,3 @@
local M = {}
local CompoundDial = require 'CompoundDial'
local Line = require 'Line'
local Table = require 'Table'
@ -268,7 +266,7 @@ _SEP_Y_ = nil
_HWP_Y_ = nil
_PLOT_Y_ = nil
M.draw_static = function(cr)
local draw_static = function(cr)
Common.drawHeader(cr, header)
for c = 1, NUM_PHYSICAL_CORES do
@ -285,7 +283,7 @@ M.draw_static = function(cr)
Table.draw_static(tbl, cr)
end
M.draw_dynamic = function(cr, trigger)
local draw_dynamic = function(cr, trigger)
update(cr, trigger)
for c = 1, NUM_PHYSICAL_CORES do
@ -300,4 +298,6 @@ M.draw_dynamic = function(cr, trigger)
Table.draw_dynamic(tbl, cr)
end
return M
return function()
return {static = draw_static, dynamic = draw_dynamic}
end

View File

@ -1,5 +1,3 @@
local M = {}
local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
@ -22,18 +20,6 @@ local read_stat_file = function()
return __tonumber(bytes_r) * BLOCK_SIZE_BYTES, __tonumber(bytes_w) * BLOCK_SIZE_BYTES
end
local update_stat = function(cr, stat, byte_cnt, update_frequency)
local delta_bytes = byte_cnt - stat.prev_byte_cnt
stat.prev_byte_cnt = byte_cnt
local plot_value = 0
if delta_bytes > 0 then
local bps = delta_bytes * update_frequency
plot_value = bps
end
Common.annotated_scale_plot_set(stat, cr, plot_value)
end
local io_label_function = function(bytes)
local new_unit, new_value = Util.convert_data_val(bytes)
return __math_floor(new_value)..' '..new_unit..'B/s'
@ -83,25 +69,37 @@ reads.byte_cnt = 0
writes.byte_cnt = 0
reads.prev_byte_cnt, writes.prev_byte_cnt = read_stat_file()
local update = function(cr, update_frequency)
local read_byte_cnt, write_byte_cnt = read_stat_file()
update_stat(cr, reads, read_byte_cnt, update_frequency)
update_stat(cr, writes, write_byte_cnt, update_frequency)
end
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.annotated_scale_plot_draw_static(reads, cr)
Common.annotated_scale_plot_draw_static(writes, cr)
end
local draw_dynamic = function(cr, update_frequency)
update(cr, update_frequency)
return function(update_freq)
local update_stat = function(cr, stat, byte_cnt)
local delta_bytes = byte_cnt - stat.prev_byte_cnt
stat.prev_byte_cnt = byte_cnt
local plot_value = 0
if delta_bytes > 0 then
local bps = delta_bytes * update_freq
plot_value = bps
end
Common.annotated_scale_plot_set(stat, cr, plot_value)
end
local update = function(cr)
local read_byte_cnt, write_byte_cnt = read_stat_file()
update_stat(cr, reads, read_byte_cnt)
update_stat(cr, writes, write_byte_cnt)
end
local draw_dynamic = function(cr)
update(cr)
Common.annotated_scale_plot_draw_dynamic(reads, cr)
Common.annotated_scale_plot_draw_dynamic(writes, cr)
end
M.draw_static = draw_static
M.draw_dynamic = draw_dynamic
return M
return {static = draw_static, dynamic = draw_dynamic}
end

View File

@ -1,5 +1,3 @@
local M = {}
local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
@ -25,12 +23,12 @@ local rows = Common.initTextRows(
_TEXT_SPACING_ = nil
M.draw_static = function(cr)
local draw_static = function(cr)
Common.drawHeader(cr, header)
Common.text_rows_draw_static(rows, cr)
end
M.draw_dynamic = function(cr, pacman_stats)
local draw_dynamic = function(cr, pacman_stats)
local last_update, last_sync = "N/A", "N/A"
if pacman_stats then
last_update, last_sync = __string_match(pacman_stats, "^%d+%s+([^%s]+)%s+([^%s]+).*")
@ -42,4 +40,6 @@ M.draw_dynamic = function(cr, pacman_stats)
Common.text_rows_draw_dynamic(rows, cr)
end
return M
return function()
return {static = draw_static, dynamic = draw_dynamic}
end

125
main.lua
View File

@ -81,49 +81,6 @@ local draw_static_surface = function(cr, cs_obj)
__cairo_paint(cr)
end
local cs_left = _make_static_surface(
Geometry.LEFT_X - Geometry.PANEL_MARGIN_X,
Geometry.TOP_Y - Geometry.PANEL_MARGIN_Y,
Geometry.SECTION_WIDTH + Geometry.PANEL_MARGIN_X * 2,
Geometry.SIDE_HEIGHT + Geometry.PANEL_MARGIN_Y * 2,
{System.draw_static, Graphics.draw_static, Processor.draw_static}
)
local cs_center = _make_static_surface(
Geometry.CENTER_LEFT_X - Geometry.PANEL_MARGIN_X,
Geometry.TOP_Y - Geometry.PANEL_MARGIN_Y,
Geometry.CENTER_WIDTH + Geometry.PANEL_MARGIN_Y * 2 + Geometry.CENTER_PAD,
Geometry.CENTER_HEIGHT + Geometry.PANEL_MARGIN_Y * 2,
{ReadWrite.draw_static, Network.draw_static}
)
local cs_right = _make_static_surface(
Geometry.RIGHT_X - Geometry.PANEL_MARGIN_X,
Geometry.TOP_Y - Geometry.PANEL_MARGIN_Y,
Geometry.SECTION_WIDTH + Geometry.PANEL_MARGIN_X * 2,
Geometry.SIDE_HEIGHT + Geometry.PANEL_MARGIN_Y * 2,
{Pacman.draw_static, FileSystem.draw_static, Power.draw_static, Memory.draw_static}
)
--
-- kill all the stuff we don't need for the main loop
--
-- local _unrequire = function(m) package.loaded[m] = nil end
-- _unrequire('Super')
-- _unrequire('Color')
-- _unrequire('Widget')
-- _unrequire('Patterns')
-- _unrequire = nil
-- _make_static_surface = nil
-- FillRect = nil
-- _G_INIT_DATA_ = nil
-- collectgarbage()
--
-- main loop
--
local using_ac = function()
-- for some reason it is much more efficient to test if the battery
-- is off than if the ac is on
@ -132,9 +89,69 @@ end
local updates = -2 -- this accounts for the first few spazzy iterations
local STATS_FILE = '/tmp/.conky_pacman'
local draw
function conky_start(update_interval)
conky_set_update_interval(update_interval)
local update_freq = 1 / update_interval
local mem = Memory(update_freq)
local rw = ReadWrite(update_freq)
local net = Network(update_freq)
local pwr = Power(update_freq)
local fs = FileSystem()
local sys = System()
local gfx = Graphics()
local proc = Processor()
local pcm = Pacman()
local cs_left = _make_static_surface(
Geometry.LEFT_X - Geometry.PANEL_MARGIN_X,
Geometry.TOP_Y - Geometry.PANEL_MARGIN_Y,
Geometry.SECTION_WIDTH + Geometry.PANEL_MARGIN_X * 2,
Geometry.SIDE_HEIGHT + Geometry.PANEL_MARGIN_Y * 2,
{sys.static, gfx.static, proc.static}
)
local cs_center = _make_static_surface(
Geometry.CENTER_LEFT_X - Geometry.PANEL_MARGIN_X,
Geometry.TOP_Y - Geometry.PANEL_MARGIN_Y,
Geometry.CENTER_WIDTH + Geometry.PANEL_MARGIN_Y * 2 + Geometry.CENTER_PAD,
Geometry.CENTER_HEIGHT + Geometry.PANEL_MARGIN_Y * 2,
{rw.static, net.static}
)
local cs_right = _make_static_surface(
Geometry.RIGHT_X - Geometry.PANEL_MARGIN_X,
Geometry.TOP_Y - Geometry.PANEL_MARGIN_Y,
Geometry.SECTION_WIDTH + Geometry.PANEL_MARGIN_X * 2,
Geometry.SIDE_HEIGHT + Geometry.PANEL_MARGIN_Y * 2,
{pcm.static, fs.static, pwr.static, mem.static}
)
draw = function(cr, _updates)
draw_static_surface(cr, cs_left)
draw_static_surface(cr, cs_center)
draw_static_surface(cr, cs_right)
local t1 = _updates % (update_freq * 10)
local is_using_ac = using_ac()
local pacman_stats = Util.read_file(STATS_FILE)
sys.dynamic(cr, pacman_stats)
gfx.dynamic(cr)
proc.dynamic(cr, t1)
rw.dynamic(cr)
net.dynamic(cr)
pcm.dynamic(cr, pacman_stats)
fs.dynamic(cr, t1)
pwr.dynamic(cr, is_using_ac)
mem.dynamic(cr)
end
end
function conky_main()
@ -144,29 +161,9 @@ function conky_main()
local cs = __cairo_xlib_surface_create(_cw.display, _cw.drawable,
_cw.visual, 1920, 1080)
local cr = __cairo_create(cs)
draw_static_surface(cr, cs_left)
draw_static_surface(cr, cs_center)
draw_static_surface(cr, cs_right)
updates = updates + 1
local t1 = updates % (UPDATE_FREQUENCY * 10)
local is_using_ac = using_ac()
local pacman_stats = Util.read_file(STATS_FILE)
System.draw_dynamic(cr, pacman_stats)
Graphics.draw_dynamic(cr)
Processor.draw_dynamic(cr, t1)
ReadWrite.draw_dynamic(cr, UPDATE_FREQUENCY)
Network.draw_dynamic(cr, UPDATE_FREQUENCY)
Pacman.draw_dynamic(cr, pacman_stats)
FileSystem.draw_dynamic(cr, t1)
Power.draw_dynamic(cr, UPDATE_FREQUENCY, is_using_ac)
Memory.draw_dynamic(cr)
draw(cr, updates)
__cairo_surface_destroy(cs)
__cairo_destroy(cr)