You are not logged in.
Hey,
I'm trying to write a quick vicious widget to check if "tun" exists (by running io.popen("ifconfig|grep tun")), and if it does simply display "enabled" or "disabled" (in order to keep track of a VPN connection). However, I can't get it to display anything (and I have to admit, my lua skills are practically zip and my understanding of vicious is shaky at best). The first few local variables are added because they seem to exist in a large majority of vicious widgets, though besides the setmetatable and "type" I have little understanding as to why they are present). So far I've looked but nothing I found explained it to me in such a way that I was able to apply it to this particular situation.
What I have so far:
--- {{{ Grab environment
local io = { popen = io.popen }
local setmetatable = setmetatable
local type = type
local tonumber = tonumber
local helpers = require("vicious.helpers")
--- }}}
--- {{{ vpn checker
local function worker(format, warg)
local result = io.popen("ifconfig | grep tun" .. warg)
local vpn = result:read("*a")
if vpn == nil or vpn == "" then
return {"disabled"}
else
return {"enabled"}
end
end
---}}}
setmetatable(_M, { __call = function(_, ...) return worker (...) end})
I've tested the function in an empty lua script with prints instead of returns, and it displayed the correct value for vpn enabled/disabled, but I'm not sure how to use it as a vicious widget.
How I (think I should) call it from my rc.lua:
vpnwidget = widget({type = "textbox" })
vicious.register(vpnwidget, vicious.widgets.vpn, "VPN: $1 |", 120)
And, of course, I add the "vpnwidget" to the widget list. I'm not certain I'm calling the script correctly (vicious.widgets.vpn) but I have added the script vpn.lua under ~/.config/awesome/vicious/widgets/ (where the rest are). I'd appreciate any replies, and an explanation alongside any solution would be much appreciated! (I'd love to understand vicious widgets better).
Thanks in advance.
Last edited by lswest (2010-10-12 21:36:47)
Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds
Offline
You need to add
module("vicious.vpn")
to your widget lua file and
require("vicious.vpn")
to vicious/widgets/init.lua.
Hope that helps.
EDIT: Some explanations (according to my understanding)
In the beginning of the widget file one has to import the functions and methods that are necessary to run the widget (no more no less).
This keeps the widget light weight. I guess in your case you would only require
local io = { popen = io.popen }
Also you have no variable warg that is passed to your worker function so you can just call
local result = io.popen("ifconfig | grep tun")
warg is an optional argument for example if you what want to check for varying interfaces like eth0, wlan0, etc. than you would write
local result = io.popen("ifconfig | grep " .. warg)
and call
vicious.register(vpnwidget, vicious.widgets.vpn, "VPN: $1 |", 120, "tun")
in your rc.lua
Last edited by kowalski (2010-10-20 11:40:37)
He who says A doesn't have to say B. He can also recognize that A was false.
Offline
Anrxc told me in relation to another widget that something "small" like this doesn't require a vicious widget and is easier achieved by a simple function directly in the rc.lua. Maybe this makes it much more simple?
Last edited by Barghest (2010-10-20 13:37:03)
Offline
@Kowalski: I appreciate the information. At the moment I couldn't get it working (one of the changes to the widget files resulted in awesome loading the default configuration). I'll keep playing with it to see if I figure it out.
@Stebalien: Probably, but I'm not too bothered by the speed of the applet (chances are I won't be checking it too often).
@Barghest: I agree that it would make it simpler, but I'm even less certain how to go about it! (At least, to have the results of said function appear in the awesome bar).
Thanks for your responses and I'll give each one a shot, but any more information on how to go about Barghest's suggestion would be appreciated.
Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds
Offline
This works here (returns "disabled"):
-- {{
local function tun()
local f = io.popen("ifconfig | grep tun")
local vpn = f:read("*a")
if vpn == nil or vpn == "" then
return {"disabled"}
else
return {"enabled"}
end
end
tunwidget = widget({type = "textbox"})
vicious.register(tunwidget, tun, "$1", 61)
-- }}
Then add tunwidget to your wibox
Last edited by Barghest (2010-10-21 08:41:37)
Offline
Wow, I completely forgot to respond here!
@Barghest, thanks, it seems to be working here too!
Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds
Offline
You need to add
module("vicious.vpn")
to your widget lua file and
require("vicious.vpn")
to vicious/widgets/init.lua.
Hope that helps.
Brother, You are the best. It helped me to add a widget too. THANKS YOU!! I was around 12 hours trying. I almost suicided. I am so happy brother!!
Thanks you again.
Offline