This post is about what must be done before modularizing rc.lua. It is more about Lua Code style, and the process of splitting the codes. And not the configuration itself. I’m using Object Oriented Approach, a beginner perspective.
It is too early for me to release my modularized awesome WM configuration. This topic itself is long enough, that I don’t want to mix it with another topic.
Goal
No long configuration, short enough to be analyzed
I have refactored rc.lua
into some files few month ago,
but it is not enough.
I’m a beginner in lua world,
and I’m not smart enough to read long source code.
So I decide to make my rc.lua
part smaller.
Each files should be small enough to be analyzed file by file.
Problem Definition
After a hard time of reading rc.lua
,
I finaly realized that rc.lua
consist of these parts
-
Sequence of code, e.g
-
Error Handling
-
Initializing Theme
-
Signals
-
User Variables: Theme, Device, Statusbar Design
-
-
Defining Variable
-
Layouts and Tags
-
Menu, submenu, menu generator
-
Rules
-
GlotbalKeys, GlobalButtons
-
ClientKeys, Clientbuttons
-
-
Other than those two above
-
Creating Wibox: Pack sequence of code into Function
-
Miscelannous
-
It takes Lua knowledge to split lua-based configuration
Loading module in Lua
There is a strange concept of array in Lua called Table. Table is a container of associative array stored in key/value pairs. Since Module in Lua is just container of stuff. Then Module in Lua is just a Table.
You can call a module by simply use the require
keyword.
Optionally, when calling a Module, it can be stored in a variable, this variable can be thought as an alias of modules.
Splitting Config
Splitting configuration source code is easy, the ‘dofile’ function can do the horsework.
This dofile
simply run another lua file,
and doesn’t do any check like require
does.
Now I can move-and-paste the lua-code
from rc.lua
to other lua file.
Now let’s see what is in this file main/theme.lua
.
Configuration Source Code: main/theme.lua
Require in Each Lua
There is something you should do each time move-and-paste lua-code. You should also move the required module.
In that configuration above,
it needs some beautiful
and gears
.
Module Containing Variable
The next steps is move each variables to Lua files.
Sample Configuration Source Code: main/user-variables.lua
Let’s say we have terminal variable
We can just move-and-paste to a file, e.g myvar.lua
and let the variable be a global variable.
As n00b coming from other language, I avoid global variable as possible.
There are some alternative on how to make module in Lua
Lua 5.0, using module function
This is deprecated.
Lua 5.2, using table
We can use any table name, e.g. _M
And in main rc.lua
, we can call
I also made the terminal in rc.lua
as local.
Module Containing Only One Variable
For module with only one variable, We can also make the call simple.
And in main rc.lua
, we can call
No need to call get()
explicitly .
Real lua.rc Sample
Let’s see our globalbutton moved to main/globalbuttons.lua
.
Do not confuse the name globalbuttons with global variables.
It is just a variable name, it could be global or local.
Original in rc.lua.
Splitted from rc.lua.
To binding/globalbuttons.lua
Configuration Source Code: binding/globalbuttons.lua
.
Calling from rc.lua
Splitting Long Module
Not everything should be packed with these code style. It all depends on your creativity and imagination.
I once had my wibox statusbar configuration. I have packed some plain sequence of code into bunch of function. Short enough for expert, but long enough for me, so I move some function into a helper.
Let’s see the code. Instead of the _M table that can be returned as public. I also make a WB table that is private, only visible for this module.
Note how I declare global table wibox_package as a bridge between these two Lua file.
in anybox/arrow/statusbar.lua
Configuration Source Code: statusbar.lua
in anybox/arrow/helper.lua
Configuration Source Code: helper.lua
Module with many containers
The issue goes further when I decorate Wibox. It has a bunch of monitoring stuff, e.g cpu, mem, netup, netdown, time, battery, diskfree, alsabar, mpd and so on. Each widget utilize icon and text. For example cpu, it has memicon and memtext.
Instead of using namespace as memicon and cmemtext.
I’m using table approach I.mem
, and W.mem
.
I’m using global table, instead of local _M
table.
in anybox/lain/lain.lua
Configuration Source Code: lain.lua
I also move long chunks, that require a lot of variable.
in anybox/lain/lain-diskfree
Configuration Source Code: lain-diskfree.lua
That’s all for now.
It doesn’t look complicated once, you get in to the source code.
In fact the source configuration is easier to be read now.
Configuration Source
This the result.
.
Thank you for reading