Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My if-then-else-end statement is failing in Lua; how can I fix it?

I'm trying to improve my rc.lua for the Awesome window manager. The default rc.lua sets up a menu with the following code:

myawesomemenu = {
   { "manual", terminal .. " -e man awesome" },
   { "edit config", editor_cmd .. " " .. awesome.conffile },
   { "restart", awesome.restart },
   { "quit", awesome.quit }
}

I use Awesome as the window manager for the GNOME desktop environment, so I want to have Awesome use the gnome-session-quit program instead of awesome.quit, but only if the environment variable "DE" is set to "gnome". Therefore, I replaced the fifth line with

{ "quit", if os.getenv("DE") == "gnome" then os.execute("/usr/bin/gnome-session-quit") else awesome.quit end }

But when I reload the file, I get "unexpected symbol near if". How can I fix this, and what causes it?

like image 832
strugee Avatar asked Jul 13 '13 04:07

strugee


2 Answers

Try this:

{ "quit", (os.getenv("DE") == "gnome") and function() os.execute("/usr/bin/gnome-session-quit") end or awesome.quit}

a and b or c is like the C expression a ? b : c, provided that b is not false.

like image 190
Yu Hao Avatar answered Nov 06 '22 16:11

Yu Hao


I'm pretty sure Lua doesn't work like that :P

Have you tried wrapping it in a function?

{
    "quit", 
    function()
        if os.getenv("DE") == "gnome" then
            os.execute("/usr/bin/gnome-session-quit")
        else
            awesome.quit
        end
    end
}

Can you also try rewriting awesome.quit instead?

_awesome_quit = awesome.quit
awesome.quit = function()
    if os.getenv("DE") == "gnome" then
        os.execute("/usr/bin/gnome-session-quit")
    else
        _awesome_quit()
    end
end
like image 27
Dave Chen Avatar answered Nov 06 '22 17:11

Dave Chen