mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-22 15:49:47 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3875d774cb | ||
|
|
dff590adc1 | ||
|
|
20c0494d44 | ||
|
|
67405d57e8 | ||
|
|
6e67a32520 | ||
|
|
c334bc1ae5 | ||
|
|
0f6318ce4c | ||
|
|
3b55089ad8 | ||
|
|
c58d31ec1d | ||
|
|
7c890c3645 | ||
|
|
7af012cc35 | ||
|
|
019b2af475 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -39,3 +39,4 @@ luac.out
|
|||||||
*.x86_64
|
*.x86_64
|
||||||
*.hex
|
*.hex
|
||||||
|
|
||||||
|
/.vscode/*
|
||||||
|
|||||||
19
README.cn.md
19
README.cn.md
@@ -84,6 +84,17 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
|
|||||||
|
|
||||||
但是第二种方法需要记得在 z.lua 位置改变或者 lua 版本升级后需要重新生成。
|
但是第二种方法需要记得在 z.lua 位置改变或者 lua 版本升级后需要重新生成。
|
||||||
|
|
||||||
|
- Nushell:
|
||||||
|
|
||||||
|
在 `env.nu` 中加入如下代码:
|
||||||
|
|
||||||
|
lua /path/to/z.lua --init nushell | save -f ~/.cache/zlua.nu
|
||||||
|
|
||||||
|
然后在 `config.nu` 中加入如下代码:
|
||||||
|
|
||||||
|
source ~/.cache/zlua.nu
|
||||||
|
alias z = _zlua
|
||||||
|
|
||||||
- Power Shell:
|
- Power Shell:
|
||||||
|
|
||||||
在你 Power Shell 的配置文件 `profile.ps1` 中放入下面语句:
|
在你 Power Shell 的配置文件 `profile.ps1` 中放入下面语句:
|
||||||
@@ -444,15 +455,13 @@ sys 0m0.030s
|
|||||||
|
|
||||||
描述力强,可以更好的实现核心功能,同时速度更快,纯 shell 开发的话,太多语句是通过子进程 shell 的模式运行,所以性能很差,而 Python 开发的话启动速度又太慢,我在 Cygwin/msys 下用 z.sh 都觉得很卡,autojump/fasd 卡到不能用。
|
描述力强,可以更好的实现核心功能,同时速度更快,纯 shell 开发的话,太多语句是通过子进程 shell 的模式运行,所以性能很差,而 Python 开发的话启动速度又太慢,我在 Cygwin/msys 下用 z.sh 都觉得很卡,autojump/fasd 卡到不能用。
|
||||||
|
|
||||||
最关键的一点,Lua 速度很快 200 KB 的可执行程序,启动速度是 python 的 3倍,perl 的 2 倍,很多命令行工具 go/rust 写成,动不动就 2MB / 3MB,他们都还没有完成加载,lua 脚本可能都运行完了。
|
最关键的一点,Lua 速度很快 200 KB 的可执行程序,启动速度是 python 的 3 倍,perl 的 2 倍,很多命令行工具 go/rust 写成,动不动就 2MB / 3MB,他们都还没有完成加载,lua 脚本可能都运行完了。
|
||||||
|
|
||||||
|
|
||||||
## Credit
|
## Credit
|
||||||
|
|
||||||
Releated projects:
|
我的推特:https://x.com/skywind3000
|
||||||
|
个人博客: https://skywind.me/blog
|
||||||
- [rupa/z](https://github.com/rupa/z): origin z.sh implementation
|
|
||||||
- [JannesMeyer/z.ps](https://github.com/JannesMeyer/z.ps): z for powershell
|
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -127,6 +127,17 @@ z -b foo bar # replace foo with bar in cwd and cd there
|
|||||||
|
|
||||||
into the same file.
|
into the same file.
|
||||||
|
|
||||||
|
- Nushell
|
||||||
|
|
||||||
|
Put something like this in your `env.nu`:
|
||||||
|
|
||||||
|
lua /path/to/z.lua --init nushell | save -f ~/.cache/zlua.nu
|
||||||
|
|
||||||
|
Then put something like this in your `config.nu`:
|
||||||
|
|
||||||
|
source ~/.cache/zlua.nu
|
||||||
|
alias z = _zlua
|
||||||
|
|
||||||
- Power Shell:
|
- Power Shell:
|
||||||
|
|
||||||
> ⚠️ **WARNING**: users of [Starship Prompt](https://starship.rs/) should add the following command *after* `starship init`.
|
> ⚠️ **WARNING**: users of [Starship Prompt](https://starship.rs/) should add the following command *after* `starship init`.
|
||||||
@@ -596,6 +607,7 @@ This project needs help for the tasks below:
|
|||||||
- Thanks to [@manhong2112](https://github.com/manhong2112) for Power Shell porting.
|
- Thanks to [@manhong2112](https://github.com/manhong2112) for Power Shell porting.
|
||||||
- Thanks to [@BarbUk](https://github.com/BarbUk) for fzf completion in Bash.
|
- Thanks to [@BarbUk](https://github.com/BarbUk) for fzf completion in Bash.
|
||||||
- Thanks to [@barlik](https://github.com/barlik) for many improvements.
|
- Thanks to [@barlik](https://github.com/barlik) for many improvements.
|
||||||
|
- Thanks to [@brglng](https://github.com/brglng) for nushell porting.
|
||||||
|
|
||||||
And many others.
|
And many others.
|
||||||
|
|
||||||
|
|||||||
143
z.lua
143
z.lua
@@ -4,7 +4,7 @@
|
|||||||
-- z.lua - a cd command that learns, by skywind 2018-2022
|
-- z.lua - a cd command that learns, by skywind 2018-2022
|
||||||
-- Licensed under MIT license.
|
-- Licensed under MIT license.
|
||||||
--
|
--
|
||||||
-- Version 1.8.18, Last Modified: 2024/03/20 22:17
|
-- Version 1.8.18, Last Modified: 2024/04/30 17:11
|
||||||
--
|
--
|
||||||
-- * 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
|
||||||
@@ -883,6 +883,7 @@ function os.interpreter()
|
|||||||
local lua = os.argv[-1]
|
local lua = os.argv[-1]
|
||||||
if lua == nil then
|
if lua == nil then
|
||||||
io.stderr:write("cannot get executable name, recompiled your lua\n")
|
io.stderr:write("cannot get executable name, recompiled your lua\n")
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
if os.path.single(lua) then
|
if os.path.single(lua) then
|
||||||
local path = os.path.which(lua)
|
local path = os.path.which(lua)
|
||||||
@@ -2000,12 +2001,14 @@ function main(argv)
|
|||||||
for _, key in ipairs(args) do
|
for _, key in ipairs(args) do
|
||||||
opts[key] = 1
|
opts[key] = 1
|
||||||
end
|
end
|
||||||
if windows then
|
if opts.nushell then
|
||||||
|
z_nushell_init(opts)
|
||||||
|
elseif windows then
|
||||||
z_windows_init(opts)
|
z_windows_init(opts)
|
||||||
elseif opts.fish then
|
elseif opts.fish then
|
||||||
z_fish_init(opts)
|
z_fish_init(opts)
|
||||||
elseif opts.powershell then
|
elseif opts.powershell then
|
||||||
z_windows_init(opts)
|
z_windows_init(opts)
|
||||||
else
|
else
|
||||||
z_shell_init(opts)
|
z_shell_init(opts)
|
||||||
end
|
end
|
||||||
@@ -2017,9 +2020,14 @@ function main(argv)
|
|||||||
z_print(M, true, false)
|
z_print(M, true, false)
|
||||||
end
|
end
|
||||||
elseif options['--complete'] then
|
elseif options['--complete'] then
|
||||||
local line = args[1] and args[1] or ''
|
local M = {}
|
||||||
local head = line:sub(Z_CMD:len()+1):gsub('^%s+', '')
|
if options['-m1'] then
|
||||||
local M = z_match({head}, Z_METHOD, Z_SUBDIR)
|
M = z_match(args and args or {}, Z_METHOD, Z_SUBDIR)
|
||||||
|
else
|
||||||
|
local line = args[1] and args[1] or ''
|
||||||
|
local head = line:sub(Z_CMD:len()+1):gsub('^%s+', '')
|
||||||
|
M = z_match({head}, Z_METHOD, Z_SUBDIR)
|
||||||
|
end
|
||||||
for _, item in pairs(M) do
|
for _, item in pairs(M) do
|
||||||
print(item.name)
|
print(item.name)
|
||||||
end
|
end
|
||||||
@@ -2187,7 +2195,8 @@ _zlua() {
|
|||||||
-s) local arg_strip="-s" ;;
|
-s) local arg_strip="-s" ;;
|
||||||
-i) local arg_inter="-i" ;;
|
-i) local arg_inter="-i" ;;
|
||||||
-I) local arg_inter="-I" ;;
|
-I) local arg_inter="-I" ;;
|
||||||
-h|--help) local arg_mode="-h" ;;
|
-h) local arg_mode="-h" ;;
|
||||||
|
--help) local arg_mode="-h" ;;
|
||||||
--purge) local arg_mode="--purge" ;;
|
--purge) local arg_mode="--purge" ;;
|
||||||
*) break ;;
|
*) break ;;
|
||||||
esac
|
esac
|
||||||
@@ -2797,6 +2806,126 @@ function z_windows_init(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------
|
||||||
|
-- nushell
|
||||||
|
-----------------------------------------------------------------------
|
||||||
|
local script_zlua_nushell = [[
|
||||||
|
def _zlua --env --wrapped [...args: string] {
|
||||||
|
if ($args | length) != 0 and $args.0 == "--add" {
|
||||||
|
with-env { _ZL_RANDOM: (random int) } { ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --add ...($args | skip 1) }
|
||||||
|
} else if ($args | length) != 0 and $args.0 == "--complete" {
|
||||||
|
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --complete ...($args | skip 1)
|
||||||
|
} else {
|
||||||
|
mut arg_mode = ''
|
||||||
|
mut arg_type = ''
|
||||||
|
mut arg_subdir = ''
|
||||||
|
mut arg_inter = ''
|
||||||
|
mut arg_strip = ''
|
||||||
|
mut count = 0
|
||||||
|
for arg in $args {
|
||||||
|
match $arg {
|
||||||
|
'-l' => { $arg_mode = '-l' },
|
||||||
|
'-e' => { $arg_mode = '-e' },
|
||||||
|
'-x' => { $arg_mode = '-x' },
|
||||||
|
'-t' => { $arg_type = '-t' },
|
||||||
|
'-r' => { $arg_type = '-r' },
|
||||||
|
'-c' => { $arg_subdir = '-c' },
|
||||||
|
'-s' => { $arg_strip = '-s' },
|
||||||
|
'-i' => { $arg_inter = '-i' },
|
||||||
|
'-I' => { $arg_inter = '-I' },
|
||||||
|
'-h' => { $arg_mode = '-h' },
|
||||||
|
'--help' => { $arg_mode = '-h' },
|
||||||
|
'--purge' => { $arg_mode = '--purge' },
|
||||||
|
_ => break
|
||||||
|
}
|
||||||
|
$count += 1
|
||||||
|
}
|
||||||
|
let args = $args | skip $count
|
||||||
|
if $arg_mode == '-h' or $arg_mode == '--purge' {
|
||||||
|
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT $arg_mode
|
||||||
|
} else if $arg_mode == '-l' or ($args | length) == 0 {
|
||||||
|
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT -l $arg_subdir $arg_type $arg_strip ...$args
|
||||||
|
} else if $arg_mode != '' {
|
||||||
|
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT $arg_mode $arg_subdir $arg_type $arg_inter ...$args
|
||||||
|
} else {
|
||||||
|
let zdest = (^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --cd $arg_type $arg_subdir $arg_inter ...$args)
|
||||||
|
if $zdest != '' and ($zdest | path exists) {
|
||||||
|
cd $zdest
|
||||||
|
if _ZL_ECHO in $env and $env._ZL_ECHO != '' {
|
||||||
|
pwd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
|
||||||
|
local script_init_nushell = [[
|
||||||
|
$env.config = ($env | default {} config).config
|
||||||
|
$env.config = ($env.config | default {} hooks)
|
||||||
|
$env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change))
|
||||||
|
$env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD))
|
||||||
|
$env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append {|_, dir| _zlua --add $dir }))
|
||||||
|
]]
|
||||||
|
|
||||||
|
local script_complete_nushell = [[
|
||||||
|
let zlua_completer = {|spans| $spans | skip 1 | _zlua --complete -m1 ...$in | lines | where {|x| $x != $env.PWD}}
|
||||||
|
|
||||||
|
$env.config = ($env.config | default {} completions)
|
||||||
|
$env.config = ($env.config | update completions ($env.config.completions | default {} external))
|
||||||
|
$env.config = ($env.config | update completions.external ($env.config.completions.external | default true enable))
|
||||||
|
if completer in $env.config.completions.external {
|
||||||
|
let orig_completer = $env.config.completions.external.completer
|
||||||
|
$env.config = ($env.config | update completions.external.completer {
|
||||||
|
{|spans|
|
||||||
|
match $spans.0 {
|
||||||
|
z => $zlua_completer,
|
||||||
|
_zlua => $zlua_completer,
|
||||||
|
_ => $orig_completer
|
||||||
|
} | do $in $spans
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
$env.config = ($env.config | update completions.external.completer {
|
||||||
|
{|spans|
|
||||||
|
match $spans.0 {
|
||||||
|
z => $zlua_completer,
|
||||||
|
_zlua => $zlua_completer,
|
||||||
|
} | do $in $spans
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------
|
||||||
|
-- initialize nushell
|
||||||
|
-----------------------------------------------------------------------
|
||||||
|
function z_nushell_init(opts)
|
||||||
|
print('$env.ZLUA_LUAEXE = \'' .. os.interpreter() .. '\'')
|
||||||
|
print('$env.ZLUA_SCRIPT = \'' .. os.scriptname() .. '\'')
|
||||||
|
local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false))
|
||||||
|
if opts.clean ~= nil then
|
||||||
|
prompt_hook = false
|
||||||
|
end
|
||||||
|
print(script_zlua_nushell)
|
||||||
|
if prompt_hook then
|
||||||
|
print(script_init_nushell)
|
||||||
|
end
|
||||||
|
print(script_complete_nushell)
|
||||||
|
if opts.enhanced ~= nil then
|
||||||
|
print('$env._ZL_MATCH_MODE = 1')
|
||||||
|
end
|
||||||
|
if opts.once ~= nil then
|
||||||
|
print('$env._ZL_ADD_ONCE = 1')
|
||||||
|
end
|
||||||
|
if opts.echo ~= nil then
|
||||||
|
print('$env._ZL_ECHO = 1')
|
||||||
|
end
|
||||||
|
if opts.nc ~= nil then
|
||||||
|
print('$env._ZL_NO_CHECK = 1')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
-- help
|
-- help
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ fi
|
|||||||
export _ZL_FZF_FLAG=${_ZL_FZF_FLAG:-"-e"}
|
export _ZL_FZF_FLAG=${_ZL_FZF_FLAG:-"-e"}
|
||||||
|
|
||||||
if [[ -z "$_ZL_ZSH_NO_FZF" ]]; then
|
if [[ -z "$_ZL_ZSH_NO_FZF" ]]; then
|
||||||
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"
|
|
||||||
else
|
|
||||||
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced fzf)"
|
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced fzf)"
|
||||||
|
else
|
||||||
|
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -z "$_ZL_NO_ALIASES" ]]; then
|
if [[ -z "$_ZL_NO_ALIASES" ]]; then
|
||||||
|
|||||||
Reference in New Issue
Block a user