minimal extui fuzzy finder for neovim

refactor: create `make_cmd()` fn

+32 -49
+32 -49
lua/artio/builtins.lua
··· 9 9 local artio = lzrq("artio") 10 10 local config = lzrq("artio.config") 11 11 12 - local builtins = {} 12 + local function cmd_callback(o) 13 + local src = o.stderr 14 + if o.code == 0 then 15 + src = o.stdout 16 + end 17 + src = src 18 + local lines = vim.split(src, "\n", { trimempty = true }) 19 + return lines 20 + end 13 21 14 - local findprg = "fd -H -p -t f --color=never" 15 - 16 - local function find_files(match) 17 - if not findprg then 18 - return {} 19 - end 20 - local farg = string.format("'%s'", match or "") 21 - local findcmd, n = findprg:gsub("%$%*", farg) 22 - if n == 0 then 23 - findcmd = findcmd .. " " .. farg 24 - end 25 - local fn = function(o) 26 - local src = o.stderr 27 - if o.code == 0 then 28 - src = o.stdout 22 + ---@param prg? string 23 + ---@return fun(arg?: string): string[] 24 + local function make_cmd(prg) 25 + return function(arg) 26 + if not prg then 27 + return {} 28 + end 29 + arg = string.format("'%s'", arg or "") 30 + local cmd, n = prg:gsub("%$%*", arg) 31 + if n == 0 then 32 + cmd = ("%s %s"):format(prg, arg) 29 33 end 30 - src = src 31 - local lines = vim.split(src, "\n", { trimempty = true }) 32 - return lines 34 + return cmd_callback(vim 35 + .system({ vim.o.shell, "-c", cmd }, { 36 + text = true, 37 + }) 38 + :wait()) 33 39 end 34 - return fn(vim 35 - .system({ vim.o.shell, "-c", findcmd }, { 36 - text = true, 37 - }) 38 - :wait()) 39 40 end 40 41 42 + local builtins = {} 43 + 44 + local findprg = "fd -H -p -t f --color=never" 45 + 41 46 builtins.files = function() 42 - local lst = find_files() 47 + local lst = make_cmd(findprg)() 43 48 44 49 return artio.generic(lst, { 45 50 prompt = "files", ··· 57 62 }) 58 63 end 59 64 60 - local function grep(input) 61 - input = input or "" 62 - local grepcmd, n = vim.o.grepprg:gsub("%$%*", input) 63 - if n == 0 then 64 - grepcmd = grepcmd .. " " .. input 65 - end 66 - 67 - local fn = function(o) 68 - local src = o.stderr 69 - if o.code == 0 then 70 - src = o.stdout 71 - end 72 - src = src 73 - local lines = vim.split(src, "\n", { trimempty = true }) 74 - return lines 75 - end 76 - return fn(vim 77 - .system({ vim.o.shell, "-c", grepcmd }, { 78 - text = true, 79 - }) 80 - :wait()) 81 - end 82 - 83 65 builtins.grep = function() 84 66 local ext = require("vim._extui.shared") 67 + local grepcmd = make_cmd(vim.o.grepprg) 85 68 86 69 return artio.pick({ 87 70 items = {}, ··· 91 74 return {} 92 75 end 93 76 94 - local lines = grep(input) 77 + local lines = grepcmd(input) 95 78 96 79 vim.fn.setloclist(ext.wins.cmd, {}, " ", { 97 80 title = "grep[" .. input .. "]",