Amongst the tools I use a lot while on my computer, there is tmux. I wouldn't
say I'm a power user, but opening tmux is one of the first thing I do in the
morning when turning on my computer. The second one would be sudo apt update && sudo apt upgrade :-)
Now, imagine I'm editing a file in my favourite editor
(vim.classic) and I want to run a bash command. I
have several possibilities. The first is to just run the command inside vim,
prefixed by a bang (:!cmd). The second one is to fire up a terminal inside
vim. And the third one is to open a new tmux window. In case of the latter, the
terminal will open at the working directory of the tmux session. Most of the
time, I want this working directory to be the root of the project I'm working
on.
This working directory is set either by the -c option, or by the current
directory at the time of creating the tmux session. In order to simplify my
tmux workflow, a while ago I wrote a bash script which was basically a big
beautiful switch-case where, given the argument <session-name>, if needed a
tmux session will be created, with a hard-coded working directory (the
left-hand-side of the case), and then attached to the tmux client.
This is all fun and well, but every time I had a new project I had to edit the
switch-case in the bash script in order to add it. Also, Zig.0.17.0 was just
out. :tada: So I've decided to port my bash script to Zig and to use a
configuration file instead of the switch-case.
Using a configuration file also allows changing the command-line interface of
the tool. I've implemented three sub-commands rather than just one command to
open a session. The three sub-commands are add, remove and open, which
semantic is quite straightforward I believe. However, we can note that the
couple add / remove will maintain the invariant that a session name will
occurs at most once in a configuration file. So I don't have to worry about the
order of the entries in it. Another bonus of the configuration file is the
possibility of a neat bash-completion of the registered session names for the
remove and open sub-commands.
I've tried Zig in the past on unfinished pet-projects and I wanted to go a bit
deeper in the language. One thing I liked was error handling and resources
management. In particular the defer keyword that aims at simplifying resource
management like freeing memory and closing files.
Zig is a language with manual memory management. Most of the time when I program, I rely on a garbage collector, so that's a change. The first thing you have to do when you need to allocate some memory in Zig is chose an allocator. Zig proposes several allocator for different purposes or use cases. As far as I understand, for a command-line tool that doesn't allocate memory in loops, the arena allocator is a good fit.
The arena allocator is defined in std.heap.ArenaAllocator where you can see
an init function and a deinint one. The latter allows to free all the
memory allocated via this allocator, simplifying a lot memory management.
Now, the main function in Zig has an init parameter, which contains, among
other things, an already initialised arena. I've learned, the hard way, that
this arena is automatically initialised before entering main and
deinitialised after leaving main. Of course, I tried to manually
deinitialised this given arena with a defer, so before leaving main. This
resulted in a segfault, when the second, already programmed, deinitialisation
occurred. Not an easy to understand error if it wasn't for some help on the IRC
zig channel :-)
One thing I really love to do is writing tests for my code. I do think we
should spend more time testing software, and less time using it. Zig has a nice
way of including unit tests next to the code, which plays the dual role of
testing and documenting it. It also provides an std.debug.allocator which
will detect memory leaks.
Overall, I'm quite happy with that little zig adventure. I've learn new things about the language and its standard library, improved my skills at parsing and understanding the compiler error messages and developed some familiarity with error handling in the control flow. Also, one of the most pleasant part was the helpful community :-)
I've published the code of the tool here for the curious.