REF make code cleaner/more functional
This commit is contained in:
parent
f05b4ea4b7
commit
1f5fa3ddd6
|
@ -9,6 +9,7 @@ return function(update_freq, config, common, width, point)
|
||||||
local NA = 'N/A'
|
local NA = 'N/A'
|
||||||
local NVIDIA_EXE = 'nvidia-settings'
|
local NVIDIA_EXE = 'nvidia-settings'
|
||||||
local __string_match = string.match
|
local __string_match = string.match
|
||||||
|
local __string_format = string.format
|
||||||
local __tonumber = tonumber
|
local __tonumber = tonumber
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
@ -112,18 +113,9 @@ return function(update_freq, config, common, width, point)
|
||||||
-- gpu status
|
-- gpu status
|
||||||
|
|
||||||
local mk_status = function(y)
|
local mk_status = function(y)
|
||||||
local obj = common.make_text_row(
|
local obj = common.make_text_row(point.x, y, width, 'Status')
|
||||||
point.x,
|
|
||||||
y,
|
|
||||||
width,
|
|
||||||
'Status'
|
|
||||||
)
|
|
||||||
local update = function()
|
local update = function()
|
||||||
if mod_state.error == false then
|
common.text_row_set(obj, mod_state.error == false and 'On' or mod_state.error)
|
||||||
common.text_row_set(obj, 'On')
|
|
||||||
else
|
|
||||||
common.text_row_set(obj, mod_state.error)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
local static = pure.partial(common.text_row_draw_static, obj)
|
local static = pure.partial(common.text_row_draw_static, obj)
|
||||||
local dynamic = pure.partial(common.text_row_draw_dynamic, obj)
|
local dynamic = pure.partial(common.text_row_draw_dynamic, obj)
|
||||||
|
@ -140,13 +132,13 @@ return function(update_freq, config, common, width, point)
|
||||||
width,
|
width,
|
||||||
'Internal Temperature',
|
'Internal Temperature',
|
||||||
function(s)
|
function(s)
|
||||||
if s == -1 then return NA else return string.format('%s°C', s) end
|
if s == -1 then return NA else return __string_format('%s°C', s) end
|
||||||
end,
|
end,
|
||||||
80
|
80
|
||||||
)
|
)
|
||||||
local update = _from_state(
|
local update = _from_state(
|
||||||
-1,
|
-1,
|
||||||
function(s) return __tonumber(s.temp_reading) end,
|
pure.compose(__tonumber, pure.getter("temp_reading")),
|
||||||
pure.partial(common.threshold_text_row_set, obj)
|
pure.partial(common.threshold_text_row_set, obj)
|
||||||
)
|
)
|
||||||
local static = pure.partial(common.threshold_text_row_draw_static, obj)
|
local static = pure.partial(common.threshold_text_row_draw_static, obj)
|
||||||
|
@ -185,7 +177,7 @@ return function(update_freq, config, common, width, point)
|
||||||
local mk_gpu_util = pure.partial(
|
local mk_gpu_util = pure.partial(
|
||||||
_mk_plot,
|
_mk_plot,
|
||||||
'GPU utilization',
|
'GPU utilization',
|
||||||
function(s) return s.gpu_utilization end
|
pure.getter("gpu_utilization")
|
||||||
)
|
)
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
@ -203,7 +195,7 @@ return function(update_freq, config, common, width, point)
|
||||||
local mk_vid_util = pure.partial(
|
local mk_vid_util = pure.partial(
|
||||||
_mk_plot,
|
_mk_plot,
|
||||||
'Video utilization',
|
'Video utilization',
|
||||||
function(s) return s.vid_utilization end
|
pure.getter("vid_utilization")
|
||||||
)
|
)
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
|
|
@ -16,6 +16,7 @@ return function(update_freq, config, common, width, point)
|
||||||
local TABLE_SECTION_BREAK = 20
|
local TABLE_SECTION_BREAK = 20
|
||||||
|
|
||||||
local __math_floor = math.floor
|
local __math_floor = math.floor
|
||||||
|
local __string_format = string.format
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- state
|
-- state
|
||||||
|
@ -59,9 +60,7 @@ return function(update_freq, config, common, width, point)
|
||||||
CACHE_X = MEM_X + CACHE_X_OFFSET + DIAL_DIAMETER / 2
|
CACHE_X = MEM_X + CACHE_X_OFFSET + DIAL_DIAMETER / 2
|
||||||
end
|
end
|
||||||
local CACHE_WIDTH = point.x + width - CACHE_X
|
local CACHE_WIDTH = point.x + width - CACHE_X
|
||||||
local format_percent = function(x)
|
local format_percent = pure.partial(__string_format, '%i%%', true)
|
||||||
return string.format('%i%%', x)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- memory bits (used no matter what)
|
-- memory bits (used no matter what)
|
||||||
local mem = common.make_dial(
|
local mem = common.make_dial(
|
||||||
|
@ -188,13 +187,7 @@ return function(update_freq, config, common, width, point)
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
num_rows)
|
num_rows)
|
||||||
local obj = common.make_text_table(
|
local obj = common.make_text_table(point.x, y, width, num_rows, 'Mem (%)')
|
||||||
point.x,
|
|
||||||
y,
|
|
||||||
width,
|
|
||||||
num_rows,
|
|
||||||
'Mem (%)'
|
|
||||||
)
|
|
||||||
local update = function()
|
local update = function()
|
||||||
for r = 1, num_rows do
|
for r = 1, num_rows do
|
||||||
text_table.set(obj, 1, r, i_o.conky(table_conky[r].comm, '(%S+)'))
|
text_table.set(obj, 1, r, i_o.conky(table_conky[r].comm, '(%S+)'))
|
||||||
|
|
|
@ -58,22 +58,14 @@ return function(update_freq, config, common, width, point)
|
||||||
|
|
||||||
|
|
||||||
local format_ac = function(watts)
|
local format_ac = function(watts)
|
||||||
if watts == 0 then
|
return watts == 0 and "A/C" or format_rapl(watts)
|
||||||
return "A/C"
|
|
||||||
else
|
|
||||||
return format_rapl(watts)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local mk_bat = function(y)
|
local mk_bat = function(y)
|
||||||
local _read_battery_power = sys.battery_power_reader(config.battery)
|
local _read_battery_power = sys.battery_power_reader(config.battery)
|
||||||
|
|
||||||
local read_battery_power = function(is_using_ac)
|
local read_battery_power = function(is_using_ac)
|
||||||
if is_using_ac then
|
return is_using_ac and 0 or _read_battery_power()
|
||||||
return 0
|
|
||||||
else
|
|
||||||
return _read_battery_power()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
local read_bat_status = sys.battery_status_reader(config.battery)
|
local read_bat_status = sys.battery_status_reader(config.battery)
|
||||||
local obj = common.make_tagged_scaled_timeseries(
|
local obj = common.make_tagged_scaled_timeseries(
|
||||||
|
@ -104,7 +96,7 @@ return function(update_freq, config, common, width, point)
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- main functions
|
-- main functions
|
||||||
|
|
||||||
return {
|
return {
|
||||||
header = 'POWER',
|
header = 'POWER',
|
||||||
point = point,
|
point = point,
|
||||||
|
|
|
@ -47,11 +47,20 @@ return function(update_freq, config, main_state, common, width, point)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local create_core = function(x, y)
|
local create_core = function(core_cols, y, c)
|
||||||
|
local dial_x = point.x +
|
||||||
|
(core_cols == 1
|
||||||
|
and (width / 2)
|
||||||
|
or (config.core_padding + DIAL_OUTER_RADIUS +
|
||||||
|
(width - 2 * (DIAL_OUTER_RADIUS + config.core_padding))
|
||||||
|
* math.fmod(c - 1, core_cols) / (core_cols - 1)))
|
||||||
|
local dial_y = y + DIAL_OUTER_RADIUS +
|
||||||
|
(2 * DIAL_OUTER_RADIUS + DIAL_SPACING)
|
||||||
|
* math.floor((c - 1) / core_cols)
|
||||||
return {
|
return {
|
||||||
loads = common.make_compound_dial(
|
loads = common.make_compound_dial(
|
||||||
x,
|
dial_x,
|
||||||
y,
|
dial_y,
|
||||||
DIAL_OUTER_RADIUS,
|
DIAL_OUTER_RADIUS,
|
||||||
DIAL_INNER_RADIUS,
|
DIAL_INNER_RADIUS,
|
||||||
DIAL_THICKNESS,
|
DIAL_THICKNESS,
|
||||||
|
@ -59,8 +68,8 @@ return function(update_freq, config, main_state, common, width, point)
|
||||||
nthreads
|
nthreads
|
||||||
),
|
),
|
||||||
coretemp = common.make_text_circle(
|
coretemp = common.make_text_circle(
|
||||||
x,
|
dial_x,
|
||||||
y,
|
dial_y,
|
||||||
DIAL_INNER_RADIUS - 2,
|
DIAL_INNER_RADIUS - 2,
|
||||||
'%s°C',
|
'%s°C',
|
||||||
80,
|
80,
|
||||||
|
@ -71,19 +80,7 @@ return function(update_freq, config, main_state, common, width, point)
|
||||||
|
|
||||||
local mk_cores = function(y)
|
local mk_cores = function(y)
|
||||||
local core_cols = ncores / config.core_rows
|
local core_cols = ncores / config.core_rows
|
||||||
local cores = {}
|
local cores = pure.map_n(pure.partial(create_core, core_cols, y), ncores)
|
||||||
for c = 1, ncores do
|
|
||||||
local dial_x = point.x +
|
|
||||||
(core_cols == 1
|
|
||||||
and (width / 2)
|
|
||||||
or (config.core_padding + DIAL_OUTER_RADIUS +
|
|
||||||
(width - 2 * (DIAL_OUTER_RADIUS + config.core_padding))
|
|
||||||
* math.fmod(c - 1, core_cols) / (core_cols - 1)))
|
|
||||||
local dial_y = y + DIAL_OUTER_RADIUS +
|
|
||||||
(2 * DIAL_OUTER_RADIUS + DIAL_SPACING)
|
|
||||||
* math.floor((c - 1) / core_cols)
|
|
||||||
cores[c] = create_core(dial_x, dial_y)
|
|
||||||
end
|
|
||||||
local coretemp_paths = cpu.get_coretemp_paths()
|
local coretemp_paths = cpu.get_coretemp_paths()
|
||||||
if #coretemp_paths ~= ncores then
|
if #coretemp_paths ~= ncores then
|
||||||
i_o.warnf('could not find all coretemp paths')
|
i_o.warnf('could not find all coretemp paths')
|
||||||
|
|
11
src/pure.lua
11
src/pure.lua
|
@ -136,6 +136,15 @@ end
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- random list things
|
-- random list things
|
||||||
|
|
||||||
|
-- a stupid but composable function
|
||||||
|
M.get = function(key, tbl)
|
||||||
|
return tbl[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
M.getter = function(key)
|
||||||
|
return M.partial(M.get, key)
|
||||||
|
end
|
||||||
|
|
||||||
M.set = function(tbl, key, value)
|
M.set = function(tbl, key, value)
|
||||||
local r = {}
|
local r = {}
|
||||||
for k, v in pairs(tbl) do
|
for k, v in pairs(tbl) do
|
||||||
|
@ -283,6 +292,8 @@ M.memoize = function(f)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M.id = function(x) return x end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- maybe
|
-- maybe
|
||||||
|
|
||||||
|
|
|
@ -42,9 +42,7 @@ M.make_setter = function(_arc, thickness, threshold_config)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
return function(percent)
|
return pure.compose(f, pure.round_percent)
|
||||||
return f(pure.round_percent(percent))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.make = function(_arc, bg_config, fg_threshold_config)
|
M.make = function(_arc, bg_config, fg_threshold_config)
|
||||||
|
|
|
@ -36,9 +36,7 @@ M.make_setter = function(_line, config, threshold_config)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
return function(percent)
|
return pure.compose(f, pure.round_percent)
|
||||||
return f(pure.round_percent(percent))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.make = function(p1, p2, bg_config, fg_threshold_config)
|
M.make = function(p1, p2, bg_config, fg_threshold_config)
|
||||||
|
|
|
@ -2,6 +2,7 @@ local M = {}
|
||||||
|
|
||||||
local err = require 'err'
|
local err = require 'err'
|
||||||
local geom = require 'geom'
|
local geom = require 'geom'
|
||||||
|
local pure = require 'pure'
|
||||||
|
|
||||||
local __string_sub = string.sub
|
local __string_sub = string.sub
|
||||||
local __cairo_toy_font_face_create = cairo_toy_font_face_create
|
local __cairo_toy_font_face_create = cairo_toy_font_face_create
|
||||||
|
@ -13,12 +14,14 @@ local __cairo_set_source = cairo_set_source
|
||||||
local __cairo_move_to = cairo_move_to
|
local __cairo_move_to = cairo_move_to
|
||||||
local __cairo_show_text = cairo_show_text
|
local __cairo_show_text = cairo_show_text
|
||||||
|
|
||||||
|
local __string_format = string.format
|
||||||
|
|
||||||
M.NULL_TEXT_STRING = '<null>'
|
M.NULL_TEXT_STRING = '<null>'
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- pure
|
-- pure
|
||||||
|
|
||||||
local trim_to_length = function(text, len)
|
local trim_to_length = function(len, text)
|
||||||
if #text > len then
|
if #text > len then
|
||||||
return __string_sub(text, 1, len)..'...'
|
return __string_sub(text, 1, len)..'...'
|
||||||
else
|
else
|
||||||
|
@ -30,11 +33,11 @@ M.make_format_function = function(format)
|
||||||
if type(format) == "function" then
|
if type(format) == "function" then
|
||||||
return format
|
return format
|
||||||
elseif type(format) == "number" and format > 0 then
|
elseif type(format) == "number" and format > 0 then
|
||||||
return function(_text) return trim_to_length(_text, format) end
|
return pure.partial(trim_to_length, format)
|
||||||
elseif type(format) == "string" then
|
elseif type(format) == "string" then
|
||||||
return function(_text) return string.format(format, _text) end
|
return pure.partial(__string_format, format, true)
|
||||||
elseif format == nil or format == false then
|
elseif format == nil or format == false then
|
||||||
return function(_text) return _text end
|
return pure.id
|
||||||
else
|
else
|
||||||
local msg = "format must be a printf string, positive int, or function: got "
|
local msg = "format must be a printf string, positive int, or function: got "
|
||||||
local t = type(format)
|
local t = type(format)
|
||||||
|
@ -106,37 +109,31 @@ M.font_height = function(font)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.x_align_function = function(x_align, font)
|
M.x_align_function = function(x_align, font)
|
||||||
if x_align == 'left' then
|
local funs = {
|
||||||
return function(text)
|
left = function(text)
|
||||||
local te = set_text_extents(text, font)
|
local te = set_text_extents(text, font)
|
||||||
return -te.x_bearing
|
return -te.x_bearing
|
||||||
end
|
end,
|
||||||
elseif x_align == 'center' then
|
center = function(text)
|
||||||
return function(text)
|
|
||||||
local te = set_text_extents(text, font)
|
local te = set_text_extents(text, font)
|
||||||
return -(te.x_bearing + te.width * 0.5)
|
return -(te.x_bearing + te.width * 0.5)
|
||||||
end
|
end,
|
||||||
elseif x_align == 'right' then
|
right = function(text)
|
||||||
return function(text)
|
|
||||||
local te = set_text_extents(text, font)
|
local te = set_text_extents(text, font)
|
||||||
return -(te.x_bearing + te.width)
|
return -(te.x_bearing + te.width)
|
||||||
end
|
end
|
||||||
else
|
}
|
||||||
err.assert_trace(nil, "invalid x_align")
|
return funs[x_align] or err.assert_trace(nil, "invalid x_align")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_delta_y = function(y_align, font)
|
M.get_delta_y = function(y_align, font)
|
||||||
local fe = set_font_extents(font)
|
local fe = set_font_extents(font)
|
||||||
if y_align == 'bottom' then
|
local descents = {
|
||||||
return -fe.descent
|
top = fe.height,
|
||||||
elseif y_align == 'top' then
|
center = 0.92 * fe.height * 0.5 - fe.descent,
|
||||||
return fe.height
|
bottom = -fe.descent
|
||||||
elseif y_align == 'center' then
|
}
|
||||||
return 0.92 * fe.height * 0.5 - fe.descent
|
return descents[y_align] or err.assert_trace(nil, "invalid y_align")
|
||||||
else
|
|
||||||
err.assert_trace(nil, "invalid y_align")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.set_font_spec = function(cr, font, source)
|
M.set_font_spec = function(cr, font, source)
|
||||||
|
|
|
@ -33,7 +33,7 @@ local _make = function(point, chars, config, threshold_config, format)
|
||||||
local f = threshold_config.pre_function
|
local f = threshold_config.pre_function
|
||||||
local setter
|
local setter
|
||||||
if f then
|
if f then
|
||||||
setter = function(x) return _setter(f(x)) end
|
setter = pure.compose(_setter, f)
|
||||||
else
|
else
|
||||||
setter = _setter
|
setter = _setter
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,7 +17,6 @@ local make_y_label_text = function(point, chars, font)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- TODO this function smells funny
|
-- TODO this function smells funny
|
||||||
M.make = function(point, h, n, font, y_format, scale_factor)
|
M.make = function(point, h, n, font, y_format, scale_factor)
|
||||||
local y_labels = {width = 0}
|
local y_labels = {width = 0}
|
||||||
|
|
Loading…
Reference in New Issue