Skip to content

Commit

Permalink
get rid of color_eyre
Browse files Browse the repository at this point in the history
Should never have added it. Annoyingly long compile times for no reason...
  • Loading branch information
NotAShelf committed Jan 25, 2025
1 parent 4fff13a commit 592fb58
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 128 deletions.
104 changes: 1 addition & 103 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "microfetch"
version = "0.4.4"
version = "0.4.5"
edition = "2021"

[lib]
Expand All @@ -13,7 +13,6 @@ path = "src/main.rs"

[dependencies]
nix = { version = "0.29", features = ["fs", "hostname", "feature"] }
color-eyre = { version = "0.6", default-features = false }
lazy_static = "1.5.0"

[dev-dependencies]
Expand All @@ -28,7 +27,7 @@ opt-level = 3

[profile.release]
strip = true
opt-level = "z"
opt-level = "s"
lto = true
codegen-units = 1
panic = "abort"
Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ 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 color_eyre::Report;
use std::io::Write;

fn main() -> Result<(), Report> {
color_eyre::install()?;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 && args[1] == "--version" {
println!("Microfetch {}", env!("CARGO_PKG_VERSION"));
Expand Down
2 changes: 0 additions & 2 deletions src/release.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use color_eyre::Result;
use nix::sys::utsname::UtsName;
use std::{
fs::File,
Expand Down Expand Up @@ -30,6 +29,5 @@ pub fn get_os_pretty_name() -> Result<String, io::Error> {
return Ok(pretty_name.to_string());
}
}

Ok("Unknown".to_string())
}
13 changes: 10 additions & 3 deletions src/system.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::colors::COLORS;
use color_eyre::Result;
use nix::sys::{statvfs::statvfs, utsname::UtsName};
use std::{
env,
Expand All @@ -8,7 +7,7 @@ use std::{
};

pub fn get_username_and_hostname(utsname: &UtsName) -> String {
let username = env::var("USER").unwrap_or("unknown_user".to_string());
let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string());
let hostname = utsname
.nodename()
.to_str()
Expand All @@ -24,7 +23,7 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
}

pub fn get_shell() -> String {
let shell_path = env::var("SHELL").unwrap_or("unknown_shell".to_string());
let shell_path = env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_string());
let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell");
shell_name.to_string()
}
Expand All @@ -34,11 +33,14 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
let block_size = vfs.block_size() as u64;
let total_blocks = vfs.blocks();
let available_blocks = vfs.blocks_available();

let total_size = block_size * total_blocks;
let used_size = total_size - (block_size * available_blocks);

let total_size = total_size as f64 / (1024.0 * 1024.0 * 1024.0);
let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0);
let usage = (used_size / total_size) * 100.0;

Ok(format!(
"{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})",
cyan = COLORS.cyan,
Expand All @@ -52,7 +54,9 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
let mut total_memory_kb = 0.0;
let mut available_memory_kb = 0.0;
let mut meminfo = String::with_capacity(2048);

File::open("/proc/meminfo")?.read_to_string(&mut meminfo)?;

for line in meminfo.lines() {
let mut split = line.split_whitespace();
match split.next().unwrap_or_default() {
Expand All @@ -65,14 +69,17 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
_ => (),
}
}

let total_memory_gb = total_memory_kb / 1024.0 / 1024.0;
let available_memory_gb = available_memory_kb / 1024.0 / 1024.0;
let used_memory_gb = total_memory_gb - available_memory_gb;

Ok((used_memory_gb, total_memory_gb))
}

let (used_memory, total_memory) = parse_memory_info()?;
let percentage_used = (used_memory / total_memory * 100.0).round() as u64;

Ok(format!(
"{used_memory:.2} GiB / {total_memory:.2} GiB ({cyan}{percentage_used}%{reset})",
cyan = COLORS.cyan,
Expand Down
20 changes: 7 additions & 13 deletions src/uptime.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
use color_eyre::Result;
use nix::sys::sysinfo::sysinfo;
use std::io;

pub fn get_current() -> Result<String, io::Error> {
let info = sysinfo().map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let uptime_seconds = info.uptime().as_secs_f64();
let uptime_seconds = info.uptime().as_secs();

let total_minutes = (uptime_seconds / 60.0).round() as u64;
let total_minutes = uptime_seconds / 60;
let days = total_minutes / (60 * 24);
let hours = (total_minutes % (60 * 24)) / 60;
let minutes = total_minutes % 60;

let mut parts = Vec::with_capacity(3);
if days > 0 {
parts.push(format!("{days} days"));
}
if hours > 0 || days > 0 {
parts.push(format!("{hours} hours"));
}
if minutes > 0 || hours > 0 || days > 0 {
parts.push(format!("{minutes} minutes"));
}
let parts = [(days, "day"), (hours, "hour"), (minutes, "minute")]
.iter()
.filter(|&&(value, _)| value > 0)
.map(|&(value, label)| format!("{value} {label}{}", if value > 1 { "s" } else { "" }))
.collect::<Vec<_>>();

Ok(parts.join(", "))
}

0 comments on commit 592fb58

Please sign in to comment.