]> git.madduck.net Git - etc/awesome.git/blob - widgets/net.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

wiki updated
[etc/awesome.git] / widgets / net.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local helpers      = require("lain.helpers")
11 local naughty      = require("naughty")
12 local wibox        = require("wibox")
13
14 local string       = { format = string.format,
15                        gsub   = string.gsub,
16                        match  = string.match }
17
18 local setmetatable = setmetatable
19
20 -- Network infos
21 -- lain.widgets.net
22
23 local function worker(args)
24     local net = { last_t = 0, last_r = 0 }
25
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
31     end
32
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()
40
41     net.widget = wibox.widget.textbox('')
42
43     helpers.set_map(iface, true)
44
45     function update()
46         net_now = {}
47
48         if iface == "" or string.match(iface, "network off")
49         then
50             iface = net.get_device()
51         end
52
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'
55
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
58
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), ',', '.')
64
65             widget = net.widget
66             settings()
67
68             net.last_t = now_t
69             net.last_r = now_r
70         end
71
72         if not string.match(net_now.carrier, "1") and notify == "on"
73         then
74             if helpers.get_map(iface)
75             then
76                 naughty.notify({
77                     title    = iface,
78                     text     = "no carrier",
79                     icon     = helpers.icons_dir .. "no_net.png",
80                     screen   = screen
81                 })
82                 helpers.set_map(iface, false)
83             end
84         else
85             helpers.set_map(iface, true)
86         end
87     end
88
89     helpers.newtimer(iface, timeout, update)
90
91     return setmetatable(net, { __index = net.widget })
92 end
93
94 return setmetatable({}, { __call = function(_, ...) return worker(...) end })