Skip to content

Commit c99a062

Browse files
authored
feat(preview): add label for files preview via 'fzf --listen' (#372)
* chore(luarc): disable third party check * chore(log): reduce logs * chore(log): reduce logs * WIP * WIP * chore * WIP * WIP * docs(depends): optional echo/curl * chore * WIP * WIP * WIP * WIP * WIP * WIP * test(yank): add tests * docs: wording * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * chore * test * chore * chore * fix(curl): ignore proxy * chore
1 parent 9e34209 commit c99a062

28 files changed

+1015
-447
lines changed

.luarc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"diagnostics.globals": ["vim", "describe", "before_each", "it"],
3-
"workspace.checkThirdParty": false
3+
"workspace.checkThirdParty": "Disable"
44
}

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ https://github.com/linrongbin16/fzfx.nvim/assets/6496887/aa5ef18c-26b4-4a93-bd0c
7070
- [Nerd fonts](https://www.nerdfonts.com/) (optional for icons).
7171
- [rg](https://github.com/BurntSushi/ripgrep) (optional for **live grep**, by default use [grep](https://man7.org/linux/man-pages/man1/grep.1.html)).
7272
- [fd](https://github.com/sharkdp/fd) (optional for **files**, by default use [find](https://man7.org/linux/man-pages/man1/find.1.html)).
73-
- [bat](https://github.com/sharkdp/bat) (optional for preview files, by default use [cat](https://man7.org/linux/man-pages/man1/cat.1.html)).
74-
- [git](https://git-scm.com/) (mandatory for **git** commands), [delta](https://github.com/dandavison/delta) (optional for preview **git diff**, **git show**, **git blame**).
75-
- [lsd](https://github.com/lsd-rs/lsd)/[eza](https://github.com/eza-community/eza) (optional for **file explorer** commands, by default use [ls](https://man7.org/linux/man-pages/man1/ls.1.html)), [echo](https://man7.org/linux/man-pages/man1/echo.1p.html) (optional for preview **file explorer** cwd).
76-
- [echo](https://man7.org/linux/man-pages/man1/echo.1p.html) (mandatory for vim **commands**, **keymaps** commands).
73+
- [bat](https://github.com/sharkdp/bat) (optional for preview files, by default use [cat](https://man7.org/linux/man-pages/man1/cat.1.html)), [curl](https://man7.org/linux/man-pages/man1/curl.1.html) (optional for preview window labels).
74+
- [echo](https://man7.org/linux/man-pages/man1/echo.1p.html) (mandatory for vim **commands**, **keymaps** commands, optional for preview window labels).
75+
- [git](https://git-scm.com/) (mandatory for **git** commands), [delta](https://github.com/dandavison/delta) (optional for preview git **diff, show, blame**).
76+
- [lsd](https://github.com/lsd-rs/lsd)/[eza](https://github.com/eza-community/eza) (optional for **file explorer** commands, by default use [ls](https://man7.org/linux/man-pages/man1/ls.1.html)).
7777

78-
> Note: `grep`, `find`, `cat`, etc are unix/linux builtin commands, while on Windows we don't have a builtin shell environment, so install rust commands such as `rg`, `fd`, `bat`, etc should be a better choice, while still recommend to [install linux shell commands on Windows](#windows) since utils like **echo**, **sleep** are often used by somewhere.
78+
> Note: `grep`, `find`, `cat` are unix/linux builtin commands, while on Windows we don't have a builtin shell environment, so install rust commands such as `rg`, `fd`, `bat` should be better choice. While still recommend Windows users [install linux shell commands](#windows) since utils like `echo`, `curl` are internally used by somewhere.
7979
8080
### Windows
8181

@@ -101,7 +101,7 @@ Install with the below 3 options:
101101

102102
<img alt="install-windows-git3.png" src="https://raw.githubusercontent.com/linrongbin16/lin.nvim.dev/main/assets/installations/install-windows-git3.png" width="70%" />
103103

104-
After this step, **git.exe** and builtin linux commands(such as **sh.exe**, **grep.exe**, **find.exe**, **sleep.exe**, **cd.exe**, **ls.exe**) will be available in `%PATH%`.
104+
After this step, **git.exe** and builtin linux commands(such as **echo.exe**, **grep.exe**, **find.exe**, **curl.exe**) will be available in `%PATH%`.
105105

106106
#### [scoop](https://scoop.sh/)
107107

@@ -113,17 +113,16 @@ Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
113113
irm get.scoop.sh | iex
114114
115115
scoop bucket add extras
116-
scoop install git
117-
scoop install mingw
118-
scoop install coreutils
119-
scoop install sleep
120-
scoop install grep
121-
scoop install findutils
116+
scoop install git # git, bash, sh
117+
scoop install coreutils # echo, ls, cat
118+
scoop install curl # curl
119+
scoop install grep # grep
120+
scoop install findutils # find
122121
```
123122

124123
#### Fix conflicts between embeded commands in `C:\Windows\System32` and portable linux commands
125124

126-
Windows actually already provide some commands (`find.exe`, `bash.exe`) in `C:\Windows\System32` (or `%SystemRoot%\system32`), which could override our installations. To fix this issue, we could prioritize the git or scoop environment variables in `%PATH%`.
125+
Windows actually already provide some commands (`find.exe`, `bash.exe`) in `C:\Windows\System32` (or `%SystemRoot%\system32`), while they are not the linux commands they are named after, but could override our installations. To fix this issue, we could prioritize the git or scoop environment variables in `%PATH%`.
127126

128127
<img alt="windows-path" src="https://github.com/linrongbin16/fzfx.nvim/assets/6496887/5296429b-daae-40f6-be16-6c065ef7bf05" width="70%" />
129128

@@ -1138,6 +1137,7 @@ local Defaults = {
11381137
marker = { "fg", "Keyword" },
11391138
spinner = { "fg", "Label" },
11401139
header = { "fg", "Comment" },
1140+
preview_label = { "fg", "PreProc" },
11411141
},
11421142

11431143
-- icons

bin/general/fzf_port.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local SELF_PATH = vim.env._FZFX_NVIM_SELF_PATH
2+
if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then
3+
io.write(string.format("|fzf_port| error! SELF_PATH is empty!"))
4+
end
5+
vim.opt.runtimepath:append(SELF_PATH)
6+
local shell_helpers = require("fzfx.shell_helpers")
7+
shell_helpers.setup("fzf_port")
8+
9+
local fzf_port_file = _G.arg[1]
10+
shell_helpers.log_debug("fzf_port_file:%s", vim.inspect(fzf_port_file))
11+
shell_helpers.log_debug("FZF_PORT:%s", vim.inspect(vim.env.FZF_PORT))
12+
13+
shell_helpers.writefile(fzf_port_file, tostring(vim.env.FZF_PORT))

bin/general/previewer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ vim.rpcrequest(
4040
local luaargs = {...}
4141
local registry_id = luaargs[1]
4242
local line = luaargs[2]
43-
return require("fzfx.rpc_helpers").call(registry_id, line)
43+
return require("fzfx.rpc_helpers").request(registry_id, line)
4444
]],
4545
{
4646
registry_id,

bin/general/provider.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ vim.rpcrequest(
3535
local luaargs = {...}
3636
local registry_id = luaargs[1]
3737
local query = luaargs[2]
38-
return require("fzfx.rpc_helpers").call(registry_id, query)
38+
return require("fzfx.rpc_helpers").request(registry_id, query)
3939
]],
4040
{
4141
registry_id,

bin/rpc/client.lua renamed to bin/rpc/notify.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
local SELF_PATH = vim.env._FZFX_NVIM_SELF_PATH
22
if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then
3-
io.write(string.format("|fzfx.bin.rpc.client| error! SELF_PATH is empty!"))
3+
io.write(string.format("|fzfx.bin.rpc.notify| error! SELF_PATH is empty!"))
44
end
55
vim.opt.runtimepath:append(SELF_PATH)
66
local shell_helpers = require("fzfx.shell_helpers")
7-
shell_helpers.setup("rpc_client")
7+
shell_helpers.setup("rpc_notify")
88

99
local SOCKET_ADDRESS = vim.env._FZFX_NVIM_SOCKET_ADDRESS
1010
shell_helpers.log_ensure(
1111
type(SOCKET_ADDRESS) == "string" and string.len(SOCKET_ADDRESS) > 0,
12-
"|fzfx.bin.rpc.client| error! SOCKET_ADDRESS must not be empty!"
12+
"error! SOCKET_ADDRESS must not be empty!"
1313
)
1414
local registry_id = _G.arg[1]
1515
local params = nil
@@ -24,10 +24,10 @@ local channel_id = vim.fn.sockconnect("pipe", SOCKET_ADDRESS, { rpc = true })
2424
shell_helpers.log_debug("channel_id:%s", vim.inspect(channel_id))
2525
shell_helpers.log_ensure(
2626
channel_id > 0,
27-
"|fzfx.bin.rpc.client| error! failed to connect socket on SOCKET_ADDRESS:%s",
27+
"error! failed to connect socket on SOCKET_ADDRESS:%s",
2828
vim.inspect(SOCKET_ADDRESS)
2929
)
30-
vim.rpcrequest(
30+
vim.rpcnotify(
3131
channel_id,
3232
"nvim_exec_lua",
3333
---@diagnostic disable-next-line: param-type-mismatch
@@ -38,7 +38,7 @@ vim.rpcrequest(
3838
if #luaargs >= 2 then
3939
params = luaargs[2]
4040
end
41-
require("fzfx.rpc_helpers").call(registry_id, params)
41+
require("fzfx.rpc_helpers").notify(registry_id, params)
4242
]],
4343
params == nil and { registry_id } or { registry_id, params }
4444
)

bin/rpc/request.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local SELF_PATH = vim.env._FZFX_NVIM_SELF_PATH
2+
if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then
3+
io.write(string.format("|fzfx.bin.rpc.request| error! SELF_PATH is empty!"))
4+
end
5+
vim.opt.runtimepath:append(SELF_PATH)
6+
local shell_helpers = require("fzfx.shell_helpers")
7+
shell_helpers.setup("rpc_request")
8+
9+
local SOCKET_ADDRESS = vim.env._FZFX_NVIM_SOCKET_ADDRESS
10+
shell_helpers.log_ensure(
11+
type(SOCKET_ADDRESS) == "string" and string.len(SOCKET_ADDRESS) > 0,
12+
"error! SOCKET_ADDRESS must not be empty!"
13+
)
14+
local registry_id = _G.arg[1]
15+
local params = nil
16+
if #_G.arg >= 2 then
17+
params = _G.arg[2]
18+
end
19+
shell_helpers.log_debug("SOCKET_ADDRESS:%s", vim.inspect(SOCKET_ADDRESS))
20+
shell_helpers.log_debug("registry_id:%s", vim.inspect(registry_id))
21+
shell_helpers.log_debug("params:%s", vim.inspect(params))
22+
23+
local channel_id = vim.fn.sockconnect("pipe", SOCKET_ADDRESS, { rpc = true })
24+
shell_helpers.log_debug("channel_id:%s", vim.inspect(channel_id))
25+
shell_helpers.log_ensure(
26+
channel_id > 0,
27+
"error! failed to connect socket on SOCKET_ADDRESS:%s",
28+
vim.inspect(SOCKET_ADDRESS)
29+
)
30+
vim.rpcrequest(
31+
channel_id,
32+
"nvim_exec_lua",
33+
---@diagnostic disable-next-line: param-type-mismatch
34+
[[
35+
local luaargs = {...}
36+
local registry_id = luaargs[1]
37+
local params = nil
38+
if #luaargs >= 2 then
39+
params = luaargs[2]
40+
end
41+
require("fzfx.rpc_helpers").request(registry_id, params)
42+
]],
43+
params == nil and { registry_id } or { registry_id, params }
44+
)
45+
vim.fn.chanclose(channel_id)

lua/fzfx.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ local function setup(options)
1414
file_log = configs.debug.file_log,
1515
})
1616

17-
if configs.debug.enable then
18-
log.debug(
19-
"|fzfx - setup| Defaults:\n%s",
20-
vim.inspect(require("fzfx.config").get_defaults())
21-
)
22-
end
23-
log.debug("|fzfx - setup| Configs:\n%s", vim.inspect(configs))
17+
-- log.debug(
18+
-- "|fzfx - setup| Defaults:\n%s",
19+
-- vim.inspect(require("fzfx.config").get_defaults())
20+
-- )
21+
-- log.debug("|fzfx - setup| Configs:\n%s", vim.inspect(configs))
2422

2523
-- cache
2624
if vim.fn.filereadable(configs.cache.dir) > 0 then

lua/fzfx/config.lua

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ local function vim_commands_previewer(line, context)
11041104
desc_or_loc.filename,
11051105
desc_or_loc.lineno
11061106
)
1107-
elseif vim.fn.executable("echo") > 0 and type(desc_or_loc) == "string" then
1107+
elseif constants.has_echo and type(desc_or_loc) == "string" then
11081108
log.debug(
11091109
"|fzfx.config - vim_commands_previewer| desc:%s",
11101110
vim.inspect(desc_or_loc)
@@ -1952,7 +1952,7 @@ local function _vim_keymaps_previewer(line, context)
19521952
def_or_loc.filename,
19531953
def_or_loc.lineno
19541954
)
1955-
elseif vim.fn.executable("echo") > 0 and type(def_or_loc) == "string" then
1955+
elseif constants.has_echo and type(def_or_loc) == "string" then
19561956
log.debug(
19571957
"|fzfx.config - vim_keymaps_previewer| desc:%s",
19581958
vim.inspect(def_or_loc)
@@ -1991,7 +1991,7 @@ local function _make_file_explorer_provider(ls_args)
19911991
local function impl(query, context)
19921992
local cwd = utils.readfile(context.cwd)
19931993
if constants.has_lsd then
1994-
return vim.fn.executable("echo") > 0
1994+
return constants.has_echo
19951995
and string.format(
19961996
"echo %s && lsd %s --color=always --header -- %s",
19971997
utils.shellescape(cwd --[[@as string]]),
@@ -2004,7 +2004,7 @@ local function _make_file_explorer_provider(ls_args)
20042004
utils.shellescape(cwd --[[@as string]])
20052005
)
20062006
elseif constants.has_eza then
2007-
return vim.fn.executable("echo") > 0
2007+
return constants.has_echo
20082008
and string.format(
20092009
"echo %s && %s --color=always %s -- %s",
20102010
utils.shellescape(cwd --[[@as string]]),
@@ -2019,7 +2019,7 @@ local function _make_file_explorer_provider(ls_args)
20192019
utils.shellescape(cwd --[[@as string]])
20202020
)
20212021
elseif vim.fn.executable("ls") > 0 then
2022-
return vim.fn.executable("echo") > 0
2022+
return constants.has_echo
20232023
and string.format(
20242024
"echo %s && ls --color=always %s %s",
20252025
utils.shellescape(cwd --[[@as string]]),
@@ -2263,10 +2263,12 @@ local Defaults = {
22632263
restricted_mode = {
22642264
previewer = _file_previewer,
22652265
previewer_type = PreviewerTypeEnum.COMMAND_LIST,
2266+
previewer_label = require("fzfx.previewer_labels").find_previewer_label,
22662267
},
22672268
unrestricted_mode = {
22682269
previewer = _file_previewer,
22692270
previewer_type = PreviewerTypeEnum.COMMAND_LIST,
2271+
previewer_label = require("fzfx.previewer_labels").find_previewer_label,
22702272
},
22712273
},
22722274
actions = {
@@ -2528,6 +2530,7 @@ local Defaults = {
25282530
previewers = {
25292531
previewer = _file_previewer,
25302532
previewer_type = PreviewerTypeEnum.COMMAND_LIST,
2533+
previewer_label = require("fzfx.previewer_labels").find_previewer_label,
25312534
},
25322535
interactions = {
25332536
delete_buffer = {
@@ -2658,10 +2661,12 @@ local Defaults = {
26582661
current_folder = {
26592662
previewer = _file_previewer,
26602663
previewer_type = PreviewerTypeEnum.COMMAND_LIST,
2664+
previewer_label = require("fzfx.previewer_labels").find_previewer_label,
26612665
},
26622666
workspace = {
26632667
previewer = _file_previewer,
26642668
previewer_type = PreviewerTypeEnum.COMMAND_LIST,
2669+
previewer_label = require("fzfx.previewer_labels").find_previewer_label,
26652670
},
26662671
},
26672672
actions = {
@@ -3997,7 +4002,7 @@ local Defaults = {
39974002
then
39984003
n = n + 1
39994004
end
4000-
if vim.fn.executable("echo") > 0 then
4005+
if constants.has_echo then
40014006
n = n + 1
40024007
end
40034008
return n > 0 and string.format("--header-lines=%d", n) or nil
@@ -4048,6 +4053,7 @@ local Defaults = {
40484053
marker = { "fg", "Keyword" },
40494054
spinner = { "fg", "Label" },
40504055
header = { "fg", "Comment" },
4056+
preview_label = { "fg", "Label" },
40514057
},
40524058

40534059
-- icons

lua/fzfx/constants.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local is_linux = not is_windows
88
and not is_macos
99
and not is_bsd
1010
and (vim.fn.has("linux") > 0 or vim.fn.has("unix") > 0)
11+
local int32_max = 2 ^ 31 - 1
1112

1213
local path_separator = is_windows and "\\" or "/"
1314

@@ -33,12 +34,16 @@ local eza = vim.fn.executable("eza") > 0 and "eza" or "exa"
3334

3435
local has_delta = vim.fn.executable("delta") > 0
3536

37+
local has_echo = vim.fn.executable("echo") > 0
38+
local has_curl = vim.fn.executable("curl") > 0
39+
3640
local M = {
3741
-- os
3842
is_windows = is_windows,
3943
is_macos = is_macos,
4044
is_bsd = is_bsd,
4145
is_linux = is_linux,
46+
int32_max = int32_max,
4247

4348
-- path
4449
path_separator = path_separator,
@@ -63,6 +68,9 @@ local M = {
6368
eza = eza,
6469

6570
has_delta = has_delta,
71+
72+
has_echo = has_echo,
73+
has_curl = has_curl,
6674
}
6775

6876
return M

0 commit comments

Comments
 (0)