diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2024-05-10 08:41:29 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2024-05-10 08:41:29 +0200 |
| commit | e814360daa0227124b83da1d83c4925f1f4665a3 (patch) | |
| tree | a41710827fbf7d9319102b25e3b549df0f60413e /init.lua | |
| parent | 229d79dd351e208c2cad17d62d2d663c12772fd4 (diff) | |
| download | vis-lockfiles-e814360daa0227124b83da1d83c4925f1f4665a3.tar.gz vis-lockfiles-e814360daa0227124b83da1d83c4925f1f4665a3.zip | |
detect backend availability ahead of time and allow user function
Diffstat (limited to 'init.lua')
| -rw-r--r-- | init.lua | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -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 |
