mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-11 18:29:49 +00:00
Slight stylistic edits to ranger_zlua.py
This commit is contained in:
@@ -2,44 +2,45 @@ import time, sys, os
|
||||
import ranger.api
|
||||
import subprocess
|
||||
|
||||
old_hook_init = ranger.api.hook_init
|
||||
|
||||
# $RANGER_LUA and $RANGER_ZLUA variables are deprecated, do not use them.
|
||||
PATH_LUA = os.environ.get('RANGER_LUA') or os.environ.get('ZLUA_LUAEXE')
|
||||
PATH_ZLUA = os.environ.get('RANGER_ZLUA') or os.environ.get('ZLUA_SCRIPT')
|
||||
ZLUA_LUAEXE = os.environ.get('RANGER_LUA') or os.environ.get('ZLUA_LUAEXE')
|
||||
ZLUA_SCRIPT = os.environ.get('RANGER_ZLUA') or os.environ.get('ZLUA_SCRIPT')
|
||||
|
||||
if not PATH_LUA:
|
||||
if not ZLUA_LUAEXE:
|
||||
for path in os.environ.get('PATH', '').split(os.path.pathsep):
|
||||
for name in ('lua', 'luajit', 'lua5.3', 'lua5.2', 'lua5.1'):
|
||||
test = os.path.join(path, name)
|
||||
test = test + (sys.platform[:3] == 'win' and ".exe" or "")
|
||||
if os.path.exists(test):
|
||||
PATH_LUA = test
|
||||
ZLUA_LUAEXE = test
|
||||
break
|
||||
|
||||
def _report_error(msg):
|
||||
sys.stderr.write('ranger_zlua: ' + msg)
|
||||
raise RuntimeError(msg)
|
||||
|
||||
if not PATH_LUA:
|
||||
if not ZLUA_LUAEXE:
|
||||
_report_error('Please install lua in $PATH or make sure $ZLUA_LUAEXE points to a lua executable.\n')
|
||||
|
||||
if (not PATH_ZLUA) or (not os.path.exists(PATH_ZLUA)):
|
||||
if (not ZLUA_SCRIPT) or (not os.path.exists(ZLUA_SCRIPT)):
|
||||
_report_error('Could not find z.lua, please make sure $ZLUA_SCRIPT is set to absolute path of z.lua.\n')
|
||||
|
||||
|
||||
|
||||
# Inform z.lua about directories the user browses to inside ranger
|
||||
old_hook_init = ranger.api.hook_init
|
||||
|
||||
def hook_init(fm):
|
||||
def update_zlua(signal):
|
||||
import os, random
|
||||
os.environ['_ZL_RANDOM'] = str(random.randint(0, 0x7fffffff))
|
||||
p = subprocess.Popen([PATH_LUA, PATH_ZLUA, "--add", signal.new.path])
|
||||
p = subprocess.Popen([ZLUA_LUAEXE, ZLUA_SCRIPT, "--add", signal.new.path])
|
||||
p.wait()
|
||||
if PATH_ZLUA and PATH_LUA and os.path.exists(PATH_ZLUA):
|
||||
if ZLUA_SCRIPT and ZLUA_LUAEXE and os.path.exists(ZLUA_SCRIPT):
|
||||
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):
|
||||
import sys, os, time
|
||||
@@ -55,13 +56,13 @@ class z(ranger.api.commands.Command):
|
||||
elif arg[:1] != '-':
|
||||
break
|
||||
if mode:
|
||||
cmd = '"%s" "%s" '%(PATH_LUA, PATH_ZLUA)
|
||||
cmd = '"%s" "%s" '%(ZLUA_LUAEXE, ZLUA_SCRIPT)
|
||||
if mode in ('-I', '-i', '--'):
|
||||
cmd += ' --cd'
|
||||
for arg in args:
|
||||
cmd += ' "%s"'%arg
|
||||
if mode in ('-e', '-x'):
|
||||
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
|
||||
path = subprocess.check_output([ZLUA_LUAEXE, ZLUA_SCRIPT, '--cd'] + args)
|
||||
path = path.decode("utf-8", "ignore")
|
||||
path = path.rstrip('\n')
|
||||
self.fm.notify(path)
|
||||
@@ -79,7 +80,7 @@ class z(ranger.api.commands.Command):
|
||||
if path and os.path.exists(path):
|
||||
self.fm.cd(path)
|
||||
else:
|
||||
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
|
||||
path = subprocess.check_output([ZLUA_LUAEXE, ZLUA_SCRIPT, '--cd'] + args)
|
||||
path = path.decode("utf-8", "ignore")
|
||||
path = path.rstrip('\n')
|
||||
if path and os.path.exists(path):
|
||||
|
||||
Reference in New Issue
Block a user