all repos — dotfiles @ fcd2a5a95b5ca3254f664d970d49cb4554b90d42

my *nix dotfiles

config/nvim/lua/fzy/shell.lua (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
local fn = vim.fn
local cmd = vim.cmd
local M = {}

function M.fzy_shell_cmd(fzy_cmd, action)
  -- save shell output to a temp file
  file = fn.tempname()
  shell_cmd = {
    '/bin/sh',
    '-c',
    fzy_cmd .. ' | fzy -p "edit > " > ' .. file
  }

  -- get current winid to jump back to
  winid = fn.win_getid()

  -- start a new buffer
  cmd('botright 10 new')
  cmd('startinsert')

  -- open a term with the fzy command,
  -- and run callback on exit
  fn.termopen(shell_cmd, { on_exit = function()
    -- delete buffer on exit
    cmd('bd!')
    fn.win_gotoid(winid)

    -- read contents of file
    local f = io.open(file, 'r')
    fzy_out = f:read('*all')

    -- housekeeping
    f:close()
    os.remove(file)

    -- run action against output
    -- ex: ':e somefile'
    vim.cmd(table.concat({ action, fzy_out }, ' '))
  end })
end

return M