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

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:

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