Running shell script in statusformatl
Description of the problem or steps to reproduce
I was attempting to try and add file type icons, and at first I thought the statusformatl took shell scripting in the sense that $(echo "test") would yield test... etc etc... But from what I later discovered it seems it's using Lua code? I've hardly played with Lua so I don't know that much, but would there be any way to make it use stdin from a shell script to put into the status?
That way I can easily add my own file type icons...
Specifications
Commit hash: 5044ccf6 OS: Linux (Arch) Terminal: GNOME Terminal
You can customize the status bar with lua code. See the default status plugin in runtime/plugins/status for examples. Also check the > help plugins documentation for information about the API. In particular see the shell lua module provided by micro for running shell commands.
It seems weirdly phrased from what I can tell, mind giving an example?
This would actually be quite useful if it was possible to do small inline stuff this way.
Commands cannot be specified in status line format definitions, but functions in Lua plugins can be registered so that parts with the function name are replaced with the string returned when the function is called.
The file at ~/.config/micro/init.lua is loaded as a plugin with initlua as the name, so it can be created and edited. Functions are usually registered using micro.SetStatusInfoFn in init function in plugins but a string with the plugin and function name has to be passed.
Functions in status line format definitions are called with the buffer being displayed passed in arguments. The content of init.lua can be edited like this:
local micro = import("micro")
function typeicon(buf)
-- getting and returning icon with filetype as key in table (like
-- dictionary in other programming languages)
return ({
-- key is "c", for example
c = "C",
["c++"] = "C++",
shell = "sh"
})[buf:FileType()] or "default"
end
function init()
micro.SetStatusInfoFn("initlua.typeicon")
end
The function can be used when specifying the function name like this in settings:
"statusformatl": "$(filename) $(initlua.typeicon)"
The function would usually be called when the screen is being updated so I do not know if the performance would be fine, but typeicon can be replaced like this so that a script would be run with the filetype passed in "$1":
local config = import("micro/config")
local shell = import("micro/shell")
function typeicon(buf)
-- sh ~/.config/micro/typeicon.sh (filetype)
local script = config.ConfigDir .. "/typeicon.sh"
local icon, err = shell.ExecCommand("sh", script, buf:FileType())
if err ~= nil then return "(error)" end
return icon
end
I think it is not clear how functions can be registered and used when only looking at the help files. ConfigDir is not in help/plugins.md but there is a pull request where it is added: #3240