feat(utils): Add scriptutils

Currently only has one util, but this may grow
This commit is contained in:
Robert Morrison 2026-04-10 02:15:36 +01:00
parent a64c787446
commit 88cec5db46
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,33 @@
-- scriptutils: some useful functions for scripts to use.
require('sherlock5512.scriptutils.types')
local M = {}
--- Run a command in a shell
-- This function only works on UNIX-like systems
-- that have a POSIX compliant shell installed.
---@param cmdline string Your command-line to send to sh
---@param timeout? integer Timeout for command
---@return shReturn status [1]
---@return string? stdout the contents of stdout from executing your cmdline
function M.shellexec(cmdline,timeout)
timeout = timeout or nil
-- by default vim.system runs async, but as we need the output immediately we don't really need that.
-- Note the timeout is passed to wait and not as an option to system,
-- In this case I don't think it really matters as we wait immediately after calling vim.system
local result = vim.system({'sh', '-c', cmdline}):wait(timeout)
-- Breaking the result into variables makes my programming intent clearer
local stdout = result.stdout
local stderr = result.stderr
local code = result.code
return {
ok = (code == 0),
code = code,
stderr = stderr,
}, stdout
end
return M

View File

@ -0,0 +1,4 @@
---@class shReturn
---@field ok boolean True if successful
---@field code integer Return code (useful for debugging)
---@field stderr string Stderr (if any)