Skip to content

Commit bdedc2a

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 32ed3ec commit bdedc2a

1 file changed

Lines changed: 119 additions & 110 deletions

File tree

templates/nushell.txt

Lines changed: 119 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,142 @@
1-
{%- let section = "# =============================================================================\n#" -%}
1+
{%- let section = "# =============================================================================" -%}
22
{%- let not_configured = "# -- not configured --" -%}
33

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

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

60-
def "nu-complete __zoxide_z" [context: string] {
61-
let ast = ast --flatten $context | skip 1
62-
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 }
67-
68-
let completions = ^zoxide query --exclude $env.PWD --list -- ...$ast.content
69-
| lines | each {
70-
if $in starts-with $"($homedir)(char psep)" {
71-
str replace $homedir "~"
72-
} else {}
73-
} | wrap value
74-
| insert span { start: ($ast | first).span.start, end: ($ast | last).span.end }
75-
76-
{
77-
options: {
78-
sort: false,
79-
case_sensitive: false,
80-
completion_algorithm: "substring",
56+
{%- endif %}
57+
58+
{{ section }}
59+
#
60+
# Completion for __zoxide_z
61+
#
62+
63+
def "nu-complete __zoxide_z" [context: string] {
64+
let ast = ast --flatten $context | skip 1
65+
66+
# If the user has typed a space after the first argument, use the custom
67+
# completer. If not, use the built-in directory completion.
68+
if ($ast | is-empty) { return null }
69+
if $ast.0.span.end >= ($context | str length) { return null }
70+
71+
let completions = ^zoxide query --exclude $env.PWD --list -- ...$ast.content
72+
| lines | each {
73+
if $in starts-with $"($homedir)(char psep)" {
74+
str replace $homedir "~"
75+
} else {}
76+
} | wrap value
77+
| insert span { start: ($ast | first).span.start, end: ($ast | last).span.end }
78+
79+
{
80+
options: {
81+
sort: false,
82+
case_sensitive: false,
83+
completion_algorithm: "substring",
84+
}
85+
completions: $completions,
8186
}
82-
completions: $completions,
8387
}
84-
}
85-
86-
{{ section }}
87-
# When using zoxide with --no-cmd, alias these internal functions as desired.
88-
#
8988

90-
# Jump to a directory using only keywords.
91-
export def --env --wrapped __zoxide_z [...rest: directory@"nu-complete __zoxide_z"] {
92-
let path = match $rest {
93-
[] => {'~'},
94-
[ '-' ] => {'-'},
95-
[ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg}
96-
_ => {
97-
^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
89+
{{ section }}
90+
#
91+
# When using zoxide with --no-cmd, alias these internal functions as desired.
92+
#
93+
94+
# Jump to a directory using only keywords.
95+
export def --env --wrapped __zoxide_z [...rest: directory@"nu-complete __zoxide_z"] {
96+
let path = match $rest {
97+
[] => {'~'},
98+
[ '-' ] => {'-'},
99+
[ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg}
100+
_ => {
101+
^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
102+
}
98103
}
104+
cd $path
105+
{%- if echo %}
106+
echo $env.PWD
107+
{%- endif %}
99108
}
100-
cd $path
101-
{%- if echo %}
102-
echo $env.PWD
103-
{%- endif %}
104-
}
105109

106-
# Jump to a directory using interactive search.
107-
export def --env --wrapped __zoxide_zi [...rest: string] {
108-
cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")'
109-
{%- if echo %}
110-
echo $env.PWD
111-
{%- endif %}
112-
}
110+
# Jump to a directory using interactive search.
111+
export def --env --wrapped __zoxide_zi [...rest: string] {
112+
cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")'
113+
{%- if echo %}
114+
echo $env.PWD
115+
{%- endif %}
116+
}
113117

114-
{{ section }}
115-
# Commands for zoxide. Disable these using --no-cmd.
116-
#
118+
{{ section }}
119+
#
120+
# Commands for zoxide. Disable these using --no-cmd.
121+
#
117122

118-
{%- match cmd %}
119-
{%- when Some with (cmd) %}
123+
{%- match cmd %}
124+
{%- when Some with (cmd) %}
120125

121-
export alias {{cmd}} = __zoxide_z
122-
export alias {{cmd}}i = __zoxide_zi
126+
export alias {{cmd}} = __zoxide_z
127+
export alias {{cmd}}i = __zoxide_zi
123128

124-
{%- when None %}
129+
{%- when None %}
125130

126-
{{ not_configured }}
131+
{{ not_configured }}
127132

128-
{%- endmatch %}
133+
{%- endmatch %}
134+
}
135+
136+
export use zoxide_integration *
129137

130138
{{ section }}
139+
#
131140
# Add this to your env file (find it by running `$nu.env-path` in Nushell):
132141
#
133142
# zoxide init nushell | save -f ~/.zoxide.nu

0 commit comments

Comments
 (0)