2019-09-04 17:03:31 +00:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
use super::{Context, Module};
|
|
|
|
use std::ffi::OsString;
|
|
|
|
|
2019-10-10 08:21:52 +00:00
|
|
|
use crate::config::RootModuleConfig;
|
|
|
|
use crate::configs::hostname::HostnameConfig;
|
|
|
|
|
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
|
|
|
|
|
|
|
let ssh_connection = env::var("SSH_CONNECTION").ok();
|
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) => {
|
|
|
|
log::debug!("hostname is not valid UTF!\n{:?}", bad);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-10 08:21:52 +00:00
|
|
|
module.set_style(config.style);
|
|
|
|
module.new_segment(
|
|
|
|
"hostname",
|
|
|
|
&format!("{}{}{}", config.prefix, host, config.suffix),
|
|
|
|
);
|
2019-09-04 17:03:31 +00:00
|
|
|
module.get_prefix().set_value("on ");
|
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|