34 lines
1.1 KiB
Lua
34 lines
1.1 KiB
Lua
-- 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
|