2个版本

0.15.1 2021年11月16日
0.15.0 2021年10月16日

#20 in #lua-bindings

MIT/Apache

355KB
8K SLoC

远程Lua (模块)

包含围绕几个远程库的Lua模块包装,包括

  1. distant-core
  2. distant-ssh2

构建

必须在当前目录内执行编译!此crate依赖于.cargo/config.toml设置,这些设置仅在从当前目录构建时使用。

# Outputs a library file (*.so for Linux, *.dylib for MacOS, *.dll for Windows)
cargo build --release

示例

libdistant_lua.solibdistant_lua.dylib 重命名为 distant_lua.so (是的,.so 用于 .dylib),并将库放在Lua路径的 根目录 中。如果库位于任何子模块中,则无法加载适当的符号。对于neovim,这意味着直接在 lua/ 目录中。

local distant = require("distant_lua")

-- The majority of the distant lua module provides async and sync variants
-- of methods; however, launching a session is currently only synchronous
local session = distant.session.launch({ host = "127.0.0.1" })

-- Sync methods are executed in a blocking fashion, returning the result of
-- the operation if successful or throwing an error if failing. Use `pcall`
-- if you want to capture the error
local success, result = pcall(session.read_dir, session, { path = "path/to/dir" })
if success then
    for _, entry in ipairs(result.entries) do
        print("Entry", entry.file_type, entry.path, entry.depth)
    end
else
    print(result)
end

-- Async methods have _async as a suffix and need to be polled from
-- Lua in some manner; the `wrap_async` function provides a convience
-- to do so taking an async distant function and a scheduling function
local schedule_fn = function(cb) end
local read_dir = distant.utils.wrap_async(session.read_dir_async, schedule_fn)
read_dir(session, { path = "path/to/dir" }, function(success, result)
    -- success: Returns true if ok and false if err
    -- result: If success is true, then is the resulting value,
    --         otherwise is the error
    print("Success", success)
    if success then
        for _, entry in ipairs(result.entries) do
            print("Entry", entry.file_type, entry.path, entry.depth)
        end
    else
        print(result)
    end
end)

-- For neovim, there exists a helper function that converts async functions
-- into functions that take callbacks, executing the asynchronous logic
-- using neovim's event loop powered by libuv
local read_dir = distant.utils.nvim_wrap_async(session.read_dir_async)
read_dir(session, { path = "path/to/dir" }, function(success, result)
    -- success: Returns true if ok and false if err
    -- result: If success is true, then is the resulting value,
    --         otherwise is the error
    print("Success", success)
    if success then
        for _, entry in ipairs(result.entries) do
            print("Entry", entry.file_type, entry.path, entry.depth)
        end
    else
        print(result)
    end
end)

测试

由于此处所述的链接,测试在单独的crate中运行: khvzak/mlua#79。您 必须 在运行测试之前构建此模块!

# From root of repository
(cd distant-lua-tests && cargo test --release)

许可协议

本项目根据您的选择,许可协议为以下之一:

Apache License,版本2.0,(LICENSE-APACHE 或 apache-license)MIT许可(LICENSE-MIT 或 mit-license)。

依赖关系

~19–38MB
~587K SLoC