all repos — dotfiles @ 9d4daac1b851d9a89b0a429277c3d4d64331812d

my *nix dotfiles

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

 1local fn = vim.fn
 2local cmd = vim.cmd
 3local M = {}
 4
 5function M.fzy_shell_cmd(fzy_cmd, action)
 6  -- save shell output to a temp file
 7  file = fn.tempname()
 8  shell_cmd = {
 9    '/bin/sh',
10    '-c',
11    fzy_cmd .. ' | fzy -p "edit > " > ' .. file
12  }
13
14  -- get current winid to jump back to
15  winid = fn.win_getid()
16
17  -- start a new buffer
18  cmd('botright 10 new')
19  cmd('startinsert')
20
21  -- open a term with the fzy command,
22  -- and run callback on exit
23  fn.termopen(shell_cmd, { on_exit = function()
24    -- delete buffer on exit
25    cmd('bd!')
26    fn.win_gotoid(winid)
27
28    -- read contents of file
29    local f = io.open(file, 'r')
30    fzy_out = f:read('*all')
31
32    -- housekeeping
33    f:close()
34    os.remove(file)
35
36    -- run action against output
37    -- ex: ':e somefile'
38    vim.cmd(table.concat({ action, fzy_out }, ' '))
39  end })
40end
41
42return M