Skip to content

Commit

Permalink
Use utils.system() instead of vim.system() (#41)
Browse files Browse the repository at this point in the history
* Use utils.system() instead of vim.system()
* Use vim.loop instead of vim.uv
  • Loading branch information
hongyuanjia authored and jalvesaq committed Feb 22, 2024
1 parent 4661ef9 commit 659db44
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lua/r/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ local windows_config = function()
end

local get_rip = function(run_cmd)
local resp = vim.system(run_cmd, { text = true }):wait()
local resp = utils.system(run_cmd, { text = true }):wait()
local rout = vim.split(resp.stdout, "\n")
local rip = {}
for _, v in pairs(rout) do
Expand Down
3 changes: 2 additions & 1 deletion lua/r/external_term.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require("r.config").get_config()
local utils = require("r.utils")
local warn = require("r").warn
local vit = require("r.utils").value_in_table

Expand Down Expand Up @@ -228,7 +229,7 @@ M.send_cmd_to_external_term = function(command)
if str:find("^-") then str = " " .. str end

if not base_pane_index then
local obj = vim.system(
local obj = utils.system(
{ "tmux", "-L", "Rnvim show-options", "-gv", "pane-base-index" },
{ text = true }
):wait()
Expand Down
7 changes: 4 additions & 3 deletions lua/r/pdf/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require("r.config").get_config()
local utils = require("r.utils")
local warn = require("r").warn
local job = require("r.job")

Expand Down Expand Up @@ -97,14 +98,14 @@ end
---@param pid number Pid of window application.
M.focus_window = function(wttl, pid)
if config.has_X_tools then
vim.system({ "wmctrl", "-a", wttl })
utils.system({ "wmctrl", "-a", wttl })
elseif
vim.env.XDG_CURRENT_DESKTOP == "sway" or vim.env.XDG_SESSION_DESKTOP == "sway"
then
if pid and pid ~= 0 then
vim.system({ "swaymsg", '[pid="' .. tostring(pid) .. '"]', "focus" })
utils.system({ "swaymsg", '[pid="' .. tostring(pid) .. '"]', "focus" })
elseif wttl then
vim.system({ "swaymsg", '[name="' .. wttl .. '"]', "focus" })
utils.system({ "swaymsg", '[name="' .. wttl .. '"]', "focus" })
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lua/r/pdf/zathura.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local warn = require("r").warn
local utils = require("r.utils")
local pdf = require("r.pdf")
local job = require("r.job")

Expand Down Expand Up @@ -37,7 +38,7 @@ M.SyncTeX_forward = function(tpath, ppath, texln)
"--synctex-pid=" .. job.get_pid(ppath),
pdfname,
}
local obj = vim.system(zfcmd, { text = true }):wait()
local obj = utils.system(zfcmd, { text = true }):wait()
if obj.code ~= 0 then
warn(obj.stderr)
return
Expand Down
2 changes: 1 addition & 1 deletion lua/r/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ end

-- Send SIGINT to R
M.signal_to_R = function(signal)
if R_pid ~= 0 then vim.system({ "kill", "-s", tostring(signal), tostring(R_pid) }) end
if R_pid ~= 0 then utils.system({ "kill", "-s", tostring(signal), tostring(R_pid) }) end
end

M.check_nvimcom_running = function()
Expand Down
3 changes: 2 additions & 1 deletion lua/r/server.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local edit = require("r.edit")
local utils = require("r.utils")
local job = require("r.job")
local config = require("r.config").get_config()
local warn = require("r").warn
Expand Down Expand Up @@ -202,7 +203,7 @@ local init_exit = function(_, data, _)
"R CMD build " .. config.rnvim_home .. "/nvimcom",
}
vim.fn.writefile(shf, config.tmpdir .. "/buildpkg.sh")
local obj = vim.system(
local obj = utils.system(
{ "sh", config.tmpdir .. "/buildpkg.sh" },
{ text = true }
)
Expand Down
108 changes: 105 additions & 3 deletions lua/r/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ end
local get_fw_info_X = function()
local config = require("r.config").get_config()
local warn = require("r").warn
local obj = vim.system({ "xprop", "-root" }, { text = true }):wait()
local obj = M.system({ "xprop", "-root" }, { text = true }):wait()
if obj.code ~= 0 then
warn("Failed to run `xprop -root`")
return
Expand All @@ -78,7 +78,7 @@ local get_fw_info_X = function()
warn("Failed to get ID of active window")
return
end
obj = vim.system({ "xprop", "-id", awin }, { text = true }):wait()
obj = M.system({ "xprop", "-id", awin }, { text = true }):wait()
if obj.code ~= 0 then
warn("xprop is required to get window PID")
return
Expand Down Expand Up @@ -112,7 +112,7 @@ end

local get_fw_info_Sway = function()
local config = require("r.config").get_config()
local obj = vim.system({ "swaymsg", "-t", "get_tree" }, { text = true }):wait()
local obj = M.system({ "swaymsg", "-t", "get_tree" }, { text = true }):wait()
local t = vim.json.decode(obj.stdout, { luanil = { object = true, array = true } })
for _, v1 in pairs(t.nodes) do
if #v1 and v1.type == "output" and v1.nodes then
Expand Down Expand Up @@ -154,4 +154,106 @@ function M.get_focused_win_info()
end
end

--- Execute a command with arguments.
--- This is a simplified version of `vim.system()` in dev version of Neovim.
--- This make sure that R.nvim can run on stable version of Neovim.
--- This function will be removed when `vim.system()` is available in the stable version.
--- See: https://github.com/jalvesaq/tmp-R-Nvim/issues/36
--- Note: Neovim source code is under Apache License 2.0.
---@param cmd string[] The command to execute.
---@param opts table Options.
function M.system(cmd, opts)
opts = opts or {}
local function close_handles(state)
for _, handle in pairs({ state.handle, state.stdout, state.stderr }) do
if not handle:is_closing() then handle:close() end
end
end

--- init state
local stdout = assert(vim.loop.new_pipe(false))
local stderr = assert(vim.loop.new_pipe(false))
local stdout_data, stderr_data
local state = {
handle = nil,
pid = nil,
done = false,
cmd = cmd,
stdout = stdout,
stderr = stderr,
result = {
code = nil,
signal = nil,
stdout = nil,
stderr = nil
}
}

--- run the command
state.handle, state.pid = vim.loop.spawn(cmd[1],
{
args = vim.list_slice(cmd, 2),
stdio = { nil, stdout, stderr },
cwd = opts.cwd,
detach = opts.detach,
hide = true
},
function(code, signal)
--- make sure to close all handles
close_handles(state)

state.done = true
state.result = {
code = code,
signal = signal,
stdout = stdout_data and table.concat(stdout_data) or nil,
stderr = stderr_data and table.concat(stderr_data) or nil
}
end
)

local function stdio_handler(steam, store)
return function(err, data)
if err then error(err) end

if data ~= nil then
if opts.text then
data = data:gsub("\r\n", "\n")
table.insert(store, data)
else
table.insert(store, data)
end
else
steam:read_stop()
steam:close()
end
end
end

if stdout then
stdout_data = {}
stdout:read_start(stdio_handler(state.stdout, stdout_data))
end
if stderr then
stderr_data = {}
stderr:read_start(stdio_handler(state.stderr, stderr_data))
end

local methods = {}
function methods:wait()
vim.wait(2 ^ 31, function() return state.done end)

if not state.done then
state.handle:kill("sigint")
close_handles(state)
local err = string.format("Command timed out: %s", table.concat(cmd, " "))
return { code = 0, signal = 2, stdout = "", stderr = err }
end

return state.result
end

return setmetatable({ pid = state.pid, _state = state }, { __index = methods })
end

return M
3 changes: 2 additions & 1 deletion lua/r/windows.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require("r.config").get_config()
local utils = require("r.utils")
local warn = require("r").warn
local saved_home = nil
local M = {}
Expand All @@ -7,7 +8,7 @@ M.set_R_home = function()
-- R and Vim use different values for the $HOME variable.
if config.set_home_env then
saved_home = vim.env.HOME
local obj = vim.system({
local obj = utils.system({
"reg.exe",
"QUERY",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
Expand Down

0 comments on commit 659db44

Please sign in to comment.