micro icon indicating copy to clipboard operation
micro copied to clipboard

Running shell script in statusformatl

Open 0neGal opened this issue 5 years ago • 4 comments

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

0neGal avatar Sep 15 '20 12:09 0neGal

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.

zyedidia avatar Sep 21 '20 00:09 zyedidia

It seems weirdly phrased from what I can tell, mind giving an example?

0neGal avatar Sep 21 '20 11:09 0neGal

This would actually be quite useful if it was possible to do small inline stuff this way.

bazzilic avatar Nov 19 '21 09:11 bazzilic

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

niten94 avatar Jun 04 '24 07:06 niten94