feat: add support for nu shell (#2847)

This commit is contained in:
David Knaack
2021-07-04 21:32:58 +02:00
committed by GitHub
parent 196cc8aa39
commit cea1a1ceb1
17 changed files with 119 additions and 28 deletions
+10 -4
View File
@@ -1,6 +1,7 @@
use crate::shadow;
use crate::utils::exec_cmd;
use crate::utils::{self, exec_cmd};
use directories_next::ProjectDirs;
use std::fs;
use std::path::PathBuf;
use std::time::Duration;
@@ -191,7 +192,12 @@ fn get_terminal_info() -> TerminalInfo {
}
fn get_config_path(shell: &str) -> Option<PathBuf> {
dirs_next::home_dir().and_then(|home_dir| {
if shell == "nu" {
return ProjectDirs::from("org", "nushell", "nu")
.map(|project_dirs| project_dirs.config_dir().join("config.toml"));
}
utils::home_dir().and_then(|home_dir| {
match shell {
"bash" => Some(".bashrc"),
"fish" => Some(".config/fish/config.fish"),
@@ -217,7 +223,7 @@ fn get_starship_config() -> String {
.map(PathBuf::from)
.ok()
.or_else(|| {
dirs_next::home_dir().map(|mut home_dir| {
utils::home_dir().map(|mut home_dir| {
home_dir.push(".config/starship.toml");
home_dir
})
@@ -276,7 +282,7 @@ mod tests {
fn test_get_config_path() {
let config_path = get_config_path("bash");
assert_eq!(
dirs_next::home_dir().unwrap().join(".bashrc"),
utils::home_dir().unwrap().join(".bashrc"),
config_path.unwrap()
);
}
+1 -1
View File
@@ -226,7 +226,7 @@ impl StarshipConfig {
} else {
// Default to using ~/.config/starship.toml
log::debug!("STARSHIP_CONFIG is not set");
let config_path = dirs_next::home_dir()?.join(".config/starship.toml");
let config_path = utils::home_dir()?.join(".config/starship.toml");
let config_path_str = config_path.to_str()?.to_owned();
log::debug!("Using default config path: {}", config_path_str);
config_path_str
+2
View File
@@ -13,6 +13,7 @@ pub struct ShellConfig<'a> {
pub ion_indicator: &'a str,
pub elvish_indicator: &'a str,
pub tcsh_indicator: &'a str,
pub nu_indicator: &'a str,
pub unknown_indicator: &'a str,
pub disabled: bool,
}
@@ -28,6 +29,7 @@ impl<'a> Default for ShellConfig<'a> {
ion_indicator: "ion",
elvish_indicator: "esh",
tcsh_indicator: "tsh",
nu_indicator: "nu",
unknown_indicator: "",
disabled: true,
}
+2 -1
View File
@@ -6,6 +6,7 @@ use std::process::Command;
use crate::config::RootModuleConfig;
use crate::config::StarshipConfig;
use crate::utils;
use std::fs::File;
use std::io::Write;
use toml::map::Map;
@@ -178,7 +179,7 @@ fn get_config_path() -> OsString {
if let Some(config_path) = env::var_os("STARSHIP_CONFIG") {
return config_path;
}
dirs_next::home_dir()
utils::home_dir()
.expect("couldn't find home directory")
.join(".config")
.join("starship.toml")
+5 -3
View File
@@ -3,8 +3,8 @@ use crate::module::Module;
use crate::utils::{exec_cmd, CommandOutput};
use crate::modules;
use crate::utils::{self, home_dir};
use clap::ArgMatches;
use dirs_next::home_dir;
use git2::{ErrorCode::UnbornBranch, Repository, RepositoryState};
use once_cell::sync::OnceCell;
use std::collections::{HashMap, HashSet};
@@ -168,7 +168,7 @@ impl<'a> Context<'a> {
pub fn expand_tilde(dir: PathBuf) -> PathBuf {
if dir.starts_with("~") {
let without_home = dir.strip_prefix("~").unwrap();
return dirs_next::home_dir().unwrap().join(without_home);
return utils::home_dir().unwrap().join(without_home);
}
dir
}
@@ -256,6 +256,7 @@ impl<'a> Context<'a> {
"zsh" => Shell::Zsh,
"elvish" => Shell::Elvish,
"tcsh" => Shell::Tcsh,
"nu" => Shell::Nu,
_ => Shell::Unknown,
}
}
@@ -492,6 +493,7 @@ pub enum Shell {
Zsh,
Elvish,
Tcsh,
Nu,
Unknown,
}
@@ -621,7 +623,7 @@ mod tests {
#[test]
fn context_constructor_should_fall_back_to_tilde_replacement_when_canonicalization_fails() {
use dirs_next::home_dir;
use utils::home_dir;
// Mock navigation to a directory which does not exist on disk
let test_path = Path::new("~/path_which_does_not_exist").to_path_buf();
+4
View File
@@ -168,6 +168,7 @@ pub fn init_stub(shell_name: &str) -> io::Result<()> {
r#"eval `({} init tcsh --print-full-init)`"#,
starship.sprint_posix()?
),
"nu" => print_script(NU_INIT, &StarshipPath::init()?.sprint_posix()?),
_ => {
let quoted_arg = shell_words::quote(shell_basename);
println!(
@@ -180,6 +181,7 @@ pub fn init_stub(shell_name: &str) -> io::Result<()> {
* powershell\\n\
* tcsh\\n\
* zsh\\n\
* nu\\n\
\\n\
Please open an issue in the starship repo if you would like to \
see support for %s:\\nhttps://github.com/starship/starship/issues/new\\n\\n\" {0} {0}",
@@ -248,6 +250,8 @@ const ELVISH_INIT: &str = include_str!("starship.elv");
const TCSH_INIT: &str = include_str!("starship.tcsh");
const NU_INIT: &str = include_str!("starship.nu");
#[cfg(test)]
mod tests {
use super::*;
+8
View File
@@ -0,0 +1,8 @@
let-env STARSHIP_SHELL = "nu"
let-env STARSHIP_SESSION = (random chars -l 16)
def starship_prompt [] {
# jobs are not supported
# status is not supported
^::STARSHIP:: prompt --cmd-duration $nu.env.CMD_DURATION_MS
}
+2 -1
View File
@@ -1,3 +1,4 @@
use crate::utils;
use ansi_term::Color;
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::OnceCell;
@@ -22,7 +23,7 @@ impl Default for StarshipLogger {
let log_dir = env::var_os("STARSHIP_CACHE")
.map(PathBuf::from)
.unwrap_or_else(|| {
dirs_next::home_dir()
utils::home_dir()
.expect("Unable to find home directory")
.join(".cache/starship")
});
+1 -1
View File
@@ -41,7 +41,7 @@ fn main() {
let shell_arg = Arg::with_name("shell")
.value_name("SHELL")
.help(
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion, elvish, tcsh",
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion, elvish, tcsh, nu",
)
.required(true);
+1 -1
View File
@@ -298,8 +298,8 @@ fn to_fish_style(pwd_dir_length: usize, dir_string: String, truncated_dir_string
mod tests {
use super::*;
use crate::test::ModuleRenderer;
use crate::utils::home_dir;
use ansi_term::Color;
use dirs_next::home_dir;
#[cfg(not(target_os = "windows"))]
use std::os::unix::fs::symlink;
#[cfg(target_os = "windows")]
+30
View File
@@ -24,6 +24,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Shell::Ion => Some(config.ion_indicator),
Shell::Elvish => Some(config.elvish_indicator),
Shell::Tcsh => Some(config.tcsh_indicator),
Shell::Nu => Some(config.nu_indicator),
Shell::Unknown => Some(config.unknown_indicator),
},
_ => None,
@@ -249,6 +250,35 @@ mod tests {
assert_eq!(expected, actual);
}
#[test]
fn test_nu_default_format() {
let expected = Some(format!("{} ", "nu"));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Nu)
.config(toml::toml! {
[shell]
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_nu_custom_format() {
let expected = Some(format!("{} ", Color::Cyan.bold().paint("nu")));
let actual = ModuleRenderer::new("shell")
.shell(Shell::Nu)
.config(toml::toml! {
[shell]
nu_indicator = "[nu](bold cyan)"
disabled = false
})
.collect();
assert_eq!(expected, actual);
}
#[test]
fn test_custom_format_conditional_indicator_match() {
let expected = Some(format!("{} ", "B"));
+5 -1
View File
@@ -2,7 +2,7 @@ use process_control::{ChildExt, Timeout};
use std::fmt::Debug;
use std::fs::read_to_string;
use std::io::Result;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
@@ -414,6 +414,10 @@ fn render_time_component((component, suffix): (&u128, &&str)) -> String {
}
}
pub fn home_dir() -> Option<PathBuf> {
directories_next::BaseDirs::new().map(|base_dirs| base_dirs.home_dir().to_owned())
}
#[cfg(test)]
mod tests {
use super::*;