Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -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::desktop::get_desktop_info;
use microfetch_lib::release::{get_os_pretty_name, get_system_info};
Expand All @@ -10,7 +10,7 @@ use microfetch_lib::uptime::get_current;
fn main_benchmark(c: &mut Criterion) {
let utsname = nix::sys::utsname::uname().expect("lol");
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("kernel_version", |b| b.iter(|| get_system_info(&utsname)));
Expand Down
11 changes: 6 additions & 5 deletions src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ pub struct Colors {

impl Colors {
const fn new(is_no_color: bool) -> Self {
match is_no_color {
true => Self {
if is_no_color {
Self {
reset: "",
blue: "",
cyan: "",
green: "",
yellow: "",
red: "",
magenta: "",
},
false => Self {
}
} else {
Self {
reset: "\x1b[0m",
blue: "\x1b[34m",
cyan: "\x1b[36m",
green: "\x1b[32m",
yellow: "\x1b[33m",
red: "\x1b[31m",
magenta: "\x1b[35m",
},
}
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@ use crate::desktop::get_desktop_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::uptime::get_current;
use std::io::Write;
use std::io::{Write, stdout};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 && args[1] == "--version" {
if Some("--version") == std::env::args().nth(1).as_deref() {
println!("Microfetch {}", env!("CARGO_PKG_VERSION"));
} else {
let utsname = nix::sys::utsname::uname()?;
let fields = Fields {
user_info: get_username_and_hostname(&utsname),
os_name: get_os_pretty_name()?,
kernel_version: get_system_info(&utsname)?,
kernel_version: get_system_info(&utsname),
shell: get_shell(),
desktop: get_desktop_info(),
uptime: get_current()?,
memory_usage: get_memory_usage()?,
storage: get_root_disk_usage()?,
colors: print_dots(),
};
print_system_info(&fields);
print_system_info(&fields)?;
}

Ok(())
Expand All @@ -50,7 +49,7 @@ struct Fields {
colors: String,
}

fn print_system_info(fields: &Fields) {
fn print_system_info(fields: &Fields) -> Result<(), Box<dyn std::error::Error>> {
use crate::colors::COLORS;

let Fields {
Expand All @@ -77,10 +76,7 @@ fn print_system_info(fields: &Fields) {
{blue} ▟█▛{cyan}▗█▖ {cyan}▟█▛ {cyan} {blue}Desktop{reset}  {desktop}
{blue} ▝█▛ {cyan}██▖{blue}▗▄▄▄▄▄▄▄▄▄▄▄ {cyan} {blue}Memory{reset}  {memory_usage}
{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()
.lock()
.write_all(format!("{}\n", system_info).as_bytes())
.expect("Failed to write to stdout");
Ok(stdout().write_all(system_info.as_bytes())?)
}
6 changes: 3 additions & 3 deletions src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::{
io::{self, BufRead, BufReader},
};

pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
Ok(format!(
pub fn get_system_info(utsname: &UtsName) -> String {
format!(
"{} {} ({})",
utsname.sysname().to_str().unwrap_or("Unknown"),
utsname.release().to_str().unwrap_or("Unknown"),
utsname.machine().to_str().unwrap_or("Unknown")
))
)
}

pub fn get_os_pretty_name() -> Result<String, io::Error> {
Expand Down
1 change: 0 additions & 1 deletion src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub fn get_root_disk_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> {
let mut total_memory_kb = 0.0;
let mut available_memory_kb = 0.0;
Expand Down
12 changes: 6 additions & 6 deletions src/uptime.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::io;
use std::{io, mem::MaybeUninit};

pub fn get_current() -> Result<String, io::Error> {
let uptime_seconds = unsafe {
let mut info: libc::sysinfo = std::mem::zeroed();
if libc::sysinfo(&mut info) != 0 {
let uptime_seconds = {
let mut info = MaybeUninit::uninit();
if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 {
return Err(io::Error::last_os_error());
}
info.uptime as u64
unsafe { info.assume_init().uptime as u64 }
};

let days = uptime_seconds / 86400;
let hours = (uptime_seconds / 3600) % 24;
let minutes = (uptime_seconds / 60) % 60;

let mut result = String::new();
let mut result = String::with_capacity(32);
if days > 0 {
result.push_str(&days.to_string());
result.push_str(if days == 1 { " day" } else { " days" });
Expand Down