Skip to content

Commit

Permalink
perf: file explorer & test (#215)
Browse files Browse the repository at this point in the history
* fix: add quotes for cwd

* perf: short for home dir

* fix: add double-dash

* feat: sort header helps

* refactor: header switch

* test: render_help

* test

* test

* format

* rename: meta_opts to metaopts

* typecheck

* perf: remove dead code

* perf: remove useless
  • Loading branch information
linrongbin16 authored Sep 28, 2023
1 parent cd6042f commit 0468619
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 103 deletions.
30 changes: 6 additions & 24 deletions bin/general/previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ vim.rpcrequest(
)
vim.fn.chanclose(channel_id)

--- @type string
local metajsonstring = shell_helpers.readfile(metafile)
local metajsonstring = shell_helpers.readfile(metafile) --[[@as string]]
shell_helpers.log_ensure(
type(metajsonstring) == "string" and string.len(metajsonstring) > 0,
"|fzfx.bin.general.previewer| error! metajson is not string! %s",
vim.inspect(metajsonstring)
)
--- @type {previewer_type:PreviewerType}
local metajson = vim.fn.json_decode(metajsonstring) --[[@as {previewer_type:PreviewerType}]]
shell_helpers.log_debug("metajson:[%s]", vim.inspect(metajson))
local metaopts = vim.fn.json_decode(metajsonstring) --[[@as PreviewerMetaOpts]]
shell_helpers.log_debug("metajson:[%s]", vim.inspect(metaopts))

--- @param l string?
local function println(l)
Expand All @@ -67,31 +65,15 @@ local function println(l)
end
end

--- @param data_buffer string
--- @param fn_line_processor fun(l:string?):nil
local function consume(data_buffer, fn_line_processor)
local i = 1
while i <= #data_buffer do
local newline_pos = shell_helpers.string_find(data_buffer, "\n", i)
if not newline_pos then
break
end
local line = data_buffer:sub(i, newline_pos)
fn_line_processor(line)
i = newline_pos + 1
end
return i
end

if metajson.previewer_type == "command" then
if metaopts.previewer_type == "command" then
local cmd = shell_helpers.readfile(resultfile)
shell_helpers.log_debug("cmd:[%s]", vim.inspect(cmd))
if cmd == nil or string.len(cmd) == 0 then
os.exit(0)
else
os.execute(cmd)
end
elseif metajson.previewer_type == "command_list" then
elseif metaopts.previewer_type == "command_list" then
local cmd = shell_helpers.readfile(resultfile)
shell_helpers.log_debug("cmd:[%s]", vim.inspect(cmd))
if cmd == nil or string.len(cmd) == 0 then
Expand All @@ -111,7 +93,7 @@ elseif metajson.previewer_type == "command_list" then
vim.inspect(cmd_splits)
)
async_spawn:run()
elseif metajson.previewer_type == "list" then
elseif metaopts.previewer_type == "list" then
local f = io.open(resultfile, "r")
shell_helpers.log_ensure(
f ~= nil,
Expand Down
15 changes: 12 additions & 3 deletions lua/fzfx/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ local function make_file_explorer_provider(ls_args)
local function wrap(query, context)
---@diagnostic disable-next-line: undefined-field
local cwd = utils.readfile(context.cwd)
if not constants.is_windows then
cwd = vim.fn.fnamemodify(cwd, ":~")
end
if constants.has_eza then
return vim.fn.executable("echo") > 0
and string.format(
Expand Down Expand Up @@ -640,11 +643,17 @@ local function make_directory_previewer(delimiter, filename_pos, lineno_pos)
local parsed =
line_helpers.PathLine:new(line, delimiter, filename_pos, lineno_pos)
if constants.has_eza then
return { constants.eza, "--color=always", "-lha", parsed.filename }
return {
constants.eza,
"--color=always",
"-lha",
"--",
parsed.filename,
}
elseif vim.fn.executable("ls") > 0 then
return { "ls", "--color=always", "-lha", parsed.filename }
return { "ls", "--color=always", "-lha", "--", parsed.filename }
elseif constants.is_windows then
return { "dir", parsed.filename }
return { "dir", "--", parsed.filename }
else
notify.echo(LogLevels.INFO, "no ls/dir/eza/exa command found.")
return nil
Expand Down
4 changes: 0 additions & 4 deletions lua/fzfx/constants.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
-- No Setup Need

--- @type boolean
local is_windows = vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0
--- @type boolean
local is_macos = vim.fn.has("mac") > 0
--- @type boolean
local is_bsd = vim.fn.has("bsd") > 0
-- we just think others are linux
--- @type boolean
local is_linux = not is_windows
and not is_macos
and not is_bsd
Expand Down
146 changes: 74 additions & 72 deletions lua/fzfx/general.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ function ProviderSwitch:provide(name, query, context)
--- @field prepend_icon_path_position integer?

--- @type ProviderMetaOpts
local meta_opts = {
local metaopts = {
pipeline = self.pipeline,
provider_type = provider_config.provider_type,
}
if
type(provider_config.line_opts) == "table"
and provider_config.line_opts.prepend_icon_by_ft ~= nil
then
meta_opts.prepend_icon_by_ft =
metaopts.prepend_icon_by_ft =
provider_config.line_opts.prepend_icon_by_ft
end
if
Expand All @@ -130,19 +130,19 @@ function ProviderSwitch:provide(name, query, context)
)
> 0
then
meta_opts.prepend_icon_path_delimiter =
metaopts.prepend_icon_path_delimiter =
provider_config.line_opts.prepend_icon_path_delimiter
end
if
type(provider_config.line_opts) == "table"
and type(provider_config.line_opts.prepend_icon_path_position)
== "number"
then
meta_opts.prepend_icon_path_position =
metaopts.prepend_icon_path_position =
provider_config.line_opts.prepend_icon_path_position
end

local metajson = vim.fn.json_encode(meta_opts) --[[@as string]]
local metajson = vim.fn.json_encode(metaopts) --[[@as string]]
utils.writefile(self.metafile, metajson)

if provider_config.provider_type == ProviderTypeEnum.PLAIN then
Expand Down Expand Up @@ -360,10 +360,16 @@ function PreviewerSwitch:preview(name, line, context)
"|fzfx.general - PreviewerSwitch:preview| invalid previewer_type! %s",
vim.inspect(self)
)
local metajson = vim.fn.json_encode({

--- @class PreviewerMetaOpts
--- @field pipeline PipelineName
--- @field previewer_type PreviewerType
local metaopts = {
pipeline = self.pipeline,
previewer_type = previewer_type,
})
}

local metajson = vim.fn.json_encode(metaopts)
utils.writefile(self.metafile, metajson)
if previewer_type == PreviewerTypeEnum.COMMAND then
local ok, result = pcall(previewer, line, context)
Expand Down Expand Up @@ -472,79 +478,75 @@ end
--- @field headers table<PipelineName, string[]>
local HeaderSwitch = {}

--- @param name string
--- @param action string
--- @return string
local function render_help(name, action)
return color.render(
color.magenta,
"Special",
"%s to " .. table.concat(utils.string_split(name, "_"), " "),
string.upper(action)
)
end

--- @param excludes string[]|nil
--- @param s string
--- @return boolean
local function skip_help(excludes, s)
if type(excludes) ~= "table" then
return false
end
for _, e in ipairs(excludes) do
if e == s then
return true
end
end
return false
end

--- @param action_configs Configs?
--- @param builder string[]
--- @param excludes string[]|nil
--- @return string[]
local function make_help_doc(action_configs, builder, excludes)
if type(action_configs) == "table" then
local action_names = {}
for name, opts in pairs(action_configs) do
if not skip_help(excludes, name) then
table.insert(action_names, name)
end
end
table.sort(action_names)
for _, name in ipairs(action_names) do
local opts = action_configs[name]
local act = opts.key
table.insert(builder, render_help(name, act))
end
end
return builder
end

--- @param provider_configs Configs
--- @param interaction_configs Configs
--- @return HeaderSwitch
function HeaderSwitch:new(provider_configs, interaction_configs)
local headers_map = {}
if clazz.instanceof(provider_configs, ProviderConfig) then
local help_builder = {}
local provider_name = DEFAULT_PIPELINE
if type(interaction_configs) == "table" then
for interaction_name, interaction_opts in pairs(interaction_configs) do
local action_key = interaction_opts.key
table.insert(
help_builder,
color.render(
color.magenta,
"Special",
"%s to "
.. table.concat(
vim.fn.split(interaction_name, "_"),
" "
),
string.upper(action_key)
)
)
end
end
headers_map[provider_name] = help_builder
headers_map[DEFAULT_PIPELINE] = make_help_doc(interaction_configs, {})
else
log.debug(
"|fzfx.general - HeaderSwitch:new| provider_configs:%s",
vim.inspect(provider_configs)
)
for provider_name, provider_opts in pairs(provider_configs) do
local help_builder = {}
for provider_name2, provider_opts2 in pairs(provider_configs) do
local switch_key2 = string.lower(provider_opts2.key)
if provider_name2 ~= provider_name then
table.insert(
help_builder,
color.render(
color.magenta,
"Special",
"%s to "
.. table.concat(
vim.fn.split(provider_name2, "_"),
" "
),
string.upper(switch_key2)
)
)
end
end
if type(interaction_configs) == "table" then
for interaction_name, interaction_opts in
pairs(interaction_configs)
do
local action_key = interaction_opts.key
table.insert(
help_builder,
color.render(
color.magenta,
"Special",
"%s to "
.. table.concat(
vim.fn.split(interaction_name, "_"),
" "
),
string.upper(action_key)
)
)
end
end
headers_map[provider_name] = help_builder
local help_builder = make_help_doc(
provider_configs,
{},
{ provider_name }
)
headers_map[provider_name] =
make_help_doc(interaction_configs, help_builder)
end
end

Expand Down Expand Up @@ -747,10 +749,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
)
if interaction_opts.reload_after_execute then
bind_builder = bind_builder
.. string.format(
"+reload(%s)+refresh-preview",
reload_query_command
)
.. string.format("+reload(%s)", reload_query_command)
end
table.insert(fzf_opts, {
"--bind",
Expand Down Expand Up @@ -910,6 +909,9 @@ local M = {
ProviderSwitch = ProviderSwitch,
PreviewerSwitch = PreviewerSwitch,
HeaderSwitch = HeaderSwitch,
render_help = render_help,
skip_help = skip_help,
make_help_doc = make_help_doc,
}

return M
Loading

0 comments on commit 0468619

Please sign in to comment.