agkozak/zsh-z

Integrating with ranger

ask1234560 opened this issue · 11 comments

Hi,
I was a user of z, thank you for creating this project.

I had created a jumper for ranger using z, this. The current implementation uses python regex for searching the pattern in the z file.

Is there any way i can use your project as standalone command in python subprocess, like z -e directory-name.

It actually already works for me -- neat!

But I keep my database in the default place -- ~/.z. Where do you keep yours? Let's get it working for you, and then we can think about how to make it it work for all users by tweaking that one line (I think that's all it will take).

(The file format is identical to that of rupa/z, so I think we can get your plugin working for people regardless of which shell or tool they're using.)

Thanks for the quick reply,

This is the issue i am facing, z command is not found
image

I wanted to replace the below logic with a subprocess call to zshz
image

You're going to want to have the program look for the database in

  • ZSHZ_DATA -- or, if it's empty, then
  • _Z_DATA -- which is also valid, or, if that's empty, then
  • ${HOME}/.z

You've got the second two accounted for. I tried

z_loc = getenv("ZSHZ_DATA") or getenv("_Z_DATA") or getenv("HOME")+"/.z"

and it seems to do the trick: I can set ZSHZ_DATA to whatever I want, and ranger (with your plugin) searches that file.

I'm not very proficient in Python, but here's what I do know. Zsh-z is not an executable; it has to be sourced into the shell and then run. So I suspect the right way to run it would be

subprocess.run(['zsh', '-c', 'source /path/to/zsh-z.plugin.zsh && zshz'])

See if you can get that to do what you want (you'll need to provide the appropriate path to the script, of course).

Ah, subprocess.run() works with the same arguments.

Thanks you,

subprocess.check_output(['zsh', '-c', 'source /path/to/zsh-z.plugin.zsh && zshz'])

It worked, this is exactly what i was looking for.

Is there any way to get file location of zsh-z.plugin.zsh. From environment or something.

The below one is working as expected with lesser lines of code. The only thing remaining is to dynamically find the location of zshz source file.

import ranger.api
from ranger.api.commands import *
from subprocess import check_output


class z(Command):
    """:z
    Uses .z file to set the current directory.
    """

    def execute(self):
        try:
            arguments = 'source /home/ananthu/.config/.zplug/repos/agkozak/zsh-z/zsh-z.plugin.zsh && zshz -e ' + \
                " ".join(self.args[1:])
            directory = check_output(
                ['zsh', '-c', arguments]).decode().rstrip("\n")
            self.fm.execute_console("cd " + directory)
        except Exception as e:
            raise Exception("Directory not found")

Perhaps the best thing to do would be to have an environment variable pointing to the script? The script isn't necessarily going to be in the PATH or the FPATH. The other thing you could do would be to install Zsh-z as a Git submodule inside your project so that the file would always be in a predictable subdirectory.

Yeah i was also thinking of creating a environment variable pointing to the script. Thank you for helping.

Yeah i was also thinking of creating a environment variable pointing to the script. Thank you for helping.

That sounds like a great idea. Good luck!