diff --git a/lua/smart-splits/tmux.lua b/lua/smart-splits/tmux.lua index df7a9b6..77d855b 100644 --- a/lua/smart-splits/tmux.lua +++ b/lua/smart-splits/tmux.lua @@ -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 @@ -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