summaryrefslogtreecommitdiff
path: root/net-speed-widget/net-speed.lua
blob: dd724a3619e5829d788a30d73db483d12107878c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
-------------------------------------------------
-- 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/'

local net_speed_widget = {}

local function convert_to_h(bytes, bits_not_bytes)
    local speed
    local dim
    local bits = bytes * 8
    if bits_not_bytes then
        units = bits
        unitsuffix = 'bps'
    else
        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

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(user_args)

    local args = user_args or {}

    local font = args.font or beautiful.font
    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 {
        {
            id = 'rx_speed',
            forced_width = width,
            align = 'right',
            font = font,
            widget = wibox.widget.textbox
        },
        {
            text = "⬇️⬆️",
            widget = wibox.widget.textbox
        },
        {
            id = 'tx_speed',
            forced_width = width,
            align = 'left',
            font = font,
            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
    }

    -- 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

    local update_widget = function(widget, stdout)

        local cur_vals = split(stdout, '\r\n')

        local cur_rx = 0
        local cur_tx = 0

        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
        end

        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), bits_not_bytes)
        widget:set_tx_text(convert_to_h(speed_tx), bits_not_bytes)

        prev_rx = cur_rx
        prev_tx = cur_tx
    end

    watch(string.format([[bash -c "cat /sys/class/net/%s/statistics/*_bytes"]], interface),
        timeout, update_widget, net_speed_widget)

    return net_speed_widget

end

return setmetatable(net_speed_widget, { __call = function(_, ...) return worker(...) end })