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

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