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.
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.
After a hard time of reading
I finaly realized that
rc.lua consist of these parts
Sequence of code, e.g
User Variables: Theme, Device, Statusbar Design
Layouts and Tags
Menu, submenu, menu generator
Other than those two above
Creating Wibox: Pack sequence of code into Function
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
Optionally, when calling a Module, it can be stored in a variable, this variable can be thought as an alias of modules.
Splitting configuration source code is easy, the ‘dofile’ function can do the horsework.
dofile simply run another lua file,
and doesn’t do any check like
Now I can move-and-paste the lua-code
rc.lua to other lua file.
Now let’s see what is in this file
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
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
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.
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
Real lua.rc Sample
Let’s see our globalbutton moved to
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.
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.
Configuration Source Code: statusbar.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’m using global table, instead of
local _M table.
Configuration Source Code: lain.lua
I also move long chunks, that require a lot of variable.
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.
This the result.
Thank you for reading