Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ With this plugin you can:

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/)


### 🔔 Callback

You can manage callbacks on events like `on_connect_success` using the following methods:

- `:add(fn)`: Add a callback function to the event.
- `:set(fn)`: Replace all existing callbacks with a new function.
- `:trigger(...)`: Manually invoke all registered callbacks with arguments.

Example:

```lua
require("remote-sshfs").callback.on_connect_success:add(function(host, mount_dir)
vim.notify("Mounted " .. host .. " at " .. mount_dir)
end)
```

All added callbacks will be triggered when the event occurs.


## 🧩 Status-line integrations

`remote-sshfs.nvim` ships a tiny helper module that exposes the current
Expand Down
4 changes: 2 additions & 2 deletions lua/remote-sshfs/connections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ M.mount_host = function(host, mount_dir, ask_pass)
local spec_host = host
local id = vim.fn.jobstart(cmd, {
on_stdout = function(_, data)
handler.sshfs_wrapper(data, mount_dir, function(event)
handler.sshfs_wrapper(data, host, mount_dir, function(event)
if event == "ask_pass" then
skip_clean = true
M.init_host(host, true)
end
end)
end,
on_stderr = function(_, data)
handler.sshfs_wrapper(data, mount_dir, function(event)
handler.sshfs_wrapper(data, host, mount_dir, function(event)
if event == "ask_pass" then
skip_clean = true
M.init_host(host, true)
Expand Down
8 changes: 5 additions & 3 deletions lua/remote-sshfs/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local log = require "remote-sshfs.log"

local M = {}

M.sshfs_wrapper = function(data, mount_dir, callback)
M.sshfs_wrapper = function(data, host, mount_dir, callback)
local output = table.concat(data, "\n")
log.line("sshfs", output)
if output == "" or string.match(output, "read:") then
Expand All @@ -13,7 +13,7 @@ M.sshfs_wrapper = function(data, mount_dir, callback)
if string.match(output, "ssh_askpass") then
M.askpass_handler(callback)
elseif string.match(output, "Authenticated") then
M.authenticated_handler(mount_dir)
M.authenticated_handler(host, mount_dir)
else
vim.notify("Connection failed: " .. string.gsub(tostring(output), "\r\n", ""))
end
Expand All @@ -33,7 +33,7 @@ M.askpass_handler = function(callback)
callback "ask_pass"
end

M.authenticated_handler = function(mount_dir)
M.authenticated_handler = function(host, mount_dir)
vim.notify "Connected to host succesfully."

if M.change_dir then
Expand All @@ -49,6 +49,8 @@ M.authenticated_handler = function(mount_dir)
utils.change_directory(mount_dir)
end
end

require("remote-sshfs").callback.on_connect_success:trigger(host, mount_dir)
end

M.setup = function(opts)
Expand Down
7 changes: 7 additions & 0 deletions lua/remote-sshfs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ local default_opts = {
},
}

local utils = require "remote-sshfs.utils"
local CallbackList = utils.CallbackList

M.callback = {
on_connect_success = CallbackList:new(),
}

M.setup_commands = function()
-- Create commands to connect/edit/reload/disconnect/find_files/live_grep
vim.api.nvim_create_user_command("RemoteSSHFSConnect", function(opts)
Expand Down
22 changes: 22 additions & 0 deletions lua/remote-sshfs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,26 @@ M.change_directory = function(path)
vim.notify("Directory changed to " .. path)
end

-- CallbackList class
M.CallbackList = {}
M.CallbackList.__index = M.CallbackList

function M.CallbackList:new()
return setmetatable({ _list = {} }, self)
end

function M.CallbackList:add(fn)
table.insert(self._list, fn)
end

function M.CallbackList:set(fn)
self._list = { fn }
end

function M.CallbackList:trigger(...)
for _, fn in ipairs(self._list) do
fn(...)
end
end

return M