From 0450775af9b1430460967ba8fb5aa434f95c4bc4 Mon Sep 17 00:00:00 2001 From: Bahex Date: Sun, 5 Jan 2025 07:42:21 +0300 Subject: [PATCH] update nushell init script to not break on future version 0.102.0 --- templates/nushell.txt | 57 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/templates/nushell.txt b/templates/nushell.txt index a131efe..2304c90 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -12,24 +12,45 @@ {%- else -%} # Initialize hook to add new entries to the database. -if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) { - $env.__zoxide_hooked = true -{%- if hook == InitHook::Prompt %} - $env.config = ($env | default {} config).config - $env.config = ($env.config | default {} hooks) - $env.config = ($env.config | update hooks ($env.config.hooks | default [] pre_prompt)) - $env.config = ($env.config | update hooks.pre_prompt ($env.config.hooks.pre_prompt | append { || - zoxide add -- $env.PWD - })) -{%- else if hook == InitHook::Pwd %} - $env.config = ($env | default {} config).config - $env.config = ($env.config | default {} hooks) - $env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change)) - $env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD)) - $env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append {|_, dir| - zoxide add -- $dir - })) -{%- endif %} +export-env { + {%- if hook == InitHook::Prompt %} + let hooked = ( + $env.config?.hooks?.pre_prompt? + | default [] + | any { try {get zoxide} catch { false } } + ) + if not $hooked { + $env.config = ( + $env.config? | default {} + | upsert hooks { default {} } + | upsert hooks.pre_prompt { default [] } + ) + + $env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append { + zoxide: true, + code: {|| zoxide add -- $env.PWD} + }) + } + {%- else if hook == InitHook::Pwd %} + let hooked = ( + $env.config?.hooks?.env_change?.PWD? + | default [] + | any { try {get zoxide} catch { false } } + ) + if not $hooked { + $env.config = ( + $env.config? | default {} + | upsert hooks { default {} } + | upsert hooks.env_change { default {} } + | upsert hooks.env_change.PWD { default [] } + ) + + $env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append { + zoxide: true, + code: {|_, dir| zoxide add -- $dir} + }) + } + {%- endif %} } {%- endif %}