From 15f4ff3a389cce34f2134c4b65414b1204a0f43f Mon Sep 17 00:00:00 2001 From: zachir Date: Sun, 19 Nov 2023 15:20:14 -0600 Subject: Improve/fix logic for using bit or byte in net-speed There is now an argument (bits_not_bytes) that will change the unit for calculating net speed. Setting it to "true", the widget will use bits instead of bytes. Setting it to "false" (or not setting it), the widget will use bytes. --- net-speed-widget/net-speed.lua | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/net-speed-widget/net-speed.lua b/net-speed-widget/net-speed.lua index d67d86a..dd724a3 100644 --- a/net-speed-widget/net-speed.lua +++ b/net-speed-widget/net-speed.lua @@ -17,25 +17,32 @@ local ICONS_DIR = WIDGET_DIR .. 'icons/' local net_speed_widget = {} -local function convert_to_h(bytes) +local function convert_to_h(bytes, bits_not_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' + if bits_not_bytes then + units = bits + unitsuffix = 'bps' else - speed = tonumber(bits) - dim = 'b/s' + units = bytes + unitsuffix = 'Bps' + end + if units < 1000 then + speed = units + dim = unitsuffix + elseif units < 1000000 then + speed = units/1000 + dim = 'K' .. unitsuffix + elseif units < 1000000000 then + speed = units/1000000 + dim = 'M' .. unitsuffix + elseif units < 1000000000000 then + speed = units/1000000000 + dim = 'G' .. unitsuffix + else + speed = tonumber(units) + dim = unitsuffix end return math.floor(speed + 0.5) .. dim end @@ -59,6 +66,7 @@ local function worker(user_args) local interface = args.interface or '*' local timeout = args.timeout or 1 local width = args.width or 100 + local bits_not_bytes = args.bits_not_bytes net_speed_widget = wibox.widget { { @@ -108,8 +116,8 @@ local function worker(user_args) local speed_rx = (cur_rx - prev_rx) / timeout local speed_tx = (cur_tx - prev_tx) / timeout - widget:set_rx_text(convert_to_h(speed_rx)) - widget:set_tx_text(convert_to_h(speed_tx)) + widget:set_rx_text(convert_to_h(speed_rx), bits_not_bytes) + widget:set_tx_text(convert_to_h(speed_tx), bits_not_bytes) prev_rx = cur_rx prev_tx = cur_tx -- cgit v1.2.3