Better way to check for root user

This commit is contained in:
Candifloss 2026-01-22 22:22:54 +05:30
parent a2b1b1fbfb
commit e3b71a2afb
4 changed files with 17 additions and 10 deletions

7
Cargo.lock generated
View File

@ -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]]

View File

@ -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"

View File

@ -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()

View File

@ -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)