From 601100e0fb9a35b5ebe89cc16af7486970758a94 Mon Sep 17 00:00:00 2001 From: Jian Date: Wed, 20 Nov 2024 21:22:28 +0800 Subject: [PATCH] feat: add nushell support --- src/cli/self_update.rs | 1 + src/cli/self_update/env.nu | 3 +++ src/cli/self_update/shell.rs | 48 ++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/cli/self_update/env.nu diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 87b01cbde5..ab77d6d277 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -384,6 +384,7 @@ the corresponding `env` file under {cargo_home}. This is usually done by running one of the following (note the leading DOT): . "{cargo_home}/env" # For sh/bash/zsh/ash/dash/pdksh source "{cargo_home}/env.fish" # For fish + source "{cargo_home}/env.nu" # For nushell "# }; } diff --git a/src/cli/self_update/env.nu b/src/cli/self_update/env.nu new file mode 100644 index 0000000000..016dee6849 --- /dev/null +++ b/src/cli/self_update/env.nu @@ -0,0 +1,3 @@ +if ("{cargo_bin}" not-in ($env.Path | split row (char esep))) { + $env.Path = ($env.Path | prepend "{cargo_bin}") +} \ No newline at end of file diff --git a/src/cli/self_update/shell.rs b/src/cli/self_update/shell.rs index 07f9168038..4cbe435ff4 100644 --- a/src/cli/self_update/shell.rs +++ b/src/cli/self_update/shell.rs @@ -77,6 +77,7 @@ fn enumerate_shells() -> Vec { Box::new(Bash), Box::new(Zsh), Box::new(Fish), + Box::new(Nu), ] } @@ -255,6 +256,53 @@ impl UnixShell for Fish { } } +struct Nu; + +impl UnixShell for Nu { + fn does_exist(&self, process: &Process) -> bool { + // nu has to either be the shell or be callable for nu setup. + matches!(process.var("SHELL"), Ok(sh) if sh.contains("nu")) + || utils::find_cmd(&["nu"], process).is_some() + } + + fn rcfiles(&self, process: &Process) -> Vec { + let mut paths = vec![]; + + if let Ok(p) = process.var("XDG_CONFIG_HOME") { + let path = PathBuf::from(p).join("nushell/"); + paths.push(path.join("env.nu")); + paths.push(path.join("config.nu")); + } + + if let Some(p) = process.home_dir() { + let path = p.join(".config/nushell/"); + paths.push(path.join("env.nu")); + paths.push(path.join("config.nu")); + } + paths + } + + fn update_rcs(&self, process: &Process) -> Vec { + let mut rcs = self.rcfiles(process); + if rcs.len() == 4 { + // The first two rcfile takes precedence (XDG_CONFIG_HOME). + rcs.truncate(2); + } + rcs + } + + fn env_script(&self) -> ShellScript { + ShellScript { + name: "env.nu", + content: include_str!("env.nu"), + } + } + + fn source_string(&self, process: &Process) -> Result { + Ok(format!(r#"source "{}/env.nu""#, cargo_home_str(process)?)) + } +} + pub(crate) fn legacy_paths(process: &Process) -> impl Iterator + '_ { let zprofiles = Zsh::zdotdir(process) .into_iter()