Skip to content

Commit c334bc1

Browse files
authored
Merge pull request #210 from brglng/feature/nushell
feat: add initial support for nushell (wip)
2 parents 7c890c3 + 0f6318c commit c334bc1

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

README.cn.md

+11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
8484

8585
但是第二种方法需要记得在 z.lua 位置改变或者 lua 版本升级后需要重新生成。
8686

87+
- Nushell:
88+
89+
`env.nu` 中加入如下代码:
90+
91+
lua /path/to/z.lua --init nushell | save -f ~/.cache/zlua.nu
92+
93+
然后在 `config.nu` 中加入如下代码:
94+
95+
source ~/.cache/zlua.nu
96+
alias z = _zlua
97+
8798
- Power Shell:
8899

89100
在你 Power Shell 的配置文件 `profile.ps1` 中放入下面语句:

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ z -b foo bar # replace foo with bar in cwd and cd there
127127

128128
into the same file.
129129

130+
- Nushell
131+
132+
Put something like this in your `env.nu`:
133+
134+
lua /path/to/z.lua --init nushell | save -f ~/.cache/zlua.nu
135+
136+
Then put something like this in your `config.nu`:
137+
138+
source ~/.cache/zlua.nu
139+
alias z = _zlua
140+
130141
- Power Shell:
131142

132143
> ⚠️ **WARNING**: users of [Starship Prompt](https://starship.rs/) should add the following command *after* `starship init`.

z.lua

+123-1
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,9 @@ function main(argv)
20012001
for _, key in ipairs(args) do
20022002
opts[key] = 1
20032003
end
2004-
if windows then
2004+
if opts.nushell then
2005+
z_nushell_init(opts)
2006+
elseif windows then
20052007
z_windows_init(opts)
20062008
elseif opts.fish then
20072009
z_fish_init(opts)
@@ -2799,6 +2801,126 @@ function z_windows_init(opts)
27992801
end
28002802

28012803

2804+
-----------------------------------------------------------------------
2805+
-- nushell
2806+
-----------------------------------------------------------------------
2807+
local script_zlua_nushell = [[
2808+
def _zlua --env --wrapped [...args: string] {
2809+
if ($args | length) != 0 and $args.0 == "--add" {
2810+
with-env { _ZL_RANDOM: (random int) } { ^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --add ...($args | skip 1) }
2811+
} else if ($args | length) != 0 and $args.0 == "--complete" {
2812+
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --complete ...($args | skip 1)
2813+
} else {
2814+
mut arg_mode = ''
2815+
mut arg_type = ''
2816+
mut arg_subdir = ''
2817+
mut arg_inter = ''
2818+
mut arg_strip = ''
2819+
mut count = 0
2820+
for arg in $args {
2821+
match $arg {
2822+
'-l' => { $arg_mode = '-l' },
2823+
'-e' => { $arg_mode = '-e' },
2824+
'-x' => { $arg_mode = '-x' },
2825+
'-t' => { $arg_type = '-t' },
2826+
'-r' => { $arg_type = '-r' },
2827+
'-c' => { $arg_subdir = '-c' },
2828+
'-s' => { $arg_strip = '-s' },
2829+
'-i' => { $arg_inter = '-i' },
2830+
'-I' => { $arg_inter = '-I' },
2831+
'-h' => { $arg_mode = '-h' },
2832+
'--help' => { $arg_mode = '-h' },
2833+
'--purge' => { $arg_mode = '--purge' },
2834+
_ => break
2835+
}
2836+
$count += 1
2837+
}
2838+
let args = $args | skip $count
2839+
if $arg_mode == '-h' or $arg_mode == '--purge' {
2840+
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT $arg_mode
2841+
} else if $arg_mode == '-l' or ($args | length) == 0 {
2842+
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT -l $arg_subdir $arg_type $arg_strip ...$args
2843+
} else if $arg_mode != '' {
2844+
^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT $arg_mode $arg_subdir $arg_type $arg_inter ...$args
2845+
} else {
2846+
let zdest = (^$env.ZLUA_LUAEXE $env.ZLUA_SCRIPT --cd $arg_type $arg_subdir $arg_inter ...$args)
2847+
if $zdest != '' and ($zdest | path exists) {
2848+
cd $zdest
2849+
if _ZL_ECHO in $env and $env._ZL_ECHO != '' {
2850+
pwd
2851+
}
2852+
}
2853+
}
2854+
}
2855+
}
2856+
]]
2857+
2858+
local script_init_nushell = [[
2859+
$env.config = ($env | default {} config).config
2860+
$env.config = ($env.config | default {} hooks)
2861+
$env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change))
2862+
$env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD))
2863+
$env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append {|_, dir| _zlua --add $dir }))
2864+
]]
2865+
2866+
local script_complete_nushell = [[
2867+
let zlua_completer = {|spans| $spans | skip 1 | _zlua --complete ...$in | lines | where {|x| $x != $env.PWD}}
2868+
2869+
$env.config = ($env.config | default {} completions)
2870+
$env.config = ($env.config | update completions ($env.config.completions | default {} external))
2871+
$env.config = ($env.config | update completions.external ($env.config.completions.external | default true enable))
2872+
if completer in $env.config.completions.external {
2873+
let orig_completer = $env.config.completions.external.completer
2874+
$env.config = ($env.config | update completions.external.completer {
2875+
{|spans|
2876+
match $spans.0 {
2877+
z => $zlua_completer,
2878+
_zlua => $zlua_completer,
2879+
_ => $orig_completer
2880+
} | do $in $spans
2881+
}
2882+
})
2883+
} else {
2884+
$env.config = ($env.config | update completions.external.completer {
2885+
{|spans|
2886+
match $spans.0 {
2887+
z => $zlua_completer,
2888+
_zlua => $zlua_completer,
2889+
} | do $in $spans
2890+
}
2891+
})
2892+
}
2893+
]]
2894+
2895+
-----------------------------------------------------------------------
2896+
-- initialize nushell
2897+
-----------------------------------------------------------------------
2898+
function z_nushell_init(opts)
2899+
print('$env.ZLUA_LUAEXE = \'' .. os.interpreter() .. '\'')
2900+
print('$env.ZLUA_SCRIPT = \'' .. os.scriptname() .. '\'')
2901+
local prompt_hook = (not os.environ("_ZL_NO_PROMPT_COMMAND", false))
2902+
if opts.clean ~= nil then
2903+
prompt_hook = false
2904+
end
2905+
print(script_zlua_nushell)
2906+
if prompt_hook then
2907+
print(script_init_nushell)
2908+
end
2909+
print(script_complete_nushell)
2910+
if opts.enhanced ~= nil then
2911+
print('$env._ZL_MATCH_MODE = 1')
2912+
end
2913+
if opts.once ~= nil then
2914+
print('$env._ZL_ADD_ONCE = 1')
2915+
end
2916+
if opts.echo ~= nil then
2917+
print('$env._ZL_ECHO = 1')
2918+
end
2919+
if opts.nc ~= nil then
2920+
print('$env._ZL_NO_CHECK = 1')
2921+
end
2922+
end
2923+
28022924
-----------------------------------------------------------------------
28032925
-- help
28042926
-----------------------------------------------------------------------

0 commit comments

Comments
 (0)