From 5cc1d08393c3d92e32b99b74c3dd95863aa65d51 Mon Sep 17 00:00:00 2001 From: Alin Mr Date: Mon, 6 May 2024 06:16:29 +0000 Subject: init.lua: refactor prompt backend --- init.lua | 38 +++++++++++++++++++++----------------- 1 file 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 -- cgit v1.2.3