add z -, z --, z -2

This commit is contained in:
skywind3000
2019-02-15 11:38:31 +08:00
parent 5c2e0c6f07
commit eea474d3ea
+35 -18
View File
@@ -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.5.0, Last Modified: 2019/02/14 22:57 -- Version 1.5.1, Last Modified: 2019/02/15 11:26
-- --
-- * 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
@@ -124,7 +124,6 @@ Z_SKIPPWD = false
os.LOG_NAME = os.getenv('_ZL_LOG_NAME') os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
----------------------------------------------------------------------- -----------------------------------------------------------------------
-- string lib -- string lib
----------------------------------------------------------------------- -----------------------------------------------------------------------
@@ -779,8 +778,16 @@ function os.getopt(argv)
if head ~= '-' then if head ~= '-' then
break break
end end
local part = arg:split('=') if arg == '-' then
options[part[1]] = part[2] ~= nil and part[2] or '' options['-'] = ''
elseif arg == '--' then
options['-'] = '-'
elseif arg:match('^-%d+$') then
options['-'] = arg:sub(2)
else
local part = arg:split('=')
options[part[1]] = part[2] ~= nil and part[2] or ''
end
end end
index = index + 1 index = index + 1
end end
@@ -1540,16 +1547,28 @@ end
----------------------------------------------------------------------- -----------------------------------------------------------------------
-- cd forward -- cd minus: "z -", "z --", "z -2"
----------------------------------------------------------------------- -----------------------------------------------------------------------
function cd_forward(args, options) function cd_minus(args, options)
end Z_SKIPPWD = true
local M = z_match({}, 'time', Z_SUBDIR)
local size = #M
----------------------------------------------------------------------- if options['-'] == '-' then
-- cd detour for i, item in ipairs(M) do
----------------------------------------------------------------------- if i > 10 then break end
function cd_forward(args, options) io.stderr:write(' ' .. tostring(i - 1) .. ' ' .. item.name .. '\n')
end
else
local level = 0
local num = options['-']
if num and num ~= '' then
level = tonumber(num)
end
if level >= 0 and level < size then
return M[level + 1].name
end
end
return nil
end end
@@ -1588,10 +1607,8 @@ function main(argv)
local path = '' local path = ''
if options['-b'] then if options['-b'] then
path = cd_backward(args, options) path = cd_backward(args, options)
elseif options['-f'] then elseif options['-'] then
path = cd_forward(args, options) path = cd_minus(args, options)
elseif options['-d'] then
path = cd_detour(args, options)
elseif #args == 0 then elseif #args == 0 then
path = nil path = nil
else else
@@ -2049,8 +2066,8 @@ function _zlua
else else
_zlua_call "$_ZL_CD" "$dest" _zlua_call "$_ZL_CD" "$dest"
end end
if test -n "$_ZL_ECHO"; pwd; end
end end
if test -n "$_ZL_ECHO"; pwd; end
end end
end end