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

Plugin command causes editor display bug #3622

Open
mkerna opened this issue Jan 21, 2025 · 5 comments
Open

Plugin command causes editor display bug #3622

mkerna opened this issue Jan 21, 2025 · 5 comments

Comments

@mkerna
Copy link

mkerna commented Jan 21, 2025

Upon execution of the plugin code below, the editor strangely shifts each line as you move through it with the arrow keys. It I remove the "-v" (verbose) option from the copy command the problem goes away. But I do want the results of the execute.

N.B. I'm using micro version 2.0.14.

local micro = import("micro")
local config = import("micro/config")

function init()
    config.MakeCommand("test", testCommand, config.NoComplete)
end

function testCommand(bp)
    local s, err = os.execute("cp -v -a main.lua main.lua2")
    if err ~= nil then
        micro.InfoBar():Error(err)
    end
end
@dmaluka
Copy link
Collaborator

dmaluka commented Jan 21, 2025

When you run os.execute(), the command's stdout is just dumped onto the same console where micro prints its output, so it messes with micro.

What you want is a function such as micro's ExecCommand() or RunCommand() (documented in help plugins) that reads the command's output into a string, and then you can do with this output whatever you need to do with it.

@mkerna
Copy link
Author

mkerna commented Jan 21, 2025

I tried using ExecCommand() and RunCommand(), but couldn't get them to deal with shell variables. The code above is the minimum to recreate the bug that I saw. The code below is what I want to do, which is to append the date to a file. It works with os.execute(), but not with shell.ExecCommand() or shell.RunCommand(). And os.execute(), of course, has the problem I showed.

Perhaps this is two bugs?

local micro = import("micro")
local config = import("micro/config")
local shell = import("micro/shell")

function init()
    config.MakeCommand("test", testCommand, config.NoComplete)
end

function testCommand(bp)
    --shell.ExecCommand("cp", "-v -a main.lua main.lua-$(date +\"%Y%m%d\")")
    shell.RunCommand("cp -v -a main.lua main.lua-$(date +\"%Y%m%d\")")
end

@Andriamanitra
Copy link
Contributor

If you want to use shell features you can execute it in a shell, eg.

shell.RunCommand("sh -c 'cp -v -a main.lua main.lua-$(date +%Y%m%d)'")

Of course you could also get the date on the Lua side of things to avoid using a subshell:

shell.RunCommand(string.format("cp -v -a main.lua main.lua-%s", os.date("%Y%m%d")))

@mkerna
Copy link
Author

mkerna commented Jan 21, 2025

That works. Thank you.

@JoeKar
Copy link
Collaborator

JoeKar commented Jan 22, 2025

That works. Thank you.

So this issue can be closed, right?

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