Skip to content

Commit

Permalink
feat(keymaps): add the show_details option
Browse files Browse the repository at this point in the history
Make it possible to show details only if the description is missing.
By default, `show_details = true`, thus it doesn't affect existing users.

Also, now "description" column contains more chars.
Previously, this column was cut more than needed.
  • Loading branch information
loyd authored and ibhagwan committed Sep 23, 2024
1 parent a34542d commit ca9b9c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,9 @@ 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_details = true,
actions = {
["enter"] = actions.keymap_apply,
["ctrl-s"] = actions.keymap_split,
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 @@ -1008,6 +1008,7 @@ M.defaults.keymaps = {
winopts = { preview = { layout = "vertical" } },
fzf_opts = { ["--tiebreak"] = "index", ["--no-multi"] = true },
ignore_patterns = { "^<SNR>", "^<Plug>" },
show_details = true,
actions = {
["enter"] = actions.keymap_apply,
["ctrl-s"] = actions.keymap_split,
Expand Down
28 changes: 22 additions & 6 deletions lua/fzf-lua/providers/nvim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ M.keymaps = function(opts)
opts = config.normalize_opts(opts, "keymaps")
if not opts then return end

local formatter = "%s │ %-14s │ %-33s │ %s"
local key_modes = opts.modes or { "n", "i", "c", "v", "t" }
local modes = {
n = "blue",
Expand All @@ -330,9 +329,25 @@ M.keymaps = function(opts)
t = "green"
}
local keymaps = {}
local format = nil

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

local add_keymap = function(keymap)
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)
end
end

local function add_keymap(keymap)
-- ignore dummy mappings
if type(keymap.rhs) == "string" and #keymap.rhs == 0 then
return
Expand All @@ -349,12 +364,13 @@ M.keymaps = function(opts)
end
end

keymap.str = string.format(formatter,
keymap.str = format(
utils.ansi_codes[modes[keymap.mode] or "blue"](keymap.mode),
keymap.lhs:gsub("%s", "<Space>"),
-- desc can be a multi-line string, normalize it
string.sub(string.gsub(keymap.desc or "", "\n%s+", "\r") or "", 1, 30),
(keymap.rhs or string.format("%s", keymap.callback)))
keymap.desc and string.gsub(keymap.desc, "\n%s+", "\r"),
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 @@ -381,7 +397,7 @@ M.keymaps = function(opts)
-- sort alphabetically
table.sort(entries)

local header_str = string.format(formatter, "m", "keymap", "description", "detail")
local header_str = format("m", "keymap", "description", "detail")
table.insert(entries, 1, header_str)

core.fzf_exec(entries, opts)
Expand Down

0 comments on commit ca9b9c7

Please sign in to comment.