diff --git a/src/modules/system.lua b/src/modules/system.lua index ddda7c1..e0e7e3f 100644 --- a/src/modules/system.lua +++ b/src/modules/system.lua @@ -2,17 +2,52 @@ local i_o = require 'i_o' local pure = require 'pure' return function(main_state, config, common, width, point) + local IP_EXE = 'ip' + local TEST_IP = '104.18.114.97' -- ipv4.icanhazip.com + local IP_REGEX = 'via ([^ ]+)' + local DEV_REGEX = 'dev ([^ ]+)' + local ERR = 'Error' + local text_spacing = config.geometry.text_spacing local __string_match = string.match + i_o.assert_exe_exists(IP_EXE) + + -- NOTE conky has a builtin gateway function but it will only output + -- whatever the output of 'ip route' is, which won't pick up changes that + -- some VPNs (like tailscale) will make to the rest of the routing table. + -- More reliable way to test the "default route" is to ask the routing table + -- how it would route to a public IP, which is what we do here. + local ip_cmd = IP_EXE..' route get '..TEST_IP + + local get_gateway = function() + local ip_glob = i_o.execute_cmd(ip_cmd) + if ip_glob == nil then + return ERR + else + local ip = __string_match(ip_glob, IP_REGEX) + if ip ~= nil then + return ip + end + + local dev = __string_match(ip_glob, DEV_REGEX) + if dev ~= nil then + return dev + end + + return ERR + end + end + local mk_stats = function(y) local obj = common.make_text_rows( point.x, y, width, text_spacing, - {'Kernel', 'Uptime', 'Last Upgrade', 'Last Sync'} + -- {'Kernel', 'Uptime', 'Last Upgrade', 'Last Sync', 'Gateway'} + {'Kernel', 'Uptime', 'Last Upgrade', 'Gateway'} ) -- just update this once common.text_rows_set(obj, 1, i_o.conky('$kernel')) @@ -25,11 +60,12 @@ return function(main_state, config, common, width, point) "^%d+%s+([^%s]+)%s+([^%s]+).*" ) common.text_rows_set(obj, 3, last_update) - common.text_rows_set(obj, 4, last_sync) + -- common.text_rows_set(obj, 4, last_sync) else common.text_rows_set(obj, 3, 'N/A') - common.text_rows_set(obj, 4, 'N/A') + -- common.text_rows_set(obj, 4, 'N/A') end + common.text_rows_set(obj, 4, get_gateway()) end local static = pure.partial(common.text_rows_draw_static, obj) local dynamic = pure.partial(common.text_rows_draw_dynamic, obj) diff --git a/src/sys.lua b/src/sys.lua index 8788af6..f5d436e 100644 --- a/src/sys.lua +++ b/src/sys.lua @@ -274,7 +274,7 @@ M.get_core_topology = function() pure.partial(gmatch_to_tableN, '(%d+),(%d+)') ) local out = - i_o.execute_cmd('lscpu -y -p=core,cpu | grep -v \'^#\' | sort -k1,1n') + i_o.execute_cmd('lscpu -y -p=core,cpu | grep -v \'^#\' | sort -k1,1n -t,') return pure.fmap_maybe(f, out) end