diff --git a/README.md b/README.md index bbd0e55..0364d25 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/remote-sshfs/connections.lua b/lua/remote-sshfs/connections.lua index a98fa2d..8804ead 100644 --- a/lua/remote-sshfs/connections.lua +++ b/lua/remote-sshfs/connections.lua @@ -138,7 +138,7 @@ 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) @@ -146,7 +146,7 @@ M.mount_host = function(host, mount_dir, ask_pass) 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) diff --git a/lua/remote-sshfs/handler.lua b/lua/remote-sshfs/handler.lua index 4b393c2..819940d 100644 --- a/lua/remote-sshfs/handler.lua +++ b/lua/remote-sshfs/handler.lua @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/lua/remote-sshfs/init.lua b/lua/remote-sshfs/init.lua index 1063caa..34ef94e 100644 --- a/lua/remote-sshfs/init.lua +++ b/lua/remote-sshfs/init.lua @@ -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) diff --git a/lua/remote-sshfs/utils.lua b/lua/remote-sshfs/utils.lua index c31ad7d..da291fe 100644 --- a/lua/remote-sshfs/utils.lua +++ b/lua/remote-sshfs/utils.lua @@ -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