mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-13 19:29:49 +00:00
version 1.1.0, supports fzf by z -I xxx
This commit is contained in:
11
README.md
11
README.md
@@ -292,6 +292,17 @@ FN="$HOME/.local/share/autojump/autojump.txt"
|
||||
awk -F '\t' '{print $2 "|" $1 "|" 0}' $FN >> ~/.zlua
|
||||
```
|
||||
|
||||
## History
|
||||
|
||||
- 1.1.0 (2019-02-02): New option '-I' to use fzf to select from multiple matches.
|
||||
- 1.0.0 (2019-02-01): Fixed minor issues and make it stable.
|
||||
- 0.5.0 (2019-01-21): supports fish shell (Daniel Lewan).
|
||||
- 0.4.1 (2019-01-20): Don't return failed exit code when $_ZL_ECHO is unbind (Mario Rodas).
|
||||
- 0.4.0 (2019-01-17): new enhanced matching algorithm,can be enabled by appending `enhanced` keyword after `--init`.
|
||||
- 0.3.0 (2018-12-26): new option `-i` to enable interactive selection.
|
||||
- 0.2.0 (2018-11-25): new option `$_ZL_ADD_ONCE` to enable updating datafile only if `$PWD` changed.
|
||||
- 0.1.0 (2018-04-30): supports windows cmd, cmder and conemu.
|
||||
- 0.0.0 (2018-03-21): initial commit, compatible with original z.sh.
|
||||
|
||||
## Credit
|
||||
|
||||
|
||||
8
z.cmd
8
z.cmd
@@ -54,12 +54,18 @@ if /i "%1"=="-x" (
|
||||
goto parse
|
||||
)
|
||||
|
||||
if /i "%1"=="-i" (
|
||||
if "%1"=="-i" (
|
||||
set "InterMode=-i"
|
||||
shift /1
|
||||
goto parse
|
||||
)
|
||||
|
||||
if "%1"=="-I" (
|
||||
set "InterMode=-I"
|
||||
shift /1
|
||||
goto parse
|
||||
)
|
||||
|
||||
if /i "%1"=="-s" (
|
||||
set "StripMode=-s"
|
||||
shift /1
|
||||
|
||||
177
z.lua
177
z.lua
@@ -4,7 +4,7 @@
|
||||
-- z.lua - z.sh implementation in lua, by skywind 2018, 2019
|
||||
-- Licensed under MIT license.
|
||||
--
|
||||
-- Version 45, Last Modified: 2019/02/01 21:07
|
||||
-- Version 1.1.0, Last Modified: 2019/02/02 14:51
|
||||
--
|
||||
-- * 10x times faster than fasd and autojump
|
||||
-- * 3x times faster than rupa/z
|
||||
@@ -94,17 +94,19 @@ os.argv = arg ~= nil and arg or {}
|
||||
-----------------------------------------------------------------------
|
||||
MAX_AGE = 5000
|
||||
DATA_FILE = '~/.zlua'
|
||||
PRINT_TO_STDERR = false
|
||||
PRINT_MODE = '<stdout>'
|
||||
PWD = ''
|
||||
Z_METHOD = 'frecent'
|
||||
Z_SUBDIR = false
|
||||
Z_INTERACTIVE = false
|
||||
Z_INTERACTIVE = 0
|
||||
Z_EXCLUDE = {}
|
||||
Z_CMD = 'z'
|
||||
Z_MATCHMODE = 0
|
||||
Z_MATCHNAME = false
|
||||
Z_SKIPPWD = false
|
||||
Z_LOGNAME = os.getenv('_ZL_LOG_NAME')
|
||||
|
||||
os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
@@ -207,23 +209,6 @@ function printT(table, level)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- write log
|
||||
-----------------------------------------------------------------------
|
||||
function mlog(text)
|
||||
if not Z_LOGNAME then
|
||||
return
|
||||
end
|
||||
local fp = io.open(Z_LOGNAME, 'a')
|
||||
if not fp then
|
||||
return
|
||||
end
|
||||
local date = "[" .. os.date('%Y-%m-%d %H:%M:%S') .. "] "
|
||||
fp:write(date .. text .. "\n")
|
||||
fp:close()
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- invoke command and retrive output
|
||||
-----------------------------------------------------------------------
|
||||
@@ -238,6 +223,23 @@ function os.call(command)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- write log
|
||||
-----------------------------------------------------------------------
|
||||
function os.log(text)
|
||||
if not os.LOG_NAME then
|
||||
return
|
||||
end
|
||||
local fp = io.open(os.LOG_NAME, 'a')
|
||||
if not fp then
|
||||
return
|
||||
end
|
||||
local date = "[" .. os.date('%Y-%m-%d %H:%M:%S') .. "] "
|
||||
fp:write(date .. text .. "\n")
|
||||
fp:close()
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- get current path
|
||||
-----------------------------------------------------------------------
|
||||
@@ -459,6 +461,13 @@ function os.path.expand(pathname)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- search executable
|
||||
-----------------------------------------------------------------------
|
||||
function os.path.search(name)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- get lua executable
|
||||
-----------------------------------------------------------------------
|
||||
@@ -1036,6 +1045,14 @@ function z_print(M, weight, number)
|
||||
maxsize = record.score:len()
|
||||
end
|
||||
end
|
||||
local fp = io.stdout
|
||||
if PRINT_MODE == '<stdout>' then
|
||||
fp = io.stdout
|
||||
elseif PRINT_MODE == '<stderr>' then
|
||||
fp = io.stderr
|
||||
else
|
||||
fp = io.open(PRINT_MODE, 'w')
|
||||
end
|
||||
for i = #N, 1, -1 do
|
||||
local record = N[i]
|
||||
local line = record.score
|
||||
@@ -1065,12 +1082,13 @@ function z_print(M, weight, number)
|
||||
end
|
||||
line = head .. ': ' .. line
|
||||
end
|
||||
if not PRINT_TO_STDERR then
|
||||
print(line)
|
||||
else
|
||||
io.stderr:write(line .. '\n')
|
||||
if fp ~= nil then
|
||||
fp:write(line .. '\n')
|
||||
end
|
||||
end
|
||||
if PRINT_MODE:sub(1, 1) ~= '<' then
|
||||
if fp ~= nil then fp:close() end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1101,35 +1119,85 @@ function z_cd(patterns)
|
||||
return nil
|
||||
elseif #M == 1 then
|
||||
return M[1].name
|
||||
elseif not Z_INTERACTIVE then
|
||||
elseif Z_INTERACTIVE == 0 then
|
||||
return M[1].name
|
||||
end
|
||||
PRINT_TO_STDERR = true
|
||||
z_print(M, true, true)
|
||||
io.stderr:write('> ')
|
||||
io.stderr:flush()
|
||||
local input = io.read('*l')
|
||||
if input == nil then
|
||||
return nil
|
||||
local retval = nil
|
||||
if Z_INTERACTIVE == 1 then
|
||||
PRINT_MODE = '<stderr>'
|
||||
z_print(M, true, true)
|
||||
io.stderr:write('> ')
|
||||
io.stderr:flush()
|
||||
local input = io.read('*l')
|
||||
if input == nil then
|
||||
return nil
|
||||
end
|
||||
local index = tonumber(input)
|
||||
if index == nil then
|
||||
return nil
|
||||
end
|
||||
if index < 1 or index > #M then
|
||||
return nil
|
||||
end
|
||||
retval = M[index].name
|
||||
elseif Z_INTERACTIVE == 2 then
|
||||
local fzf = os.getenv('_ZL_FZF')
|
||||
local tmpname = '/tmp/zlua.txt'
|
||||
local cmd = '--nth 2.. --reverse --inline-info +s --tac'
|
||||
local cmd = ((not fzf) and 'fzf' or fzf) .. ' ' .. cmd
|
||||
if not windows then
|
||||
tmpname = os.tmpname()
|
||||
cmd = 'cat "' .. tmpname .. '" | --height 35% ' .. cmd
|
||||
else
|
||||
tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '')
|
||||
tmpname = os.getenv('TMP') .. '\\zlua_' .. tmpname .. '.txt'
|
||||
cmd = 'type "' .. tmpname .. '" | ' .. cmd
|
||||
end
|
||||
PRINT_MODE = tmpname
|
||||
z_print(M, true, false)
|
||||
retval = os.call(cmd)
|
||||
-- io.stderr:write('<'..cmd..'>\n')
|
||||
os.remove(tmpname)
|
||||
if retval == '' or retval == nil then
|
||||
return nil
|
||||
end
|
||||
local pos = retval:find(' ')
|
||||
if not pos then
|
||||
return nil
|
||||
end
|
||||
retval = retval:sub(pos, -1):gsub('^%s*', '')
|
||||
end
|
||||
local index = tonumber(input)
|
||||
if index == nil then
|
||||
return nil
|
||||
end
|
||||
if index < 1 or index > #M then
|
||||
return nil
|
||||
end
|
||||
return M[index].name
|
||||
return (retval ~= '' and retval or nil)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- cd to parent directories which contains keyword
|
||||
-----------------------------------------------------------------------
|
||||
function cd_backward(args, options)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- cd forward
|
||||
-----------------------------------------------------------------------
|
||||
function cd_forward(args, options)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- cd detour
|
||||
-----------------------------------------------------------------------
|
||||
function cd_forward(args, options)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- main entry
|
||||
-----------------------------------------------------------------------
|
||||
function main(argv)
|
||||
local options, args = os.getopt(argv)
|
||||
mlog("main()")
|
||||
os.log("main()")
|
||||
if options == nil then
|
||||
return false
|
||||
elseif table.length(args) == 0 and table.length(options) == 0 then
|
||||
@@ -1139,8 +1207,8 @@ function main(argv)
|
||||
return false
|
||||
end
|
||||
if true then
|
||||
mlog("options: " .. dump(options))
|
||||
mlog("args: " .. dump(args))
|
||||
os.log("options: " .. dump(options))
|
||||
os.log("args: " .. dump(args))
|
||||
end
|
||||
if options['-c'] then
|
||||
Z_SUBDIR = true
|
||||
@@ -1151,11 +1219,19 @@ function main(argv)
|
||||
Z_METHOD = 'time'
|
||||
end
|
||||
if options['-i'] then
|
||||
Z_INTERACTIVE = true
|
||||
Z_INTERACTIVE = 1
|
||||
elseif options['-I'] then
|
||||
Z_INTERACTIVE = 2
|
||||
end
|
||||
if options['--cd'] or options['-e'] then
|
||||
local path = ''
|
||||
if #args == 0 then
|
||||
if options['-b'] then
|
||||
path = cd_backward(args, options)
|
||||
elseif options['-f'] then
|
||||
path = cd_forward(args, options)
|
||||
elseif options['-d'] then
|
||||
path = cd_detour(args, options)
|
||||
elseif #args == 0 then
|
||||
path = os.path.expand('~')
|
||||
else
|
||||
path = z_cd(args)
|
||||
@@ -1336,6 +1412,7 @@ _zlua() {
|
||||
-c) local arg_subdir="-c" ;;
|
||||
-s) local arg_strip="-s" ;;
|
||||
-i) local arg_inter="-i" ;;
|
||||
-I) local arg_inter="-I" ;;
|
||||
-h|--help) local arg_mode="-h" ;;
|
||||
*) break ;;
|
||||
esac
|
||||
@@ -1536,6 +1613,7 @@ function _zlua
|
||||
case "-c"; set arg_subdir "-c"
|
||||
case "-s"; set arg_strip "-s"
|
||||
case "-i"; set arg_inter "-i"
|
||||
case "-I"; set arg_inter "-I"
|
||||
case "-h"; set arg_mode "-h"
|
||||
case "--help"; set arg_mode "-h"
|
||||
case '*'; break
|
||||
@@ -1654,11 +1732,16 @@ if /i "%1"=="-x" (
|
||||
shift /1
|
||||
goto parse
|
||||
)
|
||||
if /i "%1"=="-i" (
|
||||
if "%1"=="-i" (
|
||||
set "InterMode=-i"
|
||||
shift /1
|
||||
goto parse
|
||||
)
|
||||
if "%1"=="-I" (
|
||||
set "InterMode=-I"
|
||||
shift /1
|
||||
goto parse
|
||||
)
|
||||
if /i "%1"=="-s" (
|
||||
set "StripMode=-s"
|
||||
shift /1
|
||||
|
||||
Reference in New Issue
Block a user