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

35 Commits
1.8.3 ... 1.8.7

Author SHA1 Message Date
skywind3000
65bfbf8dfb update lfs detection 2020-06-29 19:40:55 +08:00
skywind3000
1b9630e72d Merge branch 'master' of https://github.com/skywind3000/z.lua 2020-06-29 18:11:32 +08:00
skywind3000
c98d2d783e use lfs module or cffi in luajit if possible 2020-06-29 18:11:23 +08:00
Linwei
057e4f77f3 Merge pull request #108 from exic/patch-1
Update README.md
2020-05-29 17:34:37 +08:00
Jonas
8a90ca6df4 Update README.md
Fix typo
2020-05-29 11:14:38 +02:00
skywind3000
a4c51d8103 update doc 2020-03-28 19:48:20 +08:00
skywind3000
b40df870a6 update doc 2020-03-18 22:47:05 +08:00
skywind3000
a55bb87e1b update docs 2020-03-18 22:38:46 +08:00
skywind3000
c4512b47d2 update doc 2020-03-18 22:36:35 +08:00
skywind3000
6c567b60b3 update doc 2020-03-17 23:12:08 +08:00
skywind3000
8fff9e6471 update doc 2020-03-17 23:11:13 +08:00
skywind3000
f7a9104983 update z.lua 2020-03-17 16:38:30 +08:00
skywind3000
7b43a0afd6 Merge branch 'master' of https://github.com/skywind3000/z.lua 2020-03-17 16:37:48 +08:00
skywind3000
788c0f4b77 new --init option clean to skip hooks 2020-03-17 16:37:21 +08:00
skywind3000
47aa86b0f9 remove files 2020-03-17 11:37:04 +08:00
skywind3000
a1498c8672 update 2020-03-17 02:35:21 +08:00
skywind3000
4c34e29e08 update 2020-03-17 02:26:49 +08:00
skywind3000
3d188ab6d9 update 2020-03-17 02:05:41 +08:00
skywind3000
a5f4cb692c update 2020-03-17 00:50:27 +08:00
skywind3000
77a917d116 update czmod.z 2020-03-17 00:20:06 +08:00
skywind3000
93e9715e7d commit new iposix.c/.h 2020-03-16 23:11:09 +08:00
skywind3000
b7c3fbc4ee update new imembase.c 2020-03-16 22:44:24 +08:00
skywind3000
ebda4aa061 update module 2020-03-16 22:30:50 +08:00
skywind3000
168ec49849 update 2020-03-16 18:48:26 +08:00
skywind3000
ec123de01a update 2020-03-12 18:35:03 +08:00
skywind3000
a0d4750431 commit czmod 2020-03-11 16:23:14 +08:00
skywind3000
d615051a10 improve compinit handling 2020-03-08 00:14:04 +08:00
skywind3000
f448f2ab1f suppress compdef error message if compinit hasn't been invoked. 2020-03-05 18:09:21 +08:00
Linwei
8ed3422670 Merge pull request #93 from Aloxaf/no_compctl
Improve zsh's completion
2020-02-28 17:26:27 +08:00
Aloxaf
3edbda89a0 Improve zsh's completion 2020-02-28 15:32:30 +08:00
Linwei
c9b50b3c50 Merge pull request #92 from kidonng/add-fisher-compatibility
添加 Fisher 支持
2020-02-23 17:28:34 +08:00
Kid
228addbc49 Add Fisher compatibility 2020-02-23 13:48:37 +08:00
skywind3000
bca25eba12 fish shell: set $_ZL_ECHO in global scope 2020-02-10 19:26:22 +08:00
skywind3000
097ca07ecd update fish script 2020-02-10 18:24:20 +08:00
skywind3000
4ee783956d update doc 2020-02-10 10:52:03 +08:00
4 changed files with 96 additions and 25 deletions

View File

@@ -111,6 +111,7 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
- 设置 `$_ZL_CD` 用来指定你想用的 cd 命令,比如有人用 cd_func 。 - 设置 `$_ZL_CD` 用来指定你想用的 cd 命令,比如有人用 cd_func 。
- 设置 `$_ZL_ECHO` 为 1 可以在跳转后显示目标路径名称。 - 设置 `$_ZL_ECHO` 为 1 可以在跳转后显示目标路径名称。
- 设置 `$_ZL_MATCH_MODE` 为 1 可以打开 “增强匹配模式”。 - 设置 `$_ZL_MATCH_MODE` 为 1 可以打开 “增强匹配模式”。
- 设置 `$_ZL_HYPHEN` 为 1 可以允许关键字中包含横线 (横线默认是 lua 正则关键字,要转写成 `%-`)。
## Aging ## Aging
@@ -175,7 +176,7 @@ Frecency 是一个由 'recent' 和 'frequency' 组成的合成词,这个术语
30 /home/user/workspace/project2 30 /home/user/workspace/project2
40 /home/user/workspace/project3 40 /home/user/workspace/project3
在增强模式下使用 `"z wo"` 的话,只有 `/home/user/work` 满足匹配,因为按照第二条规则,这是唯一一条最有一段名称匹配 `wo` 的路径。 在增强模式下使用 `"z wo"` 的话,只有 `/home/user/workspace` 满足匹配,因为按照第二条规则,这是唯一一条最有一段名称匹配 `wo` 的路径。
因为最后一级目录名称总是最容易记住的,所以给到它比较高的优先级。在默认匹配算法中,你同样可以用 `"z space$"` 来达到相同的目的,但是 `"z wo"` 可以打更少的字。 因为最后一级目录名称总是最容易记住的,所以给到它比较高的优先级。在默认匹配算法中,你同样可以用 `"z space$"` 来达到相同的目的,但是 `"z wo"` 可以打更少的字。

View File

@@ -13,16 +13,26 @@ z.lua is a faster way to navigate your filesystem. It tracks your most used dire
For example, `z foo bar` would match `/foo/bar` but not `/bar/foo`. For example, `z foo bar` would match `/foo/bar` but not `/bar/foo`.
## Reputation
From people using z.lua:
- I like this in principal. Im pretty damn predictable at the command line and far too lazy to make shortcuts
- It feels far more intuitive and it's so incredibly convenient to be able to jump between folders I'm working in without having to traverse an entire tree. The shell used to feel so constraining for me, but tools like this are making me enjoy it so much more.
- I can finally have autojump-like functionality on my Raspberry Pi 1 without waiting 30 seconds every time I open a new shell. Thanks z.lua devs.
- Anyway, z.lua is a promising project. If you only need directory jumping, it may be the best choice.
## Features ## Features
- **10x** times faster than **fasd** and **autojump**, **3x** times faster than **z.sh**. - **10x** times faster than **fasd** and **autojump**, **3x** times faster than **z.sh**.
- Gain the ultimate speed with an optional [native module](https://github.com/skywind3000/czmod) written in C.
- Available for **posix shells**: bash, zsh, dash, sh, ash, ksh, busybox and etc. - Available for **posix shells**: bash, zsh, dash, sh, ash, ksh, busybox and etc.
- Available for Fish Shell, Power Shell and Windows cmd. - Available for Fish Shell, Power Shell and Windows cmd.
- [Enhanced matching algorithm](#enhanced-matching) 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. - 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. - Interactive selection enables you to choose where to go before cd.
- Intergrated with FZF (optional) for interactive selection and completion. - Integrated with FZF (optional) for interactive selection and completion.
- Quickly go back to a parent directory instead of typing "cd ../../..". - Quickly go back to a parent directory instead of typing "cd ../../..".
- Corresponding experience in different shells and operating systems. - Corresponding experience in different shells and operating systems.
- Compatible with Lua 5.1, 5.2 and 5.3+ - Compatible with Lua 5.1, 5.2 and 5.3+
@@ -196,7 +206,7 @@ If no match is found, it will fall back to default matching method.
30 /home/user/workspace/project2 30 /home/user/workspace/project2
40 /home/user/workspace/project3 40 /home/user/workspace/project3
If you use `"z wo"` in enhanced matching mode, only the `/home/user/work` will be matched, because according to rule No.2 it is the only path whose last segment matches `"wo"`. If you use `"z wo"` in enhanced matching mode, only the `/home/user/workspace` will be matched, because according to rule No.2 it is the only path whose last segment matches `"wo"`.
Since the last segment of a path is always easier to be recalled, it is sane to give it higher priority. You can also achieve this by typing `"z space$"` in both methods, but `"z wo"` is easier to type. Since the last segment of a path is always easier to be recalled, it is sane to give it higher priority. You can also achieve this by typing `"z space$"` in both methods, but `"z wo"` is easier to type.
@@ -448,18 +458,27 @@ sys 0m0.030s
As you see, z.lua is the fastest one and requires less resource. As you see, z.lua is the fastest one and requires less resource.
## Native Module
z.lua is fast enough for most case, the path tracking action will be triggered each time when you change your current directory.
## Reputation So I still recommend the pure lua script for portability and flexibility, but for someone who really care about `10ms` or `1ms` things, this module can help them to gain the ultimate speed.
- [czmod](https://github.com/skywind3000/czmod): native module to boost `z.lua`.
Average performance:
| Name | czmod | z.lua |
|-|-|-|-|-|
| **Update Time** | 1.6ms | 13.2ms |
| **Query Time** | 1.5ms | 9.8ms |
- "I like this in principal. Im pretty damn predictable at the command line and far too lazy to make shortcuts"
- "It feels far more intuitive and it's so incredibly convenient to be able to jump between folders I'm working in without having to traverse an entire tree. The shell used to feel so constraining for me, but tools like this are making me enjoy it so much more. "
- "I can finally have autojump-like functionality on my Raspberry Pi 1 without waiting 30 seconds every time I open a new shell. Thanks z.lua devs."
- "Anyway, z.lua is a promising project. If you only need directory jumping, it may be the best choice."
## History ## History
- 1.8.7 (2020-06-29): use lfs or luajit's cffi if possible.
- 1.8.4 (2020-02-10): fish shell: set `$_ZL_ECHO` to global scope.
- 1.8.3 (2020-02-09): new: `z -b -i` and `z -b -I` to jump backwards in interactive mode. - 1.8.3 (2020-02-09): new: `z -b -i` and `z -b -I` to jump backwards in interactive mode.
- 1.7.4 (2019-12-29): new: `$_ZL_HYPHEN` to treat hyphen as a normal character, see [here](https://github.com/skywind3000/z.lua/wiki/FAQ#how-to-input-a-hyphen---in-the-keyword-). - 1.7.4 (2019-12-29): new: `$_ZL_HYPHEN` to treat hyphen as a normal character, see [here](https://github.com/skywind3000/z.lua/wiki/FAQ#how-to-input-a-hyphen---in-the-keyword-).
- 1.7.3 (2019-09-07): use [lua-filesystem](http://keplerproject.github.io/luafilesystem/) package if possible when `$_ZL_USE_LFS` is `1`. - 1.7.3 (2019-09-07): use [lua-filesystem](http://keplerproject.github.io/luafilesystem/) package if possible when `$_ZL_USE_LFS` is `1`.

View File

@@ -17,7 +17,12 @@ end
set -x _ZL_DATA "$_ZL_DATA" set -x _ZL_DATA "$_ZL_DATA"
set _zlua_dir (dirname (status --current-filename)) set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME ~/.config
if functions -q fisher
set _zlua_dir $XDG_CONFIG_HOME/fisher/github.com/skywind3000/z.lua
else
set _zlua_dir (dirname (status --current-filename))
end
if test -e $_zlua_dir/z.lua if test -e $_zlua_dir/z.lua
if type -q lua if type -q lua
@@ -35,6 +40,8 @@ if test -e $_zlua_dir/z.lua
alias zz='z -i' # cd with interactive selection alias zz='z -i' # cd with interactive selection
alias zf='z -I' # use fzf to select in multiple matches alias zf='z -I' # use fzf to select in multiple matches
alias zb='z -b' # quickly cd to the parent directory alias zb='z -b' # quickly cd to the parent directory
alias zbi='z -b -i' # interactive jump backward
alias zbf='z -b -I' # interactive jump backward with fzf
set -U ZLUA_SCRIPT "$ZLUA_SCRIPT" 2> /dev/null set -U ZLUA_SCRIPT "$ZLUA_SCRIPT" 2> /dev/null
set -U ZLUA_LUAEXE "$ZLUA_LUAEXE" 2> /dev/null set -U ZLUA_LUAEXE "$ZLUA_LUAEXE" 2> /dev/null
end end

76
z.lua
View File

@@ -4,7 +4,7 @@
-- z.lua - a cd command that learns, by skywind 2018, 2019, 2020 -- z.lua - a cd command that learns, by skywind 2018, 2019, 2020
-- Licensed under MIT license. -- Licensed under MIT license.
-- --
-- Version 1.8.3, Last Modified: 2020/02/10 00:05 -- Version 1.8.7, Last Modified: 2020/06/29 18:04
-- --
-- * 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
@@ -319,14 +319,16 @@ if os.native.status then
uint32_t GetTickCount(void); uint32_t GetTickCount(void);
uint32_t GetFileAttributesA(const char *name); uint32_t GetFileAttributesA(const char *name);
uint32_t GetCurrentDirectoryA(uint32_t size, char *ptr); uint32_t GetCurrentDirectoryA(uint32_t size, char *ptr);
uint32_t GetShortPathNameA(const char *longname, char *shortname, uint32_t size);
uint32_t GetLongPathNameA(const char *shortname, char *longname, uint32_t size);
]] ]]
local kernel32 = ffi.load('kernel32.dll') local kernel32 = ffi.load('kernel32.dll')
local buffer = ffi.new('char[?]', 300) local buffer = ffi.new('char[?]', 4100)
local INVALID_FILE_ATTRIBUTES = 0xffffffff local INVALID_FILE_ATTRIBUTES = 0xffffffff
local FILE_ATTRIBUTE_DIRECTORY = 0x10 local FILE_ATTRIBUTE_DIRECTORY = 0x10
os.native.kernel32 = kernel32 os.native.kernel32 = kernel32
function os.native.GetFullPathName(name) function os.native.GetFullPathName(name)
local hr = kernel32.GetFullPathNameA(name, 290, buffer, nil) local hr = kernel32.GetFullPathNameA(name, 4096, buffer, nil)
return (hr > 0) and ffi.string(buffer, hr) or nil return (hr > 0) and ffi.string(buffer, hr) or nil
end end
function os.native.ReplaceFile(replaced, replacement) function os.native.ReplaceFile(replaced, replacement)
@@ -339,6 +341,21 @@ if os.native.status then
function os.native.GetFileAttributes(name) function os.native.GetFileAttributes(name)
return kernel32.GetFileAttributesA(name) return kernel32.GetFileAttributesA(name)
end end
function os.native.GetLongPathName(name)
local hr = kernel32.GetLongPathNameA(name, buffer, 4096)
return (hr ~= 0) and ffi.string(buffer, hr) or nil
end
function os.native.GetShortPathName(name)
local hr = kernel32.GetShortPathNameA(name, buffer, 4096)
return (hr ~= 0) and ffi.string(buffer, hr) or nil
end
function os.native.GetRealPathName(name)
local short = os.native.GetShortPathName(name)
if short then
return os.native.GetLongPathName(short)
end
return nil
end
function os.native.exists(name) function os.native.exists(name)
local attr = os.native.GetFileAttributes(name) local attr = os.native.GetFileAttributes(name)
return attr ~= INVALID_FILE_ATTRIBUTES return attr ~= INVALID_FILE_ATTRIBUTES
@@ -352,7 +369,7 @@ if os.native.status then
return (attr % (2 * isdir)) >= isdir return (attr % (2 * isdir)) >= isdir
end end
function os.native.getcwd() function os.native.getcwd()
local hr = kernel32.GetCurrentDirectoryA(299, buffer) local hr = kernel32.GetCurrentDirectoryA(4096, buffer)
if hr <= 0 then return nil end if hr <= 0 then return nil end
return ffi.string(buffer, hr) return ffi.string(buffer, hr)
end end
@@ -554,6 +571,9 @@ function os.path.exists(name)
if name == '/' then if name == '/' then
return true return true
end end
if os.native and os.native.exists then
return os.native.exists(name)
end
local ok, err, code = os.rename(name, name) local ok, err, code = os.rename(name, name)
if not ok then if not ok then
if code == 13 then if code == 13 then
@@ -1357,6 +1377,14 @@ function z_add(path)
end end
end end
if not skip then if not skip then
if windows then
if os.native and os.native.GetRealPathName then
local ts = os.native.GetRealPathName(path)
if ts then
path = ts
end
end
end
M = data_insert(M, path) M = data_insert(M, path)
count = count + 1 count = count + 1
end end
@@ -2201,16 +2229,16 @@ fi
local script_complete_zsh = [[ local script_complete_zsh = [[
_zlua_zsh_tab_completion() { _zlua_zsh_tab_completion() {
# tab completion # tab completion
local compl
read -l compl
(( $+compstate )) && compstate[insert]=menu # no expand (( $+compstate )) && compstate[insert]=menu # no expand
reply=(${(f)"$(_zlua --complete "$compl")"}) local -a tmp=(${(f)"$(_zlua --complete "${words/_zlua/z}")"})
_describe "directory" tmp -U
} }
compctl -U -K _zlua_zsh_tab_completion _zlua if [ "${+functions[compdef]}" -ne 0 ]; then
compdef _zlua_zsh_tab_completion _zlua 2> /dev/null
fi
]] ]]
----------------------------------------------------------------------- -----------------------------------------------------------------------
-- initialize bash/zsh -- initialize bash/zsh
---------------------------------------------------------------------- ----------------------------------------------------------------------
@@ -2231,6 +2259,10 @@ function z_shell_init(opts)
local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false)) local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false))
local once = os.environ("_ZL_ADD_ONCE", false) or opts.once ~= nil local once = os.environ("_ZL_ADD_ONCE", false) or opts.once ~= nil
if opts.clean ~= nil then
prompt_hook = false
end
if opts.bash ~= nil then if opts.bash ~= nil then
if prompt_hook then if prompt_hook then
if once then if once then
@@ -2400,18 +2432,24 @@ function z_fish_init(opts)
print('set -x ZLUA_SCRIPT "' .. os.scriptname() .. '"') print('set -x ZLUA_SCRIPT "' .. os.scriptname() .. '"')
print('set -x ZLUA_LUAEXE "' .. os.interpreter() .. '"') print('set -x ZLUA_LUAEXE "' .. os.interpreter() .. '"')
local once = (os.getenv("_ZL_ADD_ONCE") ~= nil) or opts.once ~= nil local once = (os.getenv("_ZL_ADD_ONCE") ~= nil) or opts.once ~= nil
local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false))
if opts.clean ~= nil then
prompt_hook = false
end
print(script_zlua_fish) print(script_zlua_fish)
if once then if prompt_hook then
print(script_init_fish_once) if once then
else print(script_init_fish_once)
print(script_init_fish) else
print(script_init_fish)
end
end end
print(script_complete_fish) print(script_complete_fish)
if opts.enhanced ~= nil then if opts.enhanced ~= nil then
print('set -x _ZL_MATCH_MODE 1') print('set -x _ZL_MATCH_MODE 1')
end end
if opts.echo then if opts.echo then
print('set _ZL_ECHO 1') print('set -g _ZL_ECHO 1')
end end
if opts.nc then if opts.nc then
print('set -x _ZL_NO_CHECK 1') print('set -x _ZL_NO_CHECK 1')
@@ -2594,6 +2632,10 @@ if (!$env:_ZL_NO_PROMPT_COMMAND -and (!$global:_zlua_inited)) {
-- initialize cmd/powershell -- initialize cmd/powershell
----------------------------------------------------------------------- -----------------------------------------------------------------------
function z_windows_init(opts) function z_windows_init(opts)
local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false))
if opts.clean ~= nil then
prompt_hook = false
end
if opts.powershell ~= nil then if opts.powershell ~= nil then
print('$script:ZLUA_LUAEXE = "' .. os.interpreter() .. '"') print('$script:ZLUA_LUAEXE = "' .. os.interpreter() .. '"')
print('$script:ZLUA_SCRIPT = "' .. os.scriptname() .. '"') print('$script:ZLUA_SCRIPT = "' .. os.scriptname() .. '"')
@@ -2610,7 +2652,9 @@ function z_windows_init(opts)
if opts.nc ~= nil then if opts.nc ~= nil then
print('$env:_ZL_NO_CHECK = 1') print('$env:_ZL_NO_CHECK = 1')
end end
print(script_init_powershell) if prompt_hook then
print(script_init_powershell)
end
else else
print('@echo off') print('@echo off')
print('setlocal EnableDelayedExpansion') print('setlocal EnableDelayedExpansion')
@@ -2648,6 +2692,7 @@ end
----------------------------------------------------------------------- -----------------------------------------------------------------------
os.lfs = {} os.lfs = {}
os.lfs.enable = os.getenv('_ZL_USE_LFS') os.lfs.enable = os.getenv('_ZL_USE_LFS')
os.lfs.enable = '1'
if os.lfs.enable ~= nil then if os.lfs.enable ~= nil then
local m = string.lower(os.lfs.enable) local m = string.lower(os.lfs.enable)
if (m == '1' or m == 'yes' or m == 'true' or m == 't') then if (m == '1' or m == 'yes' or m == 'true' or m == 't') then
@@ -2683,4 +2728,3 @@ if not pcall(debug.getlocal, 4, 1) then
end end