From f92cb0ef715867016ff33cbe4d5f621b3c1c85bd Mon Sep 17 00:00:00 2001 From: Alin Mr Date: Thu, 2 May 2024 19:22:31 +0000 Subject: init.lua: create & expand M.dir: only once --- init.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'init.lua') diff --git a/init.lua b/init.lua index c9ac004..2481a14 100644 --- a/init.lua +++ b/init.lua @@ -14,6 +14,7 @@ -- If not, see . local M = {} M.directory = '${XDG_STATE_HOME:-${HOME}/.local/state}/vis/locks' +M.directory_expanded = false -- Helper to execute a command and capture its output local function capture_cmd(cmd) @@ -27,10 +28,16 @@ local function capture_cmd(cmd) return s end +local function get_lock_dir() + if not M.directory_expanded then + M.directory = capture_cmd('printf %s "' .. M.directory .. '"') + end + return M.directory +end + local function lock_file_path(path) local filename = path:gsub('/', '%%') - assert(os.execute('mkdir -p "' .. M.directory .. '"')) - return capture_cmd('printf %s "' .. M.directory .. '/' .. filename .. '.lock"') + return get_lock_dir() .. '/' .. filename .. '.lock' end -- determine vis' pid @@ -81,6 +88,9 @@ local function create_lock_file(lfile_path) end -- create the lock file with our pid + if not io.open(lfile_path, 'w') then + assert(os.execute('mkdir -p "' .. get_lock_dir() .. '"')) + end lock_file = assert(io.open(lfile_path, 'w')) lock_file:write(vis_pid) lock_file:close() -- cgit v1.2.3