You are not logged in.

#1 2014-10-16 17:59:05

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

[SOLVED] lua if/then failure (rc.lua)

Hey fellas, trying to get my ip piped to my awesome panel and got it with io.popen() but now am having issues with an if then function within the widget register. I know it's something small but I do not know lua well enough to understand what it could be.

--grab local ip local function

local function ip_output()
	local f = io.popen("hostname -I")
	local out = f:read("*a")
	f:close()
	return { out }
end

--widget register if then statement

wifiwidget = widget({ type = "textbox" })
vicious.register(wifiwidget, ip_output,
    function (wifiwidget)
        if ip_output == nil then
            return " " .. colbred .. "n/a" .. coldef .. " "
        else
            return "" .. colbgre .. " $1" .. coldef .. " "
        end 
     end )

With the above used, a green "$1" is displayed when connected or not. *shrug* Like I stated I know it's something remedial but I don't understand lua all that well. Any guidance would be appreciated.

Now without the if/then and just registering then calling works but no nifty "n/a" notice.

vicious.register(wifiwidget, ip_output, " $1")





Solved with args[] and checking for a new line return:

vicious.register(wifiwidget, ip_output,	
	function (widget, args)
        if args[1] == "\n" then
            return " " .. colbred .. "n/a" .. coldef .. " "
        else
            return " " .. colbgre ..  args[1] .. coldef .. " "
        end 
    end, 5 )

Last edited by n1x4 (2014-10-16 19:26:08)


||github||

Offline

#2 2014-11-13 13:10:45

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: [SOLVED] lua if/then failure (rc.lua)

Is there a reason why you wrap your return value in a table in the ip_output function?

return {out}

The brackets here create a table, which always has only one element: the value of out.  You could just use

return out

Now there is no need to index args later.

My guess is that the vicious library doesn't pass multiple return values through to the callback's varargs properly (this wouldn't surprise me, as I found awesome's code to be ... let's say, less-than-awesome), so somebody worked around this by passing back multiple values in a table.  You probably copied this code, and then reduced the table to one element without realizing you don't need a table here at all.

The solution you found will work, but I thought I'd throw this out there since you seemed to not understand what was happening.

Offline

Board footer

Powered by FluxBB