diff --git a/Cargo.toml b/Cargo.toml index 2142ee1..daec654 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,3 +57,4 @@ rusqlite = { version = "0.26.3", default-features = false } ureq = { version = "2.4.0", default-features = false, features = ["native-tls", "json"] } serde = { version = "1.0.90", default-features = false, features = ["derive", "serde_derive"] } native-tls = "0.2.8" +libc = "0.2.123" diff --git a/src/internal/commands.rs b/src/internal/commands.rs index d9270a2..6020912 100644 --- a/src/internal/commands.rs +++ b/src/internal/commands.rs @@ -1,4 +1,5 @@ use crate::internal::error::{AppError, AppResult}; +use crate::internal::is_tty; use std::ffi::{OsStr, OsString}; use std::process::{Child, Command, ExitStatus, Stdio}; @@ -18,7 +19,13 @@ pub struct ShellCommand { impl ShellCommand { pub fn pacman() -> Self { - Self::new("pacman") + let pacman_cmd = Self::new("pacman"); + + if is_tty() { + pacman_cmd.arg("--color=always") + } else { + pacman_cmd + } } pub fn makepkg() -> Self { diff --git a/src/internal/mod.rs b/src/internal/mod.rs index fbc6619..5f8be49 100644 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -38,3 +38,9 @@ pub fn uwu_enabled() -> bool { pub fn uwu_debug_enabled() -> bool { env::var("AME_UWU_DEBUG").unwrap_or_else(|_| "".to_string()) == "true" } + +/// Checks if we're running in a tty. If we do we can assume that +/// the output can safely be colorized. +pub fn is_tty() -> bool { + (unsafe { libc::isatty(libc::STDIN_FILENO as i32) } != 0) +} diff --git a/src/main.rs b/src/main.rs index 4d20366..a9742b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,11 +17,7 @@ mod internal; mod operations; fn main() { - extern "C" { - fn geteuid() -> u32; - } - - if unsafe { geteuid() } == 0 { + if unsafe { libc::geteuid() } == 0 { crash("Running amethyst as root is disallowed as it can lead to system breakage. Instead, amethyst will prompt you when it needs superuser permissions".to_string(), AppExitCode::RunAsRoot); }