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 NVIDIA_EXE = 'nvidia-settings'
|
||||
local __string_match = string.match
|
||||
local __string_format = string.format
|
||||
local __tonumber = tonumber
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
@ -112,18 +113,9 @@ return function(update_freq, config, common, width, point)
|
|||
-- gpu status
|
||||
|
||||
local mk_status = function(y)
|
||||
local obj = common.make_text_row(
|
||||
point.x,
|
||||
y,
|
||||
width,
|
||||
'Status'
|
||||
)
|
||||
local obj = common.make_text_row(point.x, y, width, 'Status')
|
||||
local update = function()
|
||||
if mod_state.error == false then
|
||||
common.text_row_set(obj, 'On')
|
||||
else
|
||||
common.text_row_set(obj, mod_state.error)
|
||||
end
|
||||
common.text_row_set(obj, mod_state.error == false and 'On' or mod_state.error)
|
||||
end
|
||||
local static = pure.partial(common.text_row_draw_static, obj)
|
||||
local dynamic = pure.partial(common.text_row_draw_dynamic, obj)
|
||||
|
@ -140,13 +132,13 @@ return function(update_freq, config, common, width, point)
|
|||
width,
|
||||
'Internal Temperature',
|
||||
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,
|
||||
80
|
||||
)
|
||||
local update = _from_state(
|
||||
-1,
|
||||
function(s) return __tonumber(s.temp_reading) end,
|
||||
pure.compose(__tonumber, pure.getter("temp_reading")),
|
||||
pure.partial(common.threshold_text_row_set, 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(
|
||||
_mk_plot,
|
||||
'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(
|
||||
_mk_plot,
|
||||
'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 __math_floor = math.floor
|
||||
local __string_format = string.format
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- state
|
||||
|
@ -59,9 +60,7 @@ return function(update_freq, config, common, width, point)
|
|||
CACHE_X = MEM_X + CACHE_X_OFFSET + DIAL_DIAMETER / 2
|
||||
end
|
||||
local CACHE_WIDTH = point.x + width - CACHE_X
|
||||
local format_percent = function(x)
|
||||
return string.format('%i%%', x)
|
||||
end
|
||||
local format_percent = pure.partial(__string_format, '%i%%', true)
|
||||
|
||||
-- memory bits (used no matter what)
|
||||
local mem = common.make_dial(
|
||||
|
@ -188,13 +187,7 @@ return function(update_freq, config, common, width, point)
|
|||
}
|
||||
end,
|
||||
num_rows)
|
||||
local obj = common.make_text_table(
|
||||
point.x,
|
||||
y,
|
||||
width,
|
||||
num_rows,
|
||||
'Mem (%)'
|
||||
)
|
||||
local obj = common.make_text_table(point.x, y, width, num_rows, 'Mem (%)')
|
||||
local update = function()
|
||||
for r = 1, num_rows do
|
||||
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)
|
||||
if watts == 0 then
|
||||
return "A/C"
|
||||
else
|
||||
return format_rapl(watts)
|
||||
end
|
||||
return watts == 0 and "A/C" or format_rapl(watts)
|
||||
end
|
||||
|
||||
local mk_bat = function(y)
|
||||
local _read_battery_power = sys.battery_power_reader(config.battery)
|
||||
|
||||
local read_battery_power = function(is_using_ac)
|
||||
if is_using_ac then
|
||||
return 0
|
||||
else
|
||||
return _read_battery_power()
|
||||
end
|
||||
return is_using_ac and 0 or _read_battery_power()
|
||||
end
|
||||
local read_bat_status = sys.battery_status_reader(config.battery)
|
||||
local obj = common.make_tagged_scaled_timeseries(
|
||||
|
@ -104,7 +96,7 @@ return function(update_freq, config, common, width, point)
|
|||
|
||||
-----------------------------------------------------------------------------
|
||||
-- main functions
|
||||
|
||||
|
||||
return {
|
||||
header = 'POWER',
|
||||
point = point,
|
||||
|
|
|
@ -47,11 +47,20 @@ return function(update_freq, config, main_state, common, width, point)
|
|||
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 {
|
||||
loads = common.make_compound_dial(
|
||||
x,
|
||||
y,
|
||||
dial_x,
|
||||
dial_y,
|
||||
DIAL_OUTER_RADIUS,
|
||||
DIAL_INNER_RADIUS,
|
||||
DIAL_THICKNESS,
|
||||
|
@ -59,8 +68,8 @@ return function(update_freq, config, main_state, common, width, point)
|
|||
nthreads
|
||||
),
|
||||
coretemp = common.make_text_circle(
|
||||
x,
|
||||
y,
|
||||
dial_x,
|
||||
dial_y,
|
||||
DIAL_INNER_RADIUS - 2,
|
||||
'%s°C',
|
||||
80,
|
||||
|
@ -71,19 +80,7 @@ return function(update_freq, config, main_state, common, width, point)
|
|||
|
||||
local mk_cores = function(y)
|
||||
local core_cols = ncores / config.core_rows
|
||||
local cores = {}
|
||||
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 cores = pure.map_n(pure.partial(create_core, core_cols, y), ncores)
|
||||
local coretemp_paths = cpu.get_coretemp_paths()
|
||||
if #coretemp_paths ~= ncores then
|
||||
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
|
||||
|
||||
-- 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)
|
||||
local r = {}
|
||||
for k, v in pairs(tbl) do
|
||||
|
@ -283,6 +292,8 @@ M.memoize = function(f)
|
|||
end
|
||||
end
|
||||
|
||||
M.id = function(x) return x end
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- maybe
|
||||
|
||||
|
|
|
@ -42,9 +42,7 @@ M.make_setter = function(_arc, thickness, threshold_config)
|
|||
)
|
||||
end
|
||||
)
|
||||
return function(percent)
|
||||
return f(pure.round_percent(percent))
|
||||
end
|
||||
return pure.compose(f, pure.round_percent)
|
||||
end
|
||||
|
||||
M.make = function(_arc, bg_config, fg_threshold_config)
|
||||
|
|
|
@ -36,9 +36,7 @@ M.make_setter = function(_line, config, threshold_config)
|
|||
)
|
||||
end
|
||||
)
|
||||
return function(percent)
|
||||
return f(pure.round_percent(percent))
|
||||
end
|
||||
return pure.compose(f, pure.round_percent)
|
||||
end
|
||||
|
||||
M.make = function(p1, p2, bg_config, fg_threshold_config)
|
||||
|
|
|
@ -2,6 +2,7 @@ local M = {}
|
|||
|
||||
local err = require 'err'
|
||||
local geom = require 'geom'
|
||||
local pure = require 'pure'
|
||||
|
||||
local __string_sub = string.sub
|
||||
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_show_text = cairo_show_text
|
||||
|
||||
local __string_format = string.format
|
||||
|
||||
M.NULL_TEXT_STRING = '<null>'
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- pure
|
||||
|
||||
local trim_to_length = function(text, len)
|
||||
local trim_to_length = function(len, text)
|
||||
if #text > len then
|
||||
return __string_sub(text, 1, len)..'...'
|
||||
else
|
||||
|
@ -30,11 +33,11 @@ M.make_format_function = function(format)
|
|||
if type(format) == "function" then
|
||||
return format
|
||||
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
|
||||
return function(_text) return string.format(format, _text) end
|
||||
return pure.partial(__string_format, format, true)
|
||||
elseif format == nil or format == false then
|
||||
return function(_text) return _text end
|
||||
return pure.id
|
||||
else
|
||||
local msg = "format must be a printf string, positive int, or function: got "
|
||||
local t = type(format)
|
||||
|
@ -106,37 +109,31 @@ M.font_height = function(font)
|
|||
end
|
||||
|
||||
M.x_align_function = function(x_align, font)
|
||||
if x_align == 'left' then
|
||||
return function(text)
|
||||
local funs = {
|
||||
left = function(text)
|
||||
local te = set_text_extents(text, font)
|
||||
return -te.x_bearing
|
||||
end
|
||||
elseif x_align == 'center' then
|
||||
return function(text)
|
||||
end,
|
||||
center = function(text)
|
||||
local te = set_text_extents(text, font)
|
||||
return -(te.x_bearing + te.width * 0.5)
|
||||
end
|
||||
elseif x_align == 'right' then
|
||||
return function(text)
|
||||
end,
|
||||
right = function(text)
|
||||
local te = set_text_extents(text, font)
|
||||
return -(te.x_bearing + te.width)
|
||||
end
|
||||
else
|
||||
err.assert_trace(nil, "invalid x_align")
|
||||
end
|
||||
}
|
||||
return funs[x_align] or err.assert_trace(nil, "invalid x_align")
|
||||
end
|
||||
|
||||
M.get_delta_y = function(y_align, font)
|
||||
local fe = set_font_extents(font)
|
||||
if y_align == 'bottom' then
|
||||
return -fe.descent
|
||||
elseif y_align == 'top' then
|
||||
return fe.height
|
||||
elseif y_align == 'center' then
|
||||
return 0.92 * fe.height * 0.5 - fe.descent
|
||||
else
|
||||
err.assert_trace(nil, "invalid y_align")
|
||||
end
|
||||
local descents = {
|
||||
top = fe.height,
|
||||
center = 0.92 * fe.height * 0.5 - fe.descent,
|
||||
bottom = -fe.descent
|
||||
}
|
||||
return descents[y_align] or err.assert_trace(nil, "invalid y_align")
|
||||
end
|
||||
|
||||
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 setter
|
||||
if f then
|
||||
setter = function(x) return _setter(f(x)) end
|
||||
setter = pure.compose(_setter, f)
|
||||
else
|
||||
setter = _setter
|
||||
end
|
||||
|
|
|
@ -17,7 +17,6 @@ local make_y_label_text = function(point, chars, font)
|
|||
)
|
||||
end
|
||||
|
||||
|
||||
-- TODO this function smells funny
|
||||
M.make = function(point, h, n, font, y_format, scale_factor)
|
||||
local y_labels = {width = 0}
|
||||
|
|
Loading…
Reference in New Issue