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

Feature request, blink on command (or key press) rerun #3226

Closed
gituser23234235234887 opened this issue Apr 3, 2024 · 19 comments
Closed

Feature request, blink on command (or key press) rerun #3226

gituser23234235234887 opened this issue Apr 3, 2024 · 19 comments

Comments

@gituser23234235234887
Copy link

Description of the problem or steps to reproduce

I am copying things a lot and would like a visual representation of a successful copy.
on first copy, it shows that a successful copy was made, but on subsequent copies, there is no visual indication that a copy was made.
to ammend this, I suggest the message at the bottom to blink or do some other visual cue.

Specifications

Commit hash: micro 2.0.9
OS: kubuntu jammy
Terminal: konsole

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 4, 2024

Such blinking can be achieved by adding something like this to your init.lua:

local micro = import("micro")
local time = import("time")

local interval = time.ParseDuration("100ms")

function onCopy(bp)
    micro.InfoBar():Message("")
    micro.After(interval, function()
        micro.InfoBar():Message("Copied selection")
    end)
end

function onCopyLine(bp)
    micro.InfoBar():Message("")
    micro.After(interval, function()
        micro.InfoBar():Message("Copied line")
    end)
end

But it will not work with micro 2.0.9. This possibility was added recently in PR #3023 and is not in any official release yet.

@gituser23234235234887
Copy link
Author

@dmaluka where can I find init.lua for micro?

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 6, 2024

You just create an empty init.lua file in your ~/.config/micro directory, and for example just paste the above code into it.

See help tutorial and help plugins.

@gituser23234235234887
Copy link
Author

@dmaluka it gives me this error:

Plugin initlua: init:15: attempt to call a non-function object
stack traceback:
        init:15: in main chunk
        [G]: ?

Press enter to continue

also can you give me a similar function but for saving a file?

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 9, 2024

As I said in #3226 (comment), this does not work with older versions of micro. You'd need to compile the newest micro from the master branch on your own.

also can you give me a similar function but for saving a file?

function onSave(bp)
    local msg = micro.InfoBar().Msg
    micro.InfoBar():Message("")
    micro.After(interval, function()
        micro.InfoBar():Message(msg)
    end)
end

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 9, 2024

You'd need to compile the newest micro from the master branch on your own.

Hmm, actually there are nightly builds: https://github.com/zyedidia/micro/releases/tag/nightly which are supposed to allow users to run the latest and greatest micro without the need to compile it.

But, it says: "Assets uploaded on Tue Apr 9 00:01:33 UTC 2024 for commit 68d88b5." i.e. these builds are still being build nightly, but from v2.0.13, not from the newest master.

@zyedidia what should we do to make them build from the newest master again?

@gituser23234235234887
Copy link
Author

sorry, forgot to mention, I got the newest stable version before I replied again and it was showing that error.

$ micro --version
Version: 2.0.13
Commit hash: 68d88b57
Compiled on October 21, 2023

thanks for the save one

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 9, 2024

Yeah, but even 2.0.13 still doesn't include this feature. You'd need to download and compile Micro source code on your own:

git clone https://github.com/zyedidia/micro.git
cd micro
make
./micro

(you'd also need to install Go)

I'll try to figure out what's going on with nightly builds. (Please feel free to remind if I forget to.)

@gituser23234235234887
Copy link
Author

if I were to make a suggestion, add an option to make the infobar clear after x seconds and x=0 or x=-1 means do not clear.

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 11, 2024

if I were to make a suggestion, add an option to make the infobar clear after x seconds and x=0 or x=-1 means do not clear.

I've just uploaded PR #3244 which would allow to implement that in a plugin.

With this PR, you can add something like this to your init.lua (or to a separate plugin):

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

local lastmsg = ""
local msgcount = 0

function onAnyEvent()
    local interval = config.GetGlobalOption("clearstatus")
    local msg = micro.InfoBar().Msg
    if type(interval) == "number" and interval > 0 and msg ~= "" and msg ~= lastmsg then
        msgcount = msgcount + 1
        micro.After(time.ParseDuration(interval.."s"), function()
            msgcount = msgcount - 1
            if msgcount < 0 then
                micro.TermMessage("message count is negative: ", msgcount)
            end
            if msgcount == 0 then
                micro.InfoBar():Message("")
                lastmsg = ""
            end
        end)
    end
    lastmsg = msg
end

It will provide clearstatus option which does what you described.

@gituser23234235234887
Copy link
Author

you know, your knowledge of lua (or is it go) is very good. is there any resource or method you suggest for me to persue to learn lua (or go) as well as you?

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 14, 2024

I don't think my knowledge of Lua is very good. But Lua is a rather easy language to learn anyway. Probably what you want to know is not just how to write in Lua but how to write plugins for Micro.

In theory, the answer is: read help plugins. In practice, that's not really enough in most cases. See, in particular, the paragraph "This may seem like a small list of available functions but ..." in help plugins.

It helps a lot to know how micro is built from inside. I'd suggest digging into micro's source code (it is in Go), seeing how it implements a particular functionality that you'd like to extend in your plugin (like, in this example, the status bar messages), checking which of its internal APIs related to this functionality are accessible from Lua in one way or another, and try to use those APIs in your plugin to achieve what you need.

Also you can use existing plugins as examples, of course.

@gituser23234235234887
Copy link
Author

thanks for these, they are great help.

@gituser23234235234887
Copy link
Author

gituser23234235234887 commented Apr 20, 2024

by the way I have tested your fork #3244 @dmaluka it works fine. but clearstatus needs to be added manually to the settings.json file, it cannot be added using the set command. just leaving this info here for anyone who comes across this thread later.
EDIT: adding clearstatus once to settings.json enables the set command to update its values later. though setlocal still doesn't work

@dmaluka
Copy link
Collaborator

dmaluka commented Apr 20, 2024

Yeah, in order to make it work with the set command even if this option is not already in settings.json, you need to explicitly register it in the init() function of your plugin (or of your init.lua). But then its name cannot be just clearstatus, it needs to be in the format <plugin name>.<option name>, for example clearstatus.interval.

function init()
    config.RegisterCommonOption("clearstatus", "interval", 0)
end

(Funnily, this "plugin name" clearstatus doesn't really need to match the actual name of the plugin that registers clearstatus.interval. The above code will work in init.lua as well, not necessarily in a separate plugin.)

though setlocal still doesn't work

And to make it work, you'd need to replace this:

    local interval = config.GetGlobalOption("clearstatus.interval")

with something like this:

    local bp = micro.CurPane()
    local interval
    if bp ~= nil then
        interval = bp.Buf.Settings["clearstatus.interval"]
    else
        interval = config.GetGlobalOption("clearstatus.interval")
    end

@dmaluka
Copy link
Collaborator

dmaluka commented May 12, 2024

FYI nightly builds are now fixed (#3284) so you can grab the newest micro from https://github.com/zyedidia/micro/releases/tag/nightly.

@gituser23234235234887
Copy link
Author

FYI nightly builds are now fixed (#3284) so you can grab the newest micro from https://github.com/zyedidia/micro/releases/tag/nightly.

Hello again. I used micro 2.0.13 for my purposes and it seems to work fine. are you suggesting that 2.0.13 does not have the code needed for the init.lua that you made for me and that I need to use nightly? or is 2.0.13 good enough?

@dmaluka
Copy link
Collaborator

dmaluka commented May 14, 2024

If the lua code you have in init.lua uses micro.After(), I believe it can't possible work with 2.0.13, since micro.After() was added later, in #3023. With 2.0.13 you should get the error you mentioned in #3226 (comment).

While we're at it, if your init.lua uses onAnyEvent(), it will not work even with the nightly build, since #3244 is not merged yet... Update: ok, I've merged it just now, so it should work with the next nightly build (tomorrow).

@gituser23234235234887
Copy link
Author

If the lua code you have in init.lua uses micro.After(), I believe it can't possible work with 2.0.13, since micro.After() was added later, in #3023. With 2.0.13 you should get the error you mentioned in #3226 (comment).

While we're at it, if your init.lua uses onAnyEvent(), it will not work even with the nightly build, since #3244 is not merged yet... Update: ok, I've merged it just now, so it should work with the next nightly build (tomorrow).

I checked my version and it said 0.0 I probably had cloned your pull request commit code.
I just git pulled master and built a new binary just to be safe. well thanks for all the help and insight.

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

2 participants