瀏覽代碼

Use chcp only if sed is in PATH (#891)

chcp parsing is fragile because of the system locale. There's no convenient way to parse out the codepage value without regex just by relying on cmd.exe builtins and default binaries in PATH.

Vim can be used to parse chcp output but it requires an additional `system` per `s:system` and `chcp` can change within the same console so caching the value won't work on the terminal.

Powershell supports regex but it has a long startup even with `-NoProfile` so running it when `&shell` is not powershell slows down `:PlugInstall` more.
Jan Edmund Lazo 6 年之前
父節點
當前提交
fcfd5b7e1f
共有 1 個文件被更改,包括 7 次插入5 次删除
  1. 7 5
      plug.vim

+ 7 - 5
plug.vim

@@ -353,14 +353,16 @@ if s:is_win
 
   " Copied from fzf
   function! s:wrap_cmds(cmds)
+    let use_chcp = executable('sed')
     return map([
       \ '@echo off',
-      \ 'setlocal enabledelayedexpansion',
-      \ 'for /f "tokens=*" %%a in (''chcp'') do for %%b in (%%a) do set origchcp=%%b',
-      \ 'chcp 65001 > nul'
-    \ ]
+      \ 'setlocal enabledelayedexpansion']
+    \ + (use_chcp ? [
+      \ 'for /f "usebackq" %%a in (`chcp ^| sed "s/[^0-9]//gp"`) do set origchcp=%%a',
+      \ 'chcp 65001 > nul'] : [])
     \ + (type(a:cmds) == type([]) ? a:cmds : [a:cmds])
-    \ + ['chcp !origchcp! > nul', 'endlocal'],
+    \ + (use_chcp ? ['chcp !origchcp! > nul'] : [])
+    \ + ['endlocal'],
     \ 'v:val."\r"')
   endfunction