forked from pltanton/net_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicator.lua
117 lines (102 loc) · 3.35 KB
/
indicator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
local wibox = require("wibox")
local awful = require("awful")
local beautiful = require("beautiful")
local naughty = require("naughty")
local gears = require("gears")
local module_path = (...):match ("(.+/)[^/]+$") or ""
local indicator = {}
local function worker(args)
local args = args or {}
local widget = wibox.container.background()
local wired = wibox.widget.imagebox()
local wired_na = wibox.widget.imagebox()
-- Settings
local interfaces = args.interfaces or {"enp2s0"}
local ICON_DIR = awful.util.getdir("config").."/"..module_path.."/net_widgets/icons/"
local timeout = args.timeout or 5
local font = args.font or beautiful.font
local onclick = args.onclick
local hidedisconnected = args.hidedisconnected
local popup_position = args.popup_position or naughty.config.defaults.position
local connected = false
local function text_grabber()
local msg = ""
if connected then
for _, i in pairs(interfaces) do
local map = "N/A"
local inet = "N/A"
f = io.popen("ip addr show "..i)
for line in f:lines() do
-- inet 192.168.1.190/24 brd 192.168.1.255 scope global enp3s0
inet = string.match(line, "inet (%d+%.%d+%.%d+%.%d+)") or inet
-- link/ether 1c:6f:65:3f:48:9a brd ff:ff:ff:ff:ff:ff
mac = string.match(line, "link/ether (%x?%x?:%x?%x?:%x?%x?:%x?%x?:%x?%x?:%x?%x?)") or mac
end
f:close()
msg = "<span font_desc=\""..font.."\">"..
"┌["..i.."]\n"..
"├IP:\t"..inet.."\n"..
"└MAC:\t"..mac.."</span>"
if int ~= "N/A" then
break
end
end
else
msg = "<span font_desc=\""..font.."\">Wired network is disconnected</span>"
end
return msg
end
wired:set_image(ICON_DIR.."wired.png")
wired_na:set_image(ICON_DIR.."wired_na.png")
widget:set_widget(wired_na)
local function net_update()
connected = false
for _, i in pairs(interfaces) do
awful.spawn.easy_async("bash -c \"ip link show "..i.." | awk 'NR==1 {printf \\\"%s\\\", $9}'\"", function(stdout, stderr, reason, exit_code)
state = stdout:sub(1, stdout:len() - 1)
if (state == "UP") then
connected = true
end
if connected then
widget:set_widget(wired)
else
if not hidedisconnected then
widget:set_widget(wired_na)
else
widget:set_widget(nil)
end
end
end)
end
return true
end
net_update()
gears.timer.start_new(timeout, net_update)
local notification = nil
function widget:hide()
if notification ~= nil then
naughty.destroy(notification)
notification = nil
end
end
function widget:show(t_out)
widget:hide()
notification = naughty.notify({
preset = fs_notification_preset,
text = text_grabber(),
timeout = t_out,
screen = mouse.screen,
position = popup_position
})
end
-- Bind onclick event function
if onclick then
widget:buttons(awful.util.table.join(
awful.button({}, 1, function() awful.util.spawn(onclick) end)
))
end
widget:connect_signal('mouse::enter', function () widget:show(0) end)
widget:connect_signal('mouse::leave', function () widget:hide() end)
return widget
end
return setmetatable(indicator, {__call = function(_,...) return worker(...) end})