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

Default value for scallback is nil
[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
12 local notify_fg    = require("beautiful").fg_focus
13 local naughty      = require("naughty")
14 local wibox        = require("wibox")
15
16 local string       = { format = string.format,
17                        gsub   = string.gsub,
18                        match  = string.match }
19
20 local setmetatable = setmetatable
21
22 -- Network infos
23 -- lain.widgets.net
24
25 local function worker(args)
26     local net = { last_t = 0, last_r = 0 }
27
28     function net.get_device()
29         local ws = helpers.read_pipe("ip link show | cut -d' ' -f2,9")
30         ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN")
31         if ws ~= nil then
32             return ws:match("(%w+):")
33         else
34             return "network off"
35         end
36     end
37
38     local args     = args or {}
39     local timeout  = args.timeout or 2
40     local units    = args.units or 1024 --kb
41     local notify   = args.notify or "on"
42     local screen   = args.screen or 1
43     local settings = args.settings or function() end
44     local iface    = args.iface or net.get_device()
45
46     net.widget = wibox.widget.textbox('')
47
48     helpers.set_map(iface, true)
49
50     function update()
51         net_now = {}
52
53         if iface == "" or string.match(iface, "network off")
54         then
55             iface = net.get_device()
56         end
57
58         net_now.carrier = helpers.first_line('/sys/class/net/' .. iface ..
59                                            '/carrier') or "0"
60         net_now.state = helpers.first_line('/sys/class/net/' .. iface ..
61                                            '/operstate') or "down"
62         local now_t = helpers.first_line('/sys/class/net/' .. iface ..
63                                            '/statistics/tx_bytes') or 0
64         local now_r = helpers.first_line('/sys/class/net/' .. iface ..
65                                            '/statistics/rx_bytes') or 0
66
67         net_now.sent = (now_t - net.last_t) / timeout / units
68         net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ",", ".")
69
70         net_now.received = (now_r - net.last_r) / timeout / units
71         net_now.received = string.gsub(string.format('%.1f', net_now.received), ",", ".")
72
73         widget = net.widget
74         settings()
75
76         net.last_t = now_t
77         net.last_r = now_r
78
79         if net_now.carrier ~= "1" and notify == "on"
80         then
81             if helpers.get_map(iface)
82             then
83                 naughty.notify({
84                     title    = iface,
85                     text     = "no carrier",
86                     timeout  = 7,
87                     position = "top_left",
88                     icon     = helpers.icons_dir .. "no_net.png",
89                     fg       = notify_fg or "#FFFFFF",
90                     screen   = screen
91                 })
92                 helpers.set_map(iface, false)
93             end
94         else
95             helpers.set_map(iface, true)
96         end
97     end
98
99     helpers.newtimer(iface, timeout, update)
100
101     return setmetatable(net, { __index = net.widget })
102 end
103
104 return setmetatable({}, { __call = function(_, ...) return worker(...) end })