Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lua/wiremux/backend/tmux/action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ function M.select_window(window_id)
end

---@param target_id string
---@param keys string
---@param keys string|string[] e.g. "Enter", { cmd, "Enter" }, { "Escape", "i" }, { "C-c" }
---@return string[]
function M.send_keys(target_id, keys)
return { "send-keys", "-t", target_id, keys, "Enter" }
local cmd = { "send-keys", "-t", target_id }
if type(keys) == "table" then
vim.list_extend(cmd, keys)
else
table.insert(cmd, keys)
end
return cmd
end

---@param buffer_name string
Expand Down
4 changes: 2 additions & 2 deletions lua/wiremux/backend/tmux/operation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function submit(targets)
vim.defer_fn(function()
local batch = {}
for _, t in ipairs(targets) do
table.insert(batch, action.send_keys(t.id, ""))
table.insert(batch, action.send_keys(t.id, "Enter"))
end
client.execute(batch)
end, 300)
Expand Down Expand Up @@ -167,7 +167,7 @@ function M.create(target_name, def, st)
state.set_instance_metadata(id, target_name, st.origin_pane_id or "", vim.fn.getcwd(), kind)

if use_shell and cmd then
client.execute({ action.send_keys(id, cmd) })
client.execute({ action.send_keys(id, { cmd, "Enter" }) })
end

notify.debug("create: %s %s target=%s", kind, id, target_name)
Expand Down
4 changes: 1 addition & 3 deletions tests/helpers_operation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ function M.setup()
set_pane_option = function(pane_id, key, value)
return { "set-option", "-p", "-t", pane_id, key, value }
end,
send_keys = function(target, keys)
return { "send-keys", "-t", target, keys, "Enter" }
end,
send_keys = require("wiremux.backend.tmux.action").send_keys,
new_window = function(name, command)
local cmd = { "new-window" }
if name then
Expand Down
38 changes: 38 additions & 0 deletions tests/operation_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

local helpers = require("tests.helpers_operation")

describe("action.send_keys", function()
local action = require("wiremux.backend.tmux.action")

it("accepts single string key", function()
assert.are.same({ "send-keys", "-t", "%1", "Enter" }, action.send_keys("%1", "Enter"))
end)

it("accepts array of keys", function()
assert.are.same({ "send-keys", "-t", "%1", "i", "Enter" }, action.send_keys("%1", { "i", "Enter" }))
end)

it("accepts modifier keys", function()
assert.are.same({ "send-keys", "-t", "%1", "C-c" }, action.send_keys("%1", "C-c"))
end)
end)

describe("tmux operations", function()
local mocks

Expand Down Expand Up @@ -224,6 +240,28 @@ describe("tmux operations", function()
assert.are.equal("myapp", new_window_cmd[3])
end)

it("sends cmd with Enter for shell targets", function()
local shell_cmds
local call_count = 0

mocks.client.execute = function(cmds)
call_count = call_count + 1
if call_count == 1 then
return "%5"
else
shell_cmds = cmds
return "ok"
end
end

local st = { instances = {}, origin_pane_id = "%0" }
local def = { kind = "pane", shell = true, cmd = "npm start" }

mocks.operation.create("myapp", def, st)

assert.are.same({ { "send-keys", "-t", "%5", "npm start", "Enter" } }, shell_cmds)
end)

it("ignores function label for window name", function()
local captured_cmds
mocks.client.execute = function(cmds)
Expand Down