Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: shell opts for Windows #82

Merged
merged 4 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 98 additions & 4 deletions lua/fzfx/launch.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local constants = require("fzfx.constants")
local log = require("fzfx.log")
local shell = require("fzfx.shell")
local helpers = require("fzfx.helpers")
Expand Down Expand Up @@ -63,6 +64,89 @@ local function make_fzf_command(fzf_opts, actions, result)
return command
end

--- @class ShellOptsContext
--- @field shell string?
--- @field shellslash string?
--- @field shellcmdflag string?
--- @field shellxquote string?
--- @field shellquote string?
--- @field shellredir string?
--- @field shellpipe string?
--- @field shellxescape string?
local ShellOptsContext = {
shell = nil,
shellslash = nil,
shellcmdflag = nil,
shellxquote = nil,
shellquote = nil,
shellredir = nil,
shellpipe = nil,
shellxescape = nil,
}

--- @return ShellOptsContext
function ShellOptsContext:save()
local ctx = vim.tbl_deep_extend("force", vim.deepcopy(ShellOptsContext), {
shell = vim.o.shell,
shellslash = vim.o.shellslash,
shellcmdflag = vim.o.shellcmdflag,
shellxquote = vim.o.shellxquote,
shellquote = vim.o.shellquote,
shellredir = vim.o.shellredir,
shellpipe = vim.o.shellpipe,
shellxescape = vim.o.shellxescape,
})
log.debug(
"|fzfx.launch - ShellOptsContext:save| before, shell:%s, shellslash:%s, shellcmdflag:%s, shellxquote:%s, shellquote:%s, shellredir:%s, shellpipe:%s, shellxescape:%s",
vim.inspect(vim.o.shell),
vim.inspect(vim.o.shellslash),
vim.inspect(vim.o.shellcmdflag),
vim.inspect(vim.o.shellxquote),
vim.inspect(vim.o.shellquote),
vim.inspect(vim.o.shellredir),
vim.inspect(vim.o.shellpipe),
vim.inspect(vim.o.shellxescape)
)

if constants.is_windows then
vim.o.shell = "cmd.exe"
vim.o.shellslash = false
vim.o.shellcmdflag = "/s /c"
vim.o.shellxquote = '"'
vim.o.shellquote = ""
vim.o.shellredir = ">%s 2>&1"
vim.o.shellpipe = "2>&1| tee"
vim.o.shellxescape = ""
else
vim.o.shell = "sh"
end

log.debug(
"|fzfx.launch - ShellOptsContext:save| after, shell:%s, shellslash:%s, shellcmdflag:%s, shellxquote:%s, shellquote:%s, shellredir:%s, shellpipe:%s, shellxescape:%s",
vim.inspect(vim.o.shell),
vim.inspect(vim.o.shellslash),
vim.inspect(vim.o.shellcmdflag),
vim.inspect(vim.o.shellxquote),
vim.inspect(vim.o.shellquote),
vim.inspect(vim.o.shellredir),
vim.inspect(vim.o.shellpipe),
vim.inspect(vim.o.shellxescape)
)
return ctx
end

--- @return nil
function ShellOptsContext:restore()
vim.o.shell = self.shell
vim.o.shellslash = self.shellslash
vim.o.shellcmdflag = self.shellcmdflag
vim.o.shellxquote = self.shellxquote
vim.o.shellquote = self.shellquote
vim.o.shellredir = self.shellredir
vim.o.shellpipe = self.shellpipe
vim.o.shellxescape = self.shellxescape
end

--- @alias OnLaunchExit fun(launch:Launch):nil

--- @param popup Popup
Expand All @@ -73,6 +157,14 @@ end
--- @return Launch
function Launch:new(popup, source, fzf_opts, actions, on_launch_exit)
local result = vim.fn.tempname()

-- save shell opts
local shell_opts = ShellOptsContext:save()
local prev_fzf_default_opts = vim.env.FZF_DEFAULT_OPTS
local prev_fzf_default_command = vim.env.FZF_DEFAULT_COMMAND
vim.env.FZF_DEFAULT_OPTS = helpers.make_fzf_default_opts()
vim.env.FZF_DEFAULT_COMMAND = source

local fzf_command = make_fzf_command(fzf_opts, actions, result)

local function on_fzf_exit(jobid2, exitcode, event)
Expand Down Expand Up @@ -143,10 +235,6 @@ function Launch:new(popup, source, fzf_opts, actions, on_launch_exit)
end
end

local prev_fzf_default_opts = vim.env.FZF_DEFAULT_OPTS
local prev_fzf_default_command = vim.env.FZF_DEFAULT_COMMAND
vim.env.FZF_DEFAULT_OPTS = helpers.make_fzf_default_opts()
vim.env.FZF_DEFAULT_COMMAND = source
log.debug(
"|fzfx.popup - Launch:new| $FZF_DEFAULT_OPTS:%s",
vim.inspect(vim.env.FZF_DEFAULT_OPTS)
Expand All @@ -159,9 +247,15 @@ function Launch:new(popup, source, fzf_opts, actions, on_launch_exit)
"|fzfx.popup - Launch:new| fzf_command:%s",
vim.inspect(fzf_command)
)

-- launch
local jobid = vim.fn.termopen(fzf_command, { on_exit = on_fzf_exit }) --[[@as integer ]]

-- restore shell opts
shell_opts:restore()
vim.env.FZF_DEFAULT_COMMAND = prev_fzf_default_command
vim.env.FZF_DEFAULT_OPTS = prev_fzf_default_opts

vim.cmd([[ startinsert ]])

--- @type Launch
Expand Down
66 changes: 66 additions & 0 deletions lua/tests/minimal_powershell_init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
if vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0 then
vim.o.shell = "powershell.exe"
vim.o.shellslash = true
vim.o.shellxquote = ""
vim.o.shellcmdflag =
"-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command "
vim.o.shellquote = ""
vim.o.shellpipe = "| Out-File -Encoding UTF8 %s"
vim.o.shellredir = "| Out-File -Encoding UTF8 %s"
end

vim.o.number = true
vim.o.autoread = true
vim.o.autowrite = true
vim.o.swapfile = false
vim.o.confirm = true
vim.o.termguicolors = true

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)

local opts = {
defaults = { lazy = false },
}

require("lazy").setup({
"folke/tokyonight.nvim",
"nvim-tree/nvim-web-devicons",
{
"junegunn/fzf",
build = ":call fzf#install()",
},
{
"linrongbin16/fzfx.nvim",
dev = true,
dir = "~/github/linrongbin16/fzfx.nvim",
opts = {
debug = {
enable = true,
file_log = true,
console_log = false,
},
},
dependencies = {
"folke/tokyonight.nvim",
"nvim-tree/nvim-web-devicons",
"junegunn/fzf",
},
},
}, opts)

require("lazy").sync({ wait = true, show = false })

vim.cmd([[
colorscheme tokyonight
]])