|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +is_nvim_running_in_current_tmux_session() { |
| 4 | + # echo 'Starting session check...' |
| 5 | + local TMUX_SESSION_NAME=$(tmux display-message -p '#S') |
| 6 | + # echo "Current tmux session name: $TMUX_SESSION_NAME" |
| 7 | + |
| 8 | + # Get the list of all running nvim PIDs |
| 9 | + local NVM_PIDS=($(pgrep -f "nvim")) |
| 10 | + |
| 11 | + if [[ ${#NVM_PIDS[@]} -eq 0 ]]; then |
| 12 | + echo 'No Neovim instances found at all.' |
| 13 | + return 1 # No nvim processes found |
| 14 | + fi |
| 15 | + |
| 16 | + # echo "Neovim PIDs found: ${NVM_PIDS[*]}" |
| 17 | + |
| 18 | + # Iterate over each PID of nvim processes |
| 19 | + for PID in "${NVM_PIDS[@]}"; do |
| 20 | + # echo "Checking PID: $PID" |
| 21 | + |
| 22 | + # Ensure valid PID |
| 23 | + if ps -p "$PID" > /dev/null 2>&1; then |
| 24 | + # TODO: sesion id is not the tmux session, but user login shell session; therefore in a new tmux session this code never works as expected |
| 25 | + local SESSION_ID=$(ps -o sess= -p "$PID" | sed 's/^[^[:alnum:]]*//; s/[^[:alnum:]]*$//') # Get the session ID |
| 26 | + local TMUX_SESSION_ID=$(tmux display-message -p '#{session_id}'| sed 's/^[^[:alnum:]]*//; s/[^[:alnum:]]*$//') # Get current tmux session ID |
| 27 | + |
| 28 | + # echo "Valid PID: $PID with Session ID = $SESSION_ID, TMUX Session ID: $TMUX_SESSION_ID" |
| 29 | + |
| 30 | + # Check if this process belongs to the current tmux session |
| 31 | + if [[ "$SESSION_ID" == "$TMUX_SESSION_ID" ]]; then |
| 32 | + echo "$PID" # Return the PID if it matches |
| 33 | + return 0 # nvim found in current session |
| 34 | + fi |
| 35 | + # else |
| 36 | + # echo "Invalid PID: $PID." |
| 37 | + fi |
| 38 | + done |
| 39 | + |
| 40 | + # echo "No Neovim found in the current session." |
| 41 | + return 1 # No nvim found in the current session |
| 42 | +} |
| 43 | + |
0 commit comments