1
0
mirror of https://github.com/skywind3000/z.lua synced 2026-03-21 23:29:48 +00:00

13 Commits

Author SHA1 Message Date
Linwei
c334bc1ae5 Merge pull request #210 from brglng/feature/nushell
feat: add initial support for nushell (wip)
2024-10-10 15:17:31 +08:00
Zhaosheng Pan
0f6318ce4c fix(nushell): chdir failure 2024-10-09 14:59:05 +08:00
Zhaosheng Pan
3b55089ad8 improve README 2024-10-09 12:44:15 +08:00
Zhaosheng Pan
c58d31ec1d feat: add initial support for nushell 2024-10-08 18:11:32 +08:00
skywind3000
7c890c3645 Improve handling of the '--help' option in _zlua() function to improve
zsh compatibility.
2024-04-30 17:16:36 +08:00
skywind3000
7af012cc35 fixed: interpret _ZL_ZSH_NO_FZF in a wrong way. 2024-04-20 20:38:20 +08:00
skywind3000
019b2af475 add a return statement for nil case in os.interpreter() function. 2024-04-07 11:26:35 +08:00
Linwei
ef9a49d73d Merge pull request #158 from ibayramli/fig
Add Fig as an installation method to the README
2024-03-20 22:19:26 +08:00
skywind3000
7cd399b30d bloat version 2024-03-20 22:17:58 +08:00
Linwei
9112f0e9cc Merge pull request #189 from Arkaeriit/fix_pipes_in_filenames
Support '|' in path
2024-03-20 22:13:57 +08:00
Maxime Bouillot
cb4c9d3c8f Revert back to the old separator and improve db reading
A smarter reading function lets us support '|' in
filenames whilst still using it as the database
separator.
2024-03-20 14:38:04 +01:00
Maxime Bouillot
924f61a48b Change separator in data file
Using '|' as the data separator caused crashes if
a path with a '|' char in it was in the data file.
Changing that separator to a null byte ensures
that this kind of issue can never happen again as
the null byte can't be in a path.
2024-03-06 14:52:16 +01:00
Ilkin Bayramli
4c05e0d911 Add Fig as an installation method to the README 2022-06-15 10:12:16 -07:00
4 changed files with 183 additions and 7 deletions

View File

@@ -84,6 +84,17 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
但是第二种方法需要记得在 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 的配置文件 `profile.ps1` 中放入下面语句:

View File

@@ -58,6 +58,15 @@ z -b foo bar # replace foo with bar in cwd and cd there
## Install
- Fig (works with all shells)
[Fig](https://fig.io) adds apps, shortcuts, and autocomplete to your existing terminal.
Install `z.lua` in just one click.
<a href="https://fig.io/plugins/other/z.lua" target="_blank"><img src="https://fig.io/badges/install-with-fig.svg" /></a>
- Bash:
put something like this in your `.bashrc`:
@@ -118,6 +127,17 @@ z -b foo bar # replace foo with bar in cwd and cd there
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:
> ⚠️ **WARNING**: users of [Starship Prompt](https://starship.rs/) should add the following command *after* `starship init`.

155
z.lua
View File

@@ -4,7 +4,7 @@
-- z.lua - a cd command that learns, by skywind 2018-2022
-- Licensed under MIT license.
--
-- Version 1.8.17, Last Modified: 2022/09/29 15:47
-- Version 1.8.18, Last Modified: 2024/04/30 17:11
--
-- * 10x faster than fasd and autojump, 3x faster than z.sh
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox
@@ -128,6 +128,7 @@ Z_MATCHMODE = 0
Z_MATCHNAME = false
Z_SKIPPWD = false
Z_HYPHEN = "auto"
Z_DATA_SEPARATOR = "|"
os.LOG_NAME = os.getenv('_ZL_LOG_NAME')
@@ -882,6 +883,7 @@ function os.interpreter()
local lua = os.argv[-1]
if lua == nil then
io.stderr:write("cannot get executable name, recompiled your lua\n")
return nil
end
if os.path.single(lua) then
local path = os.path.which(lua)
@@ -1061,6 +1063,26 @@ function path_case_insensitive()
end
-----------------------------------------------------------------------
-- Read a line of the database and return a list of the 3 fields in it
-----------------------------------------------------------------------
function read_data_line(line)
local part = string.split(line, Z_DATA_SEPARATOR)
if #part <= 3 then
return part
end
-- If the part is made of more than 3 elements, it's probably because the
-- path element contains '|' that have been split. Thus, we want to
-- reconstruct it and keep the 2 last elements of part intact as the end
-- of the returned part.
local path = part[1]
for i=2,#part-2 do
path = path .. Z_DATA_SEPARATOR .. part[i]
end
return {path, part[#part-1], part[#part]}
end
-----------------------------------------------------------------------
-- load and split data
-----------------------------------------------------------------------
@@ -1073,7 +1095,7 @@ function data_load(filename)
return {}
end
for line in fp:lines() do
local part = string.split(line, '|')
local part = read_data_line(line)
local item = {}
if part and part[1] and part[2] and part[3] then
local key = insensitive and part[1]:lower() or part[1]
@@ -1135,7 +1157,7 @@ function data_save(filename, M)
end
for i = 1, #M do
local item = M[i]
local text = item.name .. '|' .. item.rank .. '|' .. item.time
local text = item.name .. Z_DATA_SEPARATOR .. item.rank .. Z_DATA_SEPARATOR .. item.time
fp:write(text .. '\n')
end
fp:close()
@@ -1979,7 +2001,9 @@ function main(argv)
for _, key in ipairs(args) do
opts[key] = 1
end
if windows then
if opts.nushell then
z_nushell_init(opts)
elseif windows then
z_windows_init(opts)
elseif opts.fish then
z_fish_init(opts)
@@ -2166,7 +2190,8 @@ _zlua() {
-s) local arg_strip="-s" ;;
-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" ;;
*) break ;;
esac
@@ -2776,6 +2801,126 @@ function z_windows_init(opts)
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 ...$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
-----------------------------------------------------------------------

View File

@@ -22,9 +22,9 @@ fi
export _ZL_FZF_FLAG=${_ZL_FZF_FLAG:-"-e"}
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)"
else
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"
fi
if [[ -z "$_ZL_NO_ALIASES" ]]; then