All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
6 * (c) 2010-2012, Peter Hofmann
10 local helpers = require("lain.helpers")
11 local naughty = require("naughty")
12 local wibox = require("wibox")
14 local string = { format = string.format,
16 match = string.match }
18 local setmetatable = setmetatable
23 local function worker(args)
24 local net = { last_t = 0, last_r = 0 }
26 function net.get_first_device()
27 local ws = helpers.read_pipe("ip link show | cut -d' ' -f2,9")
28 ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN")
29 if ws then return { ws:match("(%w+):") }
33 local args = args or {}
34 local timeout = args.timeout or 2
35 local units = args.units or 1024 --kb
36 local notify = args.notify or "on"
37 local screen = args.screen or 1
38 local settings = args.settings or function() end
39 local iface = args.iface or net.get_first_device()
41 net.widget = wibox.widget.textbox('')
43 -- Compatibility with old API where iface was a string corresponding to 1 interface
44 if type(iface) == "string" then
50 -- Mark all devices as initially online/active
51 for i, dev in ipairs(iftable) do
52 helpers.set_map(dev, true)
56 -- These are the totals over all specified interfaces
62 -- Total bytes transfered
66 for i, dev in ipairs(iftable) do
68 local dev_before = net_now[dev] or net
70 dev_now.carrier = helpers.first_line(string.format('/sys/class/net/%s/carrier', dev)) or '0'
71 dev_now.state = helpers.first_line(string.format('/sys/class/net/%s/operstate', dev)) or 'down'
73 local now_t = tonumber(helpers.first_line(string.format('/sys/class/net/%s/statistics/tx_bytes', dev)) or 0)
74 local now_r = tonumber(helpers.first_line(string.format('/sys/class/net/%s/statistics/rx_bytes', dev)) or 0)
76 if now_t ~= dev_before.last_t or now_r ~= dev_before.last_r then
77 dev_now.sent = (now_t - (dev_before.last_t or 0)) / timeout / units
78 net_now.sent = net_now.sent + dev_now.sent
79 dev_now.sent = string.gsub(string.format('%.1f', dev_now.sent), ',', '.')
80 dev_now.received = (now_r - (dev_before.last_r or 0)) / timeout / units
81 net_now.received = net_now.received + dev_now.received
82 dev_now.received = string.gsub(string.format('%.1f', dev_now.received), ',', '.')
85 total_t = total_t + now_t
86 total_r = total_r + now_r
88 net_now[dev] = dev_now
90 if string.match(dev_now.carrier, "0") and notify == "on" and helpers.get_map(dev) then
94 icon = helpers.icons_dir .. "no_net.png",
97 helpers.set_map(dev, false)
99 helpers.set_map(dev, true)
103 if total_t ~= net.last_t or total_r ~= net.last_r then
104 net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ',', '.')
105 net_now.received = string.gsub(string.format('%.1f', net_now.received), ',', '.')
115 helpers.newtimer(iface, timeout, update)
117 return setmetatable(net, { __index = net.widget })
120 return setmetatable({}, { __call = function(_, ...) return worker(...) end })