Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Nushell script #1763

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/shell/nushell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,29 @@ impl Shell for Nushell {
formatdoc! {r#"
export-env {{
$env.MISE_SHELL = "nu"

$env.config = ($env.config | upsert hooks {{
pre_prompt: ($env.config.hooks.pre_prompt ++
[{{
condition: {{|| "MISE_SHELL" in $env }}
code: {{|| mise_hook }}
}}])
env_change: {{
PWD: ($env.config.hooks.env_change.PWD ++
[{{
condition: {{|| "MISE_SHELL" in $env }}
code: {{|| mise_hook }}
}}])
}}
}})
add-hook pre_prompt {{
condition: {{ "MISE_SHELL" in $env }}
code: {{ mise_hook }}
}}
add-hook env_change.PWD {{
condition: {{ "MISE_SHELL" in $env }}
code: {{ mise_hook }}
}}
}}

def --env add-hook [field hook] {{
texastoland marked this conversation as resolved.
Show resolved Hide resolved
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 "parse vars" [] {{
$in | lines | parse "{{op}},{{name}},{{value}}"
}}
def --wrapped mise [command?: string, --help, ...rest: string] {{

export def --wrapped main [command?: string, --help, ...rest: string] {{
texastoland marked this conversation as resolved.
Show resolved Hide resolved
let commands = ["shell", "deactivate"]

if ($command == null) {{
^"{exe}"
}} else if ($command == "activate") {{
Expand All @@ -63,7 +62,7 @@ impl Shell for Nushell {
^"{exe}" $command ...$rest
}}
}}

def --env "update-env" [] {{
for $var in $in {{
if $var.op == "set" {{
Expand Down
39 changes: 19 additions & 20 deletions src/shell/snapshots/mise__shell__nushell__tests__hook_init.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ expression: "nushell.activate(exe, \" --status\".into())"
---
export-env {
$env.MISE_SHELL = "nu"

$env.config = ($env.config | upsert hooks {
pre_prompt: ($env.config.hooks.pre_prompt ++
[{
condition: {|| "MISE_SHELL" in $env }
code: {|| mise_hook }
}])
env_change: {
PWD: ($env.config.hooks.env_change.PWD ++
[{
condition: {|| "MISE_SHELL" in $env }
code: {|| mise_hook }
}])
}
})
add-hook pre_prompt {
condition: { "MISE_SHELL" in $env }
code: { mise_hook }
}
add-hook env_change.PWD {
condition: { "MISE_SHELL" in $env }
code: { 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 "parse vars" [] {
$in | lines | parse "{op},{name},{value}"
}
def --wrapped mise [command?: string, --help, ...rest: string] {

export def --wrapped main [command?: string, --help, ...rest: string] {
let commands = ["shell", "deactivate"]

if ($command == null) {
^"/some/dir/mise"
} else if ($command == "activate") {
Expand All @@ -40,7 +39,7 @@ def --wrapped mise [command?: string, --help, ...rest: string] {
^"/some/dir/mise" $command ...$rest
}
}

def --env "update-env" [] {
for $var in $in {
if $var.op == "set" {
Expand Down
Loading