REF move config/compile code completely to new module

This commit is contained in:
Nathan Dwarshuis 2022-07-17 23:09:04 -04:00
parent 7ade617e80
commit d20a9637d1
4 changed files with 105 additions and 99 deletions

View File

@ -16,6 +16,7 @@ conky.config = {
xinerama_head = 0,
double_buffer = true,
-- TODO don't hardcode screen size here
minimum_width = 1920,
minimum_height = 1080,

2
core

@ -1 +1 @@
Subproject commit ed4fc4ec0304caff96163017ee9a2ae419358a50
Subproject commit 87f03c704fe2899d5e780255ac1634d8c260c8fe

View File

@ -1,18 +1,25 @@
local M = {}
local pure = require 'pure'
local geom = require 'geom'
local fill_rect = require 'fill_rect'
local i_o = require 'i_o'
local system = require 'system'
local network = require 'network'
local processor = require 'processor'
local filesystem = require 'filesystem'
local pacman = require 'pacman'
local power = require 'power'
local readwrite = require 'readwrite'
local graphics= require 'graphics'
local memory = require 'memory'
local yaml = require 'lyaml'
local common = require 'common'
--------------------------------------------------------------------------------
-- compile entire layout
local reduce_modules_y = function(common, modlist, init_x, width, acc, new)
local reduce_modules_y = function(common_, modlist, init_x, width, acc, new)
if type(new) == "number" then
acc.next_y = acc.next_y + new
else
local m = modlist[new](common, width, geom.make_point(init_x, acc.next_y))
local r = common.compile_module(
local m = modlist[new](common_, width, geom.make_point(init_x, acc.next_y))
local r = common_.compile_module(
m.header,
m.point,
m.width,
@ -31,12 +38,12 @@ local reduce_modules_y = function(common, modlist, init_x, width, acc, new)
return acc
end
local reduce_modules_x = function(common, modlist, init_y, acc, x_mods)
local reduce_modules_x = function(common_, modlist, init_y, acc, x_mods)
if type(x_mods) == "number" then
acc.next_x = acc.next_x + x_mods
else
local r = pure.reduce(
pure.partial(reduce_modules_y, common, modlist, acc.next_x, x_mods.width),
pure.partial(reduce_modules_y, common_, modlist, acc.next_x, x_mods.width),
{next_x = acc.next_x, next_y = init_y, fgroups = acc.fgroups},
x_mods.blocks
)
@ -47,9 +54,9 @@ local reduce_modules_x = function(common, modlist, init_y, acc, x_mods)
return acc
end
local arrange_panel_modules = function(common, modlist, point, mods)
local arrange_panel_modules = function(common_, modlist, point, mods)
local r = pure.reduce(
pure.partial(reduce_modules_x, common, modlist, point.y),
pure.partial(reduce_modules_x, common_, modlist, point.y),
{next_x = point.x, next_y = point.y, fgroups = {}},
mods
)
@ -63,12 +70,12 @@ local arrange_panel_modules = function(common, modlist, point, mods)
}
end
local build_surface = function(common, box, fs)
local build_surface = function(common_, box, fs)
local panel_line_thickness = 1
-- move over by half a pixel so the lines don't need to be antialiased
local _x = box.corner.x + 0.5
local _y = box.corner.y + 0.5
local panel = common.make_panel(_x, _y, box.width, box.height, panel_line_thickness)
local panel = common_.make_panel(_x, _y, box.width, box.height, panel_line_thickness)
local cs_x = _x - panel_line_thickness * 0.5
local cs_y = _y - panel_line_thickness * 0.5
local cs_w = box.width + panel_line_thickness
@ -87,7 +94,7 @@ local build_surface = function(common, box, fs)
return {x = cs_x, y = cs_y, s = cs}
end
local reduce_static = function(common, mods, y, acc, panel_mods)
local reduce_static = function(common_, mods, y, acc, panel_mods)
if type(panel_mods) == "number" then
acc.next_x = acc.next_x + panel_mods
else
@ -95,26 +102,26 @@ local reduce_static = function(common, mods, y, acc, panel_mods)
local margin_x = margins[1]
local margin_y = margins[2]
local mpoint = geom.make_point(acc.next_x + margin_x, y + margin_y)
local r = arrange_panel_modules(common, mods, mpoint, panel_mods.columns)
local r = arrange_panel_modules(common_, mods, mpoint, panel_mods.columns)
local w = r.width + margin_x * 2
local h = r.height + margin_y * 2
local pbox = geom.make_box(acc.next_x, y, w, h)
acc.next_x = acc.next_x + w
acc.static = pure.flatten({acc.static, {build_surface(common, pbox, r.static)}})
acc.static = pure.flatten({acc.static, {build_surface(common_, pbox, r.static)}})
acc.update = pure.flatten({acc.update, r.update})
acc.dynamic = pure.flatten({acc.dynamic, r.dynamic})
end
return acc
end
M.compile_layout = function(common, point, mods, module_sets)
local compile_layout = function(common_, point, mods, module_sets)
local __cairo_set_source_surface = cairo_set_source_surface
local __cairo_paint = cairo_paint
local r = pure.reduce(
pure.partial(
reduce_static,
common,
common_,
mods,
point.y
),
@ -137,4 +144,40 @@ M.compile_layout = function(common, point, mods, module_sets)
}
end
return M
return function(update_interval, config_path)
local update_freq = 1 / update_interval
local config = yaml.load(i_o.read_file(config_path))
local cmods = config.modules
local main_state = {}
local mods = {
memory = pure.partial(memory, update_freq, cmods.memory),
readwrite = pure.partial(readwrite, update_freq, cmods.readwrite),
network = pure.partial(network, update_freq),
power = pure.partial(power, update_freq, cmods.power),
filesystem = pure.partial(filesystem, cmods.filesystem, main_state),
system = pure.partial(system, main_state),
graphics = pure.partial(graphics, update_freq, cmods.graphics),
processor = pure.partial(processor, update_freq, cmods.processor, main_state),
pacman = pure.partial(pacman, main_state)
}
local compiled = compile_layout(
common(config),
geom.make_point(table.unpack(config.layout.anchor)),
mods,
config.layout.panels
)
local STATS_FILE = '/tmp/.conky_pacman'
return function(cr, _updates)
main_state.trigger10 = _updates % (update_freq * 10)
main_state.pacman_stats = i_o.read_file(STATS_FILE)
compiled.static(cr)
compiled.update()
compiled.dynamic(cr)
end
end

114
main.lua
View File

@ -1,87 +1,49 @@
require 'cairo'
local __cairo_xlib_surface_create = cairo_xlib_surface_create
local __cairo_create = cairo_create
local __cairo_surface_destroy = cairo_surface_destroy
local __cairo_destroy = cairo_destroy
local ABS_PATH = debug.getinfo(1).source:match("@?(.*/)")
package.path = ABS_PATH..'?.lua;'..
ABS_PATH..'drawing/?.lua;'..
ABS_PATH..'schema/?.lua;'..
ABS_PATH..'core/?.lua;'..
ABS_PATH..'core/widget/?.lua;'..
ABS_PATH..'core/widget/arc/?.lua;'..
ABS_PATH..'core/widget/text/?.lua;'..
ABS_PATH..'core/widget/timeseries/?.lua;'..
ABS_PATH..'core/widget/rect/?.lua;'..
ABS_PATH..'core/widget/line/?.lua;'..
ABS_PATH..'lib/share/lua/5.4/?.lua;'..
ABS_PATH..'lib/share/lua/5.4/?/init.lua;'
package.cpath = ABS_PATH..'lib/lib/lua/5.4/?.so;'
local i_o = require 'i_o'
local geom = require 'geom'
local pure = require 'pure'
local system = require 'system'
local network = require 'network'
local processor = require 'processor'
local filesystem = require 'filesystem'
local pacman = require 'pacman'
local power = require 'power'
local readwrite = require 'readwrite'
local graphics = require 'graphics'
local memory = require 'memory'
local compile = require 'compile'
local common = require 'common'
local yaml = require 'lyaml'
local draw_dynamic
local build_main = function(update_interval, config_path)
local update_freq = 1 / update_interval
local config = yaml.load(i_o.read_file(config_path))
local cmods = config.modules
local main_state = {}
local mods = {
memory = pure.partial(memory, update_freq, cmods.memory),
readwrite = pure.partial(readwrite, update_freq, cmods.readwrite),
network = pure.partial(network, update_freq),
power = pure.partial(power, update_freq, cmods.power),
filesystem = pure.partial(filesystem, cmods.filesystem, main_state),
system = pure.partial(system, main_state),
graphics = pure.partial(graphics, update_freq, cmods.graphics),
processor = pure.partial(processor, update_freq, cmods.processor, main_state),
pacman = pure.partial(pacman, main_state)
local conky_dir = debug.getinfo(1).source:match("@?(.*/)")
local subdirs = {
'?.lua',
'drawing/?.lua',
'schema/?.lua',
'core/?.lua',
'core/widget/?.lua',
'core/widget/arc/?.lua',
'core/widget/text/?.lua',
'core/widget/timeseries/?.lua',
'core/widget/rect/?.lua',
'core/widget/line/?.lua',
'lib/share/lua/5.4/?.lua',
'lib/share/lua/5.4/?/init.lua',
}
local compiled = compile.compile_layout(
common(config),
geom.make_point(table.unpack(config.layout.anchor)),
mods,
config.layout.panels
)
local STATS_FILE = '/tmp/.conky_pacman'
return function(cr, _updates)
main_state.trigger10 = _updates % (update_freq * 10)
main_state.pacman_stats = i_o.read_file(STATS_FILE)
compiled.static(cr)
compiled.update()
compiled.dynamic(cr)
end
for i = 1, #subdirs do
package.path = package.path..';'..conky_dir..subdirs[i]
end
package.cpath = package.cpath..';'..conky_dir..'lib/lib/lua/5.4/?.so;'
--------------------------------------------------------------------------------
-- startup - this is where the modules are compiled and arranged
require 'cairo'
local compile = require 'compile'
local draw_dynamic
function conky_start(update_interval)
conky_set_update_interval(update_interval)
draw_dynamic = build_main(update_interval, ABS_PATH..'config.yml')
draw_dynamic = compile(update_interval, conky_dir..'config.yml')
end
--------------------------------------------------------------------------------
-- main loop - where all the drawing/updating happens
local __cairo_xlib_surface_create = cairo_xlib_surface_create
local __cairo_create = cairo_create
local __cairo_surface_destroy = cairo_surface_destroy
local __cairo_destroy = cairo_destroy
local updates = -2 -- this accounts for the first few spazzy iterations
function conky_main()
@ -89,7 +51,7 @@ function conky_main()
if not _cw then return end
local cs = __cairo_xlib_surface_create(_cw.display, _cw.drawable,
_cw.visual, 1920, 1080)
_cw.visual, _cw.width, _cw.height)
local cr = __cairo_create(cs)
updates = updates + 1