Skip to content

Commit 777fb21

Browse files
committed
pass string
1 parent 7fa7403 commit 777fb21

3 files changed

Lines changed: 31 additions & 71 deletions

File tree

lua/fzf-lua/libuv.lua

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,16 @@ end
8888
---@field process1? boolean
8989
---@field profiler? boolean
9090

91+
-- or table address
9192
local gen_uuid = function()
9293
-- return uv.random(10)
9394
return uv.hrtime()
9495
end
9596

9697
---@param opts fzf-lua.SpawnOpts
97-
---@param fn_transform function?
98-
---@param fn_done function?
9998
---@return uv.uv_process_t proc
10099
---@return integer pid
101-
M.spawn = function(opts, fn_transform, fn_done)
100+
M.spawn = function(opts)
102101
local EOL = opts.EOL or "\n"
103102
local EOL_data = type(opts.cmd) == "string"
104103
-- fd -0|--print0
@@ -152,8 +151,6 @@ M.spawn = function(opts, fn_transform, fn_done)
152151
end
153152
end, 200)
154153
end
155-
-- NO LONGER USED, was coroutinify callback
156-
if fn_done then fn_done(pid) end
157154
end
158155

159156
-- https://github.com/luvit/luv/blob/master/docs.md
@@ -208,13 +205,13 @@ M.spawn = function(opts, fn_transform, fn_done)
208205
end
209206

210207
local uuid = gen_uuid() -- distinguish two call when use worker pool
211-
local optstr = require("fzf-lua.libuv").serialize({ t = fn_transform, opts = opts.opts }, false)
208+
local optstr = require("fzf-lua.libuv").serialize(opts.opts, false)
212209
-- TODO: not correct for live_grep multiprocess=false? uuid?
213210
---@param data string data stream
214211
---@return string, string? line array, partial last line (no EOL)
215212
local function split_lines(data, o, id)
216213
-- io.stderr:write("[DEBUG] worker init")
217-
if not _G.fzf_lua_worker_init then
214+
if not _G.uuid then
218215
-- TODO: we can pass serialize opts to first queue...
219216
local __FILE__ = assert(debug.getinfo(1, "S")).source:gsub("^@", "")
220217
local lua = vim.fs.dirname(vim.fs.dirname(__FILE__))
@@ -238,14 +235,21 @@ M.spawn = function(opts, fn_transform, fn_done)
238235
setmetatable(vim.api, { __index = function() return function() end end })
239236
-- pcall(require, "nvim-web-devicons")
240237
-- print(e)
241-
_G.fzf_lua_worker_init = true
242238
end
239+
-- TODO: deserialize to string then eval here, otherwise we cannot use upvalue
240+
-- TODO: on buf? swtich gitsigns/diff_int.lua:32: bad argument #1 to 'decode' (string expected, got nil)
243241
if id ~= _G.uuid then -- refresh opts
244-
local d = require("fzf-lua.libuv").deserialize(o, false)
245-
_G.trans = d.t
246-
_G.opts = d.opts
242+
_G.opts = FzfLua.libuv.deserialize(o, false)
243+
local opts = _G.opts
244+
local load_fn = FzfLua.libuv.load_fn
245+
if not _G.uuid then
246+
local fn_preprocess = load_fn(opts.fn_preprocess) or opts.fn_preprocess
247+
if fn_preprocess then fn_preprocess(opts) end
248+
end
249+
if opts.fn_transform then _G.trans = load_fn(opts.fn_transform) end
247250
_G.uuid = id
248251
end
252+
249253
-- local trans = require("fzf-lua.make_entry").file
250254
-- local worker_opts = assert(_G.fzf_lua_worker_opts, "no opts")
251255
local trans = _G.trans
@@ -450,7 +454,7 @@ M.spawn_stdio = function(opts)
450454
local content = opts.contents
451455
if type(content) == "string" and content:match("%-%-color[=%s]+never") then
452456
-- perf: skip stripping ansi coloring in `make_file.entry`
453-
opts.no_ansi_colors = true
457+
-- opts.no_ansi_colors = true
454458
end
455459

456460
if opts.debug == "v" or opts.debug == 2 then
@@ -596,21 +600,14 @@ M.spawn_stdio = function(opts)
596600
if not fn_transform and not fn_postprocess then posix_exec(content) end
597601

598602
return M.spawn({
599-
cwd = opts.cwd,
600-
cmd = cmd,
601-
cb_finish = on_finish,
602-
cb_write = on_write,
603-
-- cb_write_lines = on_write_lines,
604-
cb_err = on_err,
605-
process1 = opts.process1,
606-
profiler = opts.profiler,
607-
EOL = EOL,
608-
opts = opts,
609-
},
610-
fn_transform)
611-
-- fn_transform and function(x)
612-
-- return fn_transform(x, opts)
613-
-- end)
603+
cwd = opts.cwd,
604+
cmd = cmd,
605+
cb_finish = on_finish,
606+
cb_write = on_write,
607+
cb_err = on_err,
608+
EOL = EOL,
609+
opts = opts,
610+
})
614611
end
615612

616613

lua/fzf-lua/make_entry.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,6 @@ M.file = function(x, opts)
568568
local icon, hl
569569
local colon_start_idx = 1
570570

571-
local path = path or FzfLua.path
572-
local utils = utils or FzfLua.utils
573-
local devicons = devicons or FzfLua.devicons
574-
-- print(vim.inspect(opts))
575571
if utils.__IS_WINDOWS then
576572
if string.byte(x, #x) == 13 then
577573
-- strip ^M added by the "dir /s/b" command
@@ -681,6 +677,7 @@ M.file = function(x, opts)
681677
if opts._fmt and type(opts._fmt.to) == "function" then
682678
ret[#ret + 1], _fmt_postfix = opts._fmt.to(filepath, opts, { path = path, utils = utils })
683679
else
680+
-- print(file_part, stripped_filepath, filepath)
684681
ret[#ret + 1] = file_is_ansi > 0
685682
-- filename is ansi escape colored, replace the inner string (#819)
686683
-- escape `%` in path, since `string.gsub` also use it in target (#1443)

lua/fzf-lua/shell.lua

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ M.stringify = function(contents, opts, fzf_field_index)
293293
assert(not opts.__stringified, "twice stringified")
294294
opts.__stringified = true
295295

296-
-- Convert string callbacks to callback functions
297-
for _, k in ipairs({ "fn_transform", "fn_preprocess", "fn_postprocess" }) do
298-
opts[k] = libuv.load_fn(opts[k]) or opts[k]
299-
end
296+
-- -- Convert string callbacks to callback functions
297+
-- for _, k in ipairs({ "fn_transform", "fn_preprocess", "fn_postprocess" }) do
298+
-- opts[k] = libuv.load_fn(opts[k]) or opts[k]
299+
-- end
300300

301301
local cmd, id = M.pipe_wrap_fn(function(pipe, ...)
302302
local args, n = { ... }, select("#", ...)
@@ -318,14 +318,8 @@ M.stringify = function(contents, opts, fzf_field_index)
318318
local write_cb_count = 0
319319
local pipe_want_close = false
320320
local EOL = utils.map_get(opts, "fzf_opts.--read0") and "\0" or "\n"
321-
local fn_transform = opts.fn_transform
322-
local fn_preprocess = opts.fn_preprocess
323-
local fn_postprocess = opts.fn_postprocess
324321
local co = coroutine.running()
325322

326-
-- Run the preprocess function
327-
if type(fn_preprocess) == "function" then fn_preprocess(opts) end
328-
329323
-- local on_finish = function(code, sig, from, pid)
330324
-- print("finish", pipe, pipe_want_close, code, sig, from, pid)
331325
local on_finish = function(_, _, _, _)
@@ -335,8 +329,7 @@ M.stringify = function(contents, opts, fzf_field_index)
335329
-- only close if all our uv.write calls are completed
336330
uv.close(pipe)
337331
pipe = nil
338-
-- Run the postprocess function
339-
if type(fn_postprocess) == "function" then fn_postprocess(opts) end
332+
-- if type(fn_postprocess) == "function" then fn_postprocess(opts) end
340333
end
341334
end
342335

@@ -349,23 +342,6 @@ M.stringify = function(contents, opts, fzf_field_index)
349342
if cb then cb(nil) end
350343
else
351344
write_cb_count = write_cb_count + 1
352-
if type(data) == "table" then
353-
-- cb_write_lines was sent instead of cb_lines
354-
if fn_transform then
355-
-- safely remove items while iterating
356-
local i = 1
357-
while i <= #data do
358-
local v = fn_transform(data[i], opts)
359-
if not v then
360-
table.remove(data, i)
361-
else
362-
data[i] = v
363-
i = i + 1
364-
end
365-
end
366-
end
367-
data = #data > 0 and (table.concat(data, EOL) .. EOL) or ""
368-
end
369345
uv.write(pipe, tostring(data), function(err)
370346
write_cb_count = write_cb_count - 1
371347
if cb then cb(err) end
@@ -388,9 +364,6 @@ M.stringify = function(contents, opts, fzf_field_index)
388364
end
389365

390366
if type(contents) == "string" then
391-
-- Use queue in libuv.spawn by default
392-
opts.use_queue = opts.use_queue == nil and true or opts.use_queue
393-
394367
-- Throttle pipe writes by default
395368
-- opts.throttle = opts.throttle == nil and true or opts.throttle
396369

@@ -410,18 +383,11 @@ M.stringify = function(contents, opts, fzf_field_index)
410383
cmd = contents,
411384
env = env,
412385
cb_finish = on_finish,
413-
-- cb_write_lines = on_write,
414386
cb_write = on_write,
415387
cb_pid = function(pid) if opts.PidObject then opts.PidObject:set(pid) end end,
416-
process1 = opts.process1,
417-
profiler = opts.profiler,
418-
use_queue = opts.use_queue,
419388
EOL = EOL,
420389
opts = opts,
421-
-- Must send value, 'coroutinify' adds callback as last argument
422-
-- which will conflict with the 'fn_transform' argument
423-
-- send true to force line processing without transformation
424-
}, fn_transform)
390+
})
425391
else
426392
-- callback with newline
427393
local on_write_nl = function(data, cb)

0 commit comments

Comments
 (0)