Skip to content
Open
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
3 changes: 3 additions & 0 deletions lua/sidekick/cli/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ function M.toggle(opts)
opts = filter_opts(opts)
State.with(function(state, attached)
if not state.terminal then
if state.session and state.session.toggle then
state.session:toggle()
end
return
end
if not attached then
Expand Down
28 changes: 25 additions & 3 deletions lua/sidekick/cli/session/tmux.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ local Util = require("sidekick.util")

---@class sidekick.cli.muxer.Tmux: sidekick.cli.Session
---@field tmux_pane_id string
---@field tmux_window_id string
---@field tmux_pid number
local M = {}
M.__index = M

local PANE_FORMAT =
"#{session_id}:#{pane_id}:#{pane_pid}:#{session_name}:#{?pane_current_path,#{pane_current_path},#{pane_start_path}}"
"#{session_id}:#{pane_id}:#{window_id}:#{pane_pid}:#{session_name}:#{?pane_current_path,#{pane_current_path},#{pane_start_path}}"

---@return sidekick.cli.terminal.Cmd?
function M:attach()
Expand Down Expand Up @@ -60,6 +61,7 @@ function M:spawn(cmd)
if pane then
self.id = pane.skid
self.tmux_pane_id = pane.id
self.tmux_window_id = pane.window_id
self.mux_session = pane.session_name
self.tmux_pid = pane.pid
self.started = true
Expand Down Expand Up @@ -90,14 +92,15 @@ function M.panes(opts)
local lines = Util.exec(cmd, { notify = opts.notify == true })
local panes = {} ---@type sidekick.tmux.Pane[]
for _, line in ipairs(lines or {}) do
local session_id, id, pid, session_name, cwd = line:match("^(%$%d+):(%%%d+):(%d+):(.-):(.*)$")
if id and pid and session_name and cwd then
local session_id, id, window_id, pid, session_name, cwd = line:match("^(%$%d+):(%%%d+):(@%d+):(%d+):(.-):(.*)$")
if id and window_id and pid and session_name and cwd then
pid = assert(tonumber(pid), "invalid tmux pane_pid: " .. pid) --[[@as number]]
---@class sidekick.tmux.Pane
panes[#panes + 1] = {
skid = ("tmux %s"):format(pid), -- unique id for the pane
pid = pid, -- process id of the pane
id = id, -- tmux pane id
window_id = window_id, -- tmux window id
session_name = session_name,
session_id = session_id,
cwd = cwd,
Expand Down Expand Up @@ -141,6 +144,7 @@ function M.sessions()
cwd = proc.cwd or pane.cwd,
tool = tool,
tmux_pane_id = pane.id,
tmux_window_id = pane.window_id,
tmux_pid = pane.pid,
mux_session = pane.session_name,
pids = pids,
Expand All @@ -164,6 +168,24 @@ function M:pane_id()
return self.tmux_pane_id
end

function M:focus()
local pane_id = self:pane_id()
if not pane_id then
return
end

local window_id = self.tmux_window_id or pane_id
Util.exec({ "tmux", "select-window", "-t", window_id })
Util.exec({ "tmux", "select-pane", "-t", pane_id })
if vim.env.TMUX and self.mux_session then
Util.exec({ "tmux", "switch-client", "-t", self.mux_session })
end
end

function M:toggle()
return self:focus()
end

---Send text to a tmux pane
function M:send(text)
local function send()
Expand Down
Loading