Skip to content

Commit f1080b3

Browse files
committed
Wrap nushell integration script in a module
This prevents the completion command "nu-complete __zoxide_z" from polluting the command namespace.
1 parent e7ab521 commit f1080b3

File tree

1 file changed

+102
-97
lines changed

1 file changed

+102
-97
lines changed

templates/nushell.txt

Lines changed: 102 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,122 +3,127 @@
33

44
# Code generated by zoxide. DO NOT EDIT.
55

6-
{{ section }}
7-
# Hook configuration for zoxide.
8-
#
9-
10-
{% if hook == InitHook::None -%}
11-
{{ not_configured }}
12-
13-
{%- else -%}
14-
# Initialize hook to add new entries to the database.
15-
export-env {
16-
{%- if hook == InitHook::Prompt %}
17-
$env.config = (
18-
$env.config?
19-
| default {}
20-
| upsert hooks { default {} }
21-
| upsert hooks.pre_prompt { default [] }
22-
)
23-
let __zoxide_hooked = (
24-
$env.config.hooks.pre_prompt | any { try { get __zoxide_hook } catch { false } }
25-
)
26-
if not $__zoxide_hooked {
27-
$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append {
28-
__zoxide_hook: true,
29-
code: {|| ^zoxide add -- $env.PWD}
30-
})
31-
}
32-
{%- else if hook == InitHook::Pwd %}
33-
$env.config = (
34-
$env.config?
35-
| default {}
36-
| upsert hooks { default {} }
37-
| upsert hooks.env_change { default {} }
38-
| upsert hooks.env_change.PWD { default [] }
39-
)
40-
let __zoxide_hooked = (
41-
$env.config.hooks.env_change.PWD | any { try { get __zoxide_hook } catch { false } }
42-
)
43-
if not $__zoxide_hooked {
44-
$env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append {
45-
__zoxide_hook: true,
46-
code: {|_, dir| ^zoxide add -- $dir}
47-
})
6+
module zoxide_integration {
7+
8+
{{ section }}
9+
# Hook configuration for zoxide.
10+
#
11+
12+
{% if hook == InitHook::None -%}
13+
{{ not_configured }}
14+
15+
{%- else -%}
16+
# Initialize hook to add new entries to the database.
17+
export-env {
18+
{%- if hook == InitHook::Prompt %}
19+
$env.config = (
20+
$env.config?
21+
| default {}
22+
| upsert hooks { default {} }
23+
| upsert hooks.pre_prompt { default [] }
24+
)
25+
let __zoxide_hooked = (
26+
$env.config.hooks.pre_prompt | any { try { get __zoxide_hook } catch { false } }
27+
)
28+
if not $__zoxide_hooked {
29+
$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append {
30+
__zoxide_hook: true,
31+
code: {|| ^zoxide add -- $env.PWD}
32+
})
33+
}
34+
{%- else if hook == InitHook::Pwd %}
35+
$env.config = (
36+
$env.config?
37+
| default {}
38+
| upsert hooks { default {} }
39+
| upsert hooks.env_change { default {} }
40+
| upsert hooks.env_change.PWD { default [] }
41+
)
42+
let __zoxide_hooked = (
43+
$env.config.hooks.env_change.PWD | any { try { get __zoxide_hook } catch { false } }
44+
)
45+
if not $__zoxide_hooked {
46+
$env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append {
47+
__zoxide_hook: true,
48+
code: {|_, dir| ^zoxide add -- $dir}
49+
})
50+
}
51+
{%- endif %}
4852
}
49-
{%- endif %}
50-
}
5153

52-
{%- endif %}
54+
{%- endif %}
5355

54-
{{ section }}
55-
# Completion for __zoxide_z
56-
#
56+
{{ section }}
57+
# Completion for __zoxide_z
58+
#
5759

58-
def "nu-complete __zoxide_z" [context: string] {
59-
let ast = ast --flatten $context | skip 1
60+
def "nu-complete __zoxide_z" [context: string] {
61+
let ast = ast --flatten $context | skip 1
6062

61-
# If the user has typed a space after the first argument, use the custom
62-
# completer. If not, use the built-in directory completion.
63-
if ($ast | is-empty) { return null }
64-
if $ast.0.span.end >= ($context | str length) { return null }
63+
# If the user has typed a space after the first argument, use the custom
64+
# completer. If not, use the built-in directory completion.
65+
if ($ast | is-empty) { return null }
66+
if $ast.0.span.end >= ($context | str length) { return null }
6567

66-
let completions = ^zoxide query --exclude $env.PWD --list -- ...$ast.content
67-
| lines | first 10 | wrap value
68-
| insert span { start: ($ast | first).span.start, end: ($ast | last).span.end }
68+
let completions = ^zoxide query --exclude $env.PWD --list -- ...$ast.content
69+
| lines | first 10 | wrap value
70+
| insert span { start: ($ast | first).span.start, end: ($ast | last).span.end }
6971

70-
{
71-
options: {
72-
sort: false,
73-
completion_algorithm: "fuzzy",
72+
{
73+
options: {
74+
sort: false,
75+
completion_algorithm: "fuzzy",
76+
}
77+
completions: $completions,
7478
}
75-
completions: $completions,
7679
}
77-
}
7880

79-
{{ section }}
80-
# When using zoxide with --no-cmd, alias these internal functions as desired.
81-
#
82-
83-
# Jump to a directory using only keywords.
84-
def --env --wrapped __zoxide_z [...rest: directory@"nu-complete __zoxide_z"] {
85-
let path = match $rest {
86-
[] => {'~'},
87-
[ '-' ] => {'-'},
88-
[ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg}
89-
_ => {
90-
^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
81+
{{ section }}
82+
# When using zoxide with --no-cmd, alias these internal functions as desired.
83+
#
84+
85+
# Jump to a directory using only keywords.
86+
export def --env --wrapped __zoxide_z [...rest: directory@"nu-complete __zoxide_z"] {
87+
let path = match $rest {
88+
[] => {'~'},
89+
[ '-' ] => {'-'},
90+
[ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg}
91+
_ => {
92+
^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
93+
}
9194
}
95+
cd $path
96+
{%- if echo %}
97+
echo $env.PWD
98+
{%- endif %}
9299
}
93-
cd $path
94-
{%- if echo %}
95-
echo $env.PWD
96-
{%- endif %}
97-
}
98100

99-
# Jump to a directory using interactive search.
100-
def --env --wrapped __zoxide_zi [...rest:string] {
101-
cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")'
102-
{%- if echo %}
103-
echo $env.PWD
104-
{%- endif %}
105-
}
101+
# Jump to a directory using interactive search.
102+
export def --env --wrapped __zoxide_zi [...rest: string] {
103+
cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")'
104+
{%- if echo %}
105+
echo $env.PWD
106+
{%- endif %}
107+
}
106108

107-
{{ section }}
108-
# Commands for zoxide. Disable these using --no-cmd.
109-
#
109+
{{ section }}
110+
# Commands for zoxide. Disable these using --no-cmd.
111+
#
110112

111-
{%- match cmd %}
112-
{%- when Some with (cmd) %}
113+
{%- match cmd %}
114+
{%- when Some with (cmd) %}
113115

114-
alias {{cmd}} = __zoxide_z
115-
alias {{cmd}}i = __zoxide_zi
116+
export alias {{cmd}} = __zoxide_z
117+
export alias {{cmd}}i = __zoxide_zi
116118

117-
{%- when None %}
119+
{%- when None %}
118120

119-
{{ not_configured }}
121+
{{ not_configured }}
122+
123+
{%- endmatch %}
124+
}
120125

121-
{%- endmatch %}
126+
export use zoxide_integration *
122127

123128
{{ section }}
124129
# Add this to your env file (find it by running `$nu.env-path` in Nushell):

0 commit comments

Comments
 (0)