Skip to content

Commit

Permalink
refactor(shellcmd): use lua api and update cmd flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Jint-lzxy committed Apr 22, 2023
1 parent 5b25d9a commit 8282c80
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lua/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,32 @@ local clipboard_config = function()
end
end

local win_shell_config = function()
local shell_config = function()
if global.is_windows then
vim.o.shell = vim.fn.executable("pwsh") and "pwsh" or "powershell"
vim.o.shellcmdflag =
"-NoLogo -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new();$PSDefaultParameterValues['Out-File:Encoding']='utf-8';"
vim.o.shellredir = '2>&1 | %%{ "$_" } | Out-File %s; exit $LastExitCode'
vim.o.shellpipe = '2>&1 | %%{ "$_" } | Tee-Object %s; exit $LastExitCode'
vim.o.shellquote = ""
vim.o.shellxquote = ""
if not (vim.fn.executable("pwsh") or vim.fn.executable("powershell")) then
vim.notify(
[[
Failed to setup terminal config
PowerShell is either not installed, missing from PATH, or not executable;
cmd.exe will be used instead for `:!` (shell bang).
You're recommended to install Windows PowerShell for better experience.]],
vim.log.levels.WARN,
{ title = "[core] Runtime error" }
)
return
end

local basecmd = "-NoLogo -NonInteractive -ExecutionPolicy RemoteSigned "
local ctrlcmd =
"-Command $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding"
vim.api.nvim_set_option_value("shell", vim.fn.executable("pwsh") and "pwsh" or "powershell", {})
vim.api.nvim_set_option_value("shellcmdflag", basecmd .. ctrlcmd .. ";", {})
vim.api.nvim_set_option_value("shellredir", '2>&1 | %%{ "$_" } | Out-File %s; exit $LastExitCode', {})
vim.api.nvim_set_option_value("shellpipe", '2>&1 | %%{ "$_" } | Tee-Object %s; exit $LastExitCode', {})
vim.api.nvim_set_option_value("shellquote", nil, {})
vim.api.nvim_set_option_value("shellxquote", nil, {})
end
end

Expand All @@ -134,7 +151,7 @@ local load_core = function()

neovide_config()
clipboard_config()
win_shell_config()
shell_config()

require("core.options")
require("core.mapping")
Expand Down

0 comments on commit 8282c80

Please sign in to comment.