agkozak / zsh-z

Jump quickly to directories that you have visited "frecently." A native Zsh port of z.sh with added features.
MIT License
2.02k stars 76 forks source link

Integrating with ranger #55

Closed ask1234560 closed 3 years ago

ask1234560 commented 3 years ago

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.

agkozak commented 3 years ago

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).

agkozak commented 3 years ago

(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.)

ask1234560 commented 3 years ago

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

agkozak commented 3 years ago

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

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).

agkozak commented 3 years ago

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

ask1234560 commented 3 years ago

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.

ask1234560 commented 3 years ago

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

ask1234560 commented 3 years ago

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")
agkozak commented 3 years ago

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.

ask1234560 commented 3 years ago

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

agkozak commented 3 years ago

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!