The if-then-else-end statement does not work in Lua; how can i fix this?

I am trying to improve my rc.lua for an awesome window manager . By 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 use Awesome for the gnome-session-quit awesome.quit instead of awesome.quit , but only if the environment variable "DE" is set to "gnome". So 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 an "unexpected character next to". How can I fix this and what causes it?

+6
source share
2 answers

Try the following:

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

a and b or c is similar to C a ? b : c a ? b : c , provided that b not false .

+6
source

I'm sure Lua is not working like this: 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 } 

You can 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 
+4
source

Source: https://habr.com/ru/post/949352/


All Articles