bunbun/src/main.rs

110 lines
2.9 KiB
Rust
Raw Normal View History

2024-03-31 18:49:58 +01:00
use clap::Parser;
2024-03-23 21:58:32 +00:00
use colored::Colorize;
use std::{env::var, path::PathBuf};
2024-03-23 22:35:06 +00:00
use sysinfo::System;
use whoami::*;
/// A simple and adorable sysinfo utility written in Rust.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Show ASCII art only
#[arg(short, long)]
ascii_only: bool,
/// Show CPU architecture
#[arg(short = 'x', long)]
arch: bool,
/// Show the kernel version
#[arg(short = 'k', long)]
kernel: bool,
2024-03-31 18:49:58 +01:00
/// Hide terminal colours
#[arg(short = 'z', long, default_value_t = false)]
hide_colours: bool,
}
// Display the CPU architecture
fn cpu_arch(args: &Args) {
if args.arch {
let arch = arch();
println!("{:>17} {}", "Arch".cyan().bold(), arch);
}
}
2024-03-23 21:58:32 +00:00
// Display the kernel version
fn display_kernel(args: &Args) {
if args.kernel {
let kernel = System::kernel_version().unwrap_or(String::from("N/A"));
println!("{:>19} {}", "Kernel".yellow().bold(), kernel);
}
}
2024-03-23 21:58:32 +00:00
fn main() {
let args = Args::parse();
2024-03-23 21:58:32 +00:00
let bottom = format!("c({})({})", "\"".red(), "\"".red()).to_string();
2024-03-23 23:30:23 +00:00
let ascii = ["(\\ /)", "( . .)", &bottom];
2024-03-23 21:58:32 +00:00
2024-03-23 23:17:07 +00:00
let hostname = fallible::hostname().unwrap_or(String::from("N/A"));
let user = username();
2024-03-23 23:17:07 +00:00
let combined = format!("{}@{}", user.italic(), hostname.italic());
2024-03-23 23:17:07 +00:00
2024-03-23 22:35:06 +00:00
let pretty = distro();
2024-03-24 14:13:36 +00:00
let wm: String;
2024-03-23 22:35:06 +00:00
if cfg!(windows) {
wm = "DWM".to_string();
2024-03-23 22:35:06 +00:00
} else if cfg!(unix) {
let xdg_current_desktop = var("XDG_CURRENT_DESKTOP");
let desktop = desktop_env().to_string();
2024-03-24 20:40:30 +00:00
if desktop != "Unknown: Unknown" {
wm = desktop;
} else if xdg_current_desktop.is_ok() {
wm = xdg_current_desktop.unwrap();
2024-03-24 20:40:30 +00:00
} else {
wm = "N/A".to_string();
}
} else {
2024-03-24 14:13:36 +00:00
wm = "N/A".to_string();
2024-03-23 21:58:32 +00:00
}
2024-03-23 22:35:06 +00:00
let shell_path = PathBuf::from(var("SHELL").unwrap_or(String::from("N/A")));
2024-03-31 18:49:58 +01:00
let shell = shell_path
.file_name()
.expect("Could not get $SHELL path")
.to_str()
.unwrap();
println!();
if !args.ascii_only {
println!("{: <13}{}", "", combined);
cpu_arch(&args);
display_kernel(&args);
println!("{:>8} {:>6} {}", ascii[0], "OS".red().bold(), pretty);
println!("{:>9} {:>8} {}", ascii[1], "Shell".green().bold(), shell);
println!("{:>28} {:>4} {}", ascii[2], "WM".blue().bold(), wm);
} else {
for i in ascii {
println!(" {}", i);
}
}
2024-03-24 13:50:31 +00:00
if !args.hide_colours && !args.ascii_only {
println!();
2024-03-31 18:49:58 +01:00
let colors = [
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white",
];
let mut color_string: String = " ".to_owned();
for color in colors {
color_string.push_str(&format!("{:>3}", "".color(color)));
}
println!("{}", color_string);
}
2024-03-24 20:06:25 +00:00
println!();
2024-03-23 21:58:32 +00:00
}