zyedidia / micro

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

Micro plugin with fzf and fasd #2207

Open drewauff opened 3 years ago

drewauff commented 3 years ago

I am trying to mod. this script (see at bottom)

to use this command fasd -flt | tail -n 25 | grep -Ev '\.(tiff|jpg|jpeg|png|gif|pxm|mp3|mp4|aac)$' | fzf --tac

Instead of just straight fzf

I use fasd to list recently opened files/documents/code etc (media is excluded) via fzf and open them in micro.

I want to access this while in micro.

The commands in this script are will be the same except prefixed by an 'r'. so rfv = recent file / split vertical.

I hope this make sense.

I have tried changing the command in line 9, but a error flashes up and its far to quick to read.

VERSION = "1.0.0"

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

function fzf(bp, opt)
    local output, err = shell.RunInteractiveShell("fzf", false, true)
    if err ~= nil then
        micro.InfoBar():Error(err)
    else
        fzfOpen(output, bp, opt)
    end
end

function fzf_find(bp, args, opt)
    local strings = import("strings")
    local cmd = "grep --line-buffered -rnwi "

    local pattern = ""

    for i = 1, #args do
        pattern = args[i] .. " "
    end

    cmd = cmd .. pattern .. " * | fzf"

    local output, err = shell.RunInteractiveShell(cmd, false, true)
    -- local run, err = shell.RunBackgroundShell(cmd)
    -- local output = run()

    if err ~= nil then
        micro.InfoBar():Error("could not execute fzf command: ", err)
        return
    end

    -- output = strings.Split(output, "\n")
    -- micro:TermMessage("\nOutput\n", output)

    local input = strings.Split(output, ":")

    if #input < 2 then
        micro.InfoBar():Error("bad input format: ", input)
        return
    end

    fzfOpenLine(input[1], bp, 2, input[2])
    micro.InfoBar():Message(input[1] .. " @ " .. input[2])
end

function fzfOpen(output, bp, opt)
    local strings = import("strings") 
    local strconv = import("strconv") 
    file = strings.TrimSpace(output)

    if file ~= "" then
        local buf, err = buffer.NewBufferFromFile(file)

        if err ~= nil then
            micro.InfoBar():Error(err)
        end

        if opt == 0 then
            bp:OpenBuffer(buf)
        elseif opt == 1 then
            bp:HSplitBuf(buf)
        elseif opt == 2 then
            bp:VSplitBuf(buf)
        end
    end
end

function fzfOpenLine(output, bp, opt, line)
    local strings = import("strings") 
    local strconv = import("strconv") 
    file = strings.TrimSpace(output)

    if file ~= "" then
        local buf, err = buffer.NewBufferFromFile(file)

        if err ~= nil then
            micro.InfoBar():Error(err)
        end

        local new_bp

        if opt == 0 then
            new_bp = bp:OpenBuffer(buf)
        elseif opt == 1 then
            new_bp = bp:HSplitBuf(buf)
        elseif opt == 2 then
            new_bp =bp:VSplitBuf(buf)
        end

        new_bp.Cursor.Y = tonumber(line)-1
        new_bp.Cursor:Relocate()
    end
end

function init()
    config.MakeCommand("f", function(bp, args)
        fzf(bp, 0)
    end, config.NoComplete)

    config.MakeCommand("fh", function(bp, args)
        fzf(bp, 1)
    end, config.NoComplete)

    config.MakeCommand("fv", function(bp, args)
        fzf(bp, 2)
    end, config.NoComplete)

    config.MakeCommand("ff", function(bp, args)
        fzf_find(bp, args, 2)
    end, config.NoComplete)

    config.AddRuntimeFile("fzf", config.RTHelp, "help/fzf.md")
end

Getting this to work would make it, in my eyes, the perfect (user friendly) cli editor

xxuejie commented 3 years ago

I hit the same problem, but I found a workaround by using a bash wrapper:

bash -l -c 'rg --files | fzf'
drewauff commented 3 years ago

@xxuejie unfortunately this doesn't "solve" the problem. fasd with the above switches show only the 25 most recent files, which is what Im after.

drewauff commented 3 years ago

A little update, I put the command in a little script, I getting this error, exec: not started

onwards and upwards.