zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.47k stars 1.16k forks source link

Help using WriteLog() function #3094

Closed Gavin-Holt closed 7 months ago

Gavin-Holt commented 7 months ago

Hi,

Sorry to be an idiot, I have looked and looked at the available documentation - does anyone know how to use WriteLog from Lua?

I have tried the obvious options:

    WriteLog("Hello World")
    micro.WriteLog("Hello World")
    micro:WriteLog("Hello World")
    buffer.WriteLog("Hello")
    buffer:WriteLog("Hello")
    bp.WriteLog("Hello World")
    bp.WriteLog("Hello World")
    bp.Buf:WriteLog("Hello World")
    bp.Buf:WriteLog("Hello World")

All crash micro! This is a similar problem to the unanswered https://github.com/zyedidia/micro/issues/1191 from Sep 22, 2018.

I want to use the LogBuf to capture output from Lua (the terminal is not easy to copy from):

function luaExec(Current)                   
    local prompt = "Lua > "
    local seed = editor.GetSelection(Current) or ""
    local history = "Lua"
    micro.InfoBar():Prompt(prompt, seed, history,
        function(input)
            return
        end,
        function(input, canceled)
            if input and not canceled then
                -- How to capture print output ?
                local f = loadstring([[return  ]]..input)
                local r = f()
                if r then
                    -- How to use WriteLog(r) instead ?
                    micro.TermMessage(r)
                    -- How to detect if its already open ?
                    Current:OpenLogBuf()
                end
            end
        end
    )
end

I am also struggling to detect an open LogBuf, at present my function adds a new split on each invocation.

Accessing the Go libraries within micro (from Lua) is going to be the super-power of this editor, once I grok how to call the functions. Do I need a LLM to process the Micro source + Pkg Dev help files and synthesize a list of callable Lua functions? Or is there a simpler solution using awk.exe?

Sorry if I sound frustrated, but Micro is so good I am working hard to make it my only editor - init.lua.txt

Kind Regards Gavin Holt

OS: Windows 10 Version: 2.0.13 Commit hash: https://github.com/zyedidia/micro/commit/68d88b571de6dca9fb8f03e2a3caafa2287c38d4 Compiled on December 12, 2023

dustdfg commented 7 months ago

I don't have time just right now to answer but you can use micro.Log(err). It will output to log.txt file although you will need to run micro with micro -debug

dustdfg commented 7 months ago

What error message do you get when it crash?

Gavin-Holt commented 7 months ago

Hi

Many thanks for your reply.

Kind Regards Gavin Holt

dustdfg commented 7 months ago

I think the Log.txt is a different beast, for debugging the application.

Yeah

I don't get an error message the console dies and disappears (It may be because I am running a batch file to launch Micro).

Maybe try to open not with batch file? It usually say when error in plugin and what is an error. Idk if it will help but know what it prints wouldn't be bad

want to use the LogBuf in the same way the plugin available command displays output.

Ok. What I found:

https://github.com/zyedidia/micro/blob/2d82362a6695a7e898455ce016449167ac439ddd/internal/action/command.go#L103-L114

When you print > plugin available internally micro opens logbuffer (which is io.Writer) and sends it to another function

https://github.com/zyedidia/micro/blob/2d82362a6695a7e898455ce016449167ac439ddd/internal/config/plugin_installer.go#L640

that prints to buffer via fmt.Fprintln(out, <text>)

P.S it won't help with WriteLog but you can write there as you wanted.

dustdfg commented 7 months ago
-- How to detect if its already open ?
Current:OpenLogBuf()

I think no way to do it as we expected in just one line

https://github.com/zyedidia/micro/blob/2d82362a6695a7e898455ce016449167ac439ddd/internal/action/command.go#L324-L330

> log don't check if log buffer is open it just checks if you ran it from log buffer.

  1. > log
  2. Ctrl+w to switch to non logbuf
  3. > log

You will see two bufpanes with the same content. But if you run > log from log buf, it will just close it. If you want to check if log buf is currently opened you will need to iterate thorough all the panes and check if one of them is log buf. Idk I will try to do more research

dustdfg commented 7 months ago

https://github.com/zyedidia/micro/blob/2d82362a6695a7e898455ce016449167ac439ddd/internal/action/globals.go#L8-L9

It looks like a sorta bug. We have only one pointer for global LogBufPane so we must have only one log pane at the moment. But in previous comment I said how to do it. Am I right it is a bug? @dmaluka @JoeKar

Does comment rely on the fact it is opened only by ToggleLogCmd?

It is easy to correct for > log https://github.com/dustdfg/micro/commit/da2d4cf56d92a7fd51da5aed569a931a4b1bb2d0 but I am not sure what to do with

https://github.com/zyedidia/micro/blob/2d82362a6695a7e898455ce016449167ac439ddd/internal/action/globals.go#L30-L36

And I still don't know what causes problem of issues OP

Gavin-Holt commented 7 months ago

Hi

I have been silly. Reading the Plugins.md I found the bufffer.Log function!

So to write to the global LogBuf is literally:

buffer.Log("Hello World").

However, be sure to use tostring() beforehand as there is no built in type conversion:

buffer.Log(8) -- Fails

Kind Regards Gavin Holt

I will leave the problem of multiple Log splits for brighter minds.