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

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