Skip to content

Commit 1e0e352

Browse files
author
skywind3000
committed
add ranger plugin for z.lua
1 parent 7eee4e4 commit 1e0e352

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

ranger_zlua.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import time, sys, os
2+
import ranger.api
3+
import subprocess
4+
5+
old_hook_init = ranger.api.hook_init
6+
7+
PATH_LUA = os.environ.get('RANGER_LUA')
8+
PATH_ZLUA = os.environ.get('RANGER_ZLUA')
9+
10+
if not PATH_LUA:
11+
for path in os.environ.get('PATH', '').split(os.path.pathsep):
12+
for name in ('lua', 'luajit', 'lua5.3', 'lua5.2', 'lua5.1'):
13+
test = os.path.join(path, name)
14+
test = test + (sys.platform[:3] == 'win' and ".exe" or "")
15+
if os.path.exists(test):
16+
PATH_LUA = test
17+
break
18+
19+
if not PATH_LUA:
20+
sys.stderr.write('Please install lua or set $RANGER_LUA.\n')
21+
sys.exit()
22+
23+
if (not PATH_ZLUA) or (not os.path.exists(PATH_ZLUA)):
24+
sys.stderr.write('Not find z.lua, please set $RANGER_ZLUA to absolute path of z.lua.\n')
25+
sys.exit()
26+
27+
28+
def hook_init(fm):
29+
def update_zlua(signal):
30+
import os, random
31+
os.environ['_ZL_RANDOM'] = str(random.randint(0, 0x7fffffff))
32+
p = subprocess.Popen([PATH_LUA, PATH_ZLUA, "--add", signal.new.path])
33+
p.wait()
34+
if PATH_ZLUA and PATH_LUA and os.path.exists(PATH_ZLUA):
35+
fm.signal_bind('cd', update_zlua)
36+
return old_hook_init(fm)
37+
38+
ranger.api.hook_init = hook_init
39+
40+
class z(ranger.api.commands.Command):
41+
def execute (self):
42+
import sys, os, time
43+
args = self.args[1:]
44+
if args:
45+
mode = ''
46+
for arg in args:
47+
if arg in ('-l', '-e', '-x', '-h', '--help', '--'):
48+
mode = arg
49+
break
50+
elif arg in ('-I', '-i'):
51+
mode = arg
52+
elif arg[:1] != '-':
53+
break
54+
if mode:
55+
cmd = '"%s" "%s" '%(PATH_LUA, PATH_ZLUA)
56+
if mode in ('-I', '-i', '--'):
57+
cmd += ' --cd'
58+
for arg in args:
59+
cmd += ' "%s"'%arg
60+
if mode in ('-e', '-x'):
61+
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
62+
path = path.decode("utf-8", "ignore")
63+
path = path.rstrip('\n')
64+
self.fm.notify(path)
65+
elif mode in ('-h', '-l', '--help'):
66+
p = self.fm.execute_command(cmd + '| less +G', universal_newlines=True)
67+
stdout, stderr = p.communicate()
68+
elif mode == '--':
69+
p = self.fm.execute_command(cmd + ' 2>&1 | less +G', universal_newlines=True)
70+
stdout, stderr = p.communicate()
71+
else:
72+
if mode == '-I':
73+
os.environ['_ZL_FZF_HEIGHT'] = '0'
74+
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
75+
self.fm.execute_console('redraw_window')
76+
else:
77+
p = self.fm.execute_command(cmd, universal_newlines=True, stdout=subprocess.PIPE)
78+
stdout, stderr = p.communicate()
79+
path = stdout.rstrip('\n')
80+
if path and os.path.exists(path):
81+
self.fm.cd(path)
82+
else:
83+
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
84+
path = path.decode("utf-8", "ignore")
85+
path = path.rstrip('\n')
86+
if path and os.path.exists(path):
87+
self.fm.cd(path)
88+
else:
89+
self.fm.notify('No matching found', bad = True)
90+
return True
91+

0 commit comments

Comments
 (0)