-- -- send.lua: paste text into a Rex terminal block. -- -- Usage: REX_KAK_TEXT='select 42;' rex do -s SESSION send.lua block=BLOCK_ID -- -- The text comes from the environment rather than a key=value argument, -- because rex do types those by how they're written (1.50 becomes 1.5). -- -- If the text ends with a newline, Enter is sent separately after the paste, -- so REPLs with bracketed paste on still run it. Rex API calls return nil -- plus an error rather than raising, so each one is checked. -- local block_id = rex.args.block local text = os.getenv("REX_KAK_TEXT") or "" if not block_id or block_id == "" then error("block is required") end if text == "" then error("nothing to send") end local body = text:gsub("\n+$", "") local submit = body ~= text if body ~= "" then local _, err = rex.call("block.event", { session_id = rex.session_id, block_id = block_id, event = { type = "paste", payload = { text = body } }, }) if err then error(err) end end if submit then local _, err = rex.block.call("com.superlogical.terminal", "write", { block_id = block_id, data = "\r", }) if err then error(err) end end return #text