-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
70 lines (48 loc) · 1.67 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import ranger.api
from ranger.ext.get_executables import get_executables
from os import getenv
from os.path import isfile, expanduser
from subprocess import Popen, PIPE
hook_init_prev = ranger.api.hook_init
zshz_src = expanduser(getenv("ZSHZ_SRC", ""))
def get_missing_dep():
if "zsh" not in get_executables():
return "zsh"
if not isfile(zshz_src):
return "ZSHZ_SRC"
return None
def hook_init(fm):
def z_add(signal):
cmd = f"source {zshz_src} && zshz --add '{signal.new.path}'"
Popen(["zsh", "-c", cmd])
fm.signal_bind("cd", z_add)
return hook_init_prev(fm)
if get_missing_dep() is None:
ranger.api.hook_init = hook_init
class z(ranger.api.commands.Command):
"""
:z
Jump directories with zsh-z.
"""
def execute(self):
dep = get_missing_dep()
if dep is not None:
self.fm.notify(f"Can't find {dep}.", bad=True)
return None
cmd = f"source {zshz_src} && zshz -e {' '.join(self.args[1:])}"
proc = Popen(["zsh", "-c", cmd], stdout=PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0: # not found
return None
dir = stdout.decode("utf-8").strip()
self.fm.cd(dir)
def tab(self, tabnum):
if not isfile(zshz_src):
return None
cmd = f"source {zshz_src} && zshz --complete {' '.join(self.args[1:])}"
proc = Popen(["zsh", "-c", cmd], stdout=PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0: # not found
return None
paths = stdout.decode("utf-8").strip().splitlines()
return [f"z {path}" for path in paths]