refactor(username): reformat code to match docs (#2517)

This commit is contained in:
Dario Vladović 2021-03-27 22:13:20 +01:00 committed by GitHub
parent 1336944ab7
commit 404b4f3d0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 41 deletions

View File

@ -12,20 +12,25 @@ const USERNAME_ENV_VAR: &str = "USERNAME";
/// Creates a module with the current user's username /// Creates a module with the current user's username
/// ///
/// Will display the username if any of the following criteria are met: /// Will display the username 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) [1]
/// - The current user is root (UID = 0) /// - The current user isn't the same as the one that is logged in (`$LOGNAME` != `$USER`) [2]
/// - The user is currently connected as an SSH session (`$SSH_CONNECTION`) /// - The user is currently connected as an SSH session (`$SSH_CONNECTION`) [3]
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let username = context.get_env(USERNAME_ENV_VAR)?; let username = context.get_env(USERNAME_ENV_VAR)?;
let logname = context.get_env("LOGNAME");
let is_root = is_root_user();
let is_not_login = logname.is_some() && username != logname.unwrap();
let mut module = context.new_module("username"); let mut module = context.new_module("username");
let config: UsernameConfig = UsernameConfig::try_load(module.config); let config: UsernameConfig = UsernameConfig::try_load(module.config);
if is_not_login || is_ssh_connection(&context) || is_root || config.show_always { let is_root = is_root_user();
let show_username = config.show_always
|| is_root // [1]
|| !is_login_user(&context, &username) // [2]
|| is_ssh_session(&context); // [3]
if !show_username {
return None;
}
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_style(|variable| match variable { .map_style(|variable| match variable {
@ -54,9 +59,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}); });
Some(module) Some(module)
} else { }
None
} fn is_login_user(context: &Context, username: &str) -> bool {
context
.get_env("LOGNAME")
.map(|logname| logname == username)
.unwrap_or(true)
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@ -66,13 +75,12 @@ fn is_root_user() -> bool {
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
fn is_root_user() -> bool { fn is_root_user() -> bool {
let user_uid = nix::unistd::geteuid(); nix::unistd::geteuid() == nix::unistd::ROOT
user_uid == nix::unistd::ROOT
} }
fn is_ssh_connection(context: &Context) -> bool { fn is_ssh_session(context: &Context) -> bool {
let ssh_env = ["SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY"]; let ssh_env = ["SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY"];
ssh_env.iter().any(|env| context.get_env(env).is_some()) ssh_env.iter().any(|env| context.get_env_os(env).is_some())
} }
#[cfg(test)] #[cfg(test)]