Skip to content

Commit

Permalink
Merge pull request #9 from cptpiepmatz/nushell-support
Browse files Browse the repository at this point in the history
Nushell Support
  • Loading branch information
zekroTJA authored Mar 5, 2024
2 parents 023a677 + 0af8b2b commit 7fdf401
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ impl ShellEnv for Shell {
match self {
Self::Bash | Self::Sh | Self::Zsh => Ok(format!("export {key}=\"{val}\"")),
Self::Cmd | Self::PowerShell => Ok(format!("$env:{key} = \"{val}\"")),
// use toml format to let Nushell parse the values
Self::Nushell => Ok(format!("{key} = '''{val}'''")),
_ => Err(Error::UnsupportedShell),
}
}
Expand All @@ -57,6 +59,13 @@ impl ShellEnv for Shell {
#[cfg(windows)]
return Ok(format!("{}:{}", env::to_gitbash_path_var(curr), new));
}
Self::Nushell => {
#[cfg(not(windows))]
return Ok(format!("{new}:{curr}"));

#[cfg(windows)]
return Ok(format!("{new};{curr}"));
}
Self::Cmd | Self::PowerShell => Ok(format!("{new};{curr}")),
_ => Err(Error::UnsupportedShell),
}
Expand All @@ -72,6 +81,14 @@ impl ShellEnv for Shell {
.join("Documents")
.join("WindowsPowerShell")
.join("Microsoft.PowerShell_profile.ps1")),
#[cfg(not(windows))]
Self::Nushell => Ok(home.join(".config").join("nushell").join("config.nu")),
#[cfg(windows)]
Self::Nushell => Ok(home
.join("AppData")
.join("Roaming")
.join("nushell")
.join("config.nu")),
_ => Err(Error::UnsupportedShell),
}
}
Expand All @@ -85,7 +102,9 @@ impl ShellEnv for Shell {
#[cfg(windows)]
return Ok(env::to_gitbash_path(&path.as_ref().to_string_lossy()));
}
Self::Cmd | Self::PowerShell => Ok(path.as_ref().to_string_lossy().to_string()),
Self::Cmd | Self::PowerShell | Self::Nushell => {
Ok(path.as_ref().to_string_lossy().to_string())
}
_ => Err(Error::UnsupportedShell),
}
}
Expand All @@ -94,6 +113,26 @@ impl ShellEnv for Shell {
match self {
Self::Bash | Self::Sh | Self::Zsh => Ok(r#"eval "$(goup env)""#),
Self::Cmd | Self::PowerShell => Ok("goup env | Out-String | Invoke-Expression"),

// Nushell doesn't support eval
// https://www.nushell.sh/book/how_nushell_code_gets_run.html#eval-function
Self::Nushell => {
#[cfg(not(windows))]
return Ok("load-env (\
goup env \
| from toml \
| update PATH {do $env.ENV_CONVERSIONS.PATH.from_string $in}\
)");

#[cfg(windows)]
return Ok("load-env (\
goup env \
| from toml \
| rename -c {PATH: Path} \
| update Path {do $env.ENV_CONVERSIONS.Path.from_string $in}\
)");
}

_ => Err(Error::UnsupportedShell),
}
}
Expand Down

0 comments on commit 7fdf401

Please sign in to comment.