mirror of
https://github.com/NotAShelf/microfetch.git
synced 2025-02-27 21:41:33 +00:00
Compare commits
No commits in common. "927f6077b4b78cd112bc6932aab5287edc413ce8" and "17152f9d142ce0badde14018703bb1590e3df947" have entirely different histories.
927f6077b4
...
17152f9d14
3 changed files with 58 additions and 49 deletions
|
@ -4,7 +4,7 @@ version = "0.3.3"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nix = { version = "0.29", features = ["fs", "hostname", "feature"] }
|
nix = {version = "0.29", features = ["fs", "hostname"]}
|
||||||
color-eyre = { version = "0.6", default-features = false }
|
color-eyre = { version = "0.6", default-features = false }
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
|
72
src/main.rs
72
src/main.rs
|
@ -16,40 +16,43 @@ use nix::sys::sysinfo::sysinfo;
|
||||||
fn main() -> Result<(), Report> {
|
fn main() -> Result<(), Report> {
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
|
|
||||||
let fields = Fields {
|
let user_info = get_username_and_hostname()?;
|
||||||
user_info: get_username_and_hostname()?,
|
let os_name = get_os_pretty_name()?;
|
||||||
os_name: get_os_pretty_name()?,
|
let kernel_version = get_system_info()?;
|
||||||
kernel_version: get_system_info()?,
|
let shell = get_shell()?;
|
||||||
shell: get_shell()?,
|
let uptime = get_current()?;
|
||||||
uptime: get_current()?,
|
let window_manager = get_desktop_info()?;
|
||||||
window_manager: get_desktop_info()?,
|
let sys_info = sysinfo()?;
|
||||||
memory_usage: get_memory_usage(sysinfo()?),
|
let memory_usage = get_memory_usage(sys_info);
|
||||||
storage: get_root_disk_usage()?,
|
let storage = get_root_disk_usage()?;
|
||||||
colors: print_dots()?,
|
let colors = print_dots()?;
|
||||||
};
|
|
||||||
|
|
||||||
print_system_info(&fields);
|
print_system_info(
|
||||||
|
&user_info,
|
||||||
|
&os_name,
|
||||||
|
&kernel_version,
|
||||||
|
&shell,
|
||||||
|
&uptime,
|
||||||
|
&window_manager,
|
||||||
|
&memory_usage,
|
||||||
|
&storage,
|
||||||
|
&colors,
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Struct to hold all the fields we need to print
|
fn print_system_info(
|
||||||
// helps avoid clippy warnings about argument count
|
user_info: &str,
|
||||||
// and makes it easier to pass around, though its
|
os_name: &str,
|
||||||
// not like we need to
|
kernel_version: &str,
|
||||||
struct Fields {
|
shell: &str,
|
||||||
user_info: String,
|
uptime: &str,
|
||||||
os_name: String,
|
window_manager: &str,
|
||||||
kernel_version: String,
|
memory_usage: &str,
|
||||||
shell: String,
|
storage: &str,
|
||||||
uptime: String,
|
colors: &str,
|
||||||
window_manager: String,
|
) {
|
||||||
memory_usage: String,
|
|
||||||
storage: String,
|
|
||||||
colors: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_system_info(fields: &Fields) {
|
|
||||||
println!(
|
println!(
|
||||||
"
|
"
|
||||||
{CYAN} ▟█▖ {BLUE}▝█▙ ▗█▛ {user_info} ~{RESET}
|
{CYAN} ▟█▖ {BLUE}▝█▙ ▗█▛ {user_info} ~{RESET}
|
||||||
|
@ -61,15 +64,6 @@ fn print_system_info(fields: &Fields) {
|
||||||
{BLUE} ▝█▛ {CYAN}██▖{BLUE}▗▄▄▄▄▄▄▄▄▄▄▄ {CYAN} {BLUE}Memory{RESET} {memory_usage}
|
{BLUE} ▝█▛ {CYAN}██▖{BLUE}▗▄▄▄▄▄▄▄▄▄▄▄ {CYAN} {BLUE}Memory{RESET} {memory_usage}
|
||||||
{BLUE} ▝ {CYAN}▟█▜█▖{BLUE}▀▀▀▀▀██▛▀▀▘ {CYAN} {BLUE}Storage (/){RESET} {storage}
|
{BLUE} ▝ {CYAN}▟█▜█▖{BLUE}▀▀▀▀▀██▛▀▀▘ {CYAN} {BLUE}Storage (/){RESET} {storage}
|
||||||
{CYAN} ▟█▘ ▜█▖ {BLUE}▝█▛ {CYAN} {BLUE}Colors{RESET} {colors}
|
{CYAN} ▟█▘ ▜█▖ {BLUE}▝█▛ {CYAN} {BLUE}Colors{RESET} {colors}
|
||||||
",
|
"
|
||||||
user_info = fields.user_info,
|
|
||||||
os_name = fields.os_name,
|
|
||||||
kernel_version = fields.kernel_version,
|
|
||||||
shell = fields.shell,
|
|
||||||
uptime = fields.uptime,
|
|
||||||
window_manager = fields.window_manager,
|
|
||||||
memory_usage = fields.memory_usage,
|
|
||||||
storage = fields.storage,
|
|
||||||
colors = fields.colors,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,30 @@
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use std::fs::read_to_string;
|
use std::fs::{self, read_to_string};
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
pub fn get_system_info() -> nix::Result<String> {
|
// Try to detect OS type as accurately as possible and without depending on uname.
|
||||||
let utsname = nix::sys::utsname::uname()?;
|
// /etc/os-release should generally imply Linux, and /etc/bsd-release would imply BSD system.
|
||||||
Ok(format!(
|
fn detect_os() -> Result<&'static str, io::Error> {
|
||||||
"{} {} ({})",
|
if fs::metadata("/etc/os-release").is_ok() || fs::metadata("/usr/lib/os-release").is_ok() {
|
||||||
utsname.sysname().to_str().unwrap_or("Unknown"),
|
Ok("Linux")
|
||||||
utsname.release().to_str().unwrap_or("Unknown"),
|
} else if fs::metadata("/etc/rc.conf").is_ok() || fs::metadata("/etc/bsd-release").is_ok() {
|
||||||
utsname.machine().to_str().unwrap_or("Unknown")
|
Ok("BSD")
|
||||||
))
|
} else {
|
||||||
|
Ok("Unknown")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_system_info() -> Result<String, io::Error> {
|
||||||
|
let system = detect_os()?;
|
||||||
|
|
||||||
|
let kernel_release = read_to_string("/proc/sys/kernel/osrelease")?;
|
||||||
|
let kernel_release = kernel_release.trim();
|
||||||
|
|
||||||
|
let architecture = read_to_string("/proc/sys/kernel/arch")?;
|
||||||
|
let architecture = architecture.trim();
|
||||||
|
|
||||||
|
let result = format!("{system} {kernel_release} ({architecture})");
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue