2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig, Shell};
|
2021-02-20 14:40:49 +00:00
|
|
|
|
|
|
|
use crate::configs::shell::ShellConfig;
|
|
|
|
use crate::formatter::StringFormatter;
|
|
|
|
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|
|
|
let mut module = context.new_module("shell");
|
|
|
|
let config: ShellConfig = ShellConfig::try_load(module.config);
|
|
|
|
|
|
|
|
if config.disabled {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-01-04 09:49:42 +00:00
|
|
|
let shell = &context.shell;
|
2021-02-20 14:40:49 +00:00
|
|
|
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"indicator" => match shell {
|
|
|
|
Shell::Bash => Some(config.bash_indicator),
|
|
|
|
Shell::Fish => Some(config.fish_indicator),
|
|
|
|
Shell::Zsh => Some(config.zsh_indicator),
|
|
|
|
Shell::PowerShell => Some(config.powershell_indicator),
|
|
|
|
Shell::Ion => Some(config.ion_indicator),
|
|
|
|
Shell::Elvish => Some(config.elvish_indicator),
|
2021-02-27 18:55:27 +00:00
|
|
|
Shell::Tcsh => Some(config.tcsh_indicator),
|
2021-07-04 19:32:58 +00:00
|
|
|
Shell::Nu => Some(config.nu_indicator),
|
2021-07-16 19:25:01 +00:00
|
|
|
Shell::Xonsh => Some(config.xonsh_indicator),
|
2022-01-10 05:47:53 +00:00
|
|
|
Shell::Cmd => Some(config.cmd_indicator),
|
2021-04-30 18:48:05 +00:00
|
|
|
Shell::Unknown => Some(config.unknown_indicator),
|
2021-02-20 14:40:49 +00:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-10-04 17:46:33 +00:00
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-03-23 21:20:29 +00:00
|
|
|
.map(|var| match var {
|
|
|
|
"bash_indicator" => Some(Ok(config.bash_indicator)),
|
|
|
|
"fish_indicator" => Some(Ok(config.fish_indicator)),
|
|
|
|
"zsh_indicator" => Some(Ok(config.zsh_indicator)),
|
|
|
|
"powershell_indicator" => Some(Ok(config.powershell_indicator)),
|
|
|
|
"ion_indicator" => Some(Ok(config.ion_indicator)),
|
|
|
|
"elvish_indicator" => Some(Ok(config.elvish_indicator)),
|
|
|
|
"tcsh_indicator" => Some(Ok(config.tcsh_indicator)),
|
2021-07-16 19:25:01 +00:00
|
|
|
"xonsh_indicator" => Some(Ok(config.xonsh_indicator)),
|
2022-01-10 05:47:53 +00:00
|
|
|
"cmd_indicator" => Some(Ok(config.cmd_indicator)),
|
2021-04-30 18:48:05 +00:00
|
|
|
"unknown_indicator" => Some(Ok(config.unknown_indicator)),
|
2021-03-23 21:20:29 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2021-02-20 14:40:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `shell`: \n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::context::Shell;
|
|
|
|
use crate::test::ModuleRenderer;
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2021-02-20 14:40:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_none_if_disabled() {
|
|
|
|
let expected = None;
|
|
|
|
let actual = ModuleRenderer::new("shell").shell(Shell::Bash).collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_none_if_unknown_shell() {
|
|
|
|
let expected = None;
|
|
|
|
let actual = ModuleRenderer::new("shell").shell(Shell::Unknown).collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bash_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("bsh")));
|
2021-02-20 14:40:49 +00:00
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Bash)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bash_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("bash")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Bash)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
bash_indicator = "[bash](bold cyan)"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fish_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("fsh")));
|
2021-02-20 14:40:49 +00:00
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fish_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("fish")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
fish_indicator = "[fish](cyan bold)"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zsh_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("zsh")));
|
2021-02-20 14:40:49 +00:00
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Zsh)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zsh_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("zsh")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Bash)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
bash_indicator = "[zsh](bold cyan)"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_powershell_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("psh")));
|
2021-02-20 14:40:49 +00:00
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::PowerShell)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_powershell_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("powershell")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::PowerShell)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
powershell_indicator = "[powershell](bold cyan)"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ion_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("ion")));
|
2021-02-20 14:40:49 +00:00
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Ion)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ion_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("ion")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Ion)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
ion_indicator = "[ion](bold cyan)"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_elvish_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("esh")));
|
2021-02-20 14:40:49 +00:00
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Elvish)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_elvish_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("elvish")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Elvish)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
elvish_indicator = "[elvish](bold cyan)"
|
2021-07-04 19:32:58 +00:00
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nu_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("nu")));
|
2021-07-04 19:32:58 +00:00
|
|
|
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)"
|
2021-07-16 19:25:01 +00:00
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_xonsh_default_format() {
|
2021-10-04 17:46:33 +00:00
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("xsh")));
|
2021-07-16 19:25:01 +00:00
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Xonsh)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_xonsh_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("xonsh")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Xonsh)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
xonsh_indicator = "[xonsh](bold cyan)"
|
2021-02-20 14:40:49 +00:00
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2021-03-23 21:20:29 +00:00
|
|
|
|
2022-01-10 05:47:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_cmd_default_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::White.bold().paint("cmd")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Cmd)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_custom_format() {
|
|
|
|
let expected = Some(format!("{} ", Color::Cyan.bold().paint("cmd")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Cmd)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
cmd_indicator = "[cmd](bold cyan)"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:20:29 +00:00
|
|
|
#[test]
|
|
|
|
fn test_custom_format_conditional_indicator_match() {
|
|
|
|
let expected = Some(format!("{} ", "B"));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Bash)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
bash_indicator = "B"
|
|
|
|
format = "($bash_indicator )"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_format_conditional_indicator_no_match() {
|
|
|
|
let expected = None;
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
bash_indicator = "B"
|
|
|
|
format = "($indicator )"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2021-10-04 17:46:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_style() {
|
|
|
|
let expected = Some(format!("{}", Color::White.bold().paint("fish")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
format = "[fish]($style)"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_style() {
|
|
|
|
let expected = Some(format!("{}", Color::Cyan.bold().paint("fish")));
|
|
|
|
let actual = ModuleRenderer::new("shell")
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[shell]
|
|
|
|
format = "[fish]($style)"
|
|
|
|
style = "cyan bold"
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2021-02-20 14:40:49 +00:00
|
|
|
}
|