2020-07-07 22:45:32 +00:00
|
|
|
use super::{Context, Module};
|
2019-09-04 17:03:31 +00:00
|
|
|
use std::ffi::OsString;
|
|
|
|
|
2022-03-26 09:42:19 +00:00
|
|
|
use crate::config::ModuleConfig;
|
2019-10-10 08:21:52 +00:00
|
|
|
use crate::configs::hostname::HostnameConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-10 08:21:52 +00:00
|
|
|
|
2019-09-04 17:03:31 +00:00
|
|
|
/// Creates a module with the system hostname
|
|
|
|
///
|
|
|
|
/// Will display the hostname if all of the following criteria are met:
|
|
|
|
/// - hostname.disabled is absent or false
|
|
|
|
/// - hostname.ssh_only is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("hostname");
|
2019-10-10 08:21:52 +00:00
|
|
|
let config: HostnameConfig = HostnameConfig::try_load(module.config);
|
2019-09-04 17:03:31 +00:00
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let ssh_connection = context.get_env("SSH_CONNECTION");
|
2019-10-10 08:21:52 +00:00
|
|
|
if config.ssh_only && ssh_connection.is_none() {
|
2019-09-04 17:03:31 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let os_hostname: OsString = gethostname::gethostname();
|
|
|
|
|
|
|
|
let host = match os_hostname.into_string() {
|
|
|
|
Ok(host) => host,
|
|
|
|
Err(bad) => {
|
2020-09-28 20:38:50 +00:00
|
|
|
log::warn!("hostname is not valid UTF!\n{:?}", bad);
|
2019-09-04 17:03:31 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-14 16:22:25 +00: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
|
2020-11-23 19:38:11 +00:00
|
|
|
let host = if !config.trim_at.is_empty() {
|
2019-10-14 16:22:25 +00:00
|
|
|
if let Some(index) = host.find(config.trim_at) {
|
|
|
|
host.split_at(index).0
|
|
|
|
} else {
|
|
|
|
host.as_ref()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
host.as_ref()
|
|
|
|
};
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
2022-05-04 13:30:16 +00:00
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"ssh_symbol" => {
|
|
|
|
if ssh_connection.is_some() {
|
|
|
|
Some(config.ssh_symbol)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
2020-07-07 22:45:32 +00:00
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"hostname" => Some(Ok(host)),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `hostname`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-09-04 17:03:31 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
use ansi_term::{Color, Style};
|
2021-12-09 21:15:25 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
macro_rules! get_hostname {
|
|
|
|
() => {
|
|
|
|
if let Some(hostname) = gethostname::gethostname().into_string().ok() {
|
|
|
|
hostname
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"hostname was not tested because gethostname failed! \
|
|
|
|
This could be caused by your hostname containing invalid UTF."
|
|
|
|
);
|
2021-02-11 20:08:17 +00:00
|
|
|
return;
|
2020-08-07 19:13:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-05-04 13:30:16 +00:00
|
|
|
fn ssh_only_false_no_ssh() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.collect();
|
2020-09-08 16:09:21 +00:00
|
|
|
let expected = Some(format!("{} in ", style().paint(hostname)));
|
2022-05-04 13:30:16 +00:00
|
|
|
println!("{}", expected.as_ref().unwrap());
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
2022-05-04 13:30:16 +00: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)));
|
2020-08-07 19:13:12 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn no_ssh() {
|
2020-08-07 19:13:12 +00: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 20:08:17 +00:00
|
|
|
fn ssh() {
|
2020-08-07 19:13:12 +00: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 13:30:16 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"{} in ",
|
|
|
|
style().paint("🌐 ".to_owned() + &hostname)
|
|
|
|
));
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn no_trim_at() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.collect();
|
2020-09-08 16:09:21 +00:00
|
|
|
let expected = Some(format!("{} in ", style().paint(hostname)));
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn trim_at() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let hostname = get_hostname!();
|
2021-12-09 21:15:25 +00: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 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = trim_at
|
|
|
|
})
|
|
|
|
.collect();
|
2020-09-08 16:09:21 +00:00
|
|
|
let expected = Some(format!("{} in ", style().paint(remainder)));
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn style() -> Style {
|
|
|
|
Color::Green.bold().dimmed()
|
|
|
|
}
|
|
|
|
}
|