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

Integration with ranger #61

Closed
rif opened this issue Mar 3, 2019 · 7 comments
Closed

Integration with ranger #61

rif opened this issue Mar 3, 2019 · 7 comments

Comments

@rif
Copy link

rif commented Mar 3, 2019

What would be the equivalent command for z.lua to integrate with ranger directory switch?

https://github.com/ranger/ranger/wiki/Integration-with-other-programs#fasd

Great tool, by the way!

Thanks,
-rif

@skywind3000
Copy link
Owner

skywind3000 commented Mar 8, 2019

Sorry for reply late, I was studying writing plugins for ranger, because I seldom use it.

here is the first script for path tracking:

import ranger.api
import subprocess

old_hook_init = ranger.api.hook_init

PATH_LUA='/usr/bin/lua'
PATH_ZLUA='/home/skywind/.local/etc/z.lua'

def hook_init(fm):
    def update_zlua(signal):
        subprocess.Popen([PATH_LUA, PATH_ZLUA, "--add", signal.new.path])
    fm.signal_bind('cd', update_zlua)
    return old_hook_init(fm)

ranger.api.hook_init = hook_init

Remember to change PATH_LUA and PATH_ZLUA, and I still need time to implement a z command in ranger.

@skywind3000
Copy link
Owner

skywind3000 commented Mar 8, 2019

OK, finished, ranger_zlua.py, copy it to ~/.config/ranger/plugins:

import ranger.api
import subprocess

old_hook_init = ranger.api.hook_init

PATH_LUA='/usr/bin/lua'
PATH_ZLUA='/home/skywind/.local/etc/z.lua'

def hook_init(fm):
    def update_zlua(signal):
        subprocess.Popen([PATH_LUA, PATH_ZLUA, "--add", signal.new.path])
    fm.signal_bind('cd', update_zlua)
    return old_hook_init(fm)

ranger.api.hook_init = hook_init

class z(ranger.api.commands.Command):
    def execute (self):
        args = self.args[1:]
        if args:
            directory = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
            directory = directory.decode("utf-8", "ignore")
            directory = directory.rstrip('\n')
            self.fm.cd(directory)
        return True

@skywind3000
Copy link
Owner

I submitted a ranger_zlua.py file here:
https://github.com/skywind3000/z.lua/blob/master/ranger_zlua.py

You can use it in your ~/.config/ranger/plugins directly, remember to set environment variable:

export RANGER_ZLUA="/path/to/z.lua"

@skywind3000
Copy link
Owner

I mention it in the FAQ

@rif
Copy link
Author

rif commented Mar 8, 2019

Thank you very much!

@rif rif closed this as completed Mar 8, 2019
@nedludd
Copy link

nedludd commented Mar 12, 2019

Can you explain more how this works? I followed the instructions but don't see a z command in ranger that overrides the existing one. Can you add a little explanation?

@hengstchon
Copy link

Can you explain more how this works? I followed the instructions but don't see a z command in ranger that overrides the existing one. Can you add a little explanation?

It's just like ranger-autojump.

Using :z in ranger to invoke the z command.

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

4 participants