2020-07-21 04:30:41 +02:00
|
|
|
-------------------------------------------------
|
|
|
|
-- Net Speed Widget for Awesome Window Manager
|
|
|
|
-- Shows current upload/download speed
|
|
|
|
-- More details could be found here:
|
|
|
|
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/net-speed-widget
|
|
|
|
|
|
|
|
-- @author Pavel Makhov
|
|
|
|
-- @copyright 2020 Pavel Makhov
|
|
|
|
-------------------------------------------------
|
|
|
|
|
|
|
|
local watch = require("awful.widget.watch")
|
|
|
|
local wibox = require("wibox")
|
|
|
|
|
|
|
|
local HOME_DIR = os.getenv("HOME")
|
|
|
|
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/net-speed-widget/'
|
|
|
|
local ICONS_DIR = WIDGET_DIR .. 'icons/'
|
|
|
|
|
2021-05-22 11:59:51 +02:00
|
|
|
local net_speed_widget = {}
|
2020-07-21 04:30:41 +02:00
|
|
|
|
|
|
|
local function convert_to_h(bytes)
|
|
|
|
local speed
|
|
|
|
local dim
|
|
|
|
local bits = bytes * 8
|
|
|
|
if bits < 1000 then
|
|
|
|
speed = bits
|
|
|
|
dim = 'b/s'
|
|
|
|
elseif bits < 1000000 then
|
|
|
|
speed = bits/1000
|
|
|
|
dim = 'kb/s'
|
|
|
|
elseif bits < 1000000000 then
|
|
|
|
speed = bits/1000000
|
|
|
|
dim = 'mb/s'
|
|
|
|
elseif bits < 1000000000000 then
|
|
|
|
speed = bits/1000000000
|
|
|
|
dim = 'gb/s'
|
|
|
|
else
|
|
|
|
speed = tonumber(bits)
|
|
|
|
dim = 'b/s'
|
|
|
|
end
|
|
|
|
return math.floor(speed + 0.5) .. dim
|
|
|
|
end
|
|
|
|
|
|
|
|
local function split(string_to_split, separator)
|
|
|
|
if separator == nil then separator = "%s" end
|
|
|
|
local t = {}
|
|
|
|
|
|
|
|
for str in string.gmatch(string_to_split, "([^".. separator .."]+)") do
|
|
|
|
table.insert(t, str)
|
|
|
|
end
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2020-12-06 02:57:04 +01:00
|
|
|
local function worker(user_args)
|
2020-07-21 04:30:41 +02:00
|
|
|
|
2020-12-06 02:57:04 +01:00
|
|
|
local args = user_args or {}
|
2020-07-21 04:30:41 +02:00
|
|
|
|
2020-08-04 03:25:07 +02:00
|
|
|
local interface = args.interface or '*'
|
2020-09-19 10:08:15 +02:00
|
|
|
local timeout = args.timeout or 1
|
2021-05-22 11:59:51 +02:00
|
|
|
local width = args.width or 55
|
|
|
|
|
2021-06-04 02:57:48 +02:00
|
|
|
net_speed_widget = wibox.widget {
|
2021-05-22 11:59:51 +02:00
|
|
|
{
|
|
|
|
id = 'rx_speed',
|
|
|
|
forced_width = width,
|
|
|
|
align = 'right',
|
|
|
|
widget = wibox.widget.textbox
|
|
|
|
},
|
|
|
|
{
|
|
|
|
image = ICONS_DIR .. 'down.svg',
|
|
|
|
widget = wibox.widget.imagebox
|
|
|
|
},
|
|
|
|
{
|
|
|
|
image = ICONS_DIR .. 'up.svg',
|
|
|
|
widget = wibox.widget.imagebox
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id = 'tx_speed',
|
|
|
|
forced_width = width,
|
|
|
|
align = 'left',
|
|
|
|
widget = wibox.widget.textbox
|
|
|
|
},
|
|
|
|
layout = wibox.layout.fixed.horizontal,
|
|
|
|
set_rx_text = function(self, new_rx_speed)
|
|
|
|
self:get_children_by_id('rx_speed')[1]:set_text(tostring(new_rx_speed))
|
|
|
|
end,
|
|
|
|
set_tx_text = function(self, new_tx_speed)
|
|
|
|
self:get_children_by_id('tx_speed')[1]:set_text(tostring(new_tx_speed))
|
|
|
|
end
|
|
|
|
}
|
2020-07-21 04:30:41 +02:00
|
|
|
|
2021-06-06 03:26:13 +02:00
|
|
|
-- make sure these are not shared across different worker/widgets (e.g. two monitors)
|
|
|
|
-- otherwise the speed will be randomly split among the worker in each monitor
|
|
|
|
local prev_rx = 0
|
|
|
|
local prev_tx = 0
|
|
|
|
|
2020-12-06 02:57:04 +01:00
|
|
|
local update_widget = function(widget, stdout)
|
2020-07-21 04:30:41 +02:00
|
|
|
|
|
|
|
local cur_vals = split(stdout, '\r\n')
|
|
|
|
|
2020-08-04 03:25:07 +02:00
|
|
|
local cur_rx = 0
|
|
|
|
local cur_tx = 0
|
|
|
|
|
2021-06-06 03:26:13 +02:00
|
|
|
for i, v in ipairs(cur_vals) do
|
|
|
|
if i%2 == 1 then cur_rx = cur_rx + v end
|
|
|
|
if i%2 == 0 then cur_tx = cur_tx + v end
|
2020-08-04 03:25:07 +02:00
|
|
|
end
|
|
|
|
|
2021-06-06 03:24:32 +02:00
|
|
|
local speed_rx = (cur_rx - prev_rx) / timeout
|
|
|
|
local speed_tx = (cur_tx - prev_tx) / timeout
|
2020-07-21 04:30:41 +02:00
|
|
|
|
2020-08-04 03:25:07 +02:00
|
|
|
widget:set_rx_text(convert_to_h(speed_rx))
|
|
|
|
widget:set_tx_text(convert_to_h(speed_tx))
|
2020-07-21 04:30:41 +02:00
|
|
|
|
|
|
|
prev_rx = cur_rx
|
|
|
|
prev_tx = cur_tx
|
|
|
|
end
|
|
|
|
|
2020-12-06 02:57:04 +01:00
|
|
|
watch(string.format([[bash -c "cat /sys/class/net/%s/statistics/*_bytes"]], interface),
|
|
|
|
timeout, update_widget, net_speed_widget)
|
2020-07-21 04:30:41 +02:00
|
|
|
|
|
|
|
return net_speed_widget
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(net_speed_widget, { __call = function(_, ...) return worker(...) end })
|