1
0
mirror of https://github.com/skywind3000/z.lua synced 2026-03-22 15:49:47 +00:00

46 Commits

Author SHA1 Message Date
skywind3000
7eee4e4b13 optimize with ffi (luajit builtin module). 2019-03-04 20:29:43 +08:00
skywind3000
edd71f7a6e update doc 2019-03-02 12:16:50 +08:00
skywind3000
f63a832921 improve errno handling in os.path.exists 2019-03-02 12:06:02 +08:00
skywind3000
f92ad79e2c fixed: os.path.isdir not work for symbol links close #59. 2019-03-02 11:55:39 +08:00
skywind3000
38c1741f4f prevent writing file racing 2019-03-01 14:45:03 +08:00
skywind3000
fdff5c5e1e commit new README.md 2019-02-28 12:24:20 +08:00
skywind3000
b691d537f2 z -b should not match current directory, close #56 2019-02-25 23:20:15 +08:00
skywind3000
472925ac92 improve lua test 2019-02-25 11:01:00 +08:00
Linwei
5ba0e50894 Merge pull request #54 from aaronjensen/patch-1
Use \which in case user has it aliased to type -a
2019-02-24 13:12:20 +08:00
Aaron Jensen
c769dba4dd Use \which in case user has it aliased to type -a 2019-02-23 19:40:26 -08:00
skywind3000
bbca83c9a5 update doc 2019-02-22 22:54:22 +08:00
skywind3000
53c127a4a2 update doc 2019-02-22 22:46:23 +08:00
skywind3000
5f7fa928b6 new $_ZL_FZF_HEIGHT, set to 0 to use fullscreen 2019-02-21 12:12:58 +08:00
skywind3000
68de23ecef v1.5.7: rename $_ZL_FZF_SORT to $_ZL_INT_SORT it will affect both -i and -I. 2019-02-21 11:15:02 +08:00
skywind3000
5ea08c5dac update doc 2019-02-20 23:31:37 +08:00
skywind3000
9dd0fec7ed update doc 2019-02-20 23:24:24 +08:00
skywind3000
fe11dd30c5 improve fzf sorting 2019-02-20 23:23:15 +08:00
skywind3000
db92fa0a1c new "$_ZL_FZF_SORT" to sort directories by alphabet in fzf 2019-02-20 20:24:24 +08:00
skywind3000
b1702839d2 update doc 2019-02-20 15:58:11 +08:00
skywind3000
1a0428f54e update doc 2019-02-20 15:31:11 +08:00
skywind3000
31e9d42d08 update image 2019-02-20 15:04:53 +08:00
skywind3000
14b942634c fixed picture 2019-02-20 14:52:14 +08:00
skywind3000
6cfe2150e9 images 2019-02-20 14:48:00 +08:00
skywind3000
a4c589a724 _ZL_FZF_FLAG can be used to override fzf parameters, default to "+s -e" 2019-02-20 12:25:07 +08:00
skywind3000
cb8021599c update doc 2019-02-19 17:11:16 +08:00
skywind3000
91a4d75e62 update doc 2019-02-19 15:26:50 +08:00
skywind3000
4d4dc8802b update doc 2019-02-19 11:43:33 +08:00
skywind3000
761b3ebf31 update 2019-02-19 11:24:46 +08:00
skywind3000
4e7ecde314 update z_print to support fz.sh
update os.path.isdir to handle windows path better
2019-02-19 11:22:05 +08:00
Linwei
e5c78e3087 Merge pull request #50 from contrun/master
fix spurious file not exists
2019-02-19 11:16:04 +08:00
B YI
1fb4f25744 fix spurious file not exists
see https://github.com/skywind3000/z.lua/issues/49
2019-02-19 10:12:01 +08:00
skywind3000
6057644adb commit new files 2019-02-18 23:25:03 +08:00
skywind3000
dc02feb424 update doc 2019-02-17 21:29:13 +08:00
skywind3000
2e658dd379 add -e to fzf 2019-02-17 17:51:03 +08:00
skywind3000
f638730d5f update doc and aliases 2019-02-17 17:24:29 +08:00
skywind3000
b9e8501b71 new: $_ZL_FZF_FLAG for additional fzf arguments
fix: environment variable parsing is flaky
2019-02-17 16:32:50 +08:00
skywind3000
31fd92f2da update doc 2019-02-17 02:31:28 +08:00
skywind3000
7af0290e11 fixed typo 2019-02-17 00:49:01 +08:00
skywind3000
8334b1adb7 update doc 2019-02-17 00:45:45 +08:00
skywind3000
f068e8597f update doc 2019-02-16 22:39:44 +08:00
skywind3000
92fc98d392 update doc 2019-02-16 14:43:54 +08:00
skywind3000
0b159b6c25 1.5.2: be aware of all arguments in fzf completion 2019-02-16 14:40:48 +08:00
skywind3000
6e330ab46f update doc for dirstack 2019-02-15 20:55:50 +08:00
skywind3000
1c9c5eb444 add newline for cmd 2019-02-15 12:16:25 +08:00
skywind3000
c4a9376c0c update doc for 1.5.1 2019-02-15 11:52:03 +08:00
skywind3000
eea474d3ea add z -, z --, z -2 2019-02-15 11:38:31 +08:00
9 changed files with 366 additions and 92 deletions

View File

@@ -18,7 +18,7 @@ z.lua 是一个快速路径切换工具,它会跟踪你在 shell 下访问过
- 使用增强匹配算法,更准确的带你去到你想去的地方。
- 低占用,能够仅在当前路径改变时才更新数据库(将 `$_ZL_ADD_ONCE` 设成 1
- 交互选择模式,如果有多个匹配结果的话,跳转前允许你进行选择。
- 集成 fzf (可选),可以用来做可视化选择或者 bash 参数补全。
- 集成 fzf (可选),可以用来做可视化选择或者参数补全。
- 快速跳转到父目录,或者项目根目录,代替反复 “cd ../../.." 。
- 兼容 lua 5.1, 5.2 和 5.3 以上版本。
- 自包含且无额外依赖,单个 `z.lua` 文件完成所有工作。
@@ -250,7 +250,9 @@ PS如果你使用 Fish shell需要 2.7.0 以上才支持该功能。
![](images/fzf.png)
当你使用 `"z -I vim"` 时12 条路径被筛选出来,并按照 frecent 排序,他们都包含 "vim" 关键字,在实际 cd 改变路径前z.lua 会调用 fzf 来让你更方便的选择你想去的地方,每条记录包含左边的 frecent 权重和右边的路径名,权重越高的排在越前面,你可以按回车选择,或者 ESC 退出
当你使用 `"z -I vim"` 时12 条路径被筛选出来,并按照 frecent 排序,他们都包含 "vim" 关键字,在实际 cd 改变路径前z.lua 会调用 fzf 来让你更方便的选择你想去的地方,每条记录包含左边的 frecent 权重和右边的路径名,权重越高的排在越前面。
你可以在 fzf 里输入一些**空格分隔**的关键字(不需要先后顺序),或者按 `CTRL+J` / `CTRL+K` (方向键的上下也可以)进行选择,`ESC` 或者 `CTRL`+`D`/`G` 放弃。
你仍然可以用老方法,通过在 `z` 命令后面添加更多关键词来精确的匹配你想去的地方,这个特性给了你一个可视化的方式来做这件事情。为了方便起见,通常把 `z -I` alias 成 `zf` (z + fuzzy finder)。如果搜索结果只有一项,那么 z.lua 会直接跳转过去,不需要启动 fzf 再选择一遍,只有多项结果要选择时,才会启动 fzf。
@@ -263,11 +265,11 @@ PS你可以使用 `$_ZL_FZF` 环境变量来精确指明 fzf 的可执行路
`"-b"` 选项可以快速回到某一级父目录,避免重复的输入 "cd ../../.."。
- **(没有参数)**`cd` 到项目根目录,即跳转到父目录中包含 (.git/.svn/.hg) 的地方
- **(没有参数)**`cd` 到项目根目录,即跳转到最近的包含 (.git/.svn/.hg) 的父目录
- **(单个参数)**`cd` 到离当前目录最近的以关键字开头的父目录,如果找不到就尝试跳到包含关键字的父目录。
- **(两个参数)**:将当前路径中的第一个关键词替换为第二个关键词。
为了使用简便,我们继续将 `z -b` 取个别名成 `zb`
将 `z -b` 别名成 `zb`
```bash
# 一直向上退到项目根目录(就是里面有一个 .git 目录的地方)
@@ -321,27 +323,50 @@ eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)"
有了 fzf 的帮助bash 下补全也非常方便了。注意看左边的权重fzf 搜索过滤的顺序是有讲究的Frecent 权重越高的越靠前,不是乱排序的,更不是简单的按字符串字母排序。这里完全保证权重越高的路径越靠前。
`z.lua` 可以同 [fz](https://github.com/changyuheng/fz) 协作以提供**更好的补全结果**,详细见 [FAQ](https://github.com/skywind3000/z.lua/wiki/FAQ#fzsh-for-better-completion)。
注意:该功能在初始化 z.lua 之前,会检测 $PATH 中是否有 fzf 这个程序,有的话才启用。
## MRU
以往我们经常使用 `cd -` 命令来回到刚才访问过的目录,而有了 fzf 的帮助,我们可以更高效的来完成这件事情
`z.lua` 提供 `dirstack` 让你更便捷的访问最近刚刚去过的目录,而不需要输入任何关键字。这个方法叫做 `dirstack`,它记录着最近你刚刚去过的 10 条最新路径,然后是用 `z -``z --` 和 `z -{num}` 来操作
```bash
# 显示当前的 dir stack
$ z --
0 /home/skywind/work/match/memory-match
1 /home/skywind/.local/etc
2 /home/skywind/software/vifm-0.9.1
3 /home/skywind/work
4 /home/skywind/work/match
# cd 到栈里 2 号路径
$ z -2
=> cd /home/skywind/software/vifm-0.9.1
# 弹出栈顶 (cd 到上一次的老路径),和 "z -0" 相同
$ z -
=> cd -
```
这个 `dirstack` 是根据 z.lua 的路径历史数据库计算的出来的,和具体的 shell 或者操作系统无关。你退出再登陆不会丢失这些记录,不同的 shell 之间也可以共享同一份记录。
此外,还能通过前面提到的 `-I` 和 `-t` 参数组和,使用 fzf 选择最近去过的目录:
```bash
alias zh='z -I -t .'
```
首先定义个新的别名 `zh`(回到历史路径的意思),我们使用了 `-t` 参数来告诉 `z.lua` 按时间戳为权重进行排序,同时 `-I` 启用 fzf 进行搜索,最后句号代表任意路径。
方便起见,定义个新的别名 `zh`(回到历史路径的意思),我们 `-t` 参数来告诉 `z.lua` 按时间戳为权重排序,同时 `-I` 启用 fzf 搜索,最后句号代表任意路径。
那么当我们在命令行敲入 zh 时,就可以用 fzf 选择最近呆过的路径了:
那么当我们在命令行敲入 zh 时,就可以用 fzf 进行历史路径操作了:
![](images/mru.png)
第一列数据代表该路径多少秒以前访问过,第二列是路径名称。此时,你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位或者用光标键的上和下CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
第一列上次访问距今多少秒,第二列是目录名。你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位或者用光标键的上和下CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。
注意:最好把增强匹配模式打开,这样查看历史路径时可以把当前路径过滤掉。
## Tips
@@ -368,7 +393,7 @@ FN="$HOME/.local/share/autojump/autojump.txt"
awk -F '\t' '{print $2 "|" $1 "|" 0}' $FN >> ~/.zlua
```
要更好的使用 `z.lua`,别忘记阅读:[Frequently Asked Questions](https://github.com/skywind3000/z.lua/wiki/FAQ)。
## Benchmark

View File

@@ -19,10 +19,10 @@ For example, `z foo bar` would match `/foo/bar` but not `/bar/foo`.
- **10x** times faster than **fasd** and **autojump**, **3x** times faster than **z.sh**.
- Available for **posix shells**: bash, zsh, dash, sh, ash, ksh, busybox and etc.
- Available for Fish Shell, Power Shell and Windows cmd.
- Enhanced matching mode takes you to where ever you want precisely.
- [Enhanced matching algorithm](#enhanced-matching) takes you to where ever you want precisely.
- Allow updating database only if `$PWD` changed with "$_ZL_ADD_ONCE" set to 1.
- Interactive selection enables you to choose where to go before cd.
- Intergrated with FZF (optional) for interactive selection and bash completion.
- Intergrated with FZF (optional) for interactive selection and completion.
- Quickly go back to a parent directory instead of typing "cd ../../..".
- Corresponding experience in different shells and operating systems.
- Compatible with Lua 5.1, 5.2 and 5.3+
@@ -34,7 +34,7 @@ For example, `z foo bar` would match `/foo/bar` but not `/bar/foo`.
```bash
z foo # cd to most frecent dir matching foo
z foo bar # cd to most frecent dir matching foo and bar
z -r foo # cd to highest ranked dir matching foo
z -r foo # cd to the highest ranked dir matching foo
z -t foo # cd to most recently accessed dir matching foo
z -l foo # list matches instead of cd
z -c foo # restrict matches to subdirs of $PWD
@@ -219,14 +219,14 @@ If no match is found, it will fall back to default matching method.
- Skip the current directory:
when you are calling `z xxx` but the best match is the current directory, z.lua will choose the 2nd best match result for you. Assuming the database:
When you are calling `z xxx` but the best match is the current directory, z.lua will choose the 2nd best match result for you. Assuming the database:
10 /Users/Great_Wall/.rbenv/versions/2.4.1/lib/ruby/gems
20 /Library/Ruby/Gems/2.0.0/gems
When I use `z gems` by default, it will take me to `/Library/Ruby/Gems/2.0.0/gems`, but it's not what I want, so I press up arrow and execute `z gems` again, it will take me to `/Users/Great_Wall/.rbenv/versions/2.4.1/lib/ruby/gems` and this what I want.
Of course I can always use `z env gems` to indicate what I want precisely. Skip the current directory means when you use `z xxx` you always want to change directory instead of stay in the same directory and do nothing if current directory is the best match.
Of course, I can always use `z env gems` to indicate what I want precisely. Skip the current directory means when you use `z xxx` you always want to change directory instead of stay in the same directory and do nothing if current directory is the best match.
The default matching method is designed to be compatible with original z.sh, but the enhanced matching method is much more handy and exclusive to z.lua.
@@ -235,7 +235,7 @@ The default matching method is designed to be compatible with original z.sh, but
By default, z.lua will add current directory to database each time before display command prompt (correspond with z.sh). But there is an option to allow z.lua add path only if current working directory changed.
To enable this, you can set `$_ZL_ADD_ONCE` to `1` before init z.lua. Or you can init z.lua on linux like this:
To enable this, you can set `$_ZL_ADD_ONCE` to `1` before init z.lua. Or you can initialize z.lua on linux like this:
````bash
eval "$(lua /path/to/z.lua --init bash once)"
@@ -243,7 +243,8 @@ eval "$(lua /path/to/z.lua --init zsh once)"
source (lua /path/to/z.lua --init fish once | psub)
````
It could be much faster on slow hardware or Cygwin/MSYS.
With `add once` mode off (default), z.lua will consider the time you spent in the directory (like z.sh). When this mode is on, consider the times you accessed the directory (like autojump), and that could be much faster on slow hardware.
## Interactive Selection
@@ -261,19 +262,22 @@ And then you can input the number and choose where to go before actual cd. eg. i
NOTE: for fish shell, this feature requires fish 2.7.0 or above.
## FZF Supports
From version 1.1.0, a new option `"-I"` will allow you to use fzf to select when there are multiple matches.
![](images/fzf.png)
When we use `"z -I vim"`12 paths contains keyword "vim" has been matched and ordered by their frecent value, the higher frecent comes with the higher rank. Then without cd to the highest ranked path, z.lua passes all the candidates to fzf. And you can use fzf to select where you want to go, or ESC to quit.
When we use `"z -I vim"`12 paths contains keyword "vim" has been matched and ordered by their frecent value, the higher frecent comes with the higher rank. Then without cd to the highest ranked path, z.lua passes all the candidates to fzf.
Now you can input some space separated keywords (no order required) or use `CTRL+J`/`CTRL+K` (same as `UP`/`DOWN`) to select where you want to go, or `ESC` / `CTRL`+`D`/`G` to give up.
Of course, you can always give more keywords to `z` command to match your destination precisely. `"z -I"` is similar to `"z -i"`, but use fzf. Both `"-i"` and `"-I"` provide you another way for path navigation.
Usually, `z -I` can be aliased to `zf` (z + fuzzy finder) for convenience. If there are only one path matched, `z -I` will jump to it directly, fzf will only be invoked for multiple matches.
Usually, `z -I` can be aliased to `zf` (z + fuzzy finder) for convenience. If there are only one path matched, `z -I` will jump to it directly, fzf will only be invoked for multiple matches. `"z -I ."` or `"zf ."` can be used to use fzf select from entire database.
`"z -I ."` or `"zf ."` can be used to use fzf select from entire database.
For more information about this, please visit [wiki - effective with fzf](https://github.com/skywind3000/z.lua/wiki/Effective-with-fzf).
NOTE: For fish shell, this feature requires fish 2.7.0 or above. You can specify fzf executable in `$_ZL_FZF` environment variable, `"fzf"` will be called by default.
@@ -282,8 +286,8 @@ NOTE: For fish shell, this feature requires fish 2.7.0 or above. You can specify
New option `"-b"` can quickly go back to a specific parent directory in bash instead of typing "cd ../../.." redundantly.
- **(No argument)**: `cd` into the project root, the project root is one of the parent directory with `.git`/`.hg`/`.svn` in it
- **(One argument)**: `cd` into the closest parent starting with keyword, if not find, goto the parent containing keyword.
- **(No argument)**: `cd` into the project root, the project root the nearest parent directory with `.git`/`.hg`/`.svn` in it.
- **(One argument)**: `cd` into the closest parent starting with keyword, if not find, go to the parent containing keyword.
- **(Two arguments)**: replace the first value with the second one (in the current path).
Let's start by aliasing `z -b` to `zb`:
@@ -337,36 +341,62 @@ Then press `<tab>` after `z xxx`:
![](images/complete-2.png)
With the help of fzf, completion in bash is much easier now.
With the help of fzf, completion in bash is much easier now.
`z.lua` can cooperate with [fz](https://github.com/changyuheng/fz) for **better completion** result in both bash and zsh, for more information see [FAQ](https://github.com/skywind3000/z.lua/wiki/FAQ#fzsh-for-better-completion).
NOTE: To enable this, command `fzf` must be found in `$PATH` before initialization.
## Most Recently Accessed Path
We always use `cd -` to go back to last accessed directory, but with the help of `fzf` we can achieve this productively:
`z.lua` provides a fast way to visit MRU directories without typing any keyword. That is `dirstack`, which records recently visited paths and can be manipulated by `z -`, `z --` and `z -{num}`:
```bash
# display current dir stack
$ z --
0 /home/skywind/work/match/memory-match
1 /home/skywind/.local/etc
2 /home/skywind/software/vifm-0.9.1
3 /home/skywind/work
4 /home/skywind/work/match
# cd to the 2nd directory in the stack
$ z -2
=> cd /home/skywind/software/vifm-0.9.1
# popup stacktop (cd to previous directory), same as "z -0"
$ z -
=> cd -
```
The `dirstack` is calculated from z.lua's database, and has no dependency on shells or systems. You will not lost records after re-login, and history can be shared across shells and sessions.
There is another way to access MRU directories interactively by utilizing parameter `-I` (fzf) and `-t` (sort by time):
```bash
alias zh='z -I -t .'
```
The new alias `zh` (jump to history) uses a parameter `-t` to sort the result by time and `-I` to use `fzf` for selection. the result is:
The new alias `zh` (jump to history) is very easy to input:
![](images/mru.png)
The first column indicates how many seconds ago you have visited, and the second column is the path name. With `zh`, you can type some character to use string matching in fzf, or use `<Up>`/`<Down>` (as well as `CTRL+j/k`) to move the selector (red `>`) up and down.
At last, press `<enter>` to accept or `<ESC>` to give up.
At last, press `<enter>` to accept or `<ESC>` to give up.
Remember to enable the [enhanced matching](#enhanced-matching) algorithm, the current working directory can be skipped with it.
## Tips
Recommended aliases you may find useful:
```bash
alias zc='z -c' # restrict matches to subdirs of $PWD
alias zz='z -i' # cd with interactive selection
alias zz='z -c' # restrict matches to subdirs of $PWD
alias zi='z -i' # cd with interactive selection
alias zf='z -I' # use fzf to select in multiple matches
alias zb='z -b' # quickly cd to the parent directory
```
@@ -385,11 +415,12 @@ FN="$HOME/.local/share/autojump/autojump.txt"
awk -F '\t' '{print $2 "|" $1 "|" 0}' $FN >> ~/.zlua
```
Don't forget to read the [Frequently Asked Questions](https://github.com/skywind3000/z.lua/wiki/FAQ).
## Benchmark
The slowest part is adding path to history data file. It will run every time when you press enter (installed in $PROMPT_COMMAND). so I profile it on my nas:
The slowest part is adding path to history data file. It will run every time when you press enter (installed in $PROMPT_COMMAND). So I profile it on my NAS:
```bash
$ time autojump --add /tmp
@@ -427,6 +458,18 @@ As you see, z.lua is the fastest one and requires less resource.
## History
- 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.10 (2019-03-01): Prevent writing file racing.
- 1.5.9 (2019-02-25): `z -b` should not match current directory (close #56).
- 1.5.8 (2019-02-21): new `$_ZL_FZF_HEIGHT` to control `--height` parameter in fzf.
- 1.5.7 (2019-02-21): rename `$_ZL_FZF_SORT` to `$_ZL_INT_SORT` it will affect both `-i` and `-I`.
- 1.5.6 (2019-02-20): set `$_ZL_FZF_SORT` to 1 to sort directories by alphabet in fzf.
- 1.5.5 (2019-02-20): `$_ZL_FZF_FLAG` can be used to override fzf flags, default to "+s -e".
- 1.5.4 (2019-02-19): fixed: file/path existence detection fails on read-only fs (closed [#49](https://github.com/skywind3000/z.lua/issues/49) by [@contrun](https://github.com/contrun)).
- 1.5.3 (2019-02-17): new `$_ZL_FZF_FLAG` for passing additional flags to fzf, add `-e` argument to fzf.
- 1.5.2 (2019-02-16): be aware of all arguments in fzf completion.
- 1.5.1 (2019-02-15): new: simulated dir stack by `z -`, `z --` and `z -{num}`.
- 1.5.0 (2019-02-14): fixed minor issues in backward jumping.
- 1.4.7 (2019-02-13): Don't use regex in backward jumping (use plain text instead).
- 1.4.6 (2019-02-12): change: `_ZL_EXCLUDE_DIRS` to a comma separated list of dirs to exclude.
@@ -452,6 +495,7 @@ As you see, z.lua is the fastest one and requires less resource.
This project needs help for the tasks below:
- [ ] Support csh/tcsh.
- [ ] Completion: Actually I got little knowledge in completion, and need help to improve it.
- [ ] Completion: Implement completion for Power Shell.
- [ ] Completion: Implement completion for different arguments.

BIN
images/step1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
images/step2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
images/step3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
images/step4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

1
z.cmd
View File

@@ -108,4 +108,5 @@ if /i "%RunMode%"=="-n" (
)
:end
echo.

313
z.lua
View File

@@ -4,7 +4,7 @@
-- z.lua - a cd command that learns, by skywind 2018, 2019
-- Licensed under MIT license.
--
-- Version 1.5.0, Last Modified: 2019/02/14 22:57
-- Version 1.6.0, Last Modified: 2019/03/04 14:47
--
-- * 10x faster than fasd and autojump, 3x faster than z.sh
-- * 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')
-----------------------------------------------------------------------
-- string lib
-----------------------------------------------------------------------
@@ -159,6 +158,10 @@ function string:startswith(text)
return false
end
function string:endswith(text)
return text == "" or self:sub(-#text) == text
end
function string:lstrip()
if self == nil then return nil end
local s = self:gsub('^%s+', '')
@@ -299,10 +302,107 @@ function os.log(text)
end
-----------------------------------------------------------------------
-- ffi optimize (luajit has builtin ffi module)
-----------------------------------------------------------------------
os.native = {}
os.native.status, os.native.ffi = pcall(require, "ffi")
if os.native.status then
local ffi = os.native.ffi
if windows then
ffi.cdef[[
int GetFullPathNameA(const char *name, uint32_t size, char *out, char **name);
int ReplaceFileA(const char *dstname, const char *srcname, void *, uint32_t, void *, void *);
uint32_t GetTickCount(void);
uint32_t GetFileAttributesA(const char *name);
uint32_t GetCurrentDirectoryA(uint32_t size, char *ptr);
]]
local kernel32 = ffi.load('kernel32.dll')
local buffer = ffi.new('char[?]', 300)
local INVALID_FILE_ATTRIBUTES = 0xffffffff
local FILE_ATTRIBUTE_DIRECTORY = 0x10
os.native.kernel32 = kernel32
function os.native.GetFullPathName(name)
local hr = kernel32.GetFullPathNameA(name, 290, buffer, nil)
return (hr > 0) and ffi.string(buffer, hr) or nil
end
function os.native.ReplaceFile(replaced, replacement)
local hr = kernel32.ReplaceFileA(replaced, replacement, nil, 2, nil, nil)
return (hr ~= 0) and true or false
end
function os.native.GetTickCount()
return kernel32.GetTickCount()
end
function os.native.GetFileAttributes(name)
return kernel32.GetFileAttributesA(name)
end
function os.native.exists(name)
local attr = os.native.GetFileAttributes(name)
return attr ~= INVALID_FILE_ATTRIBUTES
end
function os.native.isdir(name)
local attr = os.native.GetFileAttributes(name)
local isdir = FILE_ATTRIBUTE_DIRECTORY
if attr == INVALID_FILE_ATTRIBUTES then
return false
end
return (attr % (2 * isdir)) >= isdir
end
function os.native.getcwd()
local hr = kernel32.GetCurrentDirectoryA(299, buffer)
if hr <= 0 then return nil end
return ffi.string(buffer, hr)
end
else
ffi.cdef[[
typedef struct { long tv_sec; long tv_usec; } timeval;
int gettimeofday(timeval *tv, void *tz);
int access(const char *name, int mode);
char *realpath(const char *path, char *resolve);
char *getcwd(char *buf, size_t size);
]]
local timeval = ffi.new('timeval[?]', 1)
local buffer = ffi.new('char[?]', 4100)
function os.native.gettimeofday()
local hr = ffi.C.gettimeofday(timeval, nil)
local sec = tonumber(timeval[0].tv_sec)
local usec = tonumber(timeval[0].tv_usec)
return sec + (usec * 0.000001)
end
function os.native.access(name, mode)
return ffi.C.access(name, mode)
end
function os.native.realpath(name)
local path = ffi.C.realpath(name, buffer)
return (path ~= nil) and ffi.string(buffer) or nil
end
function os.native.getcwd()
local hr = ffi.C.getcwd(buffer, 4099)
return hr ~= nil and ffi.string(buffer) or nil
end
end
function os.native.tickcount()
if windows then
return os.native.GetTickCount()
else
return math.floor(os.native.gettimeofday() * 1000)
end
end
os.native.init = true
end
-----------------------------------------------------------------------
-- get current path
-----------------------------------------------------------------------
function os.pwd()
if os.native and os.native.getcwd then
local hr = os.native.getcwd()
if hr then return hr end
end
if os.getcwd then
return os.getcwd()
end
if windows then
local fp = io.popen('cd')
if fp == nil then
@@ -369,6 +469,10 @@ end
-----------------------------------------------------------------------
function os.path.abspath(path)
if path == '' then path = '.' end
if os.native and os.native.GetFullPathName then
local test = os.native.GetFullPathName(path)
if test then return test end
end
if windows then
local script = 'FOR /f "delims=" %%i IN ("%s") DO @echo %%~fi'
local script = string.format(script, path)
@@ -419,22 +523,24 @@ end
function os.path.isdir(pathname)
if pathname == '/' then
return true
elseif pathname == '' then
return false
elseif windows then
if pathname == '\\' then
return true
elseif pathname:match('^%a:[/\\]$') then
return true
end
end
local name = pathname .. '/'
local ok, err, code = os.rename(name, name)
if not ok then
if code == 13 then
return true
end
return false
if os.native and os.native.isdir then
return os.native.isdir(pathname)
end
return true
if clink and os.isdir then
return os.isdir(pathname)
end
local name = pathname
if (not name:endswith('/')) and (not name:endswith('\\')) then
name = name .. os.path.sep
end
return os.path.exists(name)
end
@@ -446,6 +552,18 @@ function os.path.exists(name)
if not ok then
if code == 13 then
return true
elseif code == 30 then
local f = io.open(name,"r")
if f ~= nil then
io.close(f)
return true
end
elseif name:sub(-1) == '/' and code == 20 and (not windows) then
local test = name .. '.'
ok, err, code = os.rename(test, test)
if code == 16 or code == 13 or code == 22 then
return true
end
end
return false
end
@@ -758,6 +876,41 @@ function os.scriptname()
end
-----------------------------------------------------------------------
-- get environ
-----------------------------------------------------------------------
function os.environ(name, default)
local value = os.getenv(name)
if os.envmap ~= nil and type(os.envmap) == 'table' then
local t = os.envmap[name]
value = (t ~= nil and type(t) == 'string') and t or value
end
if value == nil then
return default
elseif type(default) == 'boolean' then
value = value:lower()
if value == '0' or value == '' or value == 'no' then
return false
elseif value == 'false' or value == 'n' or value == 'f' then
return false
else
return true
end
elseif type(default) == 'number' then
value = tonumber(value)
if value == nil then
return default
else
return value
end
elseif type(default) == 'string' then
return value
elseif type(default) == 'table' then
return value:sep(',')
end
end
-----------------------------------------------------------------------
-- parse option
-----------------------------------------------------------------------
@@ -779,8 +932,16 @@ function os.getopt(argv)
if head ~= '-' then
break
end
local part = arg:split('=')
options[part[1]] = part[2] ~= nil and part[2] or ''
if arg == '-' then
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
index = index + 1
end
@@ -817,6 +978,9 @@ function math.random_init()
seed = seed .. rnd
end
seed = seed .. tostring(os.clock() * 10000000)
if os.native and os.native.tickcount then
seed = seed .. tostring(os.native.tickcount())
end
local number = 0
for i = 1, seed:len() do
local k = string.byte(seed:sub(i, i))
@@ -906,15 +1070,29 @@ function data_save(filename, M)
local tmpname = nil
local i
filename = os.path.expand(filename)
if windows then
fp = io.open(filename, 'w')
else
math.random_init()
math.random_init()
while true do
tmpname = filename .. '.' .. tostring(os.time())
tmpname = tmpname .. math.random_string(8)
local rnd = os.getenv('_ZL_RANDOM')
tmpname = tmpname .. '' .. (rnd and rnd or '')
-- print('tmpname: '..tmpname)
if os.native and os.native.tickcount then
local key = os.native.tickcount() % 1000
tmpname = tmpname .. string.format('%03d', key)
tmpname = tmpname .. math.random_string(5)
else
tmpname = tmpname .. math.random_string(8)
end
if not os.path.exists(tmpname) then
-- print('tmpname: '..tmpname)
break
end
end
if windows then
if os.native and os.native.ReplaceFile then
fp = io.open(tmpname, 'w')
else
fp = io.open(filename, 'w')
tmpname = nil
end
else
fp = io.open(tmpname, 'w')
end
if fp == nil then
@@ -927,7 +1105,11 @@ function data_save(filename, M)
end
fp:close()
if tmpname ~= nil then
os.rename(tmpname, filename)
if windows then
os.native.ReplaceFile(filename, tmpname)
else
os.rename(tmpname, filename)
end
os.remove(tmpname)
end
return true
@@ -1228,13 +1410,14 @@ function z_match(patterns, method, subdir)
subdir = subdir ~= nil and subdir or false
local M = data_load(DATA_FILE)
M = data_select(M, patterns, false)
M = data_filter(M)
if Z_MATCHNAME then
local N = data_select(M, patterns, true)
N = data_filter(N)
if #N > 0 then
M = N
end
end
M = data_filter(M)
M = data_update_frecent(M)
if method == 'time' then
current = os.time()
@@ -1287,7 +1470,7 @@ end
-----------------------------------------------------------------------
function z_print(M, weight, number)
local N = {}
local maxsize = 10
local maxsize = 9
local numsize = string.len(tostring(#M))
for _, item in pairs(M) do
local record = {}
@@ -1380,6 +1563,9 @@ function z_cd(patterns)
elseif Z_INTERACTIVE == 0 then
return M[1].name
end
if os.environ('_ZL_INT_SORT', false) then
table.sort(M, function (a, b) return a.name < b.name end)
end
local retval = nil
if Z_INTERACTIVE == 1 then
PRINT_MODE = '<stderr>'
@@ -1399,19 +1585,22 @@ function z_cd(patterns)
end
retval = M[index].name
elseif Z_INTERACTIVE == 2 then
local fzf = os.getenv('_ZL_FZF')
local fzf = os.environ('_ZL_FZF', 'fzf')
local tmpname = '/tmp/zlua.txt'
local cmd = '--nth 2.. --reverse --inline-info +s --tac'
local cmd = ((not fzf) and 'fzf' or fzf) .. ' ' .. cmd
local cmd = '--nth 2.. --reverse --inline-info --tac '
local flag = os.environ('_ZL_FZF_FLAG', '')
flag = (flag == '' or flag == nil) and '+s -e' or flag
cmd = ((fzf == '') and 'fzf' or fzf) .. ' ' .. cmd .. ' ' .. flag
if not windows then
tmpname = os.tmpname()
if not os.getenv('_ZL_FZF_FULLSCR') then
cmd = cmd .. ' --height 35%'
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
if height ~= nil and height ~= '' and height ~= '0' then
cmd = cmd .. ' --height ' .. height
end
cmd = cmd .. ' < "' .. tmpname .. '"'
else
tmpname = os.tmpname():gsub('\\', ''):gsub('%.', '')
tmpname = os.getenv('TMP') .. '\\zlua_' .. tmpname .. '.txt'
tmpname = os.environ('TMP', '') .. '\\zlua_' .. tmpname .. '.txt'
cmd = 'type "' .. tmpname .. '" | ' .. cmd
end
PRINT_MODE = tmpname
@@ -1498,6 +1687,7 @@ function cd_backward(args, options, pwd)
end
return os.path.normpath(path)
else
pwd = os.path.split(pwd)
local test = windows and pwd:gsub('\\', '/') or pwd
local key = windows and args[1]:lower() or args[1]
if not key:match('%u') then
@@ -1540,16 +1730,28 @@ end
-----------------------------------------------------------------------
-- cd forward
-- cd minus: "z -", "z --", "z -2"
-----------------------------------------------------------------------
function cd_forward(args, options)
end
-----------------------------------------------------------------------
-- cd detour
-----------------------------------------------------------------------
function cd_forward(args, options)
function cd_minus(args, options)
Z_SKIPPWD = true
local M = z_match({}, 'time', Z_SUBDIR)
local size = #M
if options['-'] == '-' then
for i, item in ipairs(M) do
if i > 10 then break end
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
@@ -1588,10 +1790,8 @@ function main(argv)
local path = ''
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 options['-'] then
path = cd_minus(args, options)
elseif #args == 0 then
path = nil
else
@@ -1693,7 +1893,7 @@ function z_init()
table.insert(Z_EXCLUDE, name)
end
end
if _zl_cmd ~= nil then
if _zl_cmd ~= nil and _zl_cmd ~= '' then
Z_CMD = _zl_cmd
end
if _zl_matchname ~= nil then
@@ -1723,7 +1923,7 @@ end
-- initialize clink hooks
-----------------------------------------------------------------------
function z_clink_init()
local once = os.getenv("_ZL_ADD_ONCE")
local once = os.environ("_ZL_ADD_ONCE", false)
local previous = ''
function z_add_to_database()
pwd = clink.get_cwd()
@@ -1883,8 +2083,7 @@ if [ "$TERM" != "dumb" ] && command -v fzf >/dev/null 2>&1; then
# To redraw line after fzf closes (printf '\e[5n')
bind '"\e[0n": redraw-current-line'
_zlua_fzf_complete() {
local query="${COMP_WORDS[COMP_CWORD]}"
local selected=$(_zlua | sed "s|$HOME|\~|" | $zlua_fzf --query "$query" | sed 's/^[0-9,.]* *//')
local selected=$(_zlua -l "${COMP_WORDS[@]:1}" | sed "s|$HOME|\~|" | $zlua_fzf | sed 's/^[0-9,.]* *//')
if [ -n "$selected" ]; then
COMPREPLY=( "$selected" )
fi
@@ -1924,12 +2123,12 @@ function z_shell_init(opts)
print(script)
end
local prompt_hook = (os.getenv("_ZL_NO_PROMPT_COMMAND") == nil)
local once = (os.getenv("_ZL_ADD_ONCE") ~= nil) or opts.once ~= nil
local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false))
local once = os.environ("_ZL_ADD_ONCE", false) or opts.once ~= nil
if opts.bash ~= nil then
if prompt_hook then
if opts.once then
if once then
print(script_init_bash_once)
elseif opts.fast then
print(script_init_bash_fast)
@@ -1939,10 +2138,14 @@ function z_shell_init(opts)
end
print(script_complete_bash)
if opts.fzf ~= nil then
fzf_cmd = "fzf --nth 2 --reverse --inline-info +s --tac"
if not os.getenv('_ZL_FZF_FULLSCR') then
fzf_cmd = fzf_cmd .. ' --height 35%'
fzf_cmd = "fzf --nth 2.. --reverse --inline-info --tac "
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
if height ~= nil and height ~= '' and height ~= '0' then
fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' '
end
local flag = os.environ('_ZL_FZF_FLAG', '')
flag = (flag == '' or flag == nil) and '+s -e' or flag
fzf_cmd = fzf_cmd .. ' ' .. flag .. ' '
print('zlua_fzf="' .. fzf_cmd .. '"')
print(script_fzf_complete_bash)
end
@@ -2049,8 +2252,8 @@ function _zlua
else
_zlua_call "$_ZL_CD" "$dest"
end
if test -n "$_ZL_ECHO"; pwd; end
end
if test -n "$_ZL_ECHO"; pwd; end
end
end

View File

@@ -6,20 +6,21 @@ ZLUA_SCRIPT="${0:A:h}/z.lua"
# search lua executable
if [[ -z "$ZLUA_EXEC" ]]; then
if [[ -x "$(which lua)" ]]; then
ZLUA_EXEC="$(which lua)"
elif [[ -x "$(which lua5.3)" ]]; then
ZLUA_EXEC="$(which lua5.3)"
elif [[ -x "$(which lua5.2)" ]]; then
ZLUA_EXEC="$(which lua5.2)"
elif [[ -x "$(which lua5.1)" ]]; then
ZLUA_EXEC="$(which lua5.1)"
if [[ -x "$(command which lua)" ]]; then
ZLUA_EXEC="$(command which lua)"
elif [[ -x "$(command which lua5.3)" ]]; then
ZLUA_EXEC="$(command which lua5.3)"
elif [[ -x "$(command which lua5.2)" ]]; then
ZLUA_EXEC="$(command which lua5.2)"
elif [[ -x "$(command which lua5.1)" ]]; then
ZLUA_EXEC="$(command which lua5.1)"
else
echo "Not find lua in your $PATH, please install it."
return
fi
fi
export _ZL_FZF_FLAG="-e"
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"