REF make things more functional

This commit is contained in:
Nathan Dwarshuis 2022-07-23 22:55:30 -04:00
parent f4a73a213b
commit e7c05bb0d7
3 changed files with 39 additions and 46 deletions

View File

@ -19,6 +19,8 @@ local style = require 'style'
local source = require 'source'
local pure = require 'pure'
local __string_format = string.format
return function(config)
local M = {}
@ -78,16 +80,12 @@ return function(config)
local _left_text_style = _text_row_style('left', patterns.text.inactive)
local _right_text_style = _text_row_style('right', patterns.text.active)
local _bare_text = function(pt, _text, _style)
return text.make_plain(pt, _text, _style)
end
local _left_text = function(pt, _text)
return _bare_text(pt, _text, _left_text_style)
return text.make_plain(pt, _text, _left_text_style)
end
local _right_text = function(pt, _text)
return _bare_text(pt, _text, _right_text_style)
return text.make_plain(pt, _text, _right_text_style)
end
-----------------------------------------------------------------------------
@ -108,11 +106,11 @@ return function(config)
)
local _format_percent_label = function(_)
return function(z) return string.format('%i%%', math.floor(z * 100)) end
return function(z) return __string_format('%i%%', math.floor(z * 100)) end
end
local _format_percent_maybe = function(z)
if z == -1 then return 'N/A' else return string.format('%s%%', z) end
return z == -1 and 'N/A' or __string_format('%s%%', z)
end
local _percent_label_config = timeseries.label_config(
@ -265,7 +263,7 @@ return function(config)
else
num_fmt = '%.0f'
end
return string.format('%s %s', num_fmt, unit)
return __string_format('%s %s', num_fmt, unit)
end
M.converted_y_label_format_generator = function(unit)
@ -274,7 +272,7 @@ return function(config)
local conversion_factor = plot_max / new_max
local fmt = M.y_label_format_string(new_max, new_prefix..unit..'/s')
return function(bytes)
return string.format(fmt, bytes / conversion_factor)
return __string_format(fmt, bytes / conversion_factor)
end
end
end
@ -325,11 +323,7 @@ return function(config)
local make_differential = function(update_frequency)
return function(x0, x1)
-- mask overflow
if x1 > x0 then
return (x1 - x0) * update_frequency
else
return 0
end
return x1 > x0 and (x1 - x0) * update_frequency or 0
end
end
@ -694,7 +688,7 @@ return function(config)
local active_blocks = function(blockspecs)
local bs = pure.filter(function(b) return b[2] end, blockspecs)
return pure.map(function(b) return mk_block(table.unpack(b)) end, bs)
return pure.map(pure.compose(mk_block, table.unpack), bs)
end
local mk_separator = function(width, x, y)

View File

@ -239,6 +239,10 @@ M.partial = function(f, ...)
return load(src, 'partial_apply', 't', {f = f, args = args})()
end
M.flip = function(f)
return function(x, y) return f(y, x) end
end
M.compose = function(f, ...)
if #{...} == 0 then
return f
@ -271,6 +275,9 @@ M.memoize = function(f)
end
end
--------------------------------------------------------------------------------
-- maybe
M.maybe = function(def, f, x)
if x == nil then
return def

View File

@ -81,7 +81,7 @@ end
M.meminfo_field_reader = function(field)
local pattern = fmt_mem_field(field)
return function()
return tonumber(i_o.read_file(MEMINFO_PATH, pattern))
return __tonumber(i_o.read_file(MEMINFO_PATH, pattern))
end
end
@ -93,9 +93,7 @@ local SYSFS_RAPL = '/sys/class/powercap'
M.intel_powercap_reader = function(dev)
local uj = __string_format('%s/%s/energy_uj', SYSFS_RAPL, dev)
i_o.assert_file_readable(uj)
return function()
return read_micro(uj)
end
return function() return read_micro(uj) end
end
--------------------------------------------------------------------------------
@ -112,9 +110,7 @@ end
M.battery_power_reader = function(battery)
local current = format_power_path(battery, 'current_now')
local voltage = format_power_path(battery, 'voltage_now')
return function()
return read_micro(current) * read_micro(voltage)
end
return function() return read_micro(current) * read_micro(voltage) end
end
M.battery_status_reader = function(battery)
@ -127,9 +123,10 @@ end
--------------------------------------------------------------------------------
-- disk io
M.get_disk_paths = function(devs)
return pure.map(pure.partial(string.format, '/sys/block/%s/stat', true), devs)
end
M.get_disk_paths = pure.partial(
pure.map,
pure.partial(__string_format, '/sys/block/%s/stat', true)
)
-- fields 3 and 7 (sectors read and written)
local RW_REGEX = '%s+%d+%s+%d+%s+(%d+)%s+%d+%s+%d+%s+%d+%s+(%d+)'
@ -138,20 +135,15 @@ local RW_REGEX = '%s+%d+%s+%d+%s+(%d+)%s+%d+%s+%d+%s+%d+%s+(%d+)'
-- see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/types.h?id=v4.4-rc6#n121
local BLOCK_SIZE_BYTES = 512
M.get_disk_io = function(path)
local r, w = __string_match(i_o.read_file(path), RW_REGEX)
return __tonumber(r) * BLOCK_SIZE_BYTES, __tonumber(w) * BLOCK_SIZE_BYTES
end
M.get_total_disk_io = function(paths)
local r = 0
local w = 0
for i = 1, #paths do
local _r, _w = M.get_disk_io(paths[i])
r = r + _r
w = w + _w
local _r, _w = __string_match(i_o.read_file(paths[i]), RW_REGEX)
r = r + __tonumber(_r)
w = w + __tonumber(_w)
end
return r, w
return r * BLOCK_SIZE_BYTES, w * BLOCK_SIZE_BYTES
end
--------------------------------------------------------------------------------
@ -162,19 +154,18 @@ end
local NET_DIR = '/sys/class/net'
local get_interfaces = function()
local cmd = string.format('realpath %s/* | grep -v virtual', NET_DIR)
local cmd = __string_format('realpath %s/* | grep -v virtual', NET_DIR)
local f = pure.partial(gmatch_to_table1, '/([^/\n]+)\n')
return pure.maybe({}, f, i_o.execute_cmd(cmd))
end
M.get_net_interface_paths = function()
local is = get_interfaces()
return pure.map(
function(s)
local dir = string.format('%s/%s/statistics/', NET_DIR, s)
local dir = __string_format('%s/%s/statistics/', NET_DIR, s)
return {rx = dir..'rx_bytes', tx = dir..'tx_bytes'}
end,
is
get_interfaces()
)
end
@ -184,11 +175,11 @@ end
-- ASSUME nproc and lscpu will always be available
M.get_core_number = function()
return tonumber(i_o.read_file('/proc/cpuinfo', 'cpu cores%s+:%s(%d+)'))
return __tonumber(i_o.read_file('/proc/cpuinfo', 'cpu cores%s+:%s(%d+)'))
end
M.get_cpu_number = function()
return tonumber(i_o.execute_cmd('nproc', nil, '*n'))
return __tonumber(i_o.execute_cmd('nproc', nil, '*n'))
end
local get_coretemp_dir = function()
@ -241,14 +232,15 @@ end
M.get_coretemp_paths = function()
local get_paths = function(indexer)
local d = get_coretemp_dir()
local get_labels = function(dir)
i_o.assert_exe_exists('grep')
return i_o.execute_cmd(string.format('grep Core %s/temp*_label', dir))
end
i_o.assert_exe_exists('grep')
local get_labels = pure.compose(
i_o.execute_cmd,
pure.partial(string.format, 'grep Core %s/temp*_label', true)
)
local to_tuple = function(m)
return {
indexer[tonumber(m[2])],
string.format('%s/%s_input', d, m[1])
__string_format('%s/%s_input', d, m[1])
}
end
local f = pure.compose(