skywind3000 / z.lua

:zap: A new cd command that helps you navigate faster by learning your habits.
MIT License
2.94k stars 137 forks source link

希望添加当执行 `z foo bar` 其中 `foo` 被记录过,其子目录 `bar` 没有被记录过时也能 `cd` 的功能 #175

Open vaaandark opened 1 year ago

vaaandark commented 1 year ago

我设想的做法是当 z one two three four 失败后,使用去掉了最后一个 pattern 的 one two three 来匹配,当在数据中匹配到了 one two three ,就在这个路径中找有没有子目录 four ,如果就直接 cd 进入。

效果类似于:

$ grep 'linux/drivers' ~/.zlua # 记录中无 linux/driver
$ grep 'linux' ~/.zlua # 记录中有 linux
/home/xxx/linux|2|1676041029
$ z linux drivers
$ pwd
/home/xxx/linux/drivers

我认为可以添加如下几行实现这个功能:

--- a/z.lua
+++ b/z.lua
@@ -1954,6 +1954,12 @@ function main(argv)
                                        path = os.path.norm(path)
                                end
                        end
+      if path == nil then
+        local prefix = table.pack(table.unpack(args, 1, #args - 1))
+        local last = args[#args]
+        path = z_cd(prefix)..os.path.sep..last
+        path = os.path.isdir(path) and path or nil
+      end
                end
                if path ~= nil then
                        io.write(path .. (options['-e'] and "\n" or ""))
vaaandark commented 1 year ago

更正一下自己的实现,应该用 if path == nil and #args > 1 then

skywind3000 commented 1 year ago

OK 回头我看看