Skip to content

Commit

Permalink
feat(keymaps): new option show_desc (default:true)
Browse files Browse the repository at this point in the history
ref #1566
  • Loading branch information
ibhagwan committed Dec 15, 2024
1 parent 3359cb1 commit 95f1d1d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,7 @@ require'fzf-lua'.setup {
-- by default, we ignore <Plug> and <SNR> mappings
-- set `ignore_patterns = false` to disable filtering
ignore_patterns = { "^<SNR>", "^<Plug>" },
-- by default, both description and details are shown
-- `false` shows details only if the description is missing
show_desc = true,
show_details = true,
actions = {
["enter"] = actions.keymap_apply,
Expand Down
1 change: 1 addition & 0 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ M.defaults.keymaps = {
winopts = { preview = { layout = "vertical" } },
fzf_opts = { ["--tiebreak"] = "index", ["--no-multi"] = true },
ignore_patterns = { "^<SNR>", "^<Plug>" },
show_desc = true,
show_details = true,
actions = {
["enter"] = actions.keymap_apply,
Expand Down
45 changes: 23 additions & 22 deletions lua/fzf-lua/providers/nvim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,22 +329,23 @@ M.keymaps = function(opts)
t = "green"
}
local keymaps = {}
local format = nil

if opts.show_details then
local formatter = "%s │ %-14s │ %-33s │ %s"

format = function(mode, lhs, desc, rhs)
-- we don't trim `lhs` here because it's better to violate the structure
-- than to cut off the most useful information
return string.format(formatter, mode, lhs, string.sub(desc or "", 1, 33), rhs)
end
else
local formatter = "%s │ %-14s │ %s"

format = function(mode, lhs, desc, rhs)
return string.format(formatter, mode, lhs, desc or rhs)
local separator = ""
local fields = { "mode", "lhs", "desc", "rhs" }
local field_fmt = { mode = "%s", lhs = "%-14s", desc = "%-33s", rhs = "%s" }

if opts.show_desc == false then field_fmt.desc = nil end
if opts.show_details == false then field_fmt.rhs = nil end

local format = function(info)
info.desc = string.sub(info.desc or "", 1, 33)
local ret
for _, f in ipairs(fields) do
if field_fmt[f] then
ret = string.format("%s%s" .. field_fmt[f], ret or "",
ret and string.format(" %s ", separator) or "", info[f] or "")
end
end
return ret
end

local function add_keymap(keymap)
Expand All @@ -364,13 +365,13 @@ M.keymaps = function(opts)
end
end

keymap.str = format(
utils.ansi_codes[modes[keymap.mode] or "blue"](keymap.mode),
keymap.lhs:gsub("%s", "<Space>"),
keymap.str = format({
mode = utils.ansi_codes[modes[keymap.mode] or "blue"](keymap.mode),
lhs = keymap.lhs:gsub("%s", "<Space>"),
-- desc can be a multi-line string, normalize it
keymap.desc and string.gsub(keymap.desc, "\n%s+", "\r"),
keymap.rhs or string.format("%s", keymap.callback)
)
desc = keymap.desc and string.gsub(keymap.desc, "\n%s+", "\r"),
rhs = keymap.rhs or string.format("%s", keymap.callback)
})

local k = string.format("[%s:%s:%s]", keymap.buffer, keymap.mode, keymap.lhs)
keymaps[k] = keymap
Expand All @@ -397,7 +398,7 @@ M.keymaps = function(opts)
-- sort alphabetically
table.sort(entries)

local header_str = format("m", "keymap", "description", "detail")
local header_str = format({ mode = "m", lhs = "keymap", desc = "description", rhs = "detail" })
table.insert(entries, 1, header_str)

core.fzf_exec(entries, opts)
Expand Down

0 comments on commit 95f1d1d

Please sign in to comment.