-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtmux.lua
More file actions
155 lines (138 loc) · 4.8 KB
/
tmux.lua
File metadata and controls
155 lines (138 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---Provide `opencode` in a [`tmux`](https://github.com/tmux/tmux) pane in the current window.
---@class opencode.provider.Tmux : opencode.Provider
---
---@field opts opencode.provider.tmux.Opts
---
---The `tmux` pane ID where `opencode` is running (internal use only).
---@field pane_id? string
local Tmux = {}
Tmux.__index = Tmux
Tmux.name = "tmux"
---@class opencode.provider.tmux.Opts
---
---`tmux` options for creating the pane.
---@field options? string
---
---Focus the opencode pane when created. Default: `false`
---@field focus? boolean
---
---Auto-close the tmux pane when opencode exits. Default: `true`
---@field auto_close? boolean
--
---Allow `allow-passthrough` on the opencode pane.
-- When enabled, opencode.nvim will use your configured tmux `allow-passthrough` option on its pane.
-- This allows opencode to use OSC escape sequences, but may leak escape codes to the buffer
-- (e.g., "=31337;OK" appearing in your buffer).
--
-- Limitations of having allow-passthrough disabled in the opencode pane:
-- - can't display images
-- - can't use special (terminal specific; non-system) clipboards
-- - may have issues setting window properties like the title from the pane
--
-- If you enable this, consider also enabling `focus` to auto-focus the pane on creation,
-- which can help avoid OSC code leakage while opencode is sending escape sequences on startup.
--
-- Default: `false` (allow-passthrough is disabled to prevent OSC code leakage)
---@field allow_passthrough? boolean
---@param opts? opencode.provider.tmux.Opts
---@return opencode.provider.Tmux
function Tmux.new(opts)
local self = setmetatable({}, Tmux)
self.opts = opts or {}
self.pane_id = nil
return self
end
---Check if we're running in a `tmux` session.
function Tmux.health()
if vim.fn.executable("tmux") ~= 1 then
return "`tmux` executable not found in `$PATH`.", {
"Install `tmux` and ensure it's in your `$PATH`.",
}
end
if not vim.env.TMUX then
return "Not running in a `tmux` session.", {
"Launch Neovim in a `tmux` session.",
}
end
return true
end
---Get the `tmux` pane ID where we started `opencode`, if it still exists.
---Ideally we'd find existing panes by title or command, but `tmux` doesn't make that straightforward.
---@return string|nil pane_id
function Tmux:get_pane_id()
local ok = self.health()
if ok ~= true then
error(ok, 0)
end
if self.pane_id then
-- Confirm it still exists
if vim.fn.system("tmux list-panes -t " .. self.pane_id):match("can't find pane") then
self.pane_id = nil
end
end
return self.pane_id
end
---Create or kill the `opencode` pane.
function Tmux:toggle()
local pane_id = self:get_pane_id()
if pane_id then
self:stop()
else
self:start()
end
end
---Start `opencode` in pane.
function Tmux:start()
local pane_id = self:get_pane_id()
if not pane_id then
-- Create new pane
local detach_flag = self.opts.focus and "" or "-d"
self.pane_id = vim.fn.system(
string.format("tmux split-window %s -P -F '#{pane_id}' %s '%s'", detach_flag, self.opts.options or "", self.cmd)
)
local disable_passthrough = self.opts.allow_passthrough ~= true -- default true (disable passthrough)
if disable_passthrough and self.pane_id and self.pane_id ~= "" then
vim.fn.system(string.format("tmux set-option -t %s -p allow-passthrough off", vim.trim(self.pane_id)))
end
end
end
---Kill the `opencode` pane.
function Tmux:stop()
local pane_id = self:get_pane_id()
if pane_id and self.opts.auto_close ~= false then
vim.fn.system("tmux kill-pane -t " .. pane_id)
self.pane_id = nil
end
end
---Find an `opencode` server running in a sibling pane of the current tmux window.
---@return opencode.cli.server.Server|nil
function Tmux:find_server()
if self.health() ~= true then
return nil
end
local session_window = vim.fn.system("tmux display-message -p '#{session_name}:#{window_index}'"):gsub("\n", "")
local panes_output = vim.fn.system(string.format("tmux list-panes -t '%s' -F '#{pane_tty}'", session_window))
for tty in panes_output:gmatch("[^\r\n]+") do
local tty_short = tty:gsub("^/dev/", "")
local ps_output = vim.fn.system(string.format("ps -t %s -o pid,command", tty_short))
for line in ps_output:gmatch("[^\r\n]+") do
local pid = line:match("^%s*(%d+).*opencode")
if pid then
local lsof = vim.fn.system(string.format("lsof -w -iTCP -sTCP:LISTEN -P -n -a -p %s", pid))
local port = lsof:match(":(%d+) %(LISTEN%)")
if port then
local ok, path = pcall(require("opencode.cli.client").get_path, tonumber(port))
if ok then
return {
pid = tonumber(pid),
port = tonumber(port),
cwd = path.directory or path.worktree,
}
end
end
end
end
end
return nil
end
return Tmux