From 3486e37a43ff506f57e55c18cc8ff259f9eaf761 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Tue, 19 Jul 2022 19:15:26 -0400 Subject: [PATCH] ENH use conky.conf as bootstrap file for config loading/validation --- config.yml => config/fallback.yml | 3 ++ schema.yml => config/schema.yml | 17 +++++++ conky.conf | 75 ++++++++++++++++++++++++++++--- core | 2 +- drawing/compile.lua | 5 ++- main.lua | 26 ++--------- 6 files changed, 99 insertions(+), 29 deletions(-) rename config.yml => config/fallback.yml (97%) rename schema.yml => config/schema.yml (95%) diff --git a/config.yml b/config/fallback.yml similarity index 97% rename from config.yml rename to config/fallback.yml index d5b1f39..3d1ddde 100644 --- a/config.yml +++ b/config/fallback.yml @@ -1,3 +1,6 @@ +bootstrap: + update_interval: 1 + dimensions: [1920, 1080] modules: filesystem: show_smart: true diff --git a/schema.yml b/config/schema.yml similarity index 95% rename from schema.yml rename to config/schema.yml index f2ce938..9c12ad9 100644 --- a/schema.yml +++ b/config/schema.yml @@ -4,7 +4,24 @@ required: [modules, layout] additionalProperties: false properties: + bootstrap: + required: [update_interval, dimensions] + additionalProperties: false + properties: + update_interval: + description: the update interval (seconds) + type: number + dimensions: + description: the max width/height of the conky window + type: array + minItems: 2 + maxItems: 2 + items: + type: integer + minimum: 1 + modules: + required: [filesystem, graphics, memory, power, processor, readwrite] additionalProperties: false properties: filesystem: diff --git a/conky.conf b/conky.conf index af27bf8..464e991 100644 --- a/conky.conf +++ b/conky.conf @@ -1,6 +1,72 @@ -local update_interval = 1 -- in seconds +-------------------------------------------------------------------------------- +-- set up paths 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', +} + +for i = 1, #subdirs do + subdirs[i] = conky_dir..subdirs[i] +end + +package.path = table.concat(subdirs, ';') + +package.cpath = conky_dir..'lib/lib/lua/5.4/?.so;' + +-------------------------------------------------------------------------------- +-- select global config to use (fallback to default if none found) + +local yaml = require 'lyaml' +local i_o = require 'i_o' + +local schema_path = conky_dir..'/config/schema.yml' + +local try_config_paths = { + '/home/ndwar/.config/conky.yml', + conky_dir..'config/fallback.yml' +} +local config_path +local config + +for i = 1, #try_config_paths do + config_path = try_config_paths[i] + local r = i_o.read_file(config_path) + if r ~= nil then + config = yaml.load(r) + break + end +end + +i_o.printf('Using config at %s', config_path) + +if i_o.exit_code_cmd('command -v yajsv > /dev/null') == 0 then + local cmd = string.format('yajsv -q -s %s %s', schema_path, config_path) + assert(i_o.exit_code_cmd(cmd) == 0, 'ERROR: config failed validation') +else + print(string.format('WARNING: could not validate config')) +end + +local bootstrap = config.bootstrap + +local startup_hook = string.format( + 'start %f %s %s %s', + bootstrap.update_interval, + config_path, + package.path, + package.cpath +) conky.config = { background = false, @@ -18,9 +84,8 @@ conky.config = { xinerama_head = 0, double_buffer = true, - -- TODO don't hardcode screen size here - minimum_width = 1920, - minimum_height = 1080, + minimum_width = bootstrap.dimensions[1], + minimum_height = bootstrap.dimensions[2], draw_shades = false, draw_outline = false, @@ -34,7 +99,7 @@ conky.config = { -- Lua Load lua_load = conky_dir..'main.lua', lua_draw_hook_post = 'main', - lua_startup_hook = string.format('start %f %s', update_interval, conky_dir) + lua_startup_hook = startup_hook } --control updates entirely in lua diff --git a/core b/core index aa0d027..8f14951 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit aa0d02748b4316c44e21cc4af24727aba82159ff +Subproject commit 8f1495175f08bdc0716ef11b73d3babb10874c8b diff --git a/drawing/compile.lua b/drawing/compile.lua index e727ab8..1b7136b 100644 --- a/drawing/compile.lua +++ b/drawing/compile.lua @@ -146,7 +146,10 @@ end return function(update_interval, config_path) local update_freq = 1 / update_interval - local config = yaml.load(i_o.read_file(config_path)) + local default_config = 'config.yml' + -- local r = i_o.read_file(config_path) or i_o.read_file(default_config) + local r = i_o.read_file(config_path) or i_o.read_file(default_config) + local config = yaml.load(r) local cmods = config.modules local main_state = {} diff --git a/main.lua b/main.lua index ec5b191..1c9da52 100644 --- a/main.lua +++ b/main.lua @@ -8,27 +8,9 @@ local __cairo_create local __cairo_surface_destroy local __cairo_destroy -function conky_start(update_interval, conky_dir) - 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', - } - - 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;' +function conky_start(update_interval, config_path, path, cpath) + package.path = package.path..';'..path + package.cpath = package.cpath..';'..cpath require 'cairo' @@ -41,7 +23,7 @@ function conky_start(update_interval, conky_dir) conky_set_update_interval(update_interval) - draw_dynamic = compile(update_interval, conky_dir..'config.yml') + draw_dynamic = compile(update_interval, config_path) end --------------------------------------------------------------------------------