diff --git a/Cargo.lock b/Cargo.lock index 91515a5..b6fa8b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,11 +11,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "libc" +version = "0.2.180" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" + [[package]] name = "prettyprompt" version = "0.3.0" dependencies = [ "ansi_term", + "libc", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d5a3f3c..7c0c206 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,11 @@ [package] name = "prettyprompt" version = "0.3.0" -edition = "2021" +edition = "2024" [dependencies] ansi_term = "0.12" +libc = "0.2.180" [[bin]] name = "prettyprompt" diff --git a/src/indicators/pwd.rs b/src/indicators/pwd.rs index 21fb3a3..4f0f320 100644 --- a/src/indicators/pwd.rs +++ b/src/indicators/pwd.rs @@ -1,4 +1,4 @@ -use crate::indicators::git::{repo_name, MAIN_COL}; +use crate::indicators::git::{MAIN_COL, repo_name}; use ansi_term::ANSIGenericString; use ansi_term::Colour::RGB; use std::env::current_dir; @@ -77,7 +77,7 @@ pub fn pwd( format!( "{}{}{}", REPO_COL.paint(repo_name), // Repo name - MAIN_COL.paint(" \u{F02A2} "), // Seperator + MAIN_COL.paint(" \u{F02A2} "), // Separator PATH_COL.italic().paint(path) // Sub-directory ) .into() diff --git a/src/indicators/user.rs b/src/indicators/user.rs index 451303f..50eee6d 100644 --- a/src/indicators/user.rs +++ b/src/indicators/user.rs @@ -1,21 +1,20 @@ use ansi_term::ANSIGenericString; use ansi_term::Colour::RGB; -use std::env::var; // For environment variables + +use libc::geteuid; pub const USER_SYMBOL: &str = "\u{276F}"; // User indicator symbol: "❯" pub const ROOT_COL: ansi_term::Colour = RGB(255, 53, 94); // If the user is root pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); // Regular user -const ROOT_USER: &str = "root"; // Root username constant -/// Username of current user -pub fn username() -> String { - var("USER").unwrap_or_else(|_| "UnknownUser".to_string()) +// Check if the current user is root +pub fn is_root() -> bool { + unsafe { geteuid() == 0 } // Uid for root is 0 } /// Root user indicator pub fn indicator() -> ANSIGenericString<'static, str> { - let user = username(); - if user == ROOT_USER { + if is_root() { ROOT_COL.paint(USER_SYMBOL) } else { NORMIE_COL.paint(USER_SYMBOL)