Skip to content

Commit

Permalink
compat: add vim.system fallback (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries authored Jun 21, 2023
1 parent d9eef8c commit f1fa0cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lua/sg/system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ local async = require "plenary.async"

return {
async = async.wrap(function(a, b, c)
return vim.system(a, b, vim.schedule_wrap(c))
return require("sg.utils").system(a, b, vim.schedule_wrap(c))
end, 3),
}
18 changes: 18 additions & 0 deletions lua/sg/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ utils.joinpath = vim.fs.joinpath or function(...)
return (table.concat({ ... }, "/"):gsub("//+", "/"))
end

-- COMPAT(0.10.0)
-- So far only handle stdout, no other items are handled.
-- Probably will break on me unexpectedly. Nice
utils.system = vim.system
or function(cmd, opts, on_exit)
local stdout = ""
opts.stdout_buffered = true
opts.on_stdout = function(_, data)
stdout = stdout .. table.concat(data, "")
end
opts.on_exit = function()
stdout = stdout .. "\n"
on_exit { stdout = stdout, compat = true }
end

vim.fn.jobstart(cmd, opts)
end

return utils
15 changes: 15 additions & 0 deletions lua/tests/async_compat_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require("plenary.async").tests.add_to_env()

local system = require "sg.system"

local eq = assert.are.same

describe("compat", function()
describe("vim.system", function()
a.it("void functions can call wrapped functions", function()
local obj = system.async({ "echo", "hello" }, { text = true })
local result = obj.stdout
eq(result, "hello\n")
end)
end)
end)

0 comments on commit f1fa0cf

Please sign in to comment.