Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running shell script in statusformatl #1860

Open
0neGal opened this issue Sep 15, 2020 · 4 comments
Open

Running shell script in statusformatl #1860

0neGal opened this issue Sep 15, 2020 · 4 comments

Comments

@0neGal
Copy link

0neGal commented Sep 15, 2020

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: 5044ccf
OS: Linux (Arch)
Terminal: GNOME Terminal

@zyedidia
Copy link
Owner

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.

@0neGal
Copy link
Author

0neGal commented Sep 21, 2020

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

@bazzilic
Copy link
Contributor

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

@niten94
Copy link
Contributor

niten94 commented Jun 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants