skywind3000 / z.lua

:zap: A new cd command that helps you navigate faster by learning your habits.
MIT License
3k stars 141 forks source link

Integration with ranger #61

Closed rif closed 5 years ago

rif commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

I mention it in the FAQ

rif commented 5 years ago

Thank you very much!

nedludd commented 5 years ago

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 commented 5 years ago

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.