Skip to content

Commit 28e156b

Browse files
authored
Merge pull request #17 from ErrorNoInternet/main
treewide: clean up a few things
2 parents 671ce6b + b814b2b commit 28e156b

File tree

6 files changed

+24
-28
lines changed

6 files changed

+24
-28
lines changed

benches/benchmark.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use criterion::{criterion_group, criterion_main, Criterion};
1+
use criterion::{Criterion, criterion_group, criterion_main};
22
use microfetch_lib::colors::print_dots;
33
use microfetch_lib::desktop::get_desktop_info;
44
use microfetch_lib::release::{get_os_pretty_name, get_system_info};
@@ -10,7 +10,7 @@ use microfetch_lib::uptime::get_current;
1010
fn main_benchmark(c: &mut Criterion) {
1111
let utsname = nix::sys::utsname::uname().expect("lol");
1212
c.bench_function("user_info", |b| {
13-
b.iter(|| get_username_and_hostname(&utsname))
13+
b.iter(|| get_username_and_hostname(&utsname));
1414
});
1515
c.bench_function("os_name", |b| b.iter(get_os_pretty_name));
1616
c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname)));

src/colors.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,26 @@ pub struct Colors {
1212

1313
impl Colors {
1414
const fn new(is_no_color: bool) -> Self {
15-
match is_no_color {
16-
true => Self {
15+
if is_no_color {
16+
Self {
1717
reset: "",
1818
blue: "",
1919
cyan: "",
2020
green: "",
2121
yellow: "",
2222
red: "",
2323
magenta: "",
24-
},
25-
false => Self {
24+
}
25+
} else {
26+
Self {
2627
reset: "\x1b[0m",
2728
blue: "\x1b[34m",
2829
cyan: "\x1b[36m",
2930
green: "\x1b[32m",
3031
yellow: "\x1b[33m",
3132
red: "\x1b[31m",
3233
magenta: "\x1b[35m",
33-
},
34+
}
3435
}
3536
}
3637
}

src/main.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@ use crate::desktop::get_desktop_info;
99
use crate::release::{get_os_pretty_name, get_system_info};
1010
use crate::system::{get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname};
1111
use crate::uptime::get_current;
12-
use std::io::Write;
12+
use std::io::{Write, stdout};
1313

1414
fn main() -> Result<(), Box<dyn std::error::Error>> {
15-
let args: Vec<String> = std::env::args().collect();
16-
if args.len() > 1 && args[1] == "--version" {
15+
if Some("--version") == std::env::args().nth(1).as_deref() {
1716
println!("Microfetch {}", env!("CARGO_PKG_VERSION"));
1817
} else {
1918
let utsname = nix::sys::utsname::uname()?;
2019
let fields = Fields {
2120
user_info: get_username_and_hostname(&utsname),
2221
os_name: get_os_pretty_name()?,
23-
kernel_version: get_system_info(&utsname)?,
22+
kernel_version: get_system_info(&utsname),
2423
shell: get_shell(),
2524
desktop: get_desktop_info(),
2625
uptime: get_current()?,
2726
memory_usage: get_memory_usage()?,
2827
storage: get_root_disk_usage()?,
2928
colors: print_dots(),
3029
};
31-
print_system_info(&fields);
30+
print_system_info(&fields)?;
3231
}
3332

3433
Ok(())
@@ -50,7 +49,7 @@ struct Fields {
5049
colors: String,
5150
}
5251

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

5655
let Fields {
@@ -77,10 +76,7 @@ fn print_system_info(fields: &Fields) {
7776
{blue} ▟█▛{cyan}▗█▖ {cyan}▟█▛ {cyan} {blue}Desktop{reset}  {desktop}
7877
{blue} ▝█▛ {cyan}██▖{blue}▗▄▄▄▄▄▄▄▄▄▄▄ {cyan} {blue}Memory{reset}  {memory_usage}
7978
{blue} ▝ {cyan}▟█▜█▖{blue}▀▀▀▀▀██▛▀▀▘ {cyan}󱥎 {blue}Storage (/){reset}  {storage}
80-
{cyan} ▟█▘ ▜█▖ {blue}▝█▛ {cyan} {blue}Colors{reset}  {colors}");
79+
{cyan} ▟█▘ ▜█▖ {blue}▝█▛ {cyan} {blue}Colors{reset}  {colors}\n");
8180

82-
std::io::stdout()
83-
.lock()
84-
.write_all(format!("{}\n", system_info).as_bytes())
85-
.expect("Failed to write to stdout");
81+
Ok(stdout().write_all(system_info.as_bytes())?)
8682
}

src/release.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use std::{
44
io::{self, BufRead, BufReader},
55
};
66

7-
pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
8-
Ok(format!(
7+
pub fn get_system_info(utsname: &UtsName) -> String {
8+
format!(
99
"{} {} ({})",
1010
utsname.sysname().to_str().unwrap_or("Unknown"),
1111
utsname.release().to_str().unwrap_or("Unknown"),
1212
utsname.machine().to_str().unwrap_or("Unknown")
13-
))
13+
)
1414
}
1515

1616
pub fn get_os_pretty_name() -> Result<String, io::Error> {

src/system.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
4949
}
5050

5151
pub fn get_memory_usage() -> Result<String, io::Error> {
52-
#[inline(always)]
5352
fn parse_memory_info() -> Result<(f64, f64), io::Error> {
5453
let mut total_memory_kb = 0.0;
5554
let mut available_memory_kb = 0.0;

src/uptime.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use std::io;
1+
use std::{io, mem::MaybeUninit};
22

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

1212
let days = uptime_seconds / 86400;
1313
let hours = (uptime_seconds / 3600) % 24;
1414
let minutes = (uptime_seconds / 60) % 60;
1515

16-
let mut result = String::new();
16+
let mut result = String::with_capacity(32);
1717
if days > 0 {
1818
result.push_str(&days.to_string());
1919
result.push_str(if days == 1 { " day" } else { " days" });

0 commit comments

Comments
 (0)