push almost all of colors back to compile time using macros

This commit is contained in:
poz 2025-12-12 22:00:47 +01:00
commit 9197065529
No known key found for this signature in database
5 changed files with 70 additions and 55 deletions

View file

@ -1,8 +1,8 @@
use criterion::{Criterion, criterion_group, criterion_main}; use criterion::{Criterion, criterion_group, criterion_main};
use microfetch_lib::{ use microfetch_lib::{
UtsName, UtsName,
colors::print_dots,
desktop::get_desktop_info, desktop::get_desktop_info,
dots::print_dots,
release::{get_os_pretty_name, get_system_info}, release::{get_os_pretty_name, get_system_info},
system::{ system::{
get_memory_usage, get_memory_usage,

View file

@ -1,5 +1,22 @@
use std::sync::LazyLock; use std::sync::LazyLock;
// All this because concat!() doesn't accept const parameters
// See https://github.com/rust-lang/rust/issues/31383
#[macro_export]
macro_rules! RESET {() => {"\x1b[0m"}}
#[macro_export]
macro_rules! BLUE {() => {"\x1b[34m"}}
#[macro_export]
macro_rules! CYAN {() => {"\x1b[34m"}}
#[macro_export]
macro_rules! GREEN {() => {"\x1b[32m"}}
#[macro_export]
macro_rules! YELLOW {() => {"\x1b[33m"}}
#[macro_export]
macro_rules! RED {() => {"\x1b[31m"}}
#[macro_export]
macro_rules! MAGENTA {() => {"\x1b[35m"}}
pub struct Colors { pub struct Colors {
pub reset: &'static str, pub reset: &'static str,
pub blue: &'static str, pub blue: &'static str,
@ -7,7 +24,6 @@ pub struct Colors {
pub green: &'static str, pub green: &'static str,
pub yellow: &'static str, pub yellow: &'static str,
pub red: &'static str, pub red: &'static str,
pub magenta: &'static str,
} }
impl Colors { impl Colors {
@ -20,62 +36,26 @@ impl Colors {
green: "", green: "",
yellow: "", yellow: "",
red: "", red: "",
magenta: "",
} }
} else { } else {
Self { Self {
reset: "\x1b[0m", reset: RESET!(),
blue: "\x1b[34m", blue: BLUE!(),
cyan: "\x1b[36m", cyan: CYAN!(),
green: "\x1b[32m", green: GREEN!(),
yellow: "\x1b[33m", yellow: YELLOW!(),
red: "\x1b[31m", red: RED!(),
magenta: "\x1b[35m",
} }
} }
} }
} }
pub static COLORS: LazyLock<Colors> = LazyLock::new(|| { pub static IS_NO_COLOR: LazyLock<bool> = LazyLock::new(|| {
// Check for NO_COLOR once at startup
const NO_COLOR: *const libc::c_char = c"NO_COLOR".as_ptr(); const NO_COLOR: *const libc::c_char = c"NO_COLOR".as_ptr();
let is_no_color = unsafe { !libc::getenv(NO_COLOR).is_null() }; unsafe { !libc::getenv(NO_COLOR).is_null() }
Colors::new(is_no_color)
}); });
#[must_use] pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
#[cfg_attr(feature = "hotpath", hotpath::measure)] Colors::new(*IS_NO_COLOR)
pub fn print_dots() -> String { });
// Pre-calculate capacity: 6 color codes + " " (glyph + 2 spaces) per color
const GLYPH: &str = "";
let capacity = COLORS.blue.len()
+ COLORS.cyan.len()
+ COLORS.green.len()
+ COLORS.yellow.len()
+ COLORS.red.len()
+ COLORS.magenta.len()
+ COLORS.reset.len()
+ (GLYPH.len() + 2) * 6;
let mut result = String::with_capacity(capacity);
result.push_str(COLORS.blue);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.cyan);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.green);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.yellow);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.red);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.magenta);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.reset);
result
}

33
src/dots.rs Normal file
View file

@ -0,0 +1,33 @@
use crate::{colors::IS_NO_COLOR, BLUE, CYAN, GREEN, MAGENTA, RED, RESET, YELLOW};
macro_rules! GLYPH {() => {""}}
macro_rules! GAP {() => {" "}}
const NO_COLORS_STR: &str = concat!(
GLYPH!(), GAP!(),
GLYPH!(), GAP!(),
GLYPH!(), GAP!(),
GLYPH!(), GAP!(),
GLYPH!(), GAP!(),
GLYPH!(),
);
const COLORS_STR: &str = concat!(
BLUE!(), GLYPH!(), GAP!(),
CYAN!(), GLYPH!(), GAP!(),
GREEN!(), GLYPH!(), GAP!(),
YELLOW!(), GLYPH!(), GAP!(),
RED!(), GLYPH!(), GAP!(),
MAGENTA!(), GLYPH!(), RESET!(),
);
#[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn print_dots() -> &'static str {
if *IS_NO_COLOR {
NO_COLORS_STR
} else {
COLORS_STR
}
}

View file

@ -1,5 +1,6 @@
pub mod colors; pub mod colors;
pub mod desktop; pub mod desktop;
pub mod dots;
pub mod release; pub mod release;
pub mod syscall; pub mod syscall;
pub mod system; pub mod system;

View file

@ -1,5 +1,6 @@
mod colors; mod colors;
mod desktop; mod desktop;
mod dots;
mod release; mod release;
mod syscall; mod syscall;
mod system; mod system;
@ -10,8 +11,8 @@ use std::io::{self, Cursor, Write};
pub use microfetch_lib::UtsName; pub use microfetch_lib::UtsName;
use crate::{ use crate::{
colors::print_dots,
desktop::get_desktop_info, desktop::get_desktop_info,
dots::print_dots,
release::{get_os_pretty_name, get_system_info}, release::{get_os_pretty_name, get_system_info},
system::{ system::{
get_memory_usage, get_memory_usage,
@ -37,7 +38,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
uptime: get_current()?, uptime: get_current()?,
memory_usage: get_memory_usage()?, memory_usage: get_memory_usage()?,
storage: get_root_disk_usage()?, storage: get_root_disk_usage()?,
colors: print_dots(), dots: print_dots(),
}; };
print_system_info(&fields)?; print_system_info(&fields)?;
} }
@ -57,7 +58,7 @@ struct Fields {
desktop: String, desktop: String,
memory_usage: String, memory_usage: String,
storage: String, storage: String,
colors: String, dots: &'static str,
} }
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
@ -75,7 +76,7 @@ fn print_system_info(
desktop, desktop,
memory_usage, memory_usage,
storage, storage,
colors, dots,
} = fields; } = fields;
let cyan = COLORS.cyan; let cyan = COLORS.cyan;
@ -96,7 +97,7 @@ fn print_system_info(
{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}\n\n" {cyan} {blue} {cyan} {blue}Colors{reset} {dots}\n\n"
)?; )?;
let len = cursor.position() as usize; let len = cursor.position() as usize;