From 2a534933d46416806281499303c70aede90a36ea Mon Sep 17 00:00:00 2001 From: Tim 'Piepmatz' Hesse Date: Mon, 4 Mar 2024 17:02:56 +0100 Subject: [PATCH 1/2] added nushell support --- src/shell/mod.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/shell/mod.rs b/src/shell/mod.rs index b960329..6a8eb77 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -44,13 +44,15 @@ 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), } } fn append_to_path(&self, curr: &str, new: &str) -> Result { match self { - Self::Bash | Self::Sh | Self::Zsh => { + Self::Bash | Self::Sh | Self::Zsh | Self::Nushell => { #[cfg(not(windows))] return Ok(format!("{new}:{curr}")); @@ -72,6 +74,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), } } @@ -85,7 +95,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), } } @@ -94,6 +106,10 @@ 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"), + #[cfg(not(windows))] + Self::Nushell => Ok("load-env (goup env | from toml | update PATH {do $env.ENV_CONVERSIONS.PATH.from_string $in})"), + #[cfg(windows)] + Self::Nushell => Ok("load-env (goup env | from toml | update Path {do $env.ENV_CONVERSIONS.Path.from_string $in})"), _ => Err(Error::UnsupportedShell), } } From 0af8b2b0c3a35b1fec6e3880912f22bbbfb8df47 Mon Sep 17 00:00:00 2001 From: Tim 'Piepmatz' Hesse Date: Mon, 4 Mar 2024 23:11:28 +0100 Subject: [PATCH 2/2] properly implemented nushell support for windows --- src/shell/mod.rs | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/shell/mod.rs b/src/shell/mod.rs index 6a8eb77..b33d0a0 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -44,21 +44,28 @@ 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}\"")), + // use toml format to let Nushell parse the values + Self::Nushell => Ok(format!("{key} = '''{val}'''")), _ => Err(Error::UnsupportedShell), } } fn append_to_path(&self, curr: &str, new: &str) -> Result { match self { - Self::Bash | Self::Sh | Self::Zsh | Self::Nushell => { + Self::Bash | Self::Sh | Self::Zsh => { #[cfg(not(windows))] return Ok(format!("{new}:{curr}")); #[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), } @@ -106,10 +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"), - #[cfg(not(windows))] - Self::Nushell => Ok("load-env (goup env | from toml | update PATH {do $env.ENV_CONVERSIONS.PATH.from_string $in})"), - #[cfg(windows)] - Self::Nushell => Ok("load-env (goup env | from toml | update Path {do $env.ENV_CONVERSIONS.Path.from_string $in})"), + + // 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), } }