Skip to content

Commit

Permalink
Merge pull request #33 from mrjones2014/mrj/32/check-tmux-session
Browse files Browse the repository at this point in the history
check tmux session before attempting to change tmux panes
  • Loading branch information
mrjones2014 committed Oct 14, 2022
2 parents 227f3ae + cdf88ab commit 0575759
Showing 1 changed file with 22 additions and 3 deletions.
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

0 comments on commit 0575759

Please sign in to comment.