irongit

Configure synchronization of files and directories through a plugin.

moved some messages into debug

huncholanehuncholaneauthored
parent 62eb8cbcommit b09af245b54a358379460c6148dc8bef8648b892Browse files

4 files changed, +46 -22

+15-1lua/syncmap/log.lua
@@ -9,9 +9,11 @@ end
99
1010 ---@type table<LogLevel, vim.log.levels>
1111 M.log_table = {
12+ ["trace"] = vim.log.levels.TRACE,
13+ ["debug"] = vim.log.levels.DEBUG,
1214 ["info"] = vim.log.levels.INFO,
1315 ["error"] = vim.log.levels.ERROR,
14- ["none"] = vim.log.levels.OFF,
16+ ["off"] = vim.log.levels.OFF,
1517 }
1618
1719 ---Logs a message based on options log level
@@ -31,6 +33,18 @@ function M.info(msg)
3133 M.log(msg, vim.log.levels.INFO)
3234 end
3335
36+---Logs message as debug
37+---@param msg any
38+function M.debug(msg)
39+ M.log(msg, vim.log.levels.DEBUG)
40+end
41+
42+---Logs message as trace
43+---@param msg any
44+function M.trace(msg)
45+ M.log(msg, vim.log.levels.TRACE)
46+end
47+
3448 ---Logs message as error
3549 ---@param msg any
3650 function M.error(msg)
+18-13lua/syncmap/rsync.lua
@@ -10,7 +10,6 @@ function M.run(args)
1010 local cmd = { "rsync", unpack(args.flags), args.src, args.dst }
1111 local result = vim.fn.system(cmd)
1212 if vim.v.shell_error ~= 0 then
13- log.error(table.concat(cmd, " ") .. " failed:\n" .. result)
1413 log.error(
1514 string.format(
1615 "Failed to run the the following rsync command.\n%s\nResult: %s",
@@ -19,7 +18,7 @@ function M.run(args)
1918 )
2019 )
2120 else
22- log.info("Successfully ran the following rsync command.\n" .. table.concat(cmd, " "))
21+ log.debug("Successfully ran the following rsync command.\n" .. table.concat(cmd, " "))
2322 end
2423 return result
2524 end
@@ -35,24 +34,30 @@ function M.spawn_watcher(args)
3534 })
3635 end
3736
38- local cmd = string.format(
39- "while inotifywait -r -e modify,create,delete %q; do rsync %s %q %q; done",
40- args.src,
41- table.concat(args.flags, " "),
42- args.src,
43- args.dst
44- )
37+ local cmd = "while inotifywait "
38+ if vim.uv.fs_stat(args.src).type == "directory" then
39+ log.debug(string.format("Setting up inotify for a file.\n%s", args.src))
40+ cmd = cmd .. string.format("-r -e modify,create,delete %q; ", args.src)
41+ else
42+ log.debug(string.format("Setting up inotify for a file.\n%s", args.src))
43+ cmd = cmd .. string.format("-e %q; ", args.src)
44+ end
45+ cmd = cmd .. string.format("do rsync %s %q %q; done", table.concat(args.flags, " "), args.src, args.dst)
4546 local cmd_str_for_log = "sh -c '" .. cmd .. "'"
4647
47- local _, pid = simple_cmd.spawn("sh", {
48+ local handle, pid = simple_cmd.spawn("sh", {
4849 args = { "-c", cmd },
4950 path = "sh",
50- cwd = args.src,
51+ -- cwd = args.src,
5152 callback = function(code, status, _, pid)
52- log.info(string.format("%s\n%d completed with %d code and %d status", cmd_str_for_log, pid, code, status))
53+ log.debug(string.format("%s\n%d completed with %d code and %d status", cmd_str_for_log, pid, code, status))
5354 end,
5455 })
55- log.info(string.format("Started the following command with pid %d\n%s", pid, cmd_str_for_log))
56+ if not handle then
57+ log.error(string.format("Failed to start the following command.\n%s\nErr: %s", cmd_str_for_log, pid))
58+ else
59+ log.debug(string.format("Started the following command with pid %d\n%s", pid, cmd_str_for_log))
60+ end
5661 return pid
5762 end
5863
+10-7lua/syncmap/state.lua
@@ -63,20 +63,21 @@ function M.sync()
6363 -- Iterate active and sync what exists
6464 for tag, pid in pairs(M.active) do
6565 if not lookup[tag] then
66- log.info(string.format("Removed from the config.\n%s\nRemoving the process %d.", tag, pid))
66+ log.info(string.format("Removed from the config.\n%s\nRemoving the process %s.", tag, pid))
6767 M.active[tag] = nil
6868 simple_cmd.kill(pid)
6969 elseif not simple_cmd.exists(pid) then
70+ local m = lookup[tag]
71+ local r = utils.row_to_rsync_params(m)
72+ M.active[tag] = rsync.spawn_watcher(r)
7073 log.info(
7174 string.format(
72- "In active table but the process %d is not running.\n%s\nStarting a new process.",
75+ "In active table but the process %s is not running.\n%s\nStarting a new process at %s.",
7376 pid,
74- tag
77+ tag,
78+ M.active[tag]
7579 )
7680 )
77- local m = lookup[tag]
78- local r = utils.row_to_rsync_params(m)
79- M.active[tag] = rsync.spawn_watcher(r)
8081 end
8182 end
8283
@@ -84,9 +85,11 @@ function M.sync()
8485 for _, m in ipairs(opts.map) do
8586 local tag = vim.fn.expand(m[1]) .. ":" .. vim.fn.expand(m[2])
8687 if not M.active[tag] then
87- log.info(string.format("%s\nIn the config but not in the active table. Starting a new process.", tag))
8888 local r = utils.row_to_rsync_params(m)
8989 M.active[tag] = rsync.spawn_watcher(r)
90+ log.info(
91+ string.format("%s\nIn the config but not in the active table.\nNew process is %s.", tag, M.active[tag])
92+ )
9093 end
9194 end
9295 M.save()
+3-1lua/syncmap/types.lua
@@ -17,9 +17,11 @@
1717 ---| string # allow custom flags too
1818
1919 ---@alias LogLevel
20+---| "trace" show trace logs
21+---| "debug" show debug logs
2022 ---| "info" show info logs
2123 ---| "error" show error logs
22----| "none" show no logs
24+---| "off" turn off logs
2325
2426 ---Ex: `--exclude-from='.gitignore'`
2527 ---@alias ExcludeFrom string @[default=".gitignore"] Adds --exclude-from in rsync