mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-23 16:19:49 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a245db0d93 | ||
|
|
dd721703c3 | ||
|
|
7920d56c89 | ||
|
|
645818ccc8 | ||
|
|
b98911a227 | ||
|
|
671830059b | ||
|
|
0fad96124b | ||
|
|
465f2b8e62 | ||
|
|
ff9d874ef4 | ||
|
|
86120d206e | ||
|
|
1e0e3523b0 |
@@ -458,6 +458,9 @@ As you see, z.lua is the fastest one and requires less resource.
|
|||||||
|
|
||||||
## History
|
## History
|
||||||
|
|
||||||
|
- 1.7.2 (2019-08-01): Improve bash/zsh shell compatibility by [@barlik](https://github.com/barlik).
|
||||||
|
- 1.7.1 (2019-06-07): Fixed: `$_ZL_DATA` failure on Linux sometimes.
|
||||||
|
- 1.7.0 (2019-03-09): Support [ranger](https://github.com/skywind3000/z.lua/wiki/FAQ#how-to-integrate-zlua-to-ranger-), fix ReplaceFile issue in luajit (windows).
|
||||||
- 1.6.0 (2019-03-04): optimize with ffi module (luajit builtin module).
|
- 1.6.0 (2019-03-04): optimize with ffi module (luajit builtin module).
|
||||||
- 1.5.11 (2019-03-02): fixed: os.path.isdir doesn't work for symbol link folders.
|
- 1.5.11 (2019-03-02): fixed: os.path.isdir doesn't work for symbol link folders.
|
||||||
- 1.5.10 (2019-03-01): Prevent writing file racing.
|
- 1.5.10 (2019-03-01): Prevent writing file racing.
|
||||||
|
|||||||
91
ranger_zlua.py
Normal file
91
ranger_zlua.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import time, sys, os
|
||||||
|
import ranger.api
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
old_hook_init = ranger.api.hook_init
|
||||||
|
|
||||||
|
PATH_LUA = os.environ.get('RANGER_LUA')
|
||||||
|
PATH_ZLUA = os.environ.get('RANGER_ZLUA')
|
||||||
|
|
||||||
|
if not PATH_LUA:
|
||||||
|
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
|
||||||
|
break
|
||||||
|
|
||||||
|
if not PATH_LUA:
|
||||||
|
sys.stderr.write('Please install lua or set $RANGER_LUA.\n')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
if (not PATH_ZLUA) or (not os.path.exists(PATH_ZLUA)):
|
||||||
|
sys.stderr.write('Not find z.lua, please set $RANGER_ZLUA to absolute path of z.lua.\n')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
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.wait()
|
||||||
|
if PATH_ZLUA and PATH_LUA and os.path.exists(PATH_ZLUA):
|
||||||
|
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
|
||||||
|
args = self.args[1:]
|
||||||
|
if args:
|
||||||
|
mode = ''
|
||||||
|
for arg in args:
|
||||||
|
if arg in ('-l', '-e', '-x', '-h', '--help', '--'):
|
||||||
|
mode = arg
|
||||||
|
break
|
||||||
|
elif arg in ('-I', '-i'):
|
||||||
|
mode = arg
|
||||||
|
elif arg[:1] != '-':
|
||||||
|
break
|
||||||
|
if mode:
|
||||||
|
cmd = '"%s" "%s" '%(PATH_LUA, PATH_ZLUA)
|
||||||
|
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 = path.decode("utf-8", "ignore")
|
||||||
|
path = path.rstrip('\n')
|
||||||
|
self.fm.notify(path)
|
||||||
|
elif mode in ('-h', '-l', '--help'):
|
||||||
|
p = self.fm.execute_command(cmd + '| less +G', universal_newlines=True)
|
||||||
|
stdout, stderr = p.communicate()
|
||||||
|
elif mode == '--':
|
||||||
|
p = self.fm.execute_command(cmd + ' 2>&1 | less +G', universal_newlines=True)
|
||||||
|
stdout, stderr = p.communicate()
|
||||||
|
else:
|
||||||
|
if mode == '-I':
|
||||||
|
os.environ['_ZL_FZF_HEIGHT'] = '0'
|
||||||
|
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
|
||||||
|
self.fm.execute_console('redraw_window')
|
||||||
|
else:
|
||||||
|
p = self.fm.execute_command(cmd, universal_newlines=True, stdout=subprocess.PIPE)
|
||||||
|
stdout, stderr = p.communicate()
|
||||||
|
path = stdout.rstrip('\n')
|
||||||
|
if path and os.path.exists(path):
|
||||||
|
self.fm.cd(path)
|
||||||
|
else:
|
||||||
|
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
|
||||||
|
path = path.decode("utf-8", "ignore")
|
||||||
|
path = path.rstrip('\n')
|
||||||
|
if path and os.path.exists(path):
|
||||||
|
self.fm.cd(path)
|
||||||
|
else:
|
||||||
|
self.fm.notify('No matching found', bad = True)
|
||||||
|
return True
|
||||||
|
|
||||||
24
z.lua
24
z.lua
@@ -4,7 +4,7 @@
|
|||||||
-- z.lua - a cd command that learns, by skywind 2018, 2019
|
-- z.lua - a cd command that learns, by skywind 2018, 2019
|
||||||
-- Licensed under MIT license.
|
-- Licensed under MIT license.
|
||||||
--
|
--
|
||||||
-- Version 1.6.0, Last Modified: 2019/03/04 14:47
|
-- Version 1.7.2, Last Modified: 2019/08/01 19:45
|
||||||
--
|
--
|
||||||
-- * 10x faster than fasd and autojump, 3x faster than z.sh
|
-- * 10x faster than fasd and autojump, 3x faster than z.sh
|
||||||
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
|
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
|
||||||
@@ -1106,7 +1106,10 @@ function data_save(filename, M)
|
|||||||
fp:close()
|
fp:close()
|
||||||
if tmpname ~= nil then
|
if tmpname ~= nil then
|
||||||
if windows then
|
if windows then
|
||||||
os.native.ReplaceFile(filename, tmpname)
|
local ok, err, code = os.rename(tmpname, filename)
|
||||||
|
if not ok then
|
||||||
|
os.native.ReplaceFile(filename, tmpname)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
os.rename(tmpname, filename)
|
os.rename(tmpname, filename)
|
||||||
end
|
end
|
||||||
@@ -1866,11 +1869,8 @@ function z_init()
|
|||||||
DATA_FILE = _zl_data
|
DATA_FILE = _zl_data
|
||||||
else
|
else
|
||||||
-- avoid windows environments affect cygwin & msys
|
-- avoid windows environments affect cygwin & msys
|
||||||
if _zl_data:sub(2, 2) ~= ':' then
|
if not string.match(_zl_data, '^%a:[/\\]') then
|
||||||
local t = _zl_data:sub(3, 3)
|
DATA_FILE = _zl_data
|
||||||
if t ~= '/' and t ~= "\\" then
|
|
||||||
DATA_FILE = _zl_data
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -2010,14 +2010,14 @@ alias ${_ZL_CMD:-z}='_zlua'
|
|||||||
local script_init_bash = [[
|
local script_init_bash = [[
|
||||||
case "$PROMPT_COMMAND" in
|
case "$PROMPT_COMMAND" in
|
||||||
*_zlua?--add*) ;;
|
*_zlua?--add*) ;;
|
||||||
*) PROMPT_COMMAND="(_zlua --add \"\$(command pwd 2>/dev/null)\" &);$PROMPT_COMMAND" ;;
|
*) PROMPT_COMMAND="(_zlua --add \"\$(command pwd 2>/dev/null)\" &)${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
|
||||||
esac
|
esac
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local script_init_bash_fast = [[
|
local script_init_bash_fast = [[
|
||||||
case "$PROMPT_COMMAND" in
|
case "$PROMPT_COMMAND" in
|
||||||
*_zlua?--add*) ;;
|
*_zlua?--add*) ;;
|
||||||
*) PROMPT_COMMAND="(_zlua --add \"\$PWD\" &);$PROMPT_COMMAND" ;;
|
*) PROMPT_COMMAND="(_zlua --add \"\$PWD\" &)${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
|
||||||
esac
|
esac
|
||||||
]]
|
]]
|
||||||
|
|
||||||
@@ -2029,7 +2029,7 @@ _zlua_precmd() {
|
|||||||
}
|
}
|
||||||
case "$PROMPT_COMMAND" in
|
case "$PROMPT_COMMAND" in
|
||||||
*_zlua_precmd*) ;;
|
*_zlua_precmd*) ;;
|
||||||
*) PROMPT_COMMAND="_zlua_precmd;$PROMPT_COMMAND" ;;
|
*) PROMPT_COMMAND="_zlua_precmd${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
|
||||||
esac
|
esac
|
||||||
]]
|
]]
|
||||||
|
|
||||||
@@ -2056,7 +2056,7 @@ local script_init_zsh = [[
|
|||||||
_zlua_precmd() {
|
_zlua_precmd() {
|
||||||
(_zlua --add "${PWD:a}" &)
|
(_zlua --add "${PWD:a}" &)
|
||||||
}
|
}
|
||||||
typeset -gaU precmd_functions
|
typeset -ga precmd_functions
|
||||||
[ -n "${precmd_functions[(r)_zlua_precmd]}" ] || {
|
[ -n "${precmd_functions[(r)_zlua_precmd]}" ] || {
|
||||||
precmd_functions[$(($#precmd_functions+1))]=_zlua_precmd
|
precmd_functions[$(($#precmd_functions+1))]=_zlua_precmd
|
||||||
}
|
}
|
||||||
@@ -2066,7 +2066,7 @@ local script_init_zsh_once = [[
|
|||||||
_zlua_precmd() {
|
_zlua_precmd() {
|
||||||
(_zlua --add "${PWD:a}" &)
|
(_zlua --add "${PWD:a}" &)
|
||||||
}
|
}
|
||||||
typeset -gaU chpwd_functions
|
typeset -ga chpwd_functions
|
||||||
[ -n "${chpwd_functions[(r)_zlua_precmd]}" ] || {
|
[ -n "${chpwd_functions[(r)_zlua_precmd]}" ] || {
|
||||||
chpwd_functions[$(($#chpwd_functions+1))]=_zlua_precmd
|
chpwd_functions[$(($#chpwd_functions+1))]=_zlua_precmd
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user