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_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+):")
30 else return "network off" end
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_device()
41 net.widget = wibox.widget.textbox('')
43 helpers.set_map(iface, true)
48 if iface == "" or string.match(iface, "network off")
50 iface = net.get_device()
53 net_now.carrier = helpers.first_line(string.format('/sys/class/net/%s/carrier', iface)) or '0'
54 net_now.state = helpers.first_line(string.format('/sys/class/net/%s/operstate', iface)) or 'down'
56 local now_t = helpers.first_line(string.format('/sys/class/net/%s/statistics/tx_bytes', iface)) or 0
57 local now_r = helpers.first_line(string.format('/sys/class/net/%s/statistics/rx_bytes', iface)) or 0
59 if now_t ~= net.last_t or now_r ~= net.last_r then
60 net_now.sent = (now_t - net.last_t) / timeout / units
61 net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ',', '.')
62 net_now.received = (now_r - net.last_r) / timeout / units
63 net_now.received = string.gsub(string.format('%.1f', net_now.received), ',', '.')
72 if not string.match(net_now.carrier, "1") and notify == "on"
74 if helpers.get_map(iface)
79 icon = helpers.icons_dir .. "no_net.png",
82 helpers.set_map(iface, false)
85 helpers.set_map(iface, true)
89 helpers.newtimer(iface, timeout, update)
91 return setmetatable(net, { __index = net.widget })
94 return setmetatable({}, { __call = function(_, ...) return worker(...) end })