~cjoly/telescope-repo.nvim

b7fb147317ffb706215ef142b9ef4d6d81976015 — Clément Joly 2 years ago b898ded
fix: inconsistant return type when looking for a previewer

`find_binary` was returning a string when it did not find a binary in
the table supplied, but a table when it found something. This caused
`table.insert` to error.

`find_binary` now always returns `nil` when it does not find a suitable
executable in the supplied list. This plays quite nicely with the
previews, as when the command supplied is `nil`, we don’t get any
preview. A better behavior might be to use a vim buffer as a previewer,
but that might have performance implications (#42).

Should fix #39 (at least the initial issue)
M lua/telescope/_extensions/repo/cached_list.lua => lua/telescope/_extensions/repo/cached_list.lua +1 -1
@@ 7,7 7,7 @@ local utils = require("telescope._extensions.repo.utils")
M.prepare_command = function(opts)
    opts = opts or {}
    opts.bin = opts.bin or utils.find_locate_binary()
    if opts.bin == "" then
    if not opts.bin then
        error("Please install locate (or one of its alternatives)")
    end
    opts.cwd = vim.env.HOME

M lua/telescope/_extensions/repo/health.lua => lua/telescope/_extensions/repo/health.lua +18 -8
@@ 46,7 46,7 @@ end

local function check_list_cmd()
    local fd_bin = utils.find_fd_binary()
    if fd_bin ~= "" then
    if fd_bin then
        health.report_ok("fd: found `" .. fd_bin .. "`\n" .. get_version(fd_bin))

        local opts = {}


@@ 59,7 59,7 @@ end

local function check_cached_list_cmd()
    local locate_bin = utils.find_locate_binary()
    if locate_bin ~= "" then
    if locate_bin then
        health.report_ok("locate: found `" .. locate_bin .. "`\n" .. get_version(locate_bin))

        local opts = {}


@@ 81,16 81,26 @@ end

local function check_previewer()
    local markdown_bin = utils.find_markdown_previewer_for_document("test_doc.md")
    if markdown_bin[1] ~= utils._markdown_previewer[1][1] then
        health.report_warn("Install `" .. utils._markdown_previewer[1][1] .. "` for a better preview of markdown files")
    if not markdown_bin then
        health.report_error("No markdown previewer found, the extension will not work properly")
    else
        health.report_ok("Will use `" .. markdown_bin[1] .. "` to preview markdown READMEs")

        if markdown_bin[1] ~= utils._markdown_previewer[1][1] then
            health.report_warn("Install `" .. utils._markdown_previewer[1][1] .. "` for a better preview of markdown files")
        end
    end
    health.report_ok("Will use `" .. markdown_bin[1] .. "` to preview markdown READMEs")

    local generic_bin = utils.find_generic_previewer_for_document("test_doc")
    if generic_bin[1] ~= utils._generic_previewer[1][1] then
        health.report_warn("Install `" .. utils._generic_previewer[1][1] .. "` for a better preview of other files")
    if not generic_bin then
        health.report_error("No markdown previewer found, the extension will not work properly")
    else
        health.report_ok("Will use `" .. generic_bin[1] .. "` to preview non-markdown READMEs")

        if generic_bin[1] ~= utils._generic_previewer[1][1] then
            health.report_warn("Install `" .. utils._generic_previewer[1][1] .. "` for a better preview of other files")
        end
    end
    health.report_ok("Will use `" .. generic_bin[1] .. "` to preview non-markdown READMEs")
end

M.check = function()

M lua/telescope/_extensions/repo/list.lua => lua/telescope/_extensions/repo/list.lua +1 -1
@@ 7,7 7,7 @@ local utils = require("telescope._extensions.repo.utils")
M.prepare_command = function(opts)
    opts = opts or {}
    opts.bin = opts.bin or utils.find_fd_binary()
    if opts.bin == "" then
    if not opts.bin then
        error("fd not found, is fd installed?")
    end
    opts.cwd = vim.env.HOME

M lua/telescope/_extensions/repo/utils.lua => lua/telescope/_extensions/repo/utils.lua +7 -3
@@ 9,7 9,7 @@ local function find_binary(binaries)
            return vim.deepcopy(binary)
        end
    end
    return ""
    return nil
end

-- Find under what name fd is installed.


@@ 26,7 26,9 @@ M._generic_previewer = { { "bat", "--style", "header,grid" }, { "cat" } }

M.find_generic_previewer_for_document = function(doc)
    local l = find_binary(M._generic_previewer)
    table.insert(l, doc)
    if l then
        table.insert(l, doc)
    end
    return l
end



@@ 35,7 37,9 @@ vim.list_extend(M._markdown_previewer, M._generic_previewer)

M.find_markdown_previewer_for_document = function(doc)
    local l = find_binary(M._markdown_previewer)
    table.insert(l, doc)
    if l then
        table.insert(l, doc)
    end
    return l
end