diff options
author | streetturtle <streetturtle@gmail.com> | 2020-07-20 22:30:41 -0400 |
---|---|---|
committer | streetturtle <streetturtle@gmail.com> | 2020-07-20 22:30:41 -0400 |
commit | caf0879a15a11442de585a53bc04929b4e9fec72 (patch) | |
tree | 687a2ede43796c0fdb3ce0e0c282eaa2b7e521af /net-speed-widget | |
parent | 3e189049e1f53c02184f1e63354ba7318a63418d (diff) |
[net-speed-widget] init commit
Diffstat (limited to 'net-speed-widget')
-rw-r--r-- | net-speed-widget/README.md | 3 | ||||
-rw-r--r-- | net-speed-widget/icons/down.svg | 10 | ||||
-rw-r--r-- | net-speed-widget/icons/up.svg | 10 | ||||
-rw-r--r-- | net-speed-widget/net-speed.lua | 128 |
4 files changed, 151 insertions, 0 deletions
diff --git a/net-speed-widget/README.md b/net-speed-widget/README.md new file mode 100644 index 0000000..1cac674 --- /dev/null +++ b/net-speed-widget/README.md @@ -0,0 +1,3 @@ +# Net Speed Widget + +The widget and readme is in progress
\ No newline at end of file diff --git a/net-speed-widget/icons/down.svg b/net-speed-widget/icons/down.svg new file mode 100644 index 0000000..9a98f39 --- /dev/null +++ b/net-speed-widget/icons/down.svg @@ -0,0 +1,10 @@ +<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" version="1.1"> + <defs> + <style id="current-color-scheme" type="text/css"> + .ColorScheme-Text { color:#dfdfdf; } .ColorScheme-Highlight { color:#4285f4; } + </style> + </defs> + <g transform="matrix(1,0,0,1,4,4)"> + <path class="ColorScheme-Text" d="M 7,2 V 10 L 3.5,6.5 2,8 8,14 14,8 12.5,6.5 9,10 V 2 Z" style="fill:currentColor"/> + </g> +</svg> diff --git a/net-speed-widget/icons/up.svg b/net-speed-widget/icons/up.svg new file mode 100644 index 0000000..e3c12a7 --- /dev/null +++ b/net-speed-widget/icons/up.svg @@ -0,0 +1,10 @@ +<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" version="1.1"> + <defs> + <style id="current-color-scheme" type="text/css"> + .ColorScheme-Text { color:#dfdfdf; } .ColorScheme-Highlight { color:#4285f4; } + </style> + </defs> + <g transform="matrix(1,0,0,1,4,4)"> + <path class="ColorScheme-Text" d="M 7,14 V 6 L 3.5,9.5 2,8 8,2 14,8 12.5,9.5 9,6 V 14 Z" style="fill:currentColor"/> + </g> +</svg> diff --git a/net-speed-widget/net-speed.lua b/net-speed-widget/net-speed.lua new file mode 100644 index 0000000..d0eaaff --- /dev/null +++ b/net-speed-widget/net-speed.lua @@ -0,0 +1,128 @@ +------------------------------------------------- +-- 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 naughty = require("naughty") +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/' + +local function show_warning(message) + naughty.notify { + preset = naughty.config.presets.critical, + title = 'Net Speed Widget', + text = message + } +end + +local net_speed_widget = wibox.widget { + { + id = 'rx_speed', + forced_width = 55, + 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 = 55, + 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 +} + +local prev_rx = 0 +local prev_tx = 0 + +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 + +local function worker(args) + + local args = args or {} + + if args.interface == nil then + show_warning("Interface name is not specified") + return + end + + local interface = args.interface + + local update_widget = function(widget, stdout, stderr) + + local cur_vals = split(stdout, '\r\n') + + local cur_rx = cur_vals[1] + local cur_tx = cur_vals[2] + + local speed_rx = cur_rx - prev_rx + local speed_tx = cur_tx - prev_tx + + net_speed_widget:set_rx_text(convert_to_h(speed_rx)) + net_speed_widget:set_tx_text(convert_to_h(speed_tx)) + + prev_rx = cur_rx + prev_tx = cur_tx + end + + watch(string.format([[bash -c "cat /sys/class/net/%s/statistics/*_bytes"]], interface), 1, update_widget, net_speed_widget) + + return net_speed_widget + +end + +return setmetatable(net_speed_widget, { __call = function(_, ...) return worker(...) end }) |