implement color_eyre; main implements Result

This commit is contained in:
raf 2024-08-03 20:00:58 +03:00
parent 5ed144719c
commit f5697754e5
Signed by: NotAShelf
GPG key ID: AF26552424E53993
3 changed files with 169 additions and 25 deletions

127
Cargo.lock generated
View file

@ -2,15 +2,142 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "backtrace"
version = "0.3.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "cc"
version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "color-eyre"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5"
dependencies = [
"backtrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
]
[[package]]
name = "eyre"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "fs_extra"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]]
name = "gimli"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "microfetch"
version = "0.1.0"
dependencies = [
"color-eyre",
"fs_extra",
"libc",
]
[[package]]
name = "miniz_oxide"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08"
dependencies = [
"adler",
]
[[package]]
name = "object"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"

View file

@ -5,5 +5,8 @@ edition = "2021"
[dependencies]
libc = "0.2"
fs_extra = "1.2"
color-eyre = { version = "0.6", default-features = false }

View file

@ -1,8 +1,9 @@
use color_eyre::{Report, Result};
use libc::statfs as libc_statfs_struct;
use std::env;
use std::ffi::CString;
use std::fs::{self, File};
use std::io::{self, BufRead};
use std::io::{self, BufRead, Read};
use std::path::Path;
use std::process::Command;
@ -13,17 +14,20 @@ const GREEN: &str = "\x1b[32m";
const YELLOW: &str = "\x1b[33m";
const RED: &str = "\x1b[31m";
fn main() {
fn main() -> Result<(), Report> {
color_eyre::install()?;
let user_info = get_username_and_hostname().expect("Failed to get username and hostname");
let os_name = get_os_pretty_name().expect("Failed to get OS name");
let kernel_version = get_kernel_info().expect("Failed to get kernel info");
let kernel_version = get_system_info().expect("Failed to get kernel info");
let uptime = get_system_uptime().expect("Failed to get uptime");
let window_manager = get_desktop_info().expect("Failed to get desktop info");
let memory_usage = get_memory_usage().expect("Failed to get memory usage");
let storage = get_disk_usage().expect("Failed to get storage info");
// Construct the ASCII art with dynamic OS name
let ascii_art = format!(
println!(
"
{CYAN} {BLUE} {} ~{RESET}
{CYAN} 🬸{BLUE}🬸 {CYAN} {CYAN} {BLUE}System{RESET} {}
@ -36,7 +40,7 @@ fn main() {
user_info, os_name, kernel_version, uptime, window_manager, memory_usage, storage
);
println!("{}", ascii_art);
Ok(())
}
fn get_username_and_hostname() -> Result<String, io::Error> {
@ -192,28 +196,38 @@ fn get_system_uptime() -> Result<String, io::Error> {
Ok(parts.join(", "))
}
fn get_kernel_info() -> Result<String, Box<dyn std::error::Error>> {
let uname_s = Command::new("uname")
.arg("-s")
.output()
.expect("Failed to execute command");
let uname_r = Command::new("uname")
.arg("-r")
.output()
.expect("Failed to execute command");
let uname_m = Command::new("uname")
.arg("-m")
.output()
.expect("Failed to execute command");
let kernel_name = String::from_utf8_lossy(&uname_s.stdout).trim().to_string();
let kernel_release = String::from_utf8_lossy(&uname_r.stdout).trim().to_string();
let machine_hardware = String::from_utf8_lossy(&uname_m.stdout).trim().to_string();
Ok(format!(
"{} {} ({})",
kernel_name, kernel_release, machine_hardware
))
fn get_system_info() -> Result<String, io::Error> {
// Try to detect OS type as accurately as possible and without depending on uname.
// /etc/os-release should generally imply Linux, and /etc/bsd-release would imply BSD system.
fn detect_os() -> Result<String, io::Error> {
if fs::metadata("/etc/os-release").is_ok() || fs::metadata("/usr/lib/os-release").is_ok() {
Ok("Linux".to_string())
} else if fs::metadata("/etc/rc.conf").is_ok() || fs::metadata("/etc/bsd-release").is_ok() {
Ok("BSD".to_string())
} else {
Ok("Unknown".to_string())
}
}
let system = detect_os()?;
let mut kernel_release = String::new();
fs::File::open("/proc/sys/kernel/osrelease")?.read_to_string(&mut kernel_release)?;
let kernel_release = kernel_release.trim().to_string(); // Remove any trailing newline
let mut cpuinfo = String::new();
fs::File::open("/proc/cpuinfo")?.read_to_string(&mut cpuinfo)?;
let architecture = if let Some(line) = cpuinfo.lines().find(|line| line.contains("flags")) {
if line.contains("lm") {
"x86_64".to_string()
} else {
"unknown".to_string()
}
} else {
"unknown".to_string()
};
let result = format!("{} {} ({})", system, kernel_release, architecture);
Ok(result)
}