Configure synchronization of files and directories through a plugin.
added user commands
3 files changed, +31 -5
+11-0lua/syncmap/commands.lua
| @@ -0,0 +1,11 @@ | ||
| 1 | +local M = { | |
| 2 | + opts = require("syncmap.default"), | |
| 3 | +} | |
| 4 | +local state = require("syncmap.state") | |
| 5 | + | |
| 6 | +function M.setup() | |
| 7 | + vim.api.nvim_create_user_command("SyncmapRestart", state.restart, {}) | |
| 8 | + vim.api.nvim_create_user_command("SyncmapStop", state.clear, {}) | |
| 9 | +end | |
| 10 | + | |
| 11 | +return M |
+4-1lua/syncmap/init.lua
| @@ -4,6 +4,7 @@ M.rsync = require("syncmap.rsync") | ||
| 4 | 4 | M.utils = require("syncmap.utils") |
| 5 | 5 | M.default = require("syncmap.default") |
| 6 | 6 | M.log = require("syncmap.log") |
| 7 | +M.commands = require("syncmap.commands") | |
| 7 | 8 | |
| 8 | 9 | M.opts = M.default |
| 9 | 10 | |
| @@ -15,7 +16,9 @@ function M.setup(opts) | ||
| 15 | 16 | M.state.opts = M.opts |
| 16 | 17 | M.rsync.opts = M.opts |
| 17 | 18 | M.log.opts = M.opts |
| 18 | - M.state.sync(M.opts) | |
| 19 | + M.commands.opts = M.opts | |
| 20 | + M.state.sync() | |
| 21 | + M.commands.setup() | |
| 19 | 22 | end |
| 20 | 23 | |
| 21 | 24 | ---Shows the current state of Syncmap |
+16-4lua/syncmap/state.lua
| @@ -48,8 +48,8 @@ end | ||
| 48 | 48 | load_state() |
| 49 | 49 | |
| 50 | 50 | ---Removes stale states, calls reverse_rsync on new folders, and starts watch on dead pids |
| 51 | ----@param opts FinalSyncmapOpts | |
| 52 | -function M.sync(opts) | |
| 51 | +function M.sync() | |
| 52 | + local opts = M.opts | |
| 53 | 53 | ---@type table<RsyncPath, SyncmapConfigMatch> |
| 54 | 54 | local lookup = {} |
| 55 | 55 | for _, m in ipairs(opts.map) do |
| @@ -96,10 +96,16 @@ function M.save() | ||
| 96 | 96 | fd:close() |
| 97 | 97 | end |
| 98 | 98 | |
| 99 | +---Kills all the active inotifywatch servers | |
| 100 | +function M.killall() | |
| 101 | + for src in M.active do | |
| 102 | + utils.kill(src) | |
| 103 | + end | |
| 104 | +end | |
| 105 | + | |
| 99 | 106 | ---Clears the saved information by deleting the state file |
| 100 | 107 | function M.clear() |
| 101 | - for _, a in ipairs(M.active) do | |
| 102 | - end | |
| 108 | + M.killall() | |
| 103 | 109 | local ok, err = os.remove(M.state_file) |
| 104 | 110 | if not ok then |
| 105 | 111 | log.error("Failed to delete state file: " .. err) |
| @@ -108,4 +114,10 @@ function M.clear() | ||
| 108 | 114 | end |
| 109 | 115 | end |
| 110 | 116 | |
| 117 | +---Restarts Syncmap | |
| 118 | +function M.restart() | |
| 119 | + M.killall() | |
| 120 | + M.sync() | |
| 121 | +end | |
| 122 | + | |
| 111 | 123 | return M |