aboutsummaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorAlin Mr <almr.oss@outlook.com>2024-05-06 06:16:29 +0000
committerAlin Mr <almr.oss@outlook.com>2024-05-06 06:16:29 +0000
commit5cc1d08393c3d92e32b99b74c3dd95863aa65d51 (patch)
tree670c940366778e8542af39f68355916dda2a620b /init.lua
parent9cd4194528b2d73d83abd1364fecd9b334793444 (diff)
downloadvis-lockfiles-5cc1d08393c3d92e32b99b74c3dd95863aa65d51.tar.gz
vis-lockfiles-5cc1d08393c3d92e32b99b74c3dd95863aa65d51.zip
init.lua: refactor prompt backend
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua38
1 files changed, 21 insertions, 17 deletions
diff --git a/init.lua b/init.lua
index 723e046..48eddb2 100644
--- a/init.lua
+++ b/init.lua
@@ -29,28 +29,32 @@ local function get_lock_dir()
return M.directory
end
--- get a user confirmation
--- return true if user wants to edit the locked file
-local function prompt(path, lfile_path, pid)
- local choices = 'a\ne\nq'
- local msg = 'Lock file found!\n\t lock file: "' .. lfile_path .. '"\n\tfile name: ' .. path ..
- '\n\tprocess ID: ' .. pid
-
- local menu_prompt = '(E)dit anyway, (Q)uit, [A]bort'
+local function prompt_backend_vis_menu(msg, menu, choices)
local cmd1 = 'printf >/dev/tty %s \'' .. msg .. '\n\''
local cmd2 = 'printf %s \'' .. choices .. '\''
- local cmd3 = 'vis-menu -b -p \'' .. menu_prompt .. '\''
- local cmd = cmd1 .. ';' .. cmd2 .. '|' .. cmd3
+ local cmd3 = 'vis-menu -b -p \'' .. menu .. '\''
+ return vis:pipe(cmd1 .. ';' .. cmd2 .. '|' .. cmd3, true)
+end
- local status, output = vis:pipe(cmd, true)
+local function prompt_backend(path, lfile_path, pid)
+ local msg = 'File is locked: ' .. path .. '\n lock: ' .. lfile_path ..
+ '\n owned by process ID: ' .. pid
+ local status, output
+ status, output = prompt_backend_vis_menu(msg, '(E)dit anyway, (Q)uit, [A]bort', 'a\ne\nq')
vis:redraw()
- if status == 0 then
- if output:sub(1, 1) == 'e' then
- return true
- elseif output:sub(1, 1) == 'q' then
- vis:exit(1)
- end
+ return (status == 0) and output:sub(1, 1)
+end
+
+-- get a user confirmation
+-- return true if user wants to edit the locked file
+local function prompt(path, lfile_path, pid)
+
+ local output = prompt_backend(path, lfile_path, pid)
+ if output == 'e' then
+ return true
+ elseif output == 'q' then
+ vis:exit(1)
end
-- else 'abort' or dialog canceled