Skip to content

Commit a2f3f14

Browse files
feat: add on_connect_success callback to be used in config (#54)
1 parent e35d87b commit a2f3f14

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ With this plugin you can:
171171

172172
To learn more about SSH configs and how to write/style one you can read more [here](https://linuxize.com/post/using-the-ssh-config-file/)
173173

174+
175+
### 🔔 Callback
176+
177+
You can manage callbacks on events like `on_connect_success` using the following methods:
178+
179+
- `:add(fn)`: Add a callback function to the event.
180+
- `:set(fn)`: Replace all existing callbacks with a new function.
181+
- `:trigger(...)`: Manually invoke all registered callbacks with arguments.
182+
183+
Example:
184+
185+
```lua
186+
require("remote-sshfs").callback.on_connect_success:add(function(host, mount_dir)
187+
vim.notify("Mounted " .. host .. " at " .. mount_dir)
188+
end)
189+
```
190+
191+
All added callbacks will be triggered when the event occurs.
192+
193+
174194
## 🧩 Status-line integrations
175195

176196
`remote-sshfs.nvim` ships a tiny helper module that exposes the current

lua/remote-sshfs/connections.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ M.mount_host = function(host, mount_dir, ask_pass)
138138
local spec_host = host
139139
local id = vim.fn.jobstart(cmd, {
140140
on_stdout = function(_, data)
141-
handler.sshfs_wrapper(data, mount_dir, function(event)
141+
handler.sshfs_wrapper(data, host, mount_dir, function(event)
142142
if event == "ask_pass" then
143143
skip_clean = true
144144
M.init_host(host, true)
145145
end
146146
end)
147147
end,
148148
on_stderr = function(_, data)
149-
handler.sshfs_wrapper(data, mount_dir, function(event)
149+
handler.sshfs_wrapper(data, host, mount_dir, function(event)
150150
if event == "ask_pass" then
151151
skip_clean = true
152152
M.init_host(host, true)

lua/remote-sshfs/handler.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local log = require "remote-sshfs.log"
44

55
local M = {}
66

7-
M.sshfs_wrapper = function(data, mount_dir, callback)
7+
M.sshfs_wrapper = function(data, host, mount_dir, callback)
88
local output = table.concat(data, "\n")
99
log.line("sshfs", output)
1010
if output == "" or string.match(output, "read:") then
@@ -13,7 +13,7 @@ M.sshfs_wrapper = function(data, mount_dir, callback)
1313
if string.match(output, "ssh_askpass") then
1414
M.askpass_handler(callback)
1515
elseif string.match(output, "Authenticated") then
16-
M.authenticated_handler(mount_dir)
16+
M.authenticated_handler(host, mount_dir)
1717
else
1818
vim.notify("Connection failed: " .. string.gsub(tostring(output), "\r\n", ""))
1919
end
@@ -33,7 +33,7 @@ M.askpass_handler = function(callback)
3333
callback "ask_pass"
3434
end
3535

36-
M.authenticated_handler = function(mount_dir)
36+
M.authenticated_handler = function(host, mount_dir)
3737
vim.notify "Connected to host succesfully."
3838

3939
if M.change_dir then
@@ -49,6 +49,8 @@ M.authenticated_handler = function(mount_dir)
4949
utils.change_directory(mount_dir)
5050
end
5151
end
52+
53+
require("remote-sshfs").callback.on_connect_success:trigger(host, mount_dir)
5254
end
5355

5456
M.setup = function(opts)

lua/remote-sshfs/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ local default_opts = {
4545
},
4646
}
4747

48+
local utils = require "remote-sshfs.utils"
49+
local CallbackList = utils.CallbackList
50+
51+
M.callback = {
52+
on_connect_success = CallbackList:new(),
53+
}
54+
4855
M.setup_commands = function()
4956
-- Create commands to connect/edit/reload/disconnect/find_files/live_grep
5057
vim.api.nvim_create_user_command("RemoteSSHFSConnect", function(opts)

lua/remote-sshfs/utils.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,26 @@ M.change_directory = function(path)
150150
vim.notify("Directory changed to " .. path)
151151
end
152152

153+
-- CallbackList class
154+
M.CallbackList = {}
155+
M.CallbackList.__index = M.CallbackList
156+
157+
function M.CallbackList:new()
158+
return setmetatable({ _list = {} }, self)
159+
end
160+
161+
function M.CallbackList:add(fn)
162+
table.insert(self._list, fn)
163+
end
164+
165+
function M.CallbackList:set(fn)
166+
self._list = { fn }
167+
end
168+
169+
function M.CallbackList:trigger(...)
170+
for _, fn in ipairs(self._list) do
171+
fn(...)
172+
end
173+
end
174+
153175
return M

0 commit comments

Comments
 (0)