Skip to content

Commit

Permalink
feat: add nushell support
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleJianCH committed Nov 20, 2024
1 parent c8eddcb commit 436dcfc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/cli/self_update/env.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if ("{cargo_bin}" not-in ($env.Path | split row (char esep))) {
$env.Path = ($env.Path | prepend "{cargo_bin}")
}
56 changes: 56 additions & 0 deletions src/cli/self_update/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn enumerate_shells() -> Vec<Shell> {
Box::new(Bash),
Box::new(Zsh),
Box::new(Fish),
Box::new(Nu),
]
}

Expand Down Expand Up @@ -255,6 +256,61 @@ 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<PathBuf> {
let p0: Vec<_> = process
.var("XDG_CONFIG_HOME")
.ok()
.iter()
.flat_map(|p| {
let path = PathBuf::from(p).join("nushell/");
[path.join("env.nu"), path.join("config.nu")]
})
.collect();

let p1: Vec<_> = process
.home_dir()
.iter()
.flat_map(|p| {
let path = PathBuf::from(p).join(".config/nushell/");
[path.join("env.nu"), path.join("config.nu")]
})
.collect();

[p0, p1].concat()
}

fn update_rcs(&self, process: &Process) -> Vec<PathBuf> {
let mut rcs = self.rcfiles(process);
if rcs.len() == 4 {
// The first two rcfile takes precedence (XDG_CONFIG_HOME).
rcs.truncate(2);
rcs
} else {
rcs
}
}

fn env_script(&self) -> ShellScript {
ShellScript {
name: "env.nu",
content: include_str!("env.nu"),
}
}

fn source_string(&self, process: &Process) -> Result<String> {
Ok(format!(r#"source "{}/env.nu""#, cargo_home_str(process)?))
}
}

pub(crate) fn legacy_paths(process: &Process) -> impl Iterator<Item = PathBuf> + '_ {
let zprofiles = Zsh::zdotdir(process)
.into_iter()
Expand Down

0 comments on commit 436dcfc

Please sign in to comment.