From 73b359d10944cb1f16184b1b83e9c5d3cca3bf47 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:01:10 +0800 Subject: [PATCH 01/11] refactor(file explorer): part 7 --- lua/fzfx/cfg/file_explorer.lua | 184 ++++++++++++++++++++------------ spec/cfg/file_explorer_spec.lua | 27 +++-- 2 files changed, 129 insertions(+), 82 deletions(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index 0818b3cbf..ed0881385 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -2,7 +2,7 @@ local str = require("fzfx.commons.str") local path = require("fzfx.commons.path") local fileio = require("fzfx.commons.fileio") -local constants = require("fzfx.lib.constants") +local consts = require("fzfx.lib.constants") local shells = require("fzfx.lib.shells") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels @@ -81,76 +81,124 @@ M.variants = { }, } ---- @param ls_args "-lh"|"-lha" +--- @param opts {include_hidden:boolean?}? +--- @return "-lh"|"-lha" +M._parse_opts = function(opts) + opts = opts or {} + local include_hidden = opts.include_hidden or false + return include_hidden and "-lha" or "-lh" +end + +--- @param opts {include_hidden:boolean?}? --- @return fun(query:string, context:fzfx.FileExplorerPipelineContext):string? -M._make_file_explorer_provider = function(ls_args) +M._make_provider_lsd = function(opts) + local args = M._parse_opts(opts) + --- @param query string --- @param context fzfx.FileExplorerPipelineContext --- @return string? local function impl(query, context) - local cwd = fileio.readfile(context.cwd) - if constants.HAS_LSD then - return constants.HAS_ECHO - and string.format( - "echo %s && lsd %s --color=always --header -- %s", - shells.shellescape(cwd --[[@as string]]), - ls_args, - shells.shellescape(cwd --[[@as string]]) - ) - or string.format( - "lsd %s --color=always --header -- %s", - ls_args, - shells.shellescape(cwd --[[@as string]]) + local cwd = fileio.readfile(context.cwd) --[[@as string]] + return consts.HAS_ECHO + and string.format( + "echo %s && lsd %s --color=always --header -- %s", + shells.shellescape(cwd), + args, + shells.shellescape(cwd) ) - elseif constants.HAS_EZA then - if str.endswith(ls_args, "a") then - ls_args = ls_args .. "a" - end - return constants.HAS_ECHO - and string.format( - "echo %s && %s --color=always %s -- %s", - shells.shellescape(cwd --[[@as string]]), - constants.EZA, - ls_args, - shells.shellescape(cwd --[[@as string]]) - ) - or string.format( - "%s --color=always %s -- %s", - constants.EZA, - ls_args, - shells.shellescape(cwd --[[@as string]]) + or string.format("lsd %s --color=always --header -- %s", args, shells.shellescape(cwd)) + end + + return impl +end + +--- @param opts {include_hidden:boolean?}? +--- @return fun(query:string, context:fzfx.FileExplorerPipelineContext):string? +M._make_provider_eza = function(opts) + local args = M._parse_opts(opts) + + --- @param query string + --- @param context fzfx.FileExplorerPipelineContext + --- @return string? + local function impl(query, context) + local cwd = fileio.readfile(context.cwd) --[[@as string]] + if str.endswith(args, "a") then + -- eza need double 'a' to show '.' and '..' directories + args = args .. "a" + end + return consts.HAS_ECHO + and string.format( + "echo %s && %s --color=always %s -- %s", + shells.shellescape(cwd), + consts.EZA, + args, + shells.shellescape(cwd) ) - elseif constants.HAS_LS then - return constants.HAS_ECHO - and string.format( - "echo %s && ls --color=always %s %s", - shells.shellescape(cwd --[[@as string]]), - ls_args, - shells.shellescape(cwd --[[@as string]]) - ) - or string.format( - "ls --color=always %s %s", - ls_args, - shells.shellescape(cwd --[[@as string]]) + or string.format("%s --color=always %s -- %s", consts.EZA, args, shells.shellescape(cwd)) + end + + return impl +end + +--- @param opts {include_hidden:boolean?}? +--- @return fun(query:string, context:fzfx.FileExplorerPipelineContext):string? +M._make_provider_ls = function(opts) + local args = M._parse_opts(opts) + + --- @param query string + --- @param context fzfx.FileExplorerPipelineContext + --- @return string? + local function impl(query, context) + local cwd = fileio.readfile(context.cwd) --[[@as string]] + return consts.HAS_ECHO + and string.format( + "echo %s && ls --color=always %s %s", + shells.shellescape(cwd), + args, + shells.shellescape(cwd) ) - else - log.echo(LogLevels.INFO, "no ls/eza/exa command found.") - return nil - end + or string.format("ls --color=always %s %s", args, shells.shellescape(cwd)) + end + + return impl +end + +--- @return fun(query:string, context:fzfx.FileExplorerPipelineContext):string? +M._make_provider_dummy = function() + --- @param query string + --- @param context fzfx.FileExplorerPipelineContext + --- @return string? + local function impl(query, context) + log.echo(LogLevels.INFO, "no ls/eza/exa command found.") + return nil end return impl end +--- @param opts {include_hidden:boolean?}? +--- @return fun(query:string, context:fzfx.FileExplorerPipelineContext):string? +M._make_provider = function(opts) + if consts.HAS_LSD then + return M._make_provider_lsd(opts) + elseif consts.HAS_EZA then + return M._make_provider_eza(opts) + elseif consts.HAS_LS then + return M._make_provider_ls(opts) + else + return M._make_provider_dummy() + end +end + M.providers = { filter_hidden = { key = "ctrl-r", - provider = M._make_file_explorer_provider("-lh"), + provider = M._make_provider(), provider_type = ProviderTypeEnum.COMMAND, }, include_hidden = { key = "ctrl-u", - provider = M._make_file_explorer_provider("-lha"), + provider = M._make_provider({ include_hidden = true }), provider_type = ProviderTypeEnum.COMMAND, }, } @@ -158,7 +206,7 @@ M.providers = { --- @param filename string --- @return string[]|nil M._directory_previewer = function(filename) - if constants.HAS_LSD then + if consts.HAS_LSD then return { "lsd", "--color=always", @@ -167,9 +215,9 @@ M._directory_previewer = function(filename) "--", filename, } - elseif constants.HAS_EZA then + elseif consts.HAS_EZA then return { - constants.EZA, + consts.EZA, "--color=always", "-lha", "--", @@ -186,10 +234,10 @@ end --- @param line string --- @param context fzfx.FileExplorerPipelineContext --- @return string[]|nil -M._file_explorer_previewer = function(line, context) - local parsed = constants.HAS_LSD and parsers_helper.parse_lsd(line, context) +M._previewer = function(line, context) + local parsed = consts.HAS_LSD and parsers_helper.parse_lsd(line, context) or ( - constants.HAS_EZA and parsers_helper.parse_eza(line, context) + consts.HAS_EZA and parsers_helper.parse_eza(line, context) or parsers_helper.parse_ls(line, context) ) if vim.fn.filereadable(parsed.filename) > 0 then @@ -201,17 +249,17 @@ M._file_explorer_previewer = function(line, context) end end -local previewer_label = constants.HAS_LSD and labels_helper.label_lsd - or (constants.HAS_EZA and labels_helper.label_eza or labels_helper.label_ls) +local previewer_label = consts.HAS_LSD and labels_helper.label_lsd + or (consts.HAS_EZA and labels_helper.label_eza or labels_helper.label_ls) M.previewers = { filter_hidden = { - previewer = M._file_explorer_previewer, + previewer = M._previewer, previewer_type = PreviewerTypeEnum.COMMAND_LIST, previewer_label = previewer_label, }, include_hidden = { - previewer = M._file_explorer_previewer, + previewer = M._previewer, previewer_type = PreviewerTypeEnum.COMMAND_LIST, previewer_label = previewer_label, }, @@ -220,9 +268,9 @@ M.previewers = { --- @param line string --- @param context fzfx.FileExplorerPipelineContext M._cd_file_explorer = function(line, context) - local parsed = constants.HAS_LSD and parsers_helper.parse_lsd(line, context) + local parsed = consts.HAS_LSD and parsers_helper.parse_lsd(line, context) or ( - constants.HAS_EZA and parsers_helper.parse_eza(line, context) + consts.HAS_EZA and parsers_helper.parse_eza(line, context) or parsers_helper.parse_ls(line, context) ) if vim.fn.isdirectory(parsed.filename) > 0 then @@ -246,7 +294,7 @@ M._upper_file_explorer = function(line, context) log.debug("|_upper_file_explorer| target:" .. vim.inspect(target)) -- Windows root folder: `C:\` -- Unix/linux root folder: `/` - local root_len = constants.IS_WINDOWS and 3 or 1 + local root_len = consts.IS_WINDOWS and 3 or 1 if vim.fn.isdirectory(target) > 0 and string.len(target) > root_len then fileio.writefile(context.cwd, target) end @@ -276,10 +324,10 @@ M.fzf_opts = { { "--prompt", path.shorten() .. " > " }, function() local n = 0 - if constants.HAS_ECHO then + if consts.HAS_ECHO then n = n + 1 end - if constants.HAS_LSD or constants.HAS_EZA or constants.HAS_LS then + if consts.HAS_LSD or consts.HAS_EZA or consts.HAS_LS then n = n + 1 end return n > 0 and string.format("--header-lines=%d", n) or nil @@ -288,7 +336,7 @@ M.fzf_opts = { --- @alias fzfx.FileExplorerPipelineContext {bufnr:integer,winnr:integer,tabnr:integer,cwd:string} --- @return fzfx.FileExplorerPipelineContext -M._file_explorer_context_maker = function() +M._context_maker = function() local temp = vim.fn.tempname() fileio.writefile(temp --[[@as string]], vim.fn.getcwd() --[[@as string]]) local context = { @@ -301,7 +349,7 @@ M._file_explorer_context_maker = function() end M.other_opts = { - context_maker = M._file_explorer_context_maker, + context_maker = M._context_maker, } return M diff --git a/spec/cfg/file_explorer_spec.lua b/spec/cfg/file_explorer_spec.lua index 9413c8f2b..f9ce064f9 100644 --- a/spec/cfg/file_explorer_spec.lua +++ b/spec/cfg/file_explorer_spec.lua @@ -50,18 +50,17 @@ describe("fzfx.cfg.file_explorer", function() ".rw-r--r-- 28k linrongbin 8 Oct 11:37 README.md", "drwxr-xr-x - linrongbin 8 Oct 11:44 test", } - it("_file_explorer_context_maker", function() - local ctx = file_explorer_cfg._file_explorer_context_maker() - -- print(string.format("file explorer context:%s\n", vim.inspect(ctx))) + it("_context_maker", function() + local ctx = file_explorer_cfg._context_maker() assert_eq(type(ctx), "table") assert_true(ctx.bufnr > 0) assert_true(ctx.winnr > 0) assert_true(ctx.tabnr > 0) assert_true(vim.fn.filereadable(ctx.cwd) > 0) end) - it("_make_file_explorer_provider", function() - local ctx = file_explorer_cfg._file_explorer_context_maker() - local f1 = file_explorer_cfg._make_file_explorer_provider("-lh") + it("_make_provider", function() + local ctx = file_explorer_cfg._context_maker() + local f1 = file_explorer_cfg._make_provider("-lh") assert_eq(type(f1), "function") local actual1 = f1("", ctx) -- print( @@ -81,7 +80,7 @@ describe("fzfx.cfg.file_explorer", function() path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) ) > 0 ) - local f2 = file_explorer_cfg._make_file_explorer_provider("-lha") + local f2 = file_explorer_cfg._make_provider("-lha") assert_eq(type(f2), "function") local actual2 = f2("", ctx) -- print( @@ -120,11 +119,11 @@ describe("fzfx.cfg.file_explorer", function() assert_eq(actual[5], "lua/fzfx/config.lua") end end) - it("_file_explorer_previewer", function() - local ctx = file_explorer_cfg._file_explorer_context_maker() + it("_previewer", function() + local ctx = file_explorer_cfg._context_maker() if constants.HAS_LSD then for _, line in ipairs(LSD_LINES) do - local actual = file_explorer_cfg._file_explorer_previewer(line, ctx) + local actual = file_explorer_cfg._previewer(line, ctx) if actual ~= nil then assert_eq(type(actual), "table") assert_true(actual[1] == constants.BAT or actual[1] == "cat" or actual[1] == "lsd") @@ -132,7 +131,7 @@ describe("fzfx.cfg.file_explorer", function() end elseif constants.HAS_EZA then for _, line in ipairs(EZA_LINES) do - local actual = file_explorer_cfg._file_explorer_previewer(line, ctx) + local actual = file_explorer_cfg._previewer(line, ctx) if actual ~= nil then assert_eq(type(actual), "table") assert_true( @@ -142,7 +141,7 @@ describe("fzfx.cfg.file_explorer", function() end else for _, line in ipairs(LS_LINES) do - local actual = file_explorer_cfg._file_explorer_previewer(line, ctx) + local actual = file_explorer_cfg._previewer(line, ctx) if actual ~= nil then assert_eq(type(actual), "table") assert_true(actual[1] == constants.BAT or actual[1] == "cat" or actual[1] == "ls") @@ -151,7 +150,7 @@ describe("fzfx.cfg.file_explorer", function() end end) it("_cd_file_explorer", function() - local ctx = file_explorer_cfg._file_explorer_context_maker() + local ctx = file_explorer_cfg._context_maker() if constants.HAS_LSD then for _, line in ipairs(LSD_LINES) do local actual = file_explorer_cfg._cd_file_explorer(line, ctx) @@ -170,7 +169,7 @@ describe("fzfx.cfg.file_explorer", function() end end) it("_upper_file_explorer", function() - local ctx = file_explorer_cfg._file_explorer_context_maker() + local ctx = file_explorer_cfg._context_maker() if constants.HAS_LSD then for _, line in ipairs(LSD_LINES) do local actual = file_explorer_cfg._upper_file_explorer(line, ctx) From de5260ce8c0744ac0e01274e3be1123b0fd0f5fa Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:02:26 +0800 Subject: [PATCH 02/11] chore --- lua/fzfx/cfg/file_explorer.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index ed0881385..40e74242b 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -223,7 +223,7 @@ M._directory_previewer = function(filename) "--", filename, } - elseif vim.fn.executable("ls") > 0 then + elseif consts.HAS_LS then return { "ls", "--color=always", "-lha", "--", filename } else log.echo(LogLevels.INFO, "no ls/eza/exa command found.") From 8dfa64e701131ce683ebaf18875651f4a8305692 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:07:18 +0800 Subject: [PATCH 03/11] chore --- lua/fzfx/cfg/file_explorer.lua | 15 +++++--- spec/cfg/file_explorer_spec.lua | 61 ++++++++++++++++----------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index 40e74242b..cfab52a72 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -101,12 +101,18 @@ M._make_provider_lsd = function(opts) local cwd = fileio.readfile(context.cwd) --[[@as string]] return consts.HAS_ECHO and string.format( - "echo %s && lsd %s --color=always --header -- %s", + "echo %s && %s %s --color=always --header -- %s", shells.shellescape(cwd), + consts.LSD, args, shells.shellescape(cwd) ) - or string.format("lsd %s --color=always --header -- %s", args, shells.shellescape(cwd)) + or string.format( + "%s %s --color=always --header -- %s", + consts.LSD, + args, + shells.shellescape(cwd) + ) end return impl @@ -152,12 +158,13 @@ M._make_provider_ls = function(opts) local cwd = fileio.readfile(context.cwd) --[[@as string]] return consts.HAS_ECHO and string.format( - "echo %s && ls --color=always %s %s", + "echo %s && %s --color=always %s %s", shells.shellescape(cwd), + consts.LS, args, shells.shellescape(cwd) ) - or string.format("ls --color=always %s %s", args, shells.shellescape(cwd)) + or string.format("%s --color=always %s %s", consts.LS, args, shells.shellescape(cwd)) end return impl diff --git a/spec/cfg/file_explorer_spec.lua b/spec/cfg/file_explorer_spec.lua index f9ce064f9..2bba25d13 100644 --- a/spec/cfg/file_explorer_spec.lua +++ b/spec/cfg/file_explorer_spec.lua @@ -17,7 +17,7 @@ describe("fzfx.cfg.file_explorer", function() local str = require("fzfx.commons.str") local path = require("fzfx.commons.path") - local constants = require("fzfx.lib.constants") + local consts = require("fzfx.lib.constants") local contexts = require("fzfx.helper.contexts") local fzf_helpers = require("fzfx.detail.fzf_helpers") @@ -60,40 +60,39 @@ describe("fzfx.cfg.file_explorer", function() end) it("_make_provider", function() local ctx = file_explorer_cfg._context_maker() - local f1 = file_explorer_cfg._make_provider("-lh") + local f1 = file_explorer_cfg._make_provider() assert_eq(type(f1), "function") local actual1 = f1("", ctx) - -- print( - -- string.format( - -- "file explorer provider1:%s\n", - -- vim.inspect(actual1) - -- ) - -- ) + print(string.format("_make_provider-1:%s\n", vim.inspect(actual1))) assert_eq(type(actual1), "string") assert_true(str.find(actual1, "echo") > 0) - assert_true( - type(str.find(actual1, "eza")) == "number" or type(str.find(actual1, "ls")) == "number" - ) + if consts.HAS_LSD then + assert_true(str.find(actual1, consts.LSD) > 0) + elseif consts.HAS_EZA then + assert_true(str.find(actual1, consts.EZA) > 0) + else + assert_true(str.find(actual1, consts.LS) > 0) + end assert_true( str.find( actual1, path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) ) > 0 ) - local f2 = file_explorer_cfg._make_provider("-lha") + + local f2 = file_explorer_cfg._make_provider({ include_hidden = true }) assert_eq(type(f2), "function") local actual2 = f2("", ctx) - -- print( - -- string.format( - -- "file explorer provider2:%s\n", - -- vim.inspect(actual2) - -- ) - -- ) + print(string.format("_make_provider-2:%s\n", vim.inspect(actual1))) assert_eq(type(actual2), "string") assert_true(str.find(actual2, "echo") > 0) - assert_true( - type(str.find(actual2, "eza")) == "number" or type(str.find(actual2, "ls")) == "number" - ) + if consts.HAS_LSD then + assert_true(str.find(actual2, consts.LSD) > 0) + elseif consts.HAS_EZA then + assert_true(str.find(actual2, consts.EZA) > 0) + else + assert_true(str.find(actual2, consts.LS) > 0) + end assert_true( str.find( actual2, @@ -121,22 +120,20 @@ describe("fzfx.cfg.file_explorer", function() end) it("_previewer", function() local ctx = file_explorer_cfg._context_maker() - if constants.HAS_LSD then + if consts.HAS_LSD then for _, line in ipairs(LSD_LINES) do local actual = file_explorer_cfg._previewer(line, ctx) if actual ~= nil then assert_eq(type(actual), "table") - assert_true(actual[1] == constants.BAT or actual[1] == "cat" or actual[1] == "lsd") + assert_true(actual[1] == consts.BAT or actual[1] == "cat" or actual[1] == "lsd") end end - elseif constants.HAS_EZA then + elseif consts.HAS_EZA then for _, line in ipairs(EZA_LINES) do local actual = file_explorer_cfg._previewer(line, ctx) if actual ~= nil then assert_eq(type(actual), "table") - assert_true( - actual[1] == constants.BAT or actual[1] == "cat" or actual[1] == constants.EZA - ) + assert_true(actual[1] == consts.BAT or actual[1] == "cat" or actual[1] == consts.EZA) end end else @@ -144,19 +141,19 @@ describe("fzfx.cfg.file_explorer", function() local actual = file_explorer_cfg._previewer(line, ctx) if actual ~= nil then assert_eq(type(actual), "table") - assert_true(actual[1] == constants.BAT or actual[1] == "cat" or actual[1] == "ls") + assert_true(actual[1] == consts.BAT or actual[1] == "cat" or actual[1] == "ls") end end end end) it("_cd_file_explorer", function() local ctx = file_explorer_cfg._context_maker() - if constants.HAS_LSD then + if consts.HAS_LSD then for _, line in ipairs(LSD_LINES) do local actual = file_explorer_cfg._cd_file_explorer(line, ctx) assert_true(actual == nil) end - elseif constants.HAS_EZA then + elseif consts.HAS_EZA then for _, line in ipairs(EZA_LINES) do local actual = file_explorer_cfg._cd_file_explorer(line, ctx) assert_true(actual == nil) @@ -170,12 +167,12 @@ describe("fzfx.cfg.file_explorer", function() end) it("_upper_file_explorer", function() local ctx = file_explorer_cfg._context_maker() - if constants.HAS_LSD then + if consts.HAS_LSD then for _, line in ipairs(LSD_LINES) do local actual = file_explorer_cfg._upper_file_explorer(line, ctx) assert_true(actual == nil) end - elseif constants.HAS_EZA then + elseif consts.HAS_EZA then for _, line in ipairs(EZA_LINES) do local actual = file_explorer_cfg._upper_file_explorer(line, ctx) assert_true(actual == nil) From 18f90ebe442b752b0c230a5f84fc42d383a661d8 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:09:34 +0800 Subject: [PATCH 04/11] chore --- spec/cfg/file_explorer_spec.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/cfg/file_explorer_spec.lua b/spec/cfg/file_explorer_spec.lua index 2bba25d13..2421141fc 100644 --- a/spec/cfg/file_explorer_spec.lua +++ b/spec/cfg/file_explorer_spec.lua @@ -58,6 +58,16 @@ describe("fzfx.cfg.file_explorer", function() assert_true(ctx.tabnr > 0) assert_true(vim.fn.filereadable(ctx.cwd) > 0) end) + it("_parse_opts", function() + local actual1 = file_explorer_cfg._parse_opts() + assert_true(actual1 == "-lh") + local actual2 = file_explorer_cfg._parse_opts({}) + assert_true(actual2 == "-lh") + local actual3 = file_explorer_cfg._parse_opts({ include_hidden = false }) + assert_true(actual3 == "-lh") + local actual4 = file_explorer_cfg._parse_opts({ include_hidden = true }) + assert_true(actual4 == "-lha") + end) it("_make_provider", function() local ctx = file_explorer_cfg._context_maker() local f1 = file_explorer_cfg._make_provider() From 133119669a9d77ccc4ba8758b451d1c342ab6df3 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:12:20 +0800 Subject: [PATCH 05/11] chore --- spec/cfg/file_explorer_spec.lua | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/spec/cfg/file_explorer_spec.lua b/spec/cfg/file_explorer_spec.lua index 2421141fc..a8080970c 100644 --- a/spec/cfg/file_explorer_spec.lua +++ b/spec/cfg/file_explorer_spec.lua @@ -68,6 +68,62 @@ describe("fzfx.cfg.file_explorer", function() local actual4 = file_explorer_cfg._parse_opts({ include_hidden = true }) assert_true(actual4 == "-lha") end) + it("_make_provider_lsd", function() + local ctx = file_explorer_cfg._context_maker() + local f = file_explorer_cfg._make_provider_lsd() + assert_eq(type(f), "function") + local actual = f("", ctx) + print(string.format("_make_provider_lsd-1:%s\n", vim.inspect(actual))) + assert_eq(type(actual), "string") + assert_true(str.find(actual, "echo") > 0) + assert_true(str.find(actual, consts.LSD) > 0) + assert_true( + str.find( + actual, + path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) + ) > 0 + ) + end) + it("_make_provider_eza", function() + local ctx = file_explorer_cfg._context_maker() + local f = file_explorer_cfg._make_provider_eza() + assert_eq(type(f), "function") + local actual = f("", ctx) + print(string.format("_make_provider_eza-1:%s\n", vim.inspect(actual))) + assert_eq(type(actual), "string") + assert_true(str.find(actual, "echo") > 0) + assert_true(str.find(actual, consts.EZA) > 0) + assert_true( + str.find( + actual, + path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) + ) > 0 + ) + end) + it("_make_provider_ls", function() + local ctx = file_explorer_cfg._context_maker() + local f = file_explorer_cfg._make_provider_ls() + assert_eq(type(f), "function") + local actual = f("", ctx) + print(string.format("_make_provider_ls-1:%s\n", vim.inspect(actual))) + assert_eq(type(actual), "string") + assert_true(str.find(actual, "echo") > 0) + assert_true(str.find(actual, consts.LS) > 0) + assert_true( + str.find( + actual, + path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) + ) > 0 + ) + end) + it("_make_provider_dummy", function() + local ctx = file_explorer_cfg._context_maker() + local f = file_explorer_cfg._make_provider_dummy() + assert_eq(type(f), "function") + local actual = f("", ctx) + print(string.format("_make_provider_dummy-1:%s\n", vim.inspect(actual))) + assert_true(actual == nil) + end) it("_make_provider", function() local ctx = file_explorer_cfg._context_maker() local f1 = file_explorer_cfg._make_provider() From a4a5d6ffd283c87562fed86c8e7154b847250e74 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:21:41 +0800 Subject: [PATCH 06/11] chore --- spec/cfg/file_explorer_spec.lua | 63 +++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/spec/cfg/file_explorer_spec.lua b/spec/cfg/file_explorer_spec.lua index a8080970c..416f75f8e 100644 --- a/spec/cfg/file_explorer_spec.lua +++ b/spec/cfg/file_explorer_spec.lua @@ -68,7 +68,7 @@ describe("fzfx.cfg.file_explorer", function() local actual4 = file_explorer_cfg._parse_opts({ include_hidden = true }) assert_true(actual4 == "-lha") end) - it("_make_provider_lsd", function() + it("_make_provider_lsd case-1", function() local ctx = file_explorer_cfg._context_maker() local f = file_explorer_cfg._make_provider_lsd() assert_eq(type(f), "function") @@ -77,6 +77,8 @@ describe("fzfx.cfg.file_explorer", function() assert_eq(type(actual), "string") assert_true(str.find(actual, "echo") > 0) assert_true(str.find(actual, consts.LSD) > 0) + assert_true(str.find(actual, "-lh") > 0) + assert_true(str.find(actual, "-lha") == nil) assert_true( str.find( actual, @@ -84,7 +86,24 @@ describe("fzfx.cfg.file_explorer", function() ) > 0 ) end) - it("_make_provider_eza", function() + it("_make_provider_lsd case-2", function() + local ctx = file_explorer_cfg._context_maker() + local f = file_explorer_cfg._make_provider_lsd({ include_hidden = true }) + assert_eq(type(f), "function") + local actual2 = f("", ctx) + print(string.format("_make_provider_lsd-2:%s\n", vim.inspect(actual2))) + assert_eq(type(actual2), "string") + assert_true(str.find(actual2, "echo") > 0) + assert_true(str.find(actual2, consts.LSD) > 0) + assert_true(str.find(actual2, "-lha") > 0) + assert_true( + str.find( + actual2, + path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) + ) > 0 + ) + end) + it("_make_provider_eza case-1", function() local ctx = file_explorer_cfg._context_maker() local f = file_explorer_cfg._make_provider_eza() assert_eq(type(f), "function") @@ -93,6 +112,8 @@ describe("fzfx.cfg.file_explorer", function() assert_eq(type(actual), "string") assert_true(str.find(actual, "echo") > 0) assert_true(str.find(actual, consts.EZA) > 0) + assert_true(str.find(actual, "-lh") > 0) + assert_true(str.find(actual, "-lha") == nil) assert_true( str.find( actual, @@ -100,7 +121,24 @@ describe("fzfx.cfg.file_explorer", function() ) > 0 ) end) - it("_make_provider_ls", function() + it("_make_provider_eza case-2", function() + local ctx = file_explorer_cfg._context_maker() + local f = file_explorer_cfg._make_provider_eza({ include_hidden = true }) + assert_eq(type(f), "function") + local actual = f("", ctx) + print(string.format("_make_provider_eza-2:%s\n", vim.inspect(actual))) + assert_eq(type(actual), "string") + assert_true(str.find(actual, "echo") > 0) + assert_true(str.find(actual, consts.EZA) > 0) + assert_true(str.find(actual, "-lhaa") > 0) + assert_true( + str.find( + actual, + path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) + ) > 0 + ) + end) + it("_make_provider_ls case-1", function() local ctx = file_explorer_cfg._context_maker() local f = file_explorer_cfg._make_provider_ls() assert_eq(type(f), "function") @@ -109,6 +147,25 @@ describe("fzfx.cfg.file_explorer", function() assert_eq(type(actual), "string") assert_true(str.find(actual, "echo") > 0) assert_true(str.find(actual, consts.LS) > 0) + assert_true(str.find(actual, "-lh") > 0) + assert_true(str.find(actual, "-lha") == nil) + assert_true( + str.find( + actual, + path.normalize(vim.fn.getcwd(), { double_backslash = true, expand = true }) + ) > 0 + ) + end) + it("_make_provider_ls case-2", function() + local ctx = file_explorer_cfg._context_maker() + local f = file_explorer_cfg._make_provider_ls({ include_hidden = true }) + assert_eq(type(f), "function") + local actual = f("", ctx) + print(string.format("_make_provider_ls-2:%s\n", vim.inspect(actual))) + assert_eq(type(actual), "string") + assert_true(str.find(actual, "echo") > 0) + assert_true(str.find(actual, consts.LS) > 0) + assert_true(str.find(actual, "-lha") > 0) assert_true( str.find( actual, From 83d6447e0914d339b5435ea80277da264b41db49 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:28:18 +0800 Subject: [PATCH 07/11] chore --- lua/fzfx/cfg/file_explorer.lua | 65 +++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index cfab52a72..e4fe59fba 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -210,28 +210,61 @@ M.providers = { }, } +M._PREVIEW_DIRECTORY_LSD = { + consts.LSD, + "--color=always", + "-lha", + "--header", + "--", +} + +M._PREVIEW_DIRECTORY_EZA = { + consts.EZA, + "--color=always", + "-lha", + "--", +} + +M._PREVIEW_DIRECTORY_LS = { + consts.LS, + "--color=always", + "-lha", + "--", +} + +--- @param filename string +--- @return string[] +M._directory_previewer_lsd = function(filename) + local args = vim.deepcopy(M._PREVIEW_DIRECTORY_LSD) + table.insert(args, filename) + return args +end + +--- @param filename string +--- @return string[] +M._directory_previewer_eza = function(filename) + local args = vim.deepcopy(M._PREVIEW_DIRECTORY_EZA) + table.insert(args, filename) + return args +end + +--- @param filename string +--- @return string[] +M._directory_previewer_ls = function(filename) + local args = vim.deepcopy(M._PREVIEW_DIRECTORY_LS) + table.insert(args, filename) + return args +end + --- @param filename string --- @return string[]|nil M._directory_previewer = function(filename) if consts.HAS_LSD then - return { - "lsd", - "--color=always", - "-lha", - "--header", - "--", - filename, - } + return M._directory_previewer_lsd(filename) elseif consts.HAS_EZA then - return { - consts.EZA, - "--color=always", - "-lha", - "--", - filename, - } + return M._directory_previewer_eza(filename) elseif consts.HAS_LS then - return { "ls", "--color=always", "-lha", "--", filename } + return M._directory_previewer_ls(filename) else log.echo(LogLevels.INFO, "no ls/eza/exa command found.") return nil From cc5c0752c150684ea6a7f810b72f4f8b943cca8d Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:38:36 +0800 Subject: [PATCH 08/11] chore --- lua/fzfx/cfg/file_explorer.lua | 56 +++++++++++++++------------- spec/cfg/file_explorer_spec.lua | 65 ++++++++++++++++++++++++++------- 2 files changed, 82 insertions(+), 39 deletions(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index e4fe59fba..c2686e354 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -210,7 +210,7 @@ M.providers = { }, } -M._PREVIEW_DIRECTORY_LSD = { +M._DIRECTORY_PREVIEWER_LSD = { consts.LSD, "--color=always", "-lha", @@ -218,57 +218,61 @@ M._PREVIEW_DIRECTORY_LSD = { "--", } -M._PREVIEW_DIRECTORY_EZA = { +M._DIRECTORY_PREVIEWER_EZA = { consts.EZA, "--color=always", "-lha", "--", } -M._PREVIEW_DIRECTORY_LS = { +M._DIRECTORY_PREVIEWER_LS = { consts.LS, "--color=always", "-lha", "--", } ---- @param filename string ---- @return string[] -M._directory_previewer_lsd = function(filename) - local args = vim.deepcopy(M._PREVIEW_DIRECTORY_LSD) - table.insert(args, filename) - return args -end +--- @param opts {lsd:boolean?,eza:boolean?,ls:boolean?}? +--- @return fun(filename:string):string[] +M._make_directory_previewer = function(opts) + opts = opts or {} ---- @param filename string ---- @return string[] -M._directory_previewer_eza = function(filename) - local args = vim.deepcopy(M._PREVIEW_DIRECTORY_EZA) - table.insert(args, filename) - return args -end + local args + if opts.lsd then + args = vim.deepcopy(M._DIRECTORY_PREVIEWER_LSD) + elseif opts.eza then + args = vim.deepcopy(M._DIRECTORY_PREVIEWER_EZA) + elseif opts.ls then + args = vim.deepcopy(M._DIRECTORY_PREVIEWER_LS) + end ---- @param filename string ---- @return string[] -M._directory_previewer_ls = function(filename) - local args = vim.deepcopy(M._PREVIEW_DIRECTORY_LS) - table.insert(args, filename) - return args + --- @param filename string + --- @return string[] + local function impl(filename) + table.insert(args, filename) + return args + end + + return impl end --- @param filename string --- @return string[]|nil M._directory_previewer = function(filename) + local f + if consts.HAS_LSD then - return M._directory_previewer_lsd(filename) + f = M._make_directory_previewer({ lsd = true }) elseif consts.HAS_EZA then - return M._directory_previewer_eza(filename) + f = M._make_directory_previewer({ eza = true }) elseif consts.HAS_LS then - return M._directory_previewer_ls(filename) + f = M._make_directory_previewer({ ls = true }) else log.echo(LogLevels.INFO, "no ls/eza/exa command found.") return nil end + + return f(filename) end --- @param line string diff --git a/spec/cfg/file_explorer_spec.lua b/spec/cfg/file_explorer_spec.lua index 416f75f8e..617d48f2d 100644 --- a/spec/cfg/file_explorer_spec.lua +++ b/spec/cfg/file_explorer_spec.lua @@ -223,22 +223,61 @@ describe("fzfx.cfg.file_explorer", function() ) > 0 ) end) + it("_make_directory_previewer with lsd", function() + local input = "lua/fzfx/config.lua" + local f = file_explorer_cfg._make_directory_previewer({ lsd = true }) + local actual = f(input) + assert_eq(type(actual), "table") + local n = #file_explorer_cfg._DIRECTORY_PREVIEWER_LSD + for i = 1, n do + assert_eq(actual[i], file_explorer_cfg._DIRECTORY_PREVIEWER_LSD[i]) + end + assert_eq(actual[#actual], input) + end) + it("_make_directory_previewer with eza", function() + local input = "lua/fzfx/config.lua" + local f = file_explorer_cfg._make_directory_previewer({ eza = true }) + local actual = f(input) + assert_eq(type(actual), "table") + local n = #file_explorer_cfg._DIRECTORY_PREVIEWER_EZA + for i = 1, n do + assert_eq(actual[i], file_explorer_cfg._DIRECTORY_PREVIEWER_EZA[i]) + end + assert_eq(actual[#actual], input) + end) + it("_make_directory_previewer with ls", function() + local input = "lua/fzfx/config.lua" + local f = file_explorer_cfg._make_directory_previewer({ ls = true }) + local actual = f(input) + assert_eq(type(actual), "table") + local n = #file_explorer_cfg._DIRECTORY_PREVIEWER_LS + for i = 1, n do + assert_eq(actual[i], file_explorer_cfg._DIRECTORY_PREVIEWER_LS[i]) + end + assert_eq(actual[#actual], input) + end) it("_directory_previewer", function() - local actual = file_explorer_cfg._directory_previewer("lua/fzfx/config.lua") + local input = "lua/fzfx/config.lua" + local actual = file_explorer_cfg._directory_previewer(input) assert_eq(type(actual), "table") - if actual[1] == "lsd" then - assert_eq(actual[1], "lsd") - assert_eq(actual[2], "--color=always") - assert_eq(actual[3], "-lha") - assert_eq(actual[4], "--header") - assert_eq(actual[5], "--") - assert_eq(actual[6], "lua/fzfx/config.lua") + if consts.HAS_LSD then + local n = #file_explorer_cfg._DIRECTORY_PREVIEWER_LSD + for i = 1, n do + assert_eq(actual[i], file_explorer_cfg._DIRECTORY_PREVIEWER_LSD[i]) + end + assert_eq(actual[#actual], input) + elseif consts.HAS_EZA then + local n = #file_explorer_cfg._DIRECTORY_PREVIEWER_EZA + for i = 1, n do + assert_eq(actual[i], file_explorer_cfg._DIRECTORY_PREVIEWER_EZA[i]) + end + assert_eq(actual[#actual], input) else - assert_true(actual[1] == "eza" or actual[1] == "ls") - assert_eq(actual[2], "--color=always") - assert_eq(actual[3], "-lha") - assert_eq(actual[4], "--") - assert_eq(actual[5], "lua/fzfx/config.lua") + local n = #file_explorer_cfg._DIRECTORY_PREVIEWER_LS + for i = 1, n do + assert_eq(actual[i], file_explorer_cfg._DIRECTORY_PREVIEWER_LS[i]) + end + assert_eq(actual[#actual], input) end end) it("_previewer", function() From 7f674441bb10673c4475952844d07fbc6171942b Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:47:28 +0800 Subject: [PATCH 09/11] chore --- lua/fzfx/cfg/file_explorer.lua | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index c2686e354..609141199 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -279,14 +279,19 @@ end --- @param context fzfx.FileExplorerPipelineContext --- @return string[]|nil M._previewer = function(line, context) - local parsed = consts.HAS_LSD and parsers_helper.parse_lsd(line, context) - or ( - consts.HAS_EZA and parsers_helper.parse_eza(line, context) - or parsers_helper.parse_ls(line, context) - ) - if vim.fn.filereadable(parsed.filename) > 0 then + local parser + if consts.HAS_LSD then + parser = parsers_helper.parse_lsd + elseif consts.HAS_EZA then + parser = parsers_helper.parse_eza + else + parser = parsers_helper.parse_ls + end + local parsed = parser(line, context) + + if path.isfile(parsed.filename) then return previewers_helper.preview_files(parsed.filename) - elseif vim.fn.isdirectory(parsed.filename) > 0 then + elseif path.isdir(parsed.filename) then return M._directory_previewer(parsed.filename) else return nil From 681a93382c4295dc5fd09771bcee1314fb1286b7 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:48:58 +0800 Subject: [PATCH 10/11] chore --- lua/fzfx/cfg/file_explorer.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index 609141199..a8e992e27 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -298,8 +298,14 @@ M._previewer = function(line, context) end end -local previewer_label = consts.HAS_LSD and labels_helper.label_lsd - or (consts.HAS_EZA and labels_helper.label_eza or labels_helper.label_ls) +local previewer_label +if consts.HAS_LSD then + previewer_label = labels_helper.label_lsd +elseif consts.HAS_EZA then + previewer_label = labels_helper.label_eza +else + previewer_label = labels_helper.label_ls +end M.previewers = { filter_hidden = { From bbcc2e46187806d9e674bb9049d5ff5ef4fdc53a Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 18 Jun 2024 16:52:22 +0800 Subject: [PATCH 11/11] chore --- lua/fzfx/cfg/file_explorer.lua | 48 ++++++++++++++++----------------- spec/cfg/file_explorer_spec.lua | 20 ++++++-------- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index a8e992e27..5807635a8 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -275,18 +275,19 @@ M._directory_previewer = function(filename) return f(filename) end +local parser +if consts.HAS_LSD then + parser = parsers_helper.parse_lsd +elseif consts.HAS_EZA then + parser = parsers_helper.parse_eza +else + parser = parsers_helper.parse_ls +end + --- @param line string --- @param context fzfx.FileExplorerPipelineContext --- @return string[]|nil M._previewer = function(line, context) - local parser - if consts.HAS_LSD then - parser = parsers_helper.parse_lsd - elseif consts.HAS_EZA then - parser = parsers_helper.parse_eza - else - parser = parsers_helper.parse_ls - end local parsed = parser(line, context) if path.isfile(parsed.filename) then @@ -323,12 +324,9 @@ M.previewers = { --- @param line string --- @param context fzfx.FileExplorerPipelineContext M._cd_file_explorer = function(line, context) - local parsed = consts.HAS_LSD and parsers_helper.parse_lsd(line, context) - or ( - consts.HAS_EZA and parsers_helper.parse_eza(line, context) - or parsers_helper.parse_ls(line, context) - ) - if vim.fn.isdirectory(parsed.filename) > 0 then + local parsed = parser(line, context) + + if path.isdir(parsed.filename) then fileio.writefile(context.cwd, parsed.filename) end end @@ -336,21 +334,21 @@ end --- @param line string --- @param context fzfx.FileExplorerPipelineContext M._upper_file_explorer = function(line, context) - log.debug( - string.format( - "|_upper_file_explorer| line:%s, context:%s", - vim.inspect(line), - vim.inspect(context) - ) - ) + -- log.debug( + -- string.format( + -- "|_upper_file_explorer| line:%s, context:%s", + -- vim.inspect(line), + -- vim.inspect(context) + -- ) + -- ) local cwd = fileio.readfile(context.cwd) --[[@as string]] - log.debug("|_upper_file_explorer| cwd:" .. vim.inspect(cwd)) + -- log.debug("|_upper_file_explorer| cwd:" .. vim.inspect(cwd)) local target = vim.fn.fnamemodify(cwd, ":h") --[[@as string]] - log.debug("|_upper_file_explorer| target:" .. vim.inspect(target)) + -- log.debug("|_upper_file_explorer| target:" .. vim.inspect(target)) -- Windows root folder: `C:\` -- Unix/linux root folder: `/` - local root_len = consts.IS_WINDOWS and 3 or 1 - if vim.fn.isdirectory(target) > 0 and string.len(target) > root_len then + local root_dir_len = consts.IS_WINDOWS and 3 or 1 + if path.isdir(target) and string.len(target) > root_dir_len then fileio.writefile(context.cwd, target) end end diff --git a/spec/cfg/file_explorer_spec.lua b/spec/cfg/file_explorer_spec.lua index 617d48f2d..0174f2433 100644 --- a/spec/cfg/file_explorer_spec.lua +++ b/spec/cfg/file_explorer_spec.lua @@ -312,39 +312,35 @@ describe("fzfx.cfg.file_explorer", function() local ctx = file_explorer_cfg._context_maker() if consts.HAS_LSD then for _, line in ipairs(LSD_LINES) do - local actual = file_explorer_cfg._cd_file_explorer(line, ctx) - assert_true(actual == nil) + file_explorer_cfg._cd_file_explorer(line, ctx) end elseif consts.HAS_EZA then for _, line in ipairs(EZA_LINES) do - local actual = file_explorer_cfg._cd_file_explorer(line, ctx) - assert_true(actual == nil) + file_explorer_cfg._cd_file_explorer(line, ctx) end else for _, line in ipairs(LS_LINES) do - local actual = file_explorer_cfg._cd_file_explorer(line, ctx) - assert_true(actual == nil) + file_explorer_cfg._cd_file_explorer(line, ctx) end end + assert_true(true) end) it("_upper_file_explorer", function() local ctx = file_explorer_cfg._context_maker() if consts.HAS_LSD then for _, line in ipairs(LSD_LINES) do - local actual = file_explorer_cfg._upper_file_explorer(line, ctx) - assert_true(actual == nil) + file_explorer_cfg._upper_file_explorer(line, ctx) end elseif consts.HAS_EZA then for _, line in ipairs(EZA_LINES) do - local actual = file_explorer_cfg._upper_file_explorer(line, ctx) - assert_true(actual == nil) + file_explorer_cfg._upper_file_explorer(line, ctx) end else for _, line in ipairs(LS_LINES) do - local actual = file_explorer_cfg._upper_file_explorer(line, ctx) - assert_true(actual == nil) + file_explorer_cfg._upper_file_explorer(line, ctx) end end + assert_true(true) end) end) end)