diff options
| author | Alin Mr <almr.oss@outlook.com> | 2024-05-03 13:22:51 +0000 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2024-05-03 17:17:23 +0200 |
| commit | 526873bef425574974358b7f26975cdd7d7cdd1b (patch) | |
| tree | 43a5aca3cc031b5c05e20e2795632a3ec7e826c3 | |
| parent | d65d6c542c8d55b8e8d00880e47fb2d3f7d8cef4 (diff) | |
| download | vis-lockfiles-526873bef425574974358b7f26975cdd7d7cdd1b.tar.gz vis-lockfiles-526873bef425574974358b7f26975cdd7d7cdd1b.zip | |
show locking pid on conflict; clearer prompt
| -rw-r--r-- | init.lua | 11 | ||||
| -rw-r--r-- | lockfiles.lua | 12 |
2 files changed, 13 insertions, 10 deletions
@@ -31,9 +31,9 @@ end -- get a user confirmation -- return true if user selected yes, false otherwise -local function confirm(path) +local function confirm(path, pid) local choices = 'no\nyes' - local prompt = path .. ' exists. Edit anyway:' + local prompt = path .. ' for PID '..pid .. ' exists. Edit anyway:' local cmd = 'printf %s \'' .. choices .. '\' | vis-menu -b -p \'' .. prompt .. '\'' -- local status, output = vis:pipe(nil, {start = 0, finish = 0}, cmd) @@ -43,7 +43,7 @@ local function confirm(path) vis:redraw() if status == 0 then - return 'yes' == output:sub(1, 3) + return output:find('^edit') ~= nil end return false end @@ -56,9 +56,10 @@ vis.events.subscribe(vis.events.FILE_OPEN, function(file) return end local lfile_path = lockfiles.lock_file_path(get_lock_dir(), file.path) - if lockfiles.create_lock_file(get_lock_dir(), lfile_path) then + local lock_pid = lockfiles.create_lock_file(get_lock_dir(), lfile_path) + if lockfiles.vis_pid == lock_pid then created_locks[file.path] = lfile_path - elseif not confirm(lfile_path) then + elseif not confirm(lfile_path, lock_pid) then vis:exit(1) end end) diff --git a/lockfiles.lua b/lockfiles.lua index d018bc9..e223e10 100644 --- a/lockfiles.lua +++ b/lockfiles.lua @@ -45,11 +45,14 @@ do end assert(vis_pid) +-- returns PID owning lock (possibly vis_pid) local function create_lock_file(lock_dir, lfile_path) + local lock_pid local lock_file = io.open(lfile_path) if lock_file then + lock_pid = lock_file:read('*n') lock_file:close() - return false + return lock_pid end -- create the lock file with our pid @@ -64,11 +67,9 @@ local function create_lock_file(lock_dir, lfile_path) -- detect a possible race lock_file = assert(io.open(lfile_path)) - local lock_pid = lock_file:read('*n') + lock_pid = tonumber(lock_file:read('*n')) lock_file:close() - local locked = tonumber(lock_pid) == vis_pid - - return locked + return lock_pid end local function delete_lock_file(lfile_path) @@ -79,6 +80,7 @@ local function delete_lock_file(lfile_path) end return { + vis_pid = vis_pid, capture_cmd = capture_cmd, lock_file_path = lock_file_path, delete_lock_file = delete_lock_file, |
