refactor(username): reformat code to match docs (#2517)
This commit is contained in:
parent
1336944ab7
commit
404b4f3d0c
|
@ -12,51 +12,60 @@ 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 parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
let show_username = config.show_always
|
||||||
formatter
|
|| is_root // [1]
|
||||||
.map_style(|variable| match variable {
|
|| !is_login_user(&context, &username) // [2]
|
||||||
"style" => {
|
|| is_ssh_session(&context); // [3]
|
||||||
let module_style = if is_root {
|
|
||||||
config.style_root
|
|
||||||
} else {
|
|
||||||
config.style_user
|
|
||||||
};
|
|
||||||
Some(Ok(module_style))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.map(|variable| match variable {
|
|
||||||
"user" => Some(Ok(&username)),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.parse(None)
|
|
||||||
});
|
|
||||||
module.set_segments(match parsed {
|
|
||||||
Ok(segments) => segments,
|
|
||||||
Err(error) => {
|
|
||||||
log::warn!("Error in module `username`:\n{}", error);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Some(module)
|
if !show_username {
|
||||||
} else {
|
return None;
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||||
|
formatter
|
||||||
|
.map_style(|variable| match variable {
|
||||||
|
"style" => {
|
||||||
|
let module_style = if is_root {
|
||||||
|
config.style_root
|
||||||
|
} else {
|
||||||
|
config.style_user
|
||||||
|
};
|
||||||
|
Some(Ok(module_style))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.map(|variable| match variable {
|
||||||
|
"user" => Some(Ok(&username)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.parse(None)
|
||||||
|
});
|
||||||
|
module.set_segments(match parsed {
|
||||||
|
Ok(segments) => segments,
|
||||||
|
Err(error) => {
|
||||||
|
log::warn!("Error in module `username`:\n{}", error);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Some(module)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)]
|
||||||
|
|
Loading…
Reference in New Issue