2020-07-08 00:45:32 +02:00
|
|
|
use super::{Context, Module};
|
2019-09-04 19:03:31 +02:00
|
|
|
use std::ffi::OsString;
|
|
|
|
|
2019-10-10 10:21:52 +02:00
|
|
|
use crate::config::RootModuleConfig;
|
|
|
|
use crate::configs::hostname::HostnameConfig;
|
2020-07-08 00:45:32 +02:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-10 10:21:52 +02:00
|
|
|
|
2019-09-04 19:03:31 +02: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-10 01:14:38 +02:00
|
|
|
let mut module = context.new_module("hostname");
|
2019-10-10 10:21:52 +02:00
|
|
|
let config: HostnameConfig = HostnameConfig::try_load(module.config);
|
2019-09-04 19:03:31 +02:00
|
|
|
|
2020-08-07 21:13:12 +02:00
|
|
|
let ssh_connection = context.get_env("SSH_CONNECTION");
|
2019-10-10 10:21:52 +02:00
|
|
|
if config.ssh_only && ssh_connection.is_none() {
|
2019-09-04 19:03:31 +02:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let os_hostname: OsString = gethostname::gethostname();
|
|
|
|
|
|
|
|
let host = match os_hostname.into_string() {
|
|
|
|
Ok(host) => host,
|
|
|
|
Err(bad) => {
|
|
|
|
log::debug!("hostname is not valid UTF!\n{:?}", bad);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-14 18:22:25 +02: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
|
|
|
|
let host = if config.trim_at != "" {
|
|
|
|
if let Some(index) = host.find(config.trim_at) {
|
|
|
|
host.split_at(index).0
|
|
|
|
} else {
|
|
|
|
host.as_ref()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
host.as_ref()
|
|
|
|
};
|
|
|
|
|
2020-07-08 00:45:32 +02:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"hostname" => Some(Ok(host)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `hostname`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-09-04 19:03:31 +02:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
2020-08-07 21:13:12 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
use ansi_term::{Color, Style};
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
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."
|
|
|
|
);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh_only_false() -> io::Result<()> {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("on {} ", style().paint(hostname)));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_ssh() -> io::Result<()> {
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = true
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh() -> io::Result<()> {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = true
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.env("SSH_CONNECTION", "something")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("on {} ", style().paint(hostname)));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_trim_at() -> io::Result<()> {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = ""
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("on {} ", style().paint(hostname)));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trim_at() -> io::Result<()> {
|
|
|
|
let hostname = get_hostname!();
|
|
|
|
let (remainder, trim_at) = hostname.split_at(1);
|
|
|
|
let actual = ModuleRenderer::new("hostname")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[hostname]
|
|
|
|
ssh_only = false
|
|
|
|
trim_at = trim_at
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("on {} ", style().paint(remainder)));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn style() -> Style {
|
|
|
|
Color::Green.bold().dimmed()
|
|
|
|
}
|
|
|
|
}
|