|
1 | 1 | #!/bin/zsh
|
2 | 2 |
|
3 | 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 | 4 |
|
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 |
| 5 | + # Check if we're currently in a tmux session |
| 6 | + if [ -z "$TMUX" ]; then |
| 7 | + echo "false" |
| 8 | + exit 1 |
14 | 9 | fi
|
15 | 10 |
|
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." |
| 11 | + # Get the current tmux session name |
| 12 | + SESSION_NAME=$(tmux display-message -p '#S') |
| 13 | + # echo "session name: $SESSION_NAME" |
| 14 | + # Get the current window name |
| 15 | + WINDOW_NAME=$(tmux display-message -p '#W') |
| 16 | + # echo "window name: $WINDOW_NAME" |
| 17 | + |
| 18 | + # Initialize a variable to track if nvim is running |
| 19 | + nvim_running=false |
| 20 | + |
| 21 | + # Iterate over all pane IDs in the current tmux session |
| 22 | + for pane in $(tmux list-panes -s -t "$SESSION_NAME" -F "#{pane_id}"); do |
| 23 | + # Get the command being run in the current pane |
| 24 | + pane_command=$(tmux display-message -p -t "$pane" "#{pane_current_command}") |
| 25 | + |
| 26 | + # Check if the current command is 'nvim' |
| 27 | + if [[ "$pane_command" == "nvim" ]]; then |
| 28 | + nvim_running=true |
| 29 | + |
| 30 | + nvim_pid=$(ps -o pid,tty,command | grep "[n]vim" | grep "$(basename $(tmux display-message -p -t "%10" "#{pane_tty}"))" | awk '{print $1}') |
| 31 | + break |
37 | 32 | fi
|
38 | 33 | done
|
39 | 34 |
|
40 |
| - # echo "No Neovim found in the current session." |
41 |
| - return 1 # No nvim found in the current session |
| 35 | + # Output the result based on whether nvim was found |
| 36 | + if $nvim_running; then |
| 37 | + echo $nvim_pid |
| 38 | + exit 0 |
| 39 | + else |
| 40 | + # echo "false" |
| 41 | + exit 1 |
| 42 | + fi |
42 | 43 | }
|
43 | 44 |
|
0 commit comments