2020-07-08 06:45:32 +08:00
|
|
|
use super::{Context, Module};
|
2019-09-04 10:03:31 -07:00
|
|
|
use std::ffi::OsString;
|
|
|
|
|
2022-03-26 10:42:19 +01:00
|
|
|
use crate::config::ModuleConfig;
|
2019-10-10 16:21:52 +08:00
|
|
|
use crate::configs::hostname::HostnameConfig;
|
2020-07-08 06:45:32 +08:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-10 16:21:52 +08:00
|
|
|
|
2019-09-04 10:03:31 -07:00
|
|
|
/// Creates a module with the system hostname
|
|
|
|
///
|
|
|
|
/// Will display the hostname if all of the following criteria are met:
|
2023-09-16 16:42:13 +02:00
|
|
|
/// - `hostname.disabled` is absent or false
|
2022-05-23 12:58:27 +02:00
|
|
|
/// - `hostname.ssh_only` is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
|
2023-09-16 16:42:13 +02:00
|
|
|
/// - `hostname.ssh_only` is false AND `hostname.detect_env_vars` is either empty or contains a defined environment variable
|
2019-09-04 10:03:31 -07:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-09-09 18:14:38 -05:00
|
|
|
let mut module = context.new_module("hostname");
|
2019-10-10 16:21:52 +08:00
|
|
|
let config: HostnameConfig = HostnameConfig::try_load(module.config);
|
2019-09-04 10:03:31 -07:00
|
|
|
|
2020-08-07 21:13:12 +02:00
|
|
|
let ssh_connection = context.get_env("SSH_CONNECTION");
|
2023-09-16 16:42:13 +02:00
|
|
|
|
|
|
|
if (config.ssh_only && ssh_connection.is_none())
|
|
|
|
|| !context.detect_env_vars(&config.detect_env_vars)
|
|
|
|
{
|
2019-09-04 10:03:31 -07:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-01-02 15:45:06 +01:00
|
|
|
let os_hostname: OsString = gethostname::gethostname();
|
2019-09-04 10:03:31 -07:00
|
|
|
|
|
|
|
let host = match os_hostname.into_string() {
|
|
|
|
Ok(host) => host,
|
|
|
|
Err(bad) => {
|
2020-09-28 22:38:50 +02:00
|
|
|
log::warn!("hostname is not valid UTF!\n{:?}", bad);
|
2019-09-04 10:03:31 -07:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-14 12:22:25 -04:00
|
|
|
//rustc doesn't let you do an "if" and an "if let" in the same if statement
|
|
|
|
// if this changes in the future this can become a lot cleaner
|
2024-08-12 18:26:35 +02:00
|
|
|
let mut host = if !config.trim_at.is_empty() {
|
2019-10-14 12:22:25 -04:00
|
|
|
if let Some(index) = host.find(config.trim_at) {
|
|
|
|
host.split_at(index).0
|
|
|
|
} else {
|
|
|
|
host.as_ref()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
host.as_ref()
|
|
|
|
};
|
|
|
|
|
2024-08-12 18:26:35 +02:00
|
|
|
if let Some(&alias) = config.aliases.get(host) {
|
|
|
|
host = alias;
|
|
|
|
}
|
|
|
|
|
2020-07-08 06:45:32 +08:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
2022-05-04 20:30:16 +07:00
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"ssh_symbol" => {
|
|
|
|
if ssh_connection.is_some() {
|
|
|
|
Some(config.ssh_symbol)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
2020-07-08 06:45:32 +08:00
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"hostname" => Some(Ok(host)),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 14:18:45 -07:00
|
|
|
.parse(None, Some(context))
|
2020-07-08 06:45:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `hostname`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-09-04 10:03:31 -07:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
2020-08-07 21:13:12 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::ModuleRenderer;
|
2022-09-04 18:44:54 +02:00
|
|
|
use nu_ansi_term::{Color, Style};
|
2021-12-09 22:15:25 +01:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2020-08-07 21:13:12 +02:00
|
|
|
|
|
|
|
macro_rules! get_hostname {
|
|
|
|
() => {
|
2024-01-02 15:45:06 +01:00
|
|
|
if let Ok(hostname) = gethostname::gethostname().into_string() {
|
2020-08-07 21:13:12 +02:00
|
|
|
hostname
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"hostname was not tested because gethostname failed! \
|
|
|
|
This could be caused by your hostname containing invalid UTF."
|
|
|
|
);
|
2021-02-11 21:08:17 +01:00
|
|
|
return;
|
2020-08-07 21:13:12 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-09-16 16:42:13 +02:00
|
|
|
fn ssh_only_false_with_empty_detect_env_vars() {
|
2020-08-07 21:13:12 +02:00
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
2023-09-16 16:42:13 +02:00
|
|
|
detect_env_vars = []
|
2020-08-07 21:13:12 +02:00
|
|
|
})
|
|
|
|
.collect();
|
2023-09-16 16:42:13 +02:00
|
|
|
|
|
|
|
let expected = Some(format!("{} in ", style().paint(hostname)));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh_only_false_with_matching_negated_env_var() {
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
detect_env_vars = ["!NEGATED"]
|
|
|
|
})
|
|
|
|
.env("NEGATED", "true")
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh_only_false_with_only_negated_env_vars() {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
detect_env_vars = ["!NEGATED_ONE", "!NEGATED_TWO", "!NEGATED_THREE"]
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-09-08 12:09:21 -04:00
|
|
|
let expected = Some(format!("{} in ", style().paint(hostname)));
|
2023-09-16 16:42:13 +02:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh_only_false_with_matching_env_var() {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
detect_env_vars = ["FORCE_HOSTNAME"]
|
|
|
|
})
|
|
|
|
.env("FORCE_HOSTNAME", "true")
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} in ", style().paint(hostname)));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh_only_false_without_matching_env_vars() {
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
detect_env_vars = ["FORCE_HOSTNAME", "!NEGATED"]
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
2022-05-04 20:30:16 +07:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2020-08-07 21:13:12 +02:00
|
|
|
|
2022-05-04 20:30:16 +07:00
|
|
|
#[test]
|
|
|
|
fn ssh_only_false_ssh() {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("{} in ", style().paint(hostname)));
|
2023-09-16 16:42:13 +02:00
|
|
|
|
2020-08-07 21:13:12 +02:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
fn no_ssh() {
|
2020-08-07 21:13:12 +02:00
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = true
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
fn ssh() {
|
2020-08-07 21:13:12 +02:00
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = true
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.env("SSH_CONNECTION", "something")
|
|
|
|
.collect();
|
2022-05-04 20:30:16 +07:00
|
|
|
let expected = Some(format!(
|
|
|
|
"{} in ",
|
2022-08-09 04:33:00 +02:00
|
|
|
style().paint("🌐 ".to_owned() + hostname.as_str())
|
2022-05-04 20:30:16 +07:00
|
|
|
));
|
2020-08-07 21:13:12 +02:00
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
fn no_trim_at() {
|
2020-08-07 21:13:12 +02:00
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.collect();
|
2020-09-08 12:09:21 -04:00
|
|
|
let expected = Some(format!("{} in ", style().paint(hostname)));
|
2020-08-07 21:13:12 +02:00
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
fn trim_at() {
|
2020-08-07 21:13:12 +02:00
|
|
|
let hostname = get_hostname!();
|
2021-12-09 22:15:25 +01:00
|
|
|
let mut hostname_iter = hostname.graphemes(true);
|
|
|
|
let remainder = hostname_iter.next().unwrap_or_default();
|
|
|
|
let trim_at = hostname_iter.collect::<String>();
|
2020-08-07 21:13:12 +02:00
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = trim_at
|
|
|
|
})
|
|
|
|
.collect();
|
2020-09-08 12:09:21 -04:00
|
|
|
let expected = Some(format!("{} in ", style().paint(remainder)));
|
2020-08-07 21:13:12 +02:00
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2024-08-12 18:26:35 +02:00
|
|
|
#[test]
|
|
|
|
fn test_alias() {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let mut toml_config = toml::toml!(
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
aliases = {}
|
|
|
|
);
|
|
|
|
toml_config["hostname"]["aliases"]
|
|
|
|
.as_table_mut()
|
|
|
|
.unwrap()
|
|
|
|
.insert(
|
|
|
|
hostname.clone(),
|
|
|
|
toml::Value::String("homeworld".to_string()),
|
|
|
|
);
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml_config)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} in ", style().paint("homeworld")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_alias_with_trim_at() {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
|
|
|
|
let mut hostname_iter = hostname.graphemes(true);
|
|
|
|
let remainder = hostname_iter.next().unwrap_or_default();
|
|
|
|
let trim_at = hostname_iter.collect::<String>();
|
|
|
|
|
|
|
|
// Trimmed hostname needs to be non-empty
|
|
|
|
if remainder.is_empty() {
|
|
|
|
log::warn!("Skipping test_alias_with_trim_at because hostname is too short");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let mut toml_config = toml::toml!(
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = trim_at
|
|
|
|
aliases = {}
|
|
|
|
);
|
|
|
|
toml_config["hostname"]["aliases"]
|
|
|
|
.as_table_mut()
|
|
|
|
.unwrap()
|
|
|
|
.insert(remainder.to_string(), toml::Value::String("🌍".to_string()));
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml_config)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} in ", style().paint("🌍")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:13:12 +02:00
|
|
|
fn style() -> Style {
|
|
|
|
Color::Green.bold().dimmed()
|
|
|
|
}
|
|
|
|
}
|