Skip to content

Commit

Permalink
feat: handle whitespaces on path (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Aug 5, 2023
1 parent 5c33043 commit f05107b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ E(x)tended commands missing in [fzf.vim](https://github.com/junegunn/fzf.vim), a

- [Feature](#feature)
- [Requirement](#requirement)
<!-- - [For Windows](#for-windows) -->
- [Path containing whitespace issue](#path-containing-whitespace-issue)
- [Install](#install)
- [vim-plug](#vim-plug)
- [packer.nvim](#packernvim)
Expand Down Expand Up @@ -60,6 +60,27 @@ E(x)tended commands missing in [fzf.vim](https://github.com/junegunn/fzf.vim), a
cargo install --locked bat
```

### Path containing whitespace issue

fzfx.nvim internally use `nvim` and `fzf`, but when there're whitespaces on the path, launching correct shell command becomes quite difficult, since it will seriously affected shell escape characters. Here're some typical cases:

1. `C:\Program Files\Neovim\bin\nvim.exe`
2. `C:\Users\Lin Rongbin\opt\fzf\fzf.exe`

For such case, please add both of them to `%PATH%` (`$env:PATH` in PowerShell), and set the `env` configuration:

```lua
require("fzfx").setup({
...
env = {
nvim = 'nvim',
fzf = 'fzf',
}
})
```

This will help fzfx.nvim avoid the shell command issue. For complete options and default configurations, please see [Configuration](#configuration).

<!-- ### For Windows -->
<!---->
<!-- Since fzf.vim rely on the `sh` shell on Windows, so you need either: -->
Expand Down Expand Up @@ -118,7 +139,7 @@ Plug 'linrongbin16/fzfx.nvim'
call plug#end()
require('fzfx').setup()
lua require('fzfx').setup()
```

### [packer.nvim](https://github.com/wbthomason/packer.nvim)
Expand Down Expand Up @@ -204,7 +225,7 @@ require("lazy").setup({

## Configuration

For complete options and default configurations, please check: [config.lua](https://github.com/linrongbin16/fzfx.nvim/blob/80b5b806b5ef3aa9f2483579b1445675efb52634/lua/fzfx/config.lua#L12).
For complete options and default configurations, please check [config.lua](https://github.com/linrongbin16/fzfx.nvim/blob/80b5b806b5ef3aa9f2483579b1445675efb52634/lua/fzfx/config.lua#L12).

## Credit

Expand Down
1 change: 1 addition & 0 deletions lua/fzfx/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ local Defaults = {

env = {
nvim = nil,
fzf = nil,
},
debug = {
enable = false,
Expand Down
13 changes: 2 additions & 11 deletions lua/fzfx/path.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local log = require("fzfx.log")
local constants = require("fzfx.constants")

local Context = {
Expand All @@ -17,12 +16,7 @@ local function normalize(path)
end

local function sep()
if Context.sep == nil then
Context.sep = (vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0)
and "\\"
or "/"
end
return Context.sep
return (constants.is_windows and vim.o.shellslash > 0) and "\\" or "/"
end

local function join(...)
Expand All @@ -31,10 +25,7 @@ end

--- @return string
local function base_dir()
if Context.base_dir == nil then
Context.base_dir = vim.fn["fzfx#nvim#base_dir"]()
end
return Context.base_dir
return vim.fn["fzfx#nvim#base_dir"]()
end

--- @return string
Expand Down
5 changes: 3 additions & 2 deletions lua/fzfx/popup.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local log = require("fzfx.log")
local conf = require("fzfx.config")
local path = require("fzfx.path")
local shell = require("fzfx.shell")

-- --- @class WindowContext
-- --- @field bufnr integer|nil
Expand Down Expand Up @@ -310,7 +311,7 @@ end
local function make_fzf_command(fzf_opts, actions, result)
fzf_opts = merge_fzf_opts(fzf_opts, actions)

local fzf_exec = vim.fn["fzf#exec"]()
local fzf_path = shell.fzf_exec()
local builder = {}
for _, opt in ipairs(fzf_opts) do
if type(opt) == "table" and #opt == 2 then
Expand All @@ -332,7 +333,7 @@ local function make_fzf_command(fzf_opts, actions, result)
vim.inspect(builder)
)
local command =
string.format("%s %s >%s", fzf_exec, table.concat(builder, " "), result)
string.format("%s %s >%s", fzf_path, table.concat(builder, " "), result)
log.debug(
"|fzfx.popup - make_fzf_command| command:%s",
vim.inspect(command)
Expand Down
43 changes: 34 additions & 9 deletions lua/fzfx/shell.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
local log = require("fzfx.log")
local path = require("fzfx.path")
local conf = require("fzfx.config")

local Context = {
--- @type string|nil
nvim_path = nil,
--- @type string|nil
fzf_path = nil,
}

--- @return string
local function make_lua_command(...)
if Context.nvim_path == nil then
Context.nvim_path = vim.v.argv[1]
--- @return string|nil
local function nvim_exec()
local exe_list = {}
table.insert(exe_list, conf.get_config().env.nvim)
table.insert(exe_list, vim.v.argv[1])
table.insert(exe_list, vim.env.VIM)
table.insert(exe_list, "nvim")
for _, e in exe_list do
if e ~= nil and vim.fn.executable(e) > 0 then
return e
end
end
local nvim_path = Context.nvim_path
log.throw("error! failed to found executable 'nvim' on path!")
return nil
end

local conf = require("fzfx.config")
local nvim_path_conf = conf.get_config().env.nvim
if nvim_path_conf ~= nil and string.len(nvim_path_conf) > 0 then
nvim_path = nvim_path_conf
--- @return string|nil
local function fzf_exec()
local exe_list = {}
table.insert(exe_list, conf.get_config().env.fzf)
table.insert(exe_list, vim.fn["fzf#fzf_exec"]())
table.insert(exe_list, "fzf")
for _, e in exe_list do
if e ~= nil and vim.fn.executable(e) > 0 then
return e
end
end
log.throw("error! failed to found executable 'nvim' on path!")
return nil
end

--- @return string
local function make_lua_command(...)
local nvim_path = nvim_exec()
local lua_path = path.join(path.base_dir(), "bin", ...)
log.debug(
"|fzfx.shell - make_lua_command| lua_path:%s",
Expand Down

0 comments on commit f05107b

Please sign in to comment.