REF make things more functional
This commit is contained in:
parent
f4a73a213b
commit
e7c05bb0d7
|
@ -19,6 +19,8 @@ local style = require 'style'
|
||||||
local source = require 'source'
|
local source = require 'source'
|
||||||
local pure = require 'pure'
|
local pure = require 'pure'
|
||||||
|
|
||||||
|
local __string_format = string.format
|
||||||
|
|
||||||
return function(config)
|
return function(config)
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
@ -78,16 +80,12 @@ return function(config)
|
||||||
local _left_text_style = _text_row_style('left', patterns.text.inactive)
|
local _left_text_style = _text_row_style('left', patterns.text.inactive)
|
||||||
local _right_text_style = _text_row_style('right', patterns.text.active)
|
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)
|
local _left_text = function(pt, _text)
|
||||||
return _bare_text(pt, _text, _left_text_style)
|
return text.make_plain(pt, _text, _left_text_style)
|
||||||
end
|
end
|
||||||
|
|
||||||
local _right_text = function(pt, _text)
|
local _right_text = function(pt, _text)
|
||||||
return _bare_text(pt, _text, _right_text_style)
|
return text.make_plain(pt, _text, _right_text_style)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
@ -108,11 +106,11 @@ return function(config)
|
||||||
)
|
)
|
||||||
|
|
||||||
local _format_percent_label = function(_)
|
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
|
end
|
||||||
|
|
||||||
local _format_percent_maybe = function(z)
|
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
|
end
|
||||||
|
|
||||||
local _percent_label_config = timeseries.label_config(
|
local _percent_label_config = timeseries.label_config(
|
||||||
|
@ -265,7 +263,7 @@ return function(config)
|
||||||
else
|
else
|
||||||
num_fmt = '%.0f'
|
num_fmt = '%.0f'
|
||||||
end
|
end
|
||||||
return string.format('%s %s', num_fmt, unit)
|
return __string_format('%s %s', num_fmt, unit)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.converted_y_label_format_generator = function(unit)
|
M.converted_y_label_format_generator = function(unit)
|
||||||
|
@ -274,7 +272,7 @@ return function(config)
|
||||||
local conversion_factor = plot_max / new_max
|
local conversion_factor = plot_max / new_max
|
||||||
local fmt = M.y_label_format_string(new_max, new_prefix..unit..'/s')
|
local fmt = M.y_label_format_string(new_max, new_prefix..unit..'/s')
|
||||||
return function(bytes)
|
return function(bytes)
|
||||||
return string.format(fmt, bytes / conversion_factor)
|
return __string_format(fmt, bytes / conversion_factor)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -325,11 +323,7 @@ return function(config)
|
||||||
local make_differential = function(update_frequency)
|
local make_differential = function(update_frequency)
|
||||||
return function(x0, x1)
|
return function(x0, x1)
|
||||||
-- mask overflow
|
-- mask overflow
|
||||||
if x1 > x0 then
|
return x1 > x0 and (x1 - x0) * update_frequency or 0
|
||||||
return (x1 - x0) * update_frequency
|
|
||||||
else
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -694,7 +688,7 @@ return function(config)
|
||||||
|
|
||||||
local active_blocks = function(blockspecs)
|
local active_blocks = function(blockspecs)
|
||||||
local bs = pure.filter(function(b) return b[2] end, 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
|
end
|
||||||
|
|
||||||
local mk_separator = function(width, x, y)
|
local mk_separator = function(width, x, y)
|
||||||
|
|
|
@ -239,6 +239,10 @@ M.partial = function(f, ...)
|
||||||
return load(src, 'partial_apply', 't', {f = f, args = args})()
|
return load(src, 'partial_apply', 't', {f = f, args = args})()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M.flip = function(f)
|
||||||
|
return function(x, y) return f(y, x) end
|
||||||
|
end
|
||||||
|
|
||||||
M.compose = function(f, ...)
|
M.compose = function(f, ...)
|
||||||
if #{...} == 0 then
|
if #{...} == 0 then
|
||||||
return f
|
return f
|
||||||
|
@ -271,6 +275,9 @@ M.memoize = function(f)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- maybe
|
||||||
|
|
||||||
M.maybe = function(def, f, x)
|
M.maybe = function(def, f, x)
|
||||||
if x == nil then
|
if x == nil then
|
||||||
return def
|
return def
|
||||||
|
|
50
src/sys.lua
50
src/sys.lua
|
@ -81,7 +81,7 @@ end
|
||||||
M.meminfo_field_reader = function(field)
|
M.meminfo_field_reader = function(field)
|
||||||
local pattern = fmt_mem_field(field)
|
local pattern = fmt_mem_field(field)
|
||||||
return function()
|
return function()
|
||||||
return tonumber(i_o.read_file(MEMINFO_PATH, pattern))
|
return __tonumber(i_o.read_file(MEMINFO_PATH, pattern))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -93,9 +93,7 @@ local SYSFS_RAPL = '/sys/class/powercap'
|
||||||
M.intel_powercap_reader = function(dev)
|
M.intel_powercap_reader = function(dev)
|
||||||
local uj = __string_format('%s/%s/energy_uj', SYSFS_RAPL, dev)
|
local uj = __string_format('%s/%s/energy_uj', SYSFS_RAPL, dev)
|
||||||
i_o.assert_file_readable(uj)
|
i_o.assert_file_readable(uj)
|
||||||
return function()
|
return function() return read_micro(uj) end
|
||||||
return read_micro(uj)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -112,9 +110,7 @@ end
|
||||||
M.battery_power_reader = function(battery)
|
M.battery_power_reader = function(battery)
|
||||||
local current = format_power_path(battery, 'current_now')
|
local current = format_power_path(battery, 'current_now')
|
||||||
local voltage = format_power_path(battery, 'voltage_now')
|
local voltage = format_power_path(battery, 'voltage_now')
|
||||||
return function()
|
return function() return read_micro(current) * read_micro(voltage) end
|
||||||
return read_micro(current) * read_micro(voltage)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.battery_status_reader = function(battery)
|
M.battery_status_reader = function(battery)
|
||||||
|
@ -127,9 +123,10 @@ end
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- disk io
|
-- disk io
|
||||||
|
|
||||||
M.get_disk_paths = function(devs)
|
M.get_disk_paths = pure.partial(
|
||||||
return pure.map(pure.partial(string.format, '/sys/block/%s/stat', true), devs)
|
pure.map,
|
||||||
end
|
pure.partial(__string_format, '/sys/block/%s/stat', true)
|
||||||
|
)
|
||||||
|
|
||||||
-- fields 3 and 7 (sectors read and written)
|
-- 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+)'
|
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
|
-- 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
|
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)
|
M.get_total_disk_io = function(paths)
|
||||||
local r = 0
|
local r = 0
|
||||||
local w = 0
|
local w = 0
|
||||||
for i = 1, #paths do
|
for i = 1, #paths do
|
||||||
local _r, _w = M.get_disk_io(paths[i])
|
local _r, _w = __string_match(i_o.read_file(paths[i]), RW_REGEX)
|
||||||
r = r + _r
|
r = r + __tonumber(_r)
|
||||||
w = w + _w
|
w = w + __tonumber(_w)
|
||||||
end
|
end
|
||||||
return r, w
|
return r * BLOCK_SIZE_BYTES, w * BLOCK_SIZE_BYTES
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -162,19 +154,18 @@ end
|
||||||
local NET_DIR = '/sys/class/net'
|
local NET_DIR = '/sys/class/net'
|
||||||
|
|
||||||
local get_interfaces = function()
|
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')
|
local f = pure.partial(gmatch_to_table1, '/([^/\n]+)\n')
|
||||||
return pure.maybe({}, f, i_o.execute_cmd(cmd))
|
return pure.maybe({}, f, i_o.execute_cmd(cmd))
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_net_interface_paths = function()
|
M.get_net_interface_paths = function()
|
||||||
local is = get_interfaces()
|
|
||||||
return pure.map(
|
return pure.map(
|
||||||
function(s)
|
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'}
|
return {rx = dir..'rx_bytes', tx = dir..'tx_bytes'}
|
||||||
end,
|
end,
|
||||||
is
|
get_interfaces()
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -184,11 +175,11 @@ end
|
||||||
-- ASSUME nproc and lscpu will always be available
|
-- ASSUME nproc and lscpu will always be available
|
||||||
|
|
||||||
M.get_core_number = function()
|
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
|
end
|
||||||
|
|
||||||
M.get_cpu_number = function()
|
M.get_cpu_number = function()
|
||||||
return tonumber(i_o.execute_cmd('nproc', nil, '*n'))
|
return __tonumber(i_o.execute_cmd('nproc', nil, '*n'))
|
||||||
end
|
end
|
||||||
|
|
||||||
local get_coretemp_dir = function()
|
local get_coretemp_dir = function()
|
||||||
|
@ -241,14 +232,15 @@ end
|
||||||
M.get_coretemp_paths = function()
|
M.get_coretemp_paths = function()
|
||||||
local get_paths = function(indexer)
|
local get_paths = function(indexer)
|
||||||
local d = get_coretemp_dir()
|
local d = get_coretemp_dir()
|
||||||
local get_labels = function(dir)
|
|
||||||
i_o.assert_exe_exists('grep')
|
i_o.assert_exe_exists('grep')
|
||||||
return i_o.execute_cmd(string.format('grep Core %s/temp*_label', dir))
|
local get_labels = pure.compose(
|
||||||
end
|
i_o.execute_cmd,
|
||||||
|
pure.partial(string.format, 'grep Core %s/temp*_label', true)
|
||||||
|
)
|
||||||
local to_tuple = function(m)
|
local to_tuple = function(m)
|
||||||
return {
|
return {
|
||||||
indexer[tonumber(m[2])],
|
indexer[tonumber(m[2])],
|
||||||
string.format('%s/%s_input', d, m[1])
|
__string_format('%s/%s_input', d, m[1])
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
local f = pure.compose(
|
local f = pure.compose(
|
||||||
|
|
Loading…
Reference in New Issue