Made some quality of life changes to ✨ Project Moon Hermit ✨ today. Just for procrastination sake and because I miss Lua.
With the recent changes, one can use moonhermit
binary as an interpreter for shell scripting. It is a very handy way to create scripts that interact with SSB. Let me show you an example called getmsg.lua
:
#!./moonhermit
-- Requires "supernova" which can be installed from Luarocks
-- https://luarocks.org/modules/gbaptista/supernova
local utils = require "utils"
local supernova = require "supernova"
local msg, error = ssb:get({id = arg[1]})
if (not error) then
local date = os.date("%A, %B %d %Y at %I:%M:%S %p\n", msg.timestamp//1000)
print(supernova.italic.yellow("From: ") .. supernova.underline.color(msg.author, "#e317e0"))
print(supernova.italic.yellow("Date: ") .. supernova.underline.color(date, "#e317e0"))
print(supernova.gradient(
utils.textwrap(msg.content.text),
{ '#FF0000', '#FFFF00', '#00FF00', '#0FF0FE', '#233CFE' }
))
else
print(error)
end
It outputs this:
The magic part is the high-level SSB library that makes retrieving a message from the running SSB server as easy as:
local msg, error = ssb:get({id = arg[1]})
Lua can return multiple values from a function (a feature I miss in other languages), so that call will either give you an error in the error
variable or a table representing the message in the msg
variable. Quite neat.
I've changed the moonhermit
interpreter to export a global table arg
that holds the arguments passed to the script. It is similar to the built-in one from Lua standard interpreter but without the negative indices.
The rest of the code is just having fun with supernova colours.