Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check tmux session before attempting to change tmux panes #33

Merged
merged 1 commit into from
Oct 14, 2022
Merged
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
25 changes: 22 additions & 3 deletions lua/smart-splits/tmux.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
local M = {}

local function get_socket_path()
local tmux = vim.env.TMUX
if not tmux or #tmux == 0 then
return nil
end

return vim.split(tmux, ',')[1]
end

local function tmux_exec(cmd)
local socket = get_socket_path()
if not socket then
return nil
end

local cmd_str = string.format('tmux -S %s %s', socket, cmd)
return vim.fn.system(cmd_str)
end

---Try to get current tmux pane ID
---returns nil if failed.
---returns nil if failed or not in a tmux session.
---@return string|nil
function M.current_pane_id()
local _, id = pcall(function()
local output = vim.fn.system('tmux display-message -p "#{pane_id}"')
local output = tmux_exec('display-message -p "#{pane_id}"')
if not output or #output == 0 then
return nil
end
Expand All @@ -23,7 +42,7 @@ end
function M.next_pane(direction)
direction = string.upper(direction)
local ok, _ = pcall(function()
vim.fn.system(string.format('tmux select-pane -%s', direction))
tmux_exec(string.format('select-pane -%s', direction))
end)

return ok
Expand Down