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

Fail to switch panes when inside a python virtual environment #406

Open
vvatikiotis opened this issue Sep 29, 2024 · 3 comments
Open

Fail to switch panes when inside a python virtual environment #406

vvatikiotis opened this issue Sep 29, 2024 · 3 comments

Comments

@vvatikiotis
Copy link

When in tmux and from there on inside a python virtual directory, nvim fails to navigate from nvim pane to nvim pane. According to #230 (comment) this should be easy to triage and fix?

Also found a somewhat similar solution, in an issue here: 7db70e0

Thanks!

@christoomey
Copy link
Owner

Did you check any of the items in the troubleshooting list in the README? I'm guessing the most relevant would be the section on the process list.

@vvatikiotis
Copy link
Author

vvatikiotis commented Oct 7, 2024

That's what TmuxNavigatorProcessList reports when inside a python poetry virtual environment:

Ss   -zsh
S+   /Users/<username>/Library/Application Support/pypoetry/venv/bin/python

I'll fiddle with it and hopefully post a solution here. Thanks!

@tfriedel
Copy link

tfriedel commented Oct 13, 2024

The issue is the ps command in "is_vim" can not find nvim because it's a subprocess of the command that created the virtualenv (e.g. poetry or pixi) and has different TTY.
So we need check also the subprocesses as well. I asked ChatGPT to write a script that does it.

Modify is_vim in tmux.conf:
is_vim="~/.tmux/check_vim.sh"

Then create ~/.tmux/check_vim.sh and chmod +x it.

check_vim.sh:

#!/usr/bin/env bash

# Get the current TTY from tmux
current_tty=$(tmux display-message -p '#{pane_tty}')
current_tty=${current_tty#/dev/} # Remove '/dev/' prefix

# Get the PIDs of processes associated with the current TTY
pids=$(ps -o pid= -t "$current_tty")
pids=($(echo "$pids")) # Convert to an array

# Initialize a set of all descendant PIDs
all_pids=("${pids[@]}")

# Initialize the list of PIDs to process
to_process=("${pids[@]}")

# Loop to find all descendant processes iteratively
while [ ${#to_process[@]} -gt 0 ]; do
  # Get the child PIDs of the current PIDs
  child_pids=$(ps --no-headers -o pid= --ppid $(
    IFS=','
    echo "${to_process[*]}"
  ))
  # Remove leading/trailing whitespace and convert to array
  child_pids=($(echo "$child_pids"))
  # Break the loop if no more child processes are found
  if [ ${#child_pids[@]} -eq 0 ]; then
    break
  fi
  # Add child PIDs to the set of all_pids
  all_pids+=("${child_pids[@]}")
  # Update to_process with the new child PIDs
  to_process=("${child_pids[@]}")
done

# Remove duplicate PIDs
all_pids_unique=($(printf "%s\n" "${all_pids[@]}" | sort -u))

# Join all PIDs into a comma-separated list for ps
pid_list=$(
  IFS=','
  echo "${all_pids_unique[*]}"
)

# Get the state and command of all descendant processes
ps_output=$(ps -o state= -o comm= -p "$pid_list")

# Check if any of the processes match the regex
echo "$ps_output" | grep -iqE '^[^TXZ ]+ +(\S+/)?g?(view|l?n?vim?x?|fzf)(diff)?$'

# Store the exit status
is_vim_running=$?

# Exit with the appropriate status
exit $is_vim_running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants