Skip to content

Commit

Permalink
feat(files): default ctrl-g to toggle .gitignore
Browse files Browse the repository at this point in the history
To disable: set `files.action["ctrl-g"]=false` or run:
```lua
:lua require("fzf-lua").files({ actions = { ["ctrl-g"]=false } })
```
Additional changes:
- Enabled git-delta auto-detection as pager for git
  status|commits|bcommits, to disable set `preview_pager=false`:
```lua
:lua require("fzf-lua").git_status({ preview_pager=false })
:FzfLua git_commits preview_pager=false
```

- Replaced `<file>` placeholders with `{file}` (backward compat)
  • Loading branch information
ibhagwan committed Jan 5, 2024
1 parent 6ee0ba3 commit 0d09025
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 34 deletions.
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,12 +684,13 @@ require'fzf-lua'.setup {
args = nil,
},
git_diff = {
-- if required, use `{file}` for argument positioning
-- e.g. `cmd_modified = "git diff --color HEAD {file} | cut -c -30"`
cmd_deleted = "git diff --color HEAD --",
cmd_modified = "git diff --color HEAD",
cmd_untracked = "git diff --color --no-index /dev/null",
-- uncomment if you wish to use git-delta as pager
-- can also be set under 'git.status.preview_pager'
-- pager = "delta --width=$FZF_PREVIEW_COLUMNS",
-- git-delta is automatically detected as pager, set `pager=false`
-- to disable, can also be set under 'git.status.preview_pager'
},
man = {
-- NOTE: remove the `-c` flag when using man-db
Expand Down Expand Up @@ -720,8 +721,8 @@ require'fzf-lua'.setup {
-- neovim terminal only supports `viu` block output
["png"] = { "viu", "-b" },
-- by default the filename is added as last argument
-- if required, use `<file>` for argument positioning
["svg"] = { "chafa", "<file>" },
-- if required, use `{file}` for argument positioning
["svg"] = { "chafa", "{file}" },
["jpg"] = { "ueberzug" },
},
-- if using `ueberzug` in the above extensions map
Expand All @@ -742,8 +743,10 @@ require'fzf-lua'.setup {
},
codeaction_native = {
diff_opts = { ctxlen = 3 },
pager = vim.fn.executable("delta") == 1
and "delta --width=$FZF_PREVIEW_COLUMNS" or nil,
-- git-delta is automatically detected as pager, set `pager=false`
-- to disable, can also be set under 'lsp.code_actions.preview_pager'
-- recommended styling for delta
--pager = [[delta --width=$COLUMNS --hunk-header-style="omit" --file-style="omit"]],
},
},
-- PROVIDERS SETUP
Expand Down Expand Up @@ -810,8 +813,8 @@ require'fzf-lua'.setup {
git_icons = true,
color_icons = true,
previewer = "git_diff",
-- uncomment if you wish to use git-delta as pager
--preview_pager = "delta --width=$FZF_PREVIEW_COLUMNS",
-- git-delta is automatically detected as pager, uncomment to disable
-- preview_pager = false,
actions = {
-- actions inherit from 'actions.files' and merge
["right"] = { fn = actions.git_unstage, reload = true },
Expand All @@ -830,9 +833,9 @@ require'fzf-lua'.setup {
commits = {
prompt = 'Commits❯ ',
cmd = "git log --color --pretty=format:'%C(yellow)%h%Creset %Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset'",
preview = "git show --pretty='%Cred%H%n%Cblue%an <%ae>%n%C(yellow)%cD%n%Cgreen%s' --color {1}",
-- uncomment if you wish to use git-delta as pager
--preview_pager = "delta --width=$FZF_PREVIEW_COLUMNS",
preview = "git show --color {1}",
-- git-delta is automatically detected as pager, uncomment to disable
-- preview_pager = false,
actions = {
["default"] = actions.git_checkout,
-- remove `exec_silent` or set to `false` to exit after yank
Expand All @@ -843,13 +846,13 @@ require'fzf-lua'.setup {
prompt = 'BCommits❯ ',
-- default preview shows a git diff vs the previous commit
-- if you prefer to see the entire commit you can use:
-- git show --color {1} --rotate-to=<file>
-- git show --color {1} --rotate-to={file}
-- {1} : commit SHA (fzf field index expression)
-- <file> : filepath placement within the commands
cmd = "git log --color --pretty=format:'%C(yellow)%h%Creset %Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset' <file>",
preview = "git diff --color {1}^! -- <file>",
-- uncomment if you wish to use git-delta as pager
--preview_pager = "delta --width=$FZF_PREVIEW_COLUMNS",
-- {file} : filepath placement within the commands
cmd = "git log --color --pretty=format:'%C(yellow)%h%Creset %Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset' {file}",
preview = "git show --color {1} -- {file}",
-- git-delta is automatically detected as pager, uncomment to disable
-- preview_pager = false,
actions = {
["default"] = actions.git_buf_edit,
["ctrl-s"] = actions.git_buf_split,
Expand Down
20 changes: 12 additions & 8 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function M._default_previewer_fn()
return type(previewer) == "function" and previewer() or previewer
end

function M._preview_pager_fn()
return vim.fn.executable("delta") == 1 and "delta --width=$FZF_PREVIEW_COLUMNS" or nil
end

M.defaults = {
nbsp = utils.nbsp,
winopts = {
Expand Down Expand Up @@ -162,6 +166,7 @@ M.defaults = {
_ctor = previewers.fzf.head,
},
git_diff = {
pager = M._preview_pager_fn,
cmd_deleted = "git diff --color HEAD --",
cmd_modified = "git diff --color HEAD",
cmd_untracked = "git diff --color --no-index /dev/null",
Expand Down Expand Up @@ -199,9 +204,7 @@ M.defaults = {
codeaction_native = {
_ctor = previewers.fzf.codeaction,
diff_opts = { ctxlen = 3 },
pager = function()
return vim.fn.executable("delta") == 1 and "delta --width=$FZF_PREVIEW_COLUMNS" or nil
end,
pager = M._preview_pager_fn,
},
},
}
Expand All @@ -224,7 +227,7 @@ M.defaults.files = {
fd_opts = "--color=never --type f --hidden --follow --exclude .git",
toggle_ignore_flag = "--no-ignore",
_actions = function() return M.globals.actions.files end,
-- actions = { ["ctrl-g"] = { actions.toggle_ignore } },
actions = { ["ctrl-g"] = { actions.toggle_ignore } },
winopts = { preview = { winopts = { cursorline = false } } },
}

Expand Down Expand Up @@ -266,8 +269,8 @@ M.defaults.git = {
prompt = "Commits> ",
cmd = "git log --color --pretty=format:'%C(yellow)%h%Creset "
.. "%Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset'",
preview = "git show --pretty='%Cred%H%n%Cblue%an <%ae>%n%C(yellow)%cD%n%Cgreen%s'"
.. " --color {1}",
preview = "git show --color {1}",
preview_pager = M._preview_pager_fn,
actions = {
["default"] = actions.git_checkout,
["ctrl-y"] = { fn = actions.git_yank_commit, exec_silent = true },
Expand All @@ -277,8 +280,9 @@ M.defaults.git = {
bcommits = {
prompt = "BCommits> ",
cmd = "git log --color --pretty=format:'%C(yellow)%h%Creset "
.. "%Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset' <file>",
preview = "git diff --color {1}^! -- <file>",
.. "%Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset' {file}",
preview = "git show --color {1} -- {file}",
preview_pager = M._preview_pager_fn,
actions = {
["default"] = actions.git_buf_edit,
["ctrl-s"] = actions.git_buf_split,
Expand Down
4 changes: 2 additions & 2 deletions lua/fzf-lua/previewer/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@ function Previewer.buffer_or_file:populate_terminal_cmd(tmpbuf, cmd, entry)
end)
end
else
-- replace `<file>` placeholder with the filename
-- replace `{file}` placeholder with the filename
local add_file = true
for i, arg in ipairs(cmd) do
if arg == "<file>" then
if type(arg) == "string" and arg:match("[<{]file[}>]") then
cmd[i] = entry.path
add_file = false
end
Expand Down
13 changes: 10 additions & 3 deletions lua/fzf-lua/previewer/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ function Previewer.git_diff:new(o, opts)
self.cmd_modified = path.git_cwd(o.cmd_modified, opts)
self.cmd_untracked = path.git_cwd(o.cmd_untracked, opts)
self.pager = opts.preview_pager == nil and o.pager or opts.preview_pager
if type(self.pager) == "function" then
self.pager = self.pager()
end
do
-- populate the icon mappings
local icons_overrides = o._fn_git_icons and o._fn_git_icons()
Expand Down Expand Up @@ -313,10 +316,14 @@ function Previewer.git_diff:cmdline(o)
-- }
-- we use ':format' directly on the user's command, see
-- issue #392 for more info (limiting diff output width)
if not cmd:match("%%s") then
cmd = cmd .. " %s"
local fname_escaped = vim.fn.shellescape(file.path)
if cmd:match("[<{]file[}>]") then
cmd = cmd:gsub("[<{]file[}>]", fname_escaped)
elseif cmd:match("%%s") then
cmd:format(fname_escaped)
else
cmd = string.format("%s %s", cmd, fname_escaped)
end
cmd = cmd:format(vim.fn.shellescape(file.path))
cmd = ("LINES=%d;COLUMNS=%d;FZF_PREVIEW_LINES=%d;FZF_PREVIEW_COLUMNS=%d;%s %s")
:format(fzf_lines, fzf_columns, fzf_lines, fzf_columns, cmd, pager)
cmd = "sh -c " .. vim.fn.shellescape(cmd)
Expand Down
12 changes: 9 additions & 3 deletions lua/fzf-lua/providers/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ M.commits = function(opts)
if not opts then return end
if opts.preview then
opts.preview = path.git_cwd(opts.preview, opts)
if type(opts.preview_pager) == "function" then
opts.preview_pager = opts.preview_pager()
end
if opts.preview_pager then
opts.preview = string.format("%s | %s", opts.preview, opts.preview_pager)
end
Expand Down Expand Up @@ -132,14 +135,17 @@ M.bcommits = function(opts)
local _, sel = utils.get_visual_selection()
range = string.format("-L %d,%d:%s --no-patch", sel.start.line, sel["end"].line, file)
end
if opts.cmd:match("<file") then
opts.cmd = opts.cmd:gsub("<file>", range or file)
if opts.cmd:match("[<{]file") then
opts.cmd = opts.cmd:gsub("[<{]file[}>]", range or file)
else
opts.cmd = opts.cmd .. " " .. (range or file)
end
if type(opts.preview) == "string" then
opts.preview = opts.preview:gsub("<file>", libuv.shellescape(file))
opts.preview = opts.preview:gsub("[<{]file[}>]", libuv.shellescape(file))
opts.preview = path.git_cwd(opts.preview, opts)
if type(opts.preview_pager) == "function" then
opts.preview_pager = opts.preview_pager()
end
if opts.preview_pager then
opts.preview = string.format("%s | %s", opts.preview, opts.preview_pager)
end
Expand Down

0 comments on commit 0d09025

Please sign in to comment.