mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-01-18 06:08:39 +00:00
Compare commits
7 commits
671ce6b8e5
...
f7d7c73073
| Author | SHA1 | Date | |
|---|---|---|---|
|
f7d7c73073 |
|||
|
28e156b9a7 |
|||
|
|
b814b2bacf |
||
|
|
0233cdc0fc |
||
|
|
88c9ff5e13 |
||
|
|
9713138e94 |
||
|
|
c139026704 |
8 changed files with 29 additions and 33 deletions
6
Cargo.lock
generated
6
Cargo.lock
generated
|
|
@ -262,7 +262,7 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "microfetch"
|
name = "microfetch"
|
||||||
version = "0.4.7"
|
version = "0.4.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"criterion",
|
"criterion",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
|
@ -272,9 +272,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nix"
|
name = "nix"
|
||||||
version = "0.29.0"
|
version = "0.30.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
|
checksum = "537bc3c4a347b87fd52ac6c03a02ab1302962cfd93373c5d7a112cdc337854cc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "microfetch"
|
name = "microfetch"
|
||||||
version = "0.4.7"
|
version = "0.4.8"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
@ -12,7 +12,7 @@ name = "microfetch"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nix = { version = "0.29", features = ["fs", "hostname", "feature"] }
|
nix = { version = "0.30", features = ["fs", "hostname", "feature"] }
|
||||||
lazy_static = "1.5"
|
lazy_static = "1.5"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use criterion::{criterion_group, criterion_main, Criterion};
|
use criterion::{Criterion, criterion_group, criterion_main};
|
||||||
use microfetch_lib::colors::print_dots;
|
use microfetch_lib::colors::print_dots;
|
||||||
use microfetch_lib::desktop::get_desktop_info;
|
use microfetch_lib::desktop::get_desktop_info;
|
||||||
use microfetch_lib::release::{get_os_pretty_name, get_system_info};
|
use microfetch_lib::release::{get_os_pretty_name, get_system_info};
|
||||||
|
|
@ -10,7 +10,7 @@ use microfetch_lib::uptime::get_current;
|
||||||
fn main_benchmark(c: &mut Criterion) {
|
fn main_benchmark(c: &mut Criterion) {
|
||||||
let utsname = nix::sys::utsname::uname().expect("lol");
|
let utsname = nix::sys::utsname::uname().expect("lol");
|
||||||
c.bench_function("user_info", |b| {
|
c.bench_function("user_info", |b| {
|
||||||
b.iter(|| get_username_and_hostname(&utsname))
|
b.iter(|| get_username_and_hostname(&utsname));
|
||||||
});
|
});
|
||||||
c.bench_function("os_name", |b| b.iter(get_os_pretty_name));
|
c.bench_function("os_name", |b| b.iter(get_os_pretty_name));
|
||||||
c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname)));
|
c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname)));
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@ pub struct Colors {
|
||||||
|
|
||||||
impl Colors {
|
impl Colors {
|
||||||
const fn new(is_no_color: bool) -> Self {
|
const fn new(is_no_color: bool) -> Self {
|
||||||
match is_no_color {
|
if is_no_color {
|
||||||
true => Self {
|
Self {
|
||||||
reset: "",
|
reset: "",
|
||||||
blue: "",
|
blue: "",
|
||||||
cyan: "",
|
cyan: "",
|
||||||
|
|
@ -21,8 +21,9 @@ impl Colors {
|
||||||
yellow: "",
|
yellow: "",
|
||||||
red: "",
|
red: "",
|
||||||
magenta: "",
|
magenta: "",
|
||||||
},
|
}
|
||||||
false => Self {
|
} else {
|
||||||
|
Self {
|
||||||
reset: "\x1b[0m",
|
reset: "\x1b[0m",
|
||||||
blue: "\x1b[34m",
|
blue: "\x1b[34m",
|
||||||
cyan: "\x1b[36m",
|
cyan: "\x1b[36m",
|
||||||
|
|
@ -30,7 +31,7 @@ impl Colors {
|
||||||
yellow: "\x1b[33m",
|
yellow: "\x1b[33m",
|
||||||
red: "\x1b[31m",
|
red: "\x1b[31m",
|
||||||
magenta: "\x1b[35m",
|
magenta: "\x1b[35m",
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/main.rs
18
src/main.rs
|
|
@ -9,18 +9,17 @@ use crate::desktop::get_desktop_info;
|
||||||
use crate::release::{get_os_pretty_name, get_system_info};
|
use crate::release::{get_os_pretty_name, get_system_info};
|
||||||
use crate::system::{get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname};
|
use crate::system::{get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname};
|
||||||
use crate::uptime::get_current;
|
use crate::uptime::get_current;
|
||||||
use std::io::Write;
|
use std::io::{Write, stdout};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
if Some("--version") == std::env::args().nth(1).as_deref() {
|
||||||
if args.len() > 1 && args[1] == "--version" {
|
|
||||||
println!("Microfetch {}", env!("CARGO_PKG_VERSION"));
|
println!("Microfetch {}", env!("CARGO_PKG_VERSION"));
|
||||||
} else {
|
} else {
|
||||||
let utsname = nix::sys::utsname::uname()?;
|
let utsname = nix::sys::utsname::uname()?;
|
||||||
let fields = Fields {
|
let fields = Fields {
|
||||||
user_info: get_username_and_hostname(&utsname),
|
user_info: get_username_and_hostname(&utsname),
|
||||||
os_name: get_os_pretty_name()?,
|
os_name: get_os_pretty_name()?,
|
||||||
kernel_version: get_system_info(&utsname)?,
|
kernel_version: get_system_info(&utsname),
|
||||||
shell: get_shell(),
|
shell: get_shell(),
|
||||||
desktop: get_desktop_info(),
|
desktop: get_desktop_info(),
|
||||||
uptime: get_current()?,
|
uptime: get_current()?,
|
||||||
|
|
@ -28,7 +27,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
storage: get_root_disk_usage()?,
|
storage: get_root_disk_usage()?,
|
||||||
colors: print_dots(),
|
colors: print_dots(),
|
||||||
};
|
};
|
||||||
print_system_info(&fields);
|
print_system_info(&fields)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -50,7 +49,7 @@ struct Fields {
|
||||||
colors: String,
|
colors: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_system_info(fields: &Fields) {
|
fn print_system_info(fields: &Fields) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
use crate::colors::COLORS;
|
use crate::colors::COLORS;
|
||||||
|
|
||||||
let Fields {
|
let Fields {
|
||||||
|
|
@ -77,10 +76,7 @@ fn print_system_info(fields: &Fields) {
|
||||||
{blue} ▟█▛{cyan}▗█▖ {cyan}▟█▛ {cyan} {blue}Desktop{reset} {desktop}
|
{blue} ▟█▛{cyan}▗█▖ {cyan}▟█▛ {cyan} {blue}Desktop{reset} {desktop}
|
||||||
{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}\n");
|
||||||
|
|
||||||
std::io::stdout()
|
Ok(stdout().write_all(system_info.as_bytes())?)
|
||||||
.lock()
|
|
||||||
.write_all(format!("{}\n", system_info).as_bytes())
|
|
||||||
.expect("Failed to write to stdout");
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,13 @@ use std::{
|
||||||
io::{self, BufRead, BufReader},
|
io::{self, BufRead, BufReader},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
|
pub fn get_system_info(utsname: &UtsName) -> String {
|
||||||
Ok(format!(
|
format!(
|
||||||
"{} {} ({})",
|
"{} {} ({})",
|
||||||
utsname.sysname().to_str().unwrap_or("Unknown"),
|
utsname.sysname().to_str().unwrap_or("Unknown"),
|
||||||
utsname.release().to_str().unwrap_or("Unknown"),
|
utsname.release().to_str().unwrap_or("Unknown"),
|
||||||
utsname.machine().to_str().unwrap_or("Unknown")
|
utsname.machine().to_str().unwrap_or("Unknown")
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,6 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_memory_usage() -> Result<String, io::Error> {
|
pub fn get_memory_usage() -> Result<String, io::Error> {
|
||||||
#[inline(always)]
|
|
||||||
fn parse_memory_info() -> Result<(f64, f64), io::Error> {
|
fn parse_memory_info() -> Result<(f64, f64), io::Error> {
|
||||||
let mut total_memory_kb = 0.0;
|
let mut total_memory_kb = 0.0;
|
||||||
let mut available_memory_kb = 0.0;
|
let mut available_memory_kb = 0.0;
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,19 @@
|
||||||
use std::io;
|
use std::{io, mem::MaybeUninit};
|
||||||
|
|
||||||
pub fn get_current() -> Result<String, io::Error> {
|
pub fn get_current() -> Result<String, io::Error> {
|
||||||
let uptime_seconds = unsafe {
|
let uptime_seconds = {
|
||||||
let mut info: libc::sysinfo = std::mem::zeroed();
|
let mut info = MaybeUninit::uninit();
|
||||||
if libc::sysinfo(&mut info) != 0 {
|
if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 {
|
||||||
return Err(io::Error::last_os_error());
|
return Err(io::Error::last_os_error());
|
||||||
}
|
}
|
||||||
info.uptime as u64
|
unsafe { info.assume_init().uptime as u64 }
|
||||||
};
|
};
|
||||||
|
|
||||||
let days = uptime_seconds / 86400;
|
let days = uptime_seconds / 86400;
|
||||||
let hours = (uptime_seconds / 3600) % 24;
|
let hours = (uptime_seconds / 3600) % 24;
|
||||||
let minutes = (uptime_seconds / 60) % 60;
|
let minutes = (uptime_seconds / 60) % 60;
|
||||||
|
|
||||||
let mut result = String::new();
|
let mut result = String::with_capacity(32);
|
||||||
if days > 0 {
|
if days > 0 {
|
||||||
result.push_str(&days.to_string());
|
result.push_str(&days.to_string());
|
||||||
result.push_str(if days == 1 { " day" } else { " days" });
|
result.push_str(if days == 1 { " day" } else { " days" });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue