Skip to content

Commit

Permalink
feature: Added possibility to have different symbol for last characte…
Browse files Browse the repository at this point in the history
…r of the prompt if user is root.
  • Loading branch information
ironiq committed Dec 3, 2023
1 parent 64ca079 commit 6616567
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/configs/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub struct CharacterConfig<'a> {
pub format: &'a str,
pub success_symbol: &'a str,
pub error_symbol: &'a str,
pub root_success_symbol: &'a str,
pub root_error_symbol: &'a str,
#[serde(alias = "vicmd_symbol")]
pub vimcmd_symbol: &'a str,
pub vimcmd_visual_symbol: &'a str,
Expand All @@ -25,6 +27,8 @@ impl<'a> Default for CharacterConfig<'a> {
format: "$symbol ",
success_symbol: "[❯](bold green)",
error_symbol: "[❯](bold red)",
root_success_symbol: "[❯](bold blue)",
root_error_symbol: "[❯](bold purple)",
vimcmd_symbol: "[❮](bold green)",
vimcmd_visual_symbol: "[❮](bold yellow)",
vimcmd_replace_symbol: "[❮](bold purple)",
Expand Down
61 changes: 56 additions & 5 deletions src/modules/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::formatter::StringFormatter;
/// The character segment prints an arrow character in a color dependent on the
/// exit-code of the last executed command:
/// - If the exit-code was "0", it will be formatted with `success_symbol`
/// (green arrow by default)
/// (green arrow by default) or `root_success_symbol` (blue arrow by default)
/// - If the exit-code was anything else, it will be formatted with
/// `error_symbol` (red arrow by default)
/// `error_symbol` (red arrow by default) or `root_error_symbol` (purple arrow
/// by default)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
enum ShellEditMode {
Normal,
Expand All @@ -21,6 +22,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
const ASSUMED_MODE: ShellEditMode = ShellEditMode::Insert;
// TODO: extend config to more modes

let is_root = is_root_user();

let mut module = context.new_module("character");
let config: CharacterConfig = CharacterConfig::try_load(module.config);

Expand Down Expand Up @@ -50,10 +53,18 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
ShellEditMode::Replace => config.vimcmd_replace_symbol,
ShellEditMode::ReplaceOne => config.vimcmd_replace_one_symbol,
ShellEditMode::Insert => {
if exit_success {
config.success_symbol
if is_root {
if exit_success {
config.root_success_symbol
} else {
config.root_error_symbol
}
} else {
config.error_symbol
if exit_success {
config.success_symbol
} else {
config.error_symbol
}
}
}
};
Expand All @@ -78,6 +89,40 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module)
}

#[cfg(all(target_os = "windows", not(test)))]
fn is_root_user() -> bool {
use deelevate::{PrivilegeLevel, Token};
let token = match Token::with_current_process() {
Ok(token) => token,
Err(e) => {
log::warn!("Failed to get process token: {e:?}");
return false;
}
};
matches!(
match token.privilege_level() {
Ok(level) => level,
Err(e) => {
log::warn!("Failed to get privilege level: {e:?}");
return false;
}
},
PrivilegeLevel::Elevated | PrivilegeLevel::HighIntegrityAdmin
)
}

#[cfg(all(target_os = "windows", test))]
#[allow(unused)]
fn is_root_user() -> bool {
false
}

#[cfg(not(target_os = "windows"))]
#[allow(unused)]
fn is_root_user() -> bool {
nix::unistd::geteuid() == nix::unistd::ROOT
}

#[cfg(test)]
mod test {
use crate::context::Shell;
Expand Down Expand Up @@ -116,13 +161,17 @@ mod test {

let exit_values = [1, 54321, -5000];

// TODO: test if user is root

// Test failure values
for status in &exit_values {
let actual = ModuleRenderer::new("character")
.config(toml::toml! {
[character]
success_symbol = "[➜](bold green)"
error_symbol = "[✖](bold red)"
root_success_symbol = "[➜](bold blue)"
root_error_symbol = "[✖](bold purple)"
})
.status(*status)
.collect();
Expand All @@ -135,6 +184,8 @@ mod test {
[character]
success_symbol = "[➜](bold green)"
error_symbol = "[✖](bold red)"
root_success_symbol = "[➜](bold blue)"
root_error_symbol = "[✖](bold purple)"
})
.status(0)
.collect();
Expand Down

0 comments on commit 6616567

Please sign in to comment.