Skip to content

Commit 5cc4f1a

Browse files
committed
Add nushell completion for __zoxide_z
1 parent fcce980 commit 5cc4f1a

File tree

1 file changed

+107
-77
lines changed

1 file changed

+107
-77
lines changed

templates/nushell.txt

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

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

6-
{{ section }}
7-
# Hook configuration for zoxide.
8-
#
6+
module zoxide_integration {
97

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-
})
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-
# When using zoxide with --no-cmd, alias these internal functions as desired.
56-
#
56+
{{ section }}
57+
# Completion for __zoxide_z
58+
#
59+
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 }
5767

58-
# Jump to a directory using only keywords.
59-
def --env --wrapped __zoxide_z [...rest: string] {
60-
let path = match $rest {
61-
[] => {'~'},
62-
[ '-' ] => {'-'},
63-
[ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg}
64-
_ => {
65-
^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
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 }
71+
72+
{
73+
options: {
74+
sort: false,
75+
completion_algorithm: "substring",
76+
}
77+
completions: $completions,
6678
}
6779
}
68-
cd $path
69-
{%- if echo %}
70-
echo $env.PWD
71-
{%- endif %}
72-
}
7380

74-
# Jump to a directory using interactive search.
75-
def --env --wrapped __zoxide_zi [...rest:string] {
76-
cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")'
77-
{%- if echo %}
78-
echo $env.PWD
79-
{%- endif %}
80-
}
81+
{{ section }}
82+
# When using zoxide with --no-cmd, alias these internal functions as desired.
83+
#
8184

82-
{{ section }}
83-
# Commands for zoxide. Disable these using --no-cmd.
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+
}
94+
}
95+
cd $path
96+
{%- if echo %}
97+
echo $env.PWD
98+
{%- endif %}
99+
}
100+
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+
}
85108

86-
{%- match cmd %}
87-
{%- when Some with (cmd) %}
109+
{{ section }}
110+
# Commands for zoxide. Disable these using --no-cmd.
111+
#
88112

89-
alias {{cmd}} = __zoxide_z
90-
alias {{cmd}}i = __zoxide_zi
113+
{%- match cmd %}
114+
{%- when Some with (cmd) %}
91115

92-
{%- when None %}
116+
export alias {{cmd}} = __zoxide_z
117+
export alias {{cmd}}i = __zoxide_zi
93118

94-
{{ not_configured }}
119+
{%- when None %}
120+
121+
{{ not_configured }}
122+
123+
{%- endmatch %}
124+
}
95125

96-
{%- endmatch %}
126+
export use zoxide_integration *
97127

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

0 commit comments

Comments
 (0)