Skip to content

Commit eea474d

Browse files
author
skywind3000
committed
add z -, z --, z -2
1 parent 5c2e0c6 commit eea474d

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

z.lua

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-- z.lua - a cd command that learns, by skywind 2018, 2019
55
-- Licensed under MIT license.
66
--
7-
-- Version 1.5.0, Last Modified: 2019/02/14 22:57
7+
-- Version 1.5.1, Last Modified: 2019/02/15 11:26
88
--
99
-- * 10x faster than fasd and autojump, 3x faster than z.sh
1010
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
@@ -124,7 +124,6 @@ Z_SKIPPWD = false
124124
os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
125125

126126

127-
128127
-----------------------------------------------------------------------
129128
-- string lib
130129
-----------------------------------------------------------------------
@@ -779,8 +778,16 @@ function os.getopt(argv)
779778
if head ~= '-' then
780779
break
781780
end
782-
local part = arg:split('=')
783-
options[part[1]] = part[2] ~= nil and part[2] or ''
781+
if arg == '-' then
782+
options['-'] = ''
783+
elseif arg == '--' then
784+
options['-'] = '-'
785+
elseif arg:match('^-%d+$') then
786+
options['-'] = arg:sub(2)
787+
else
788+
local part = arg:split('=')
789+
options[part[1]] = part[2] ~= nil and part[2] or ''
790+
end
784791
end
785792
index = index + 1
786793
end
@@ -1540,16 +1547,28 @@ end
15401547

15411548

15421549
-----------------------------------------------------------------------
1543-
-- cd forward
1544-
-----------------------------------------------------------------------
1545-
function cd_forward(args, options)
1546-
end
1547-
1548-
1549-
-----------------------------------------------------------------------
1550-
-- cd detour
1550+
-- cd minus: "z -", "z --", "z -2"
15511551
-----------------------------------------------------------------------
1552-
function cd_forward(args, options)
1552+
function cd_minus(args, options)
1553+
Z_SKIPPWD = true
1554+
local M = z_match({}, 'time', Z_SUBDIR)
1555+
local size = #M
1556+
if options['-'] == '-' then
1557+
for i, item in ipairs(M) do
1558+
if i > 10 then break end
1559+
io.stderr:write(' ' .. tostring(i - 1) .. ' ' .. item.name .. '\n')
1560+
end
1561+
else
1562+
local level = 0
1563+
local num = options['-']
1564+
if num and num ~= '' then
1565+
level = tonumber(num)
1566+
end
1567+
if level >= 0 and level < size then
1568+
return M[level + 1].name
1569+
end
1570+
end
1571+
return nil
15531572
end
15541573

15551574

@@ -1588,10 +1607,8 @@ function main(argv)
15881607
local path = ''
15891608
if options['-b'] then
15901609
path = cd_backward(args, options)
1591-
elseif options['-f'] then
1592-
path = cd_forward(args, options)
1593-
elseif options['-d'] then
1594-
path = cd_detour(args, options)
1610+
elseif options['-'] then
1611+
path = cd_minus(args, options)
15951612
elseif #args == 0 then
15961613
path = nil
15971614
else
@@ -2049,8 +2066,8 @@ function _zlua
20492066
else
20502067
_zlua_call "$_ZL_CD" "$dest"
20512068
end
2069+
if test -n "$_ZL_ECHO"; pwd; end
20522070
end
2053-
if test -n "$_ZL_ECHO"; pwd; end
20542071
end
20552072
end
20562073

0 commit comments

Comments
 (0)