2019-05-20 02:26:12 +00:00
|
|
|
use ansi_term::{Color, Style};
|
|
|
|
use std::env;
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
use super::{Context, Module};
|
|
|
|
|
|
|
|
/// Creates a segment with the current user's username
|
|
|
|
///
|
|
|
|
/// Will display the usename if any of the following criteria are met:
|
|
|
|
/// - The current user isn't the same as the one that is logged in ($LOGNAME != $USER)
|
|
|
|
/// - The current user is root (UID = 0)
|
|
|
|
/// - The user is currently connected as an SSH session ($SSH_CONNECTION)
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-05-20 02:26:12 +00:00
|
|
|
let user = env::var("USER").ok();
|
|
|
|
let logname = env::var("LOGNAME").ok();
|
|
|
|
let ssh_connection = env::var("SSH_CONNECTION").ok();
|
|
|
|
|
|
|
|
let mut module_color = Color::Yellow.bold();
|
|
|
|
|
|
|
|
if user != logname || ssh_connection.is_some() || is_root(&mut module_color) {
|
2019-07-02 20:12:53 +00:00
|
|
|
let mut module = context.new_module("username")?;
|
2019-05-20 02:26:12 +00:00
|
|
|
module.set_style(module_color);
|
|
|
|
module.new_segment("username", user?);
|
|
|
|
|
|
|
|
return Some(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_uid() -> Option<u32> {
|
|
|
|
match Command::new("id").arg("-u").output() {
|
|
|
|
Ok(output) => String::from_utf8(output.stdout)
|
|
|
|
.map(|uid| uid.trim().parse::<u32>().ok())
|
|
|
|
.ok()?,
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_root(style: &mut Style) -> bool {
|
|
|
|
match get_uid() {
|
|
|
|
Some(uid) if uid == 0 => {
|
|
|
|
style.clone_from(&Color::Red.bold());
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|