Skip to content

Commit

Permalink
feat(windows): substitute unix-style vars for cmd.exe (#1056)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Feb 26, 2024
1 parent fcf9806 commit b697e26
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
5 changes: 1 addition & 4 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ function M._default_previewer_fn()
end

function M._preview_pager_fn()
if vim.fn.executable("delta") ~= 1 then
return nil
end
return "delta --width=" .. utils._if_win("%COLUMNS%", "$COLUMNS")
return vim.fn.executable("delta") == 1 and "delta --width=$COLUMNS" or nil
end

M.defaults = {
Expand Down
2 changes: 1 addition & 1 deletion lua/fzf-lua/previewer/codeaction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ function M.native:cmdline(o)
return table.concat(lines, "\r\n")
end, "{}", self.opts.debug)
if self.pager and #self.pager > 0 and vim.fn.executable(self.pager:match("[^%s]+")) == 1 then
act = act .. " | " .. self.pager
act = act .. " | " .. utils._if_win_normalize_vars(self.pager)
end
return act
end
Expand Down
10 changes: 3 additions & 7 deletions lua/fzf-lua/previewer/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,9 @@ function Previewer.git_diff:cmdline(o)
local pager = ""
if self.pager and #self.pager > 0 and
vim.fn.executable(self.pager:match("[^%s]+")) == 1 then
pager = "| " .. self.pager
if utils.__IS_WINDOWS then
-- we are unable to use variables within a "cmd /c" without "!var!" variable expansion
-- https://superuser.com/questions/223104/setting-and-using-variable-within-same-command-line-in-windows-cmd-ex
pager = pager:gsub("%$[%a%d]+", function(x) return "!" .. x:sub(2) .. "!" end)
pager = pager:gsub("%%[%a%d]+%%", function(x) return "!" .. x:sub(2, #x - 1) .. "!" end)
end
-- style 2: as we are unable to use %var% within a "cmd /c" without !var! expansion
-- https://superuser.com/questions/223104/setting-and-using-variable-within-same-command-line-in-windows-cmd-ex
pager = "| " .. utils._if_win_normalize_vars(self.pager, 2)
end
-- with default commands we add the filepath at the end.
-- If the user configured a more complex command, e.g.:
Expand Down
6 changes: 4 additions & 2 deletions lua/fzf-lua/providers/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ M.commits = function(opts)
opts.preview_pager = opts.preview_pager()
end
if opts.preview_pager then
opts.preview = string.format("%s | %s", opts.preview, opts.preview_pager)
opts.preview = string.format("%s | %s", opts.preview,
utils._if_win_normalize_vars(opts.preview_pager))
end
if vim.o.shell and vim.o.shell:match("fish$") then
-- TODO: why does fish shell refuse to pass along $COLUMNS
Expand Down Expand Up @@ -152,7 +153,8 @@ M.bcommits = function(opts)
opts.preview_pager = opts.preview_pager()
end
if opts.preview_pager then
opts.preview = string.format("%s | %s", opts.preview, opts.preview_pager)
opts.preview = string.format("%s | %s", opts.preview,
utils._if_win_normalize_vars(opts.preview_pager))
end
end
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
Expand Down
15 changes: 15 additions & 0 deletions lua/fzf-lua/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ M._if_win = function(a, b)
end
end


-- Substitute unix style $VAR with
-- Style 1: %VAR%
-- Style 2: !VAR!
M._if_win_normalize_vars = function(cmd, style)
if not M.__IS_WINDOWS then return cmd end
local expander = style == 2 and "!" or "%"
cmd = cmd:gsub("%$[^%s]+", function(x) return expander .. x:sub(2) .. expander end)
if style == 2 then
-- also sub %VAR% for !VAR!
cmd = cmd:gsub("%%[^%s]+%%", function(x) return "!" .. x:sub(2, #x - 1) .. "!" end)
end
return cmd
end

M.shell_nop = function()
return M._if_win("break", "true")
end
Expand Down
17 changes: 17 additions & 0 deletions tests/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local fzf = require("fzf-lua")
local utils = fzf.utils

describe("Testing utils module", function()
it("separator", function()
utils.__IS_WINDOWS = false
assert.are.same(utils._if_win_normalize_vars("--w=$COLUMNS"), "--w=$COLUMNS")

utils.__IS_WINDOWS = true
assert.are.same(utils._if_win_normalize_vars("--w=$COLUMNS", 1), "--w=%COLUMNS%")
assert.are.same(utils._if_win_normalize_vars("--w=%COLUMNS%", 1), "--w=%COLUMNS%")
assert.are.same(utils._if_win_normalize_vars("-w=$C -l=$L", 1), "-w=%C% -l=%L%")
assert.are.same(utils._if_win_normalize_vars("--w=$COLUMNS", 2), "--w=!COLUMNS!")
assert.are.same(utils._if_win_normalize_vars("--w=%COLUMNS%", 2), "--w=!COLUMNS!")
assert.are.same(utils._if_win_normalize_vars("-w=$C -l=$L", 2), "-w=!C! -l=!L!")
end)
end)

0 comments on commit b697e26

Please sign in to comment.