feat(picker): Add a snacks picker with jj status command

This commit is contained in:
NicolasGB
2025-06-30 17:42:54 +02:00
parent a5b27fcb1b
commit b258574092
5 changed files with 163 additions and 17 deletions
+35 -11
View File
@@ -1,25 +1,44 @@
--- @class jj.utils
local M = {}
local M = {
executable_cache = {},
dependency_cache = {},
}
--- Cache for executable checks to avoid repeated system calls
local executable_cache = {}
--- Check if an executable exists in PATH
--- @param name string The name of the executable to check
--- @return boolean True if executable exists, false otherwise
function M.has_executable(name)
if executable_cache[name] ~= nil then
return executable_cache[name]
if M.executable_cache[name] ~= nil then
return M.executable_cache[name]
end
local exists = vim.fn.executable(name) == 1
executable_cache[name] = exists
M.executable_cache[name] = exists
return exists
end
--- Check if the dependency is currently installed
--- @param module string The dependency module
--- @return boolean
function M.has_dependency(module)
if M.dependency_cache[module] ~= nil then
return true
end
local exists, _ = pcall(require, module)
if not exists then
M.notify(string.format("Module %s not installed", module), vim.log.levels.ERROR)
return false
end
return true
end
--- Clear the executable cache (useful for testing or if PATH changes)
function M.clear_executable_cache()
executable_cache = {}
M.executable_cache = {}
end
--- Check if jj executable exists, show error if not
@@ -34,15 +53,20 @@ end
--- Execute a system command and return output with error handling
--- @param cmd string The command to execute
--- @param error_prefix string Optional error message prefix
--- @param error_prefix string|nil Optional error message prefix
--- @return string|nil output The command output, or nil if failed
--- @return boolean success Whether the command succeeded
function M.execute_command(cmd, error_prefix)
local output = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
if not success and error_prefix then
local error_message = string.format("%s: %s", error_prefix, output)
if not success then
local error_message
if error_prefix then
error_message = string.format("%s: %s", error_prefix, output)
else
error_message = output
end
M.notify(error_message, vim.log.levels.ERROR)
return nil, false
end
@@ -57,7 +81,7 @@ function M.is_jj_repo()
return false
end
local _, success = M.execute_command("jj status", "")
local _, success = M.execute_command("jj status")
return success
end
@@ -68,7 +92,7 @@ function M.get_jj_root()
return nil
end
local output, success = M.execute_command("jj root", "")
local output, success = M.execute_command("jj root")
if success and output then
return vim.trim(output)
end