From e814360daa0227124b83da1d83c4925f1f4665a3 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 10 May 2024 08:41:29 +0200 Subject: detect backend availability ahead of time and allow user function --- init.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'init.lua') diff --git a/init.lua b/init.lua index ca8e16c..ce5145c 100644 --- a/init.lua +++ b/init.lua @@ -21,6 +21,13 @@ local M = {} M.directory = '${XDG_STATE_HOME:-${HOME}/.local/state}/vis/locks' M.directory_expanded = nil +-- Detect available backends +if os.execute('command -v whiptail') then + M.prompt_backend = 'whiptail' +else + M.prompt_backend = 'vis-menu' +end + local function get_lock_dir() if M.directory_expanded ~= M.directory then M.directory = lockfiles.capture_cmd('printf %s "' .. M.directory .. '"') @@ -41,21 +48,16 @@ local function prompt_backend_vis_menu(msg, menu, choices) return vis:pipe(cmd1 .. ';' .. cmd2 .. '|' .. cmd3, true) end -local function prompt_backend(path, lfile_path, pid, preferred_backend) +local function prompt_backend(path, lfile_path, pid, backend) local msg = 'File is locked: ' .. path .. '\n lock: ' .. lfile_path .. '\n owned by process ID: ' .. pid local status, output - if preferred_backend == 'whiptail' then - local p = io.popen('command -v whiptail 2>/dev/null || :', 'r') - output = assert(p:read('*a')) - p:close() - preferred_backend = (output ~= '') and preferred_backend - end - - if preferred_backend == 'whiptail' then + if backend == 'whiptail' then local menu = 'abort "operation (a)" edit "anyway (e)" quit "vis (q)"' status, output = prompt_backend_whiptail(msg, menu) + elseif type(backend) == 'function' then + status, output = backend(path, lfile_path, pid) else status, output = prompt_backend_vis_menu(msg, '(E)dit anyway, (Q)uit, [A]bort', 'a\ne\nq') end @@ -68,7 +70,7 @@ end -- 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, 'whiptail') + local output = prompt_backend(path, lfile_path, pid, M.backend) if output == 'e' then return true elseif output == 'q' then -- cgit v1.2.3