12
12
13
13
{%- else -%}
14
14
# Initialize hook to add new entries to the database.
15
- if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) {
16
- $env.__zoxide_hooked = true
15
+ export-env {
17
16
{%- if hook == InitHook::Prompt %}
18
- $env.config = ($env | default {} config).config
19
- $env.config = ($env.config | default {} hooks)
20
- $env.config = ($env.config | update hooks ($env.config.hooks | default [] pre_prompt))
21
- $env.config = ($env.config | update hooks.pre_prompt ($env.config.hooks.pre_prompt | append { ||
22
- zoxide add -- $env.PWD
23
- }))
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
+ }
24
32
{%- else if hook == InitHook::Pwd %}
25
- $env.config = ($env | default {} config).config
26
- $env.config = ($env.config | default {} hooks)
27
- $env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change))
28
- $env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD))
29
- $env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append {|_, dir|
30
- zoxide add -- $dir
31
- }))
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
+ })
48
+ }
32
49
{%- endif %}
33
50
}
34
51
@@ -39,13 +56,14 @@ if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) {
39
56
#
40
57
41
58
# Jump to a directory using only keywords.
42
- def --env --wrapped __zoxide_z [...rest:string] {
43
- let arg0 = ($rest | append '~').0
44
- let arg0_is_dir = (try {$arg0 | path expand | path type}) == 'dir'
45
- let path = if (($rest | length) <= 1) and ($arg0 == '-' or $arg0_is_dir) {
46
- $arg0
47
- } else {
48
- (zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n")
59
+ def --env --wrapped __zoxide_z [...rest: string] {
60
+ let path = match $rest {
61
+ [] => {'~'},
62
+ [ '-' ] => {'-'},
63
+ [ $arg ] if ($arg | path type) == 'dir' => {$arg}
64
+ _ => {
65
+ zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
66
+ }
49
67
}
50
68
cd $path
51
69
{%- if echo %}
0 commit comments