mirror of
https://github.com/NotAShelf/microfetch.git
synced 2024-11-01 06:41:15 +00:00
cleanup
This commit is contained in:
parent
6dafa776d2
commit
0e4b2d2ac1
4 changed files with 67 additions and 24 deletions
|
@ -9,5 +9,18 @@ pub fn get_desktop_info() -> Result<String, io::Error> {
|
|||
// instead of just "foo"
|
||||
let desktop_env = desktop_env.trim_start_matches("none+");
|
||||
|
||||
Ok(format!("{desktop_env} ({display_backend})"))
|
||||
// Use "Unknown" if desktop_env or display_backend is empty
|
||||
let desktop_env = if desktop_env.is_empty() {
|
||||
"Unknown"
|
||||
} else {
|
||||
desktop_env
|
||||
};
|
||||
|
||||
let display_backend = if display_backend.is_empty() {
|
||||
"Unknown"
|
||||
} else {
|
||||
&display_backend
|
||||
};
|
||||
|
||||
Ok(format!("{} ({})", desktop_env, display_backend))
|
||||
}
|
||||
|
|
50
src/main.rs
50
src/main.rs
|
@ -15,16 +15,36 @@ use crate::uptime::get_system_uptime;
|
|||
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_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_root_disk_usage().expect("Failed to get storage info");
|
||||
let user_info = get_username_and_hostname()?;
|
||||
let os_name = get_os_pretty_name()?;
|
||||
let kernel_version = get_system_info()?;
|
||||
let uptime = get_system_uptime()?;
|
||||
let window_manager = get_desktop_info()?;
|
||||
let memory_usage = get_memory_usage()?;
|
||||
let storage = get_root_disk_usage()?;
|
||||
|
||||
// Construct the ASCII art with dynamic OS name
|
||||
print_system_info(
|
||||
&user_info,
|
||||
&os_name,
|
||||
&kernel_version,
|
||||
&uptime,
|
||||
&window_manager,
|
||||
&memory_usage,
|
||||
&storage,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_system_info(
|
||||
user_info: &str,
|
||||
os_name: &str,
|
||||
kernel_version: &str,
|
||||
uptime: &str,
|
||||
window_manager: &str,
|
||||
memory_usage: &str,
|
||||
storage: &str,
|
||||
) {
|
||||
println!(
|
||||
"
|
||||
{CYAN} ▗▄ {BLUE}▗▄ ▄▖ {user_info} ~{RESET}
|
||||
|
@ -34,8 +54,16 @@ fn main() -> Result<(), Report> {
|
|||
{BLUE} 🬷▛🮃{CYAN}▙ ▟▛ {CYAN} {BLUE}WM{RESET} {window_manager}
|
||||
{BLUE} 🮃 {CYAN}▟█🬴{BLUE}▀▀▀█🬴▀▀ {CYAN} {BLUE}Memory{RESET} {memory_usage}
|
||||
{CYAN} ▝▀ ▀▘ {BLUE}▀▘ {CYAN} {BLUE}Storage (/){RESET} {storage}
|
||||
"
|
||||
",
|
||||
CYAN = CYAN,
|
||||
BLUE = BLUE,
|
||||
RESET = RESET,
|
||||
user_info = user_info,
|
||||
os_name = os_name,
|
||||
kernel_version = kernel_version,
|
||||
uptime = uptime,
|
||||
window_manager = window_manager,
|
||||
memory_usage = memory_usage,
|
||||
storage = storage
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use color_eyre::Result;
|
||||
use color_eyre::{Result};
|
||||
use std::fs::{self, read_to_string};
|
||||
use std::io::{self, Read};
|
||||
|
||||
|
@ -38,11 +38,10 @@ pub fn get_system_info() -> Result<String, io::Error> {
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn get_os_pretty_name() -> Option<String> {
|
||||
let os_release_content = read_to_string("/etc/os-release").ok()?;
|
||||
let os_release_lines: Vec<&str> = os_release_content.lines().collect();
|
||||
let pretty_name = os_release_lines
|
||||
.iter()
|
||||
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
||||
let os_release_content = read_to_string("/etc/os-release")?;
|
||||
let pretty_name = os_release_content
|
||||
.lines()
|
||||
.find(|line| line.starts_with("PRETTY_NAME="))
|
||||
.map(|line| {
|
||||
line.trim_start_matches("PRETTY_NAME=")
|
||||
|
@ -50,5 +49,8 @@ pub fn get_os_pretty_name() -> Option<String> {
|
|||
.to_string()
|
||||
});
|
||||
|
||||
pretty_name
|
||||
match pretty_name {
|
||||
Some(name) => Ok(name),
|
||||
None => Ok("Unknown".to_string()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ pub fn get_username_and_hostname() -> Result<String, io::Error> {
|
|||
Ok(format!("{YELLOW}{username}{RED}@{GREEN}{hostname}"))
|
||||
}
|
||||
|
||||
pub fn get_root_disk_usage() -> Result<String, Box<dyn std::error::Error>> {
|
||||
pub fn get_root_disk_usage() -> Result<String, io::Error> {
|
||||
let vfs = statvfs("/")?;
|
||||
let block_size = vfs.block_size() as u64;
|
||||
let total_blocks = vfs.blocks();
|
||||
|
@ -25,12 +25,12 @@ pub fn get_root_disk_usage() -> Result<String, Box<dyn std::error::Error>> {
|
|||
let total_size = block_size * total_blocks;
|
||||
let used_size = total_size - (block_size * available_blocks);
|
||||
|
||||
let total_size_gib = total_size as f64 / (1024.0 * 1024.0 * 1024.0);
|
||||
let used_size_gib = used_size as f64 / (1024.0 * 1024.0 * 1024.0);
|
||||
let usage_percentage = (used_size as f64 / total_size as f64) * 100.0;
|
||||
let total_size = total_size as f64 / (1024.0 * 1024.0 * 1024.0);
|
||||
let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0);
|
||||
let usage = (used_size as f64 / total_size as f64) * 100.0;
|
||||
|
||||
Ok(format!(
|
||||
"{used_size_gib:.2} GiB / {total_size_gib:.2} GiB ({CYAN}{usage_percentage:.0}%{RESET})"
|
||||
"{used_size:.2} GiB / {total_size:.2} GiB ({CYAN}{usage:.0}%{RESET})"
|
||||
))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue