Lua is a very Popular embedded script.
The only contender I know is Squirrel.
Lua script version for this Pipe and Fork tutorial
is very similar with PHP script version.
Almost only syntax differences they have.
It would not be hard to understand between the two.
Goal: A script that continuously show date and time,
with Dzen2, and Conky.
Before you dip your toe to scripting,
you might desire to know the reason by reading this overview.
Lua only used as embedded script. But I can see Lua’s potential.
I have never seen anybody utilize Lua as main scripting tools in dotfiles,
except Conky, and AwesomeWM that use Lua as main scripting configuration.
Outside the world of Ricing, Lua also extensively used in Gaming..
I myself utilize a lot of Conky Lua a lot. So why not make a start ?
Lua is so lightweight that it lacks some common commands.
We have to emulate these below:
sleep
_ dirname _
exec
Everything is fine now.
.
Start Simple
Welcome to n00berland. Begin with simple script.
We will use this loop as a source feed to pipe.
This step won’t introduce Pipe nor Fork.
This script only show an infinite loop showing local time.
Each updated in one second interval.
We manage this interval by delaying,
using sleep code.internal function.
The first issue with Lua is,
it does not have native sleep function.
So we either call it from system or emulate it with.
Beside previous simple loop that is used as Internal Command,
this tutorial also provide Conky as External Command
in asset directory.
I made it as simple as possible.
This step is overview of Pipe between two external command.
Instead of system command, this utilize popen
using two handles: pipein and pipeout.
This is a very simple. Just a few self explanatory lines.
This very short script is using conky
as pipe source feed and less as pipe target.
Showing time and date forever in the console.
This infinite pipe run in time-less fashioned.
The second issue with Lua is,
it does not have native _ dirname _ directive.
that’s why emulate it with debug.getinfo
The same as previous.
But instead of reading from pipein,
it is managed by internal process using pipeout:write().
Fork Overview
Fork in Lua is also simple.
But it require external luaposix Library.
Arch user should get it easy in AUR (Arch user Repository).
This step use internal function as source feed,
as continuation of previous step.
This step use dzen2, with complete parameters.
This dzen2 is forked, running in the background.
Detached from the script,
no need to wait for dzen2 to finish the script.
This step also add system command that kill
any previous dzen2 instance. So it will be guaranteed,
that the dzen2 shown is coming from the latest script.
Any code after the posix.fork()
executed in both parent and child.
The child process has been detached from parent process.
The only different is the pid.
Polishing The Script
This step, we use conky again, as a source feed.
And also parameterized dzen2 as continuation of previous step.
This step add optional transset transparency,
detached from script. So we two forks, dzen and transset.