Skip to content

Commit

Permalink
fix(nu): Gracefully handle missing $env.config
Browse files Browse the repository at this point in the history
Fixes error when `$env.config` is null. Nu implements default behavior for missing
configurations enabling minimal customization in config files.

Additional simplifications:
- save hook to variable vs repeat it
- annotate `cell-path` type vs create it with `into cell-path`
  • Loading branch information
texastoland committed Mar 21, 2024
1 parent 3bde772 commit 55ecf44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
18 changes: 8 additions & 10 deletions src/shell/nushell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ impl Shell for Nushell {
formatdoc! {r#"
export-env {{
$env.MISE_SHELL = "nu"
add-hook pre_prompt {{
let mise_hook = {{
condition: {{ "MISE_SHELL" in $env }}
code: {{ mise_hook }}
}}
add-hook env_change.PWD {{
condition: {{ "MISE_SHELL" in $env }}
code: {{ mise_hook }}
}}
}}
add-hook hooks.pre_prompt $mise_hook
add-hook hooks.env_change.PWD $mise_hook
}}
def --env add-hook [field hook] {{
let field = $field | split row . | prepend hooks | into cell-path
let hooks = $env.config | get --ignore-errors $field | default []
$env.config = ($env.config | upsert $field ($hooks ++ $hook))
def --env add-hook [field: cell-path new_hook: any] {{
let old_config = $env.config? | default {{}}
let old_hooks = $old_config | get $field --ignore-errors | default []
$env.config = ($old_config | upsert $field ($old_hooks ++ $new_hook))
}}
def "parse vars" [] {{
Expand Down
16 changes: 7 additions & 9 deletions src/shell/snapshots/mise__shell__nushell__tests__hook_init.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ expression: "nushell.activate(exe, \" --status\".into())"
---
export-env {
$env.MISE_SHELL = "nu"
add-hook pre_prompt {
condition: { "MISE_SHELL" in $env }
code: { mise_hook }
}
add-hook env_change.PWD {
let mise_hook = {
condition: { "MISE_SHELL" in $env }
code: { mise_hook }
}
add-hook hooks.pre_prompt $mise_hook
add-hook hooks.env_change.PWD $mise_hook
}

def --env add-hook [field hook] {
let field = $field | split row . | prepend hooks | into cell-path
let hooks = $env.config | get --ignore-errors $field | default []
$env.config = ($env.config | upsert $field ($hooks ++ $hook))
def --env add-hook [field: cell-path new_hook: any] {
let old_config = $env.config? | default {}
let old_hooks = $old_config | get $field --ignore-errors | default []
$env.config = ($old_config | upsert $field ($old_hooks ++ $new_hook))
}

def "parse vars" [] {
Expand Down

0 comments on commit 55ecf44

Please sign in to comment.