Skip to content

Commit ba11f96

Browse files
committed
refactor: rename 'auto_run' action from 'files' to 'find' and refactor 'on_mount' hooks
The 'on_mount' hook 'auto_run' option 'files' has been renamed to 'find' for better clarity and consistency. Introduced 'normalize_action' helper to standardize 'auto_run' values, handling aliases and defaulting to 'find' for unrecognized values. Refactored 'Hooks.on_mount' to improve readability and maintainability by using helper functions 'normalize_action' and 'find_connection_by_mount'.
1 parent 0707de1 commit ba11f96

File tree

3 files changed

+96
-54
lines changed

3 files changed

+96
-54
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Auto-detects your tools: **snacks**, **telescope**, **fzf-lua**, **mini**, **oil
3131

3232
- **Zero dependencies** - Works with your existing file pickers and search tools, no forced plugins
3333
- **Auto-detection** - Launches telescope, oil, snacks, fzf-lua, mini, yazi, neo-tree, nvim-tree, ranger, lf, nnn, or netrw
34-
- **On-mount hooks** - Run your own function or built-ins (files, grep, live find/grep) right after a mount
34+
- **On-mount hooks** - Run your own function or built-ins (find, grep, live find/grep) right after a mount
3535
- **Live remote search** - Stream `rg`/`find` over SSH with snacks, fzf-lua, telescope, or mini (no local mount thrashing)
3636
- **Flexible workflow** - Explore files, change directories (`tcd`), run custom commands, or open SSH terminals
3737
- **Universal auth** - Handles SSH keys, 2FA, passwords, passphrases, host verification via floating terminal
@@ -157,7 +157,7 @@ require("sshfs").setup({
157157
},
158158
on_mount = {
159159
auto_change_to_dir = false, -- auto-change current directory to mount point
160-
auto_run = "files", -- "files" (default), "grep", "live_find", "live_grep", "terminal", "none", or a custom function(ctx)
160+
auto_run = "find", -- "find" (default), "grep", "live_find", "live_grep", "terminal", "none", or a custom function(ctx)
161161
},
162162
},
163163
ui = {

lua/sshfs/config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ local DEFAULT_CONFIG = {
3939
on_mount = {
4040
auto_change_to_dir = false, -- auto-change current directory to mount point
4141
-- Action to run after a successful mount
42-
-- "files" (default): open file picker
42+
-- "find" (default): open file picker
4343
-- "grep": open grep picker
4444
-- "live_find": run remote find over SSH and stream results
4545
-- "live_grep": run remote rg/grep over SSH and stream results
4646
-- "terminal": open SSH terminal to the mounted host
4747
-- "none" or nil: do nothing
4848
-- function(ctx): custom handler with { mount_path, host, remote_path }
49-
auto_run = "files",
49+
auto_run = "find",
5050
},
5151
on_exit = {
5252
auto_unmount = true, -- auto-disconnect all mounts on :q or exit

lua/sshfs/ui/hooks.lua

Lines changed: 92 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,128 @@
22

33
local Hooks = {}
44

5-
--- Run post-mount action (tcd + optional hook)
6-
--- @param mount_dir string Mount directory path
7-
--- @param host string Host name
8-
--- @param remote_path string|nil Remote path used for the mount
9-
--- @param config table Plugin configuration
10-
function Hooks.on_mount(mount_dir, host, remote_path, config)
11-
local hook_cfg = (config.hooks and config.hooks.on_mount) or {}
12-
local auto_change_to_dir = hook_cfg.auto_change_to_dir
13-
local action = hook_cfg.auto_run
14-
15-
-- Auto-change directory to mount point if configured
16-
if auto_change_to_dir then
17-
vim.cmd("tcd " .. vim.fn.fnameescape(mount_dir))
18-
end
19-
5+
-- Normalize hook config value into either a callable or a known preset string.
6+
--- @param action string|function|nil
7+
--- @return string|function|nil normalized
8+
local function normalize_action(action)
209
if action == nil or action == "none" then
21-
return
10+
return nil
2211
end
2312

24-
-- Allow custom handler
2513
if type(action) == "function" then
26-
local ok, err = pcall(action, {
27-
mount_path = mount_dir,
28-
host = host,
29-
remote_path = remote_path,
30-
})
31-
if not ok then
32-
vim.notify("on_mount callback failed: " .. err, vim.log.levels.ERROR)
33-
end
34-
return
14+
return action
3515
end
3616

37-
-- Normalize preset names
3817
local preset = string.lower(action)
39-
if preset == "livefiles" then
18+
if preset == "livefind" or preset == "live-find" then
4019
preset = "live_find"
41-
elseif preset == "livegrep" then
20+
elseif preset == "livegrep" or preset == "live-grep" then
4221
preset = "live_grep"
43-
elseif preset == "files" or preset == "live_find" or preset == "live_grep" or preset == "grep" or preset == "terminal" then
44-
-- keep as is
45-
else
46-
preset = "files"
4722
end
4823

49-
-- Resolve connection info for live_* actions
50-
local function get_connection()
51-
local Connections = require("sshfs.lib.connections")
52-
for _, conn in ipairs(Connections.get_all()) do
53-
if conn.mount_path == mount_dir then
54-
return conn
55-
end
24+
local allowed = {
25+
find = true,
26+
live_find = true,
27+
live_grep = true,
28+
grep = true,
29+
terminal = true,
30+
}
31+
32+
if not allowed[preset] then
33+
preset = "find"
34+
end
35+
36+
return preset
37+
end
38+
39+
-- Fetch the connection associated with a mount directory.
40+
--- @param mount_dir string
41+
--- @return table|nil connection
42+
local function find_connection_by_mount(mount_dir)
43+
local Connections = require("sshfs.lib.connections")
44+
for _, conn in ipairs(Connections.get_all()) do
45+
if conn.mount_path == mount_dir then
46+
return conn
5647
end
57-
return nil
5848
end
49+
return nil
50+
end
5951

60-
if preset == "files" then
52+
-- Execute one of the built-in preset actions.
53+
--- @param preset string
54+
--- @param mount_dir string
55+
--- @param config table
56+
--- @return nil
57+
local function run_preset_action(preset, mount_dir, config)
58+
if preset == "find" then
6159
local Picker = require("sshfs.ui.picker")
62-
local success, picker_name = Picker.open_file_picker(mount_dir, config, false)
63-
if not success then
60+
local ok, picker_name = Picker.open_file_picker(mount_dir, config, false)
61+
if not ok then
6462
vim.notify("Failed to open " .. picker_name .. " for new mount: " .. mount_dir, vim.log.levels.ERROR)
6563
end
66-
elseif preset == "grep" then
64+
return
65+
end
66+
67+
if preset == "grep" then
6768
local Picker = require("sshfs.ui.picker")
6869
Picker.grep_remote_files(nil, { dir = mount_dir })
69-
elseif preset == "live_grep" or preset == "live_find" then
70-
local conn = get_connection()
70+
return
71+
end
72+
73+
if preset == "live_grep" or preset == "live_find" then
74+
local conn = find_connection_by_mount(mount_dir)
7175
if not conn then
7276
vim.notify("No connection found for on_mount action: " .. mount_dir, vim.log.levels.WARN)
7377
return
7478
end
7579
local Picker = require("sshfs.ui.picker")
7680
local fn = preset == "live_grep" and Picker.open_live_remote_grep or Picker.open_live_remote_find
77-
local success, picker_name = fn(conn.host, conn.mount_path, conn.remote_path or ".", config)
78-
if not success then
81+
local ok, picker_name = fn(conn.host, conn.mount_path, conn.remote_path or ".", config)
82+
if not ok then
7983
vim.notify("Live action failed: " .. picker_name, vim.log.levels.ERROR)
8084
end
81-
elseif preset == "terminal" then
85+
return
86+
end
87+
88+
if preset == "terminal" then
8289
local Terminal = require("sshfs.ui.terminal")
8390
Terminal.open_ssh()
8491
end
8592
end
8693

94+
--- Run post-mount action (tcd + optional hook)
95+
--- @param mount_dir string Mount directory path
96+
--- @param host string Host name
97+
--- @param remote_path string|nil Remote path used for the mount
98+
--- @param config table Plugin configuration
99+
function Hooks.on_mount(mount_dir, host, remote_path, config)
100+
local hook_cfg = (config.hooks and config.hooks.on_mount) or {}
101+
local auto_change_to_dir = hook_cfg.auto_change_to_dir
102+
local action = normalize_action(hook_cfg.auto_run)
103+
104+
-- Auto-change directory to mount point if configured
105+
if auto_change_to_dir then
106+
vim.cmd("tcd " .. vim.fn.fnameescape(mount_dir))
107+
end
108+
109+
if action == nil then
110+
return
111+
end
112+
113+
-- Allow custom handler
114+
if type(action) == "function" then
115+
local ok, err = pcall(action, {
116+
mount_path = mount_dir,
117+
host = host,
118+
remote_path = remote_path,
119+
})
120+
if not ok then
121+
vim.notify("on_mount callback failed: " .. err, vim.log.levels.ERROR)
122+
end
123+
return
124+
end
125+
126+
run_preset_action(action, mount_dir, config)
127+
end
128+
87129
return Hooks

0 commit comments

Comments
 (0)