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

Doesn't work under virtual environment #48

Open
SLeitgeb opened this issue Dec 20, 2023 · 8 comments
Open

Doesn't work under virtual environment #48

SLeitgeb opened this issue Dec 20, 2023 · 8 comments

Comments

@SLeitgeb
Copy link

I noticed that navigation doesn't work in nvim instances launched from an activated virtual environments (I use pipenv shell for this).

Debugging with the recommended print(fp), I noticed that nvim is not reported in the cmdline key:

[{'pid': 190929, 'cmdline': ['/usr/bin/python', '/home/simon/.local/bin/pipenv', 'shell'], 'cwd': '/home/simon/test'}]

After a bit of exploration, I managed to get navigation working by extending the matching logic to also look at the current window title like this:

def is_window_vim(window, vim_id):
    fp = window.child.foreground_processes
    return re.search(vim_id, window.title, re.I) or any(re.search(vim_id, p['cmdline'][0] if len(p['cmdline']) else '', re.I) for p in fp)

Not sure if this is a good idea, thoughts? Would this fail in some scenario or could it have some unintended consequences? Anyway, it works at least as a temporary workaround.

@knubie
Copy link
Owner

knubie commented Dec 21, 2023

window.title is the kitty window title I assume? Are you setting that manually?

@SLeitgeb
Copy link
Author

It's the title of each kitty window, yes. I'm using SwayFX window manager, I guess it could be set there, both Sway in Wayland and i3 in X11 use this property to display window titles in the UI, but I always assumed that applications set these properties through Wayland / X. Not sure if this works the same in GNOME, KDE, etc.

This is how the example window looks when I run kitty @ ls | jq '.[].tabs.[].windows':

[
  {
    "at_prompt": false,
    "cmdline": [
      "/usr/bin/zsh"
    ],
    "columns": 126,
    "cwd": "/home/simon/test",
    "env": {
      "KITTY_WINDOW_ID": "1",
      "PWD": "/home/simon"
    },
    "foreground_processes": [
      {
        "cmdline": [
          "/usr/bin/python",
          "/home/simon/.local/bin/pipenv",
          "shell"
        ],
        "cwd": "/home/simon",
        "pid": 408767
      }
    ],
    "id": 1,
    "is_active": false,
    "is_focused": false,
    "is_self": false,
    "lines": 30,
    "pid": 407939,
    "title": "nvim",
    "user_vars": {}
  },
  {
    "at_prompt": false,
    "cmdline": [
      "/usr/bin/zsh"
    ],
    "columns": 126,
    "cwd": "/home/simon/test",
    "env": {
      "KITTY_WINDOW_ID": "2",
      "PWD": "/home/simon/test"
    },
    "foreground_processes": [
      {
        "cmdline": [
          "kitten",
          "@",
          "ls"
        ],
        "cwd": "/home/simon/test",
        "pid": 447456
      },
      {
        "cmdline": [
          "jq",
          ".[].tabs.[].windows"
        ],
        "cwd": "/home/simon/test",
        "pid": 447457
      }
    ],
    "id": 2,
    "is_active": true,
    "is_focused": true,
    "is_self": true,
    "lines": 30,
    "pid": 408568,
    "title": "kitty @ ls | jq '.[].tabs.[].windows'",
    "user_vars": {}
  }
]

@SLeitgeb
Copy link
Author

I found one issue with this, though. When I send nvim to the background via <C-z>, the window title remains set to nvim and the navigation doesn't work until I bring back the nvim process to the foreground. I don't know if this has a good solution …

@ghost
Copy link

ghost commented Jan 17, 2024

if window is None:
    return 

cmd = window.child.foreground_cmdline[0]
print(cmd)

# if is_window_vim(window, vim_id):
if cmd[-4:] == "nvim":
    for keymap in key_mapping.split(">"):
        encoded = encode_key_mapping(window, keymap)
        window.write_to_child(encoded)

when the focus terminal , the result of the terminal is "-zsh", and when the focus nvim , the result is nvim. perfectly solved the problem of nvim being in a python virtual environment

@knubie
Copy link
Owner

knubie commented Jan 19, 2024

Interesting, so it seems like window.child.foreground_cmdline[0] may be more reliable in general than window.child.foreground_processes[n]['cmdline'][0]. @SLeitgeb Does this work for you?

@SLeitgeb
Copy link
Author

SLeitgeb commented Jan 19, 2024

It doesn't actually work for me. Interestingly, the values depend on wether I run nvim using

$ pipenv shell
$ nvim
  1. pipenv run nvim

In the first case, both window.child.foreground_cmdline and window.child.foreground_processes['cmdline'] report ['/usr/bin/python', '/home/simon/.local/bin/pipenv', 'shell']. The only indication that the window is running nvim I found so far is window.title. I have done more testing since my previous comment and the window.title remains set correctly when sending the process to background with <C-z> and foregrounding it with fg.

Funnily enough, in the second case (pipenv run nvim), window.child.foreground_cmdline and window.child.foreground_processes['cmdline'] both report the “correct” process ('/usr/bin/nvim'), while window.title reports pipenv run nvim at first and then fg after <C-z> and fg.

However, a combination of the original check + window.title actually worked well for me over the last few weeks. This is how I'm using it right now:

def is_window_vim(window, vim_id):
    fp = window.child.foreground_processes
    return window.title == 'nvim' or any(re.search(vim_id, p['cmdline'][0] if len(p['cmdline']) else '', re.I) for p in fp)

I switched to the literal check window.title == 'nvim' after noticing that the regex check also matches e.g. windows after cd ~/.config/nvim (window.title → ~/.config/nvim). It all seems a bit hacky, but at least it works.

@ghost
Copy link

ghost commented Jan 20, 2024

@SLeitgeb That's odd, I'm using a virtual environment provided by pyenv, It may be that the tools have different implementation mechanisms

@SLeitgeb
Copy link
Author

@nanmoumou That's interesting, I thought pyenv doesn't actually deal with virtual environments, but let's you switch python versions by setting the PATH environment variable. I guess all the tools behave differently based on what processes they call, etc.

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

2 participants