From b410fbe5a6780d00e9cae76425c6fa0284f974ae Mon Sep 17 00:00:00 2001 From: Quixaq Date: Wed, 10 Jun 2026 21:18:41 +0200 Subject: [PATCH] feat: configurable colors --- crates/lib/build.rs | 94 ++++++++++++++++++++++++++++++++++++++++ crates/lib/src/colors.rs | 17 ++++++++ crates/lib/src/lib.rs | 29 +++++++------ crates/lib/src/system.rs | 10 +++-- 4 files changed, 133 insertions(+), 17 deletions(-) create mode 100644 crates/lib/build.rs diff --git a/crates/lib/build.rs b/crates/lib/build.rs new file mode 100644 index 0000000..d2a8f46 --- /dev/null +++ b/crates/lib/build.rs @@ -0,0 +1,94 @@ +use std::{env, fs, path::Path}; + +fn main() { + let mut constants: Vec = Vec::new(); + + constants.push(format!( + "pub const COLOR_LOGO1: &'static str = \"{}\";", + match env::var("COLOR_LOGO1") { + Ok(color) => { + let r = + u32::from_str_radix(&color[1..3], 16).expect("Invalid COLOR_LOGO1"); + let g = + u32::from_str_radix(&color[3..5], 16).expect("Invalid COLOR_LOGO1"); + let b = + u32::from_str_radix(&color[5..7], 16).expect("Invalid COLOR_LOGO1"); + format!("\\x1b[38;2;{r};{g};{b};m") + }, + Err(_) => "\x1b[34m".to_string(), + } + )); + + constants.push(format!( + "pub const COLOR_LOGO2: &'static str = \"{}\";", + match env::var("COLOR_LOGO2") { + Ok(color) => { + let r = + u32::from_str_radix(&color[1..3], 16).expect("Invalid COLOR_LOGO2"); + let g = + u32::from_str_radix(&color[3..5], 16).expect("Invalid COLOR_LOGO2"); + let b = + u32::from_str_radix(&color[5..7], 16).expect("Invalid COLOR_LOGO2"); + format!("\\x1b[38;2;{r};{g};{b};m") + }, + Err(_) => "\x1b[36m".to_string(), + } + )); + + constants.push(format!( + "pub const COLOR_ICON: &'static str = \"{}\";", + match env::var("COLOR_ICON") { + Ok(color) => { + let r = + u32::from_str_radix(&color[1..3], 16).expect("Invalid COLOR_ICON"); + let g = + u32::from_str_radix(&color[3..5], 16).expect("Invalid COLOR_ICON"); + let b = + u32::from_str_radix(&color[5..7], 16).expect("Invalid COLOR_ICON"); + format!("\\x1b[38;2;{r};{g};{b};m") + }, + Err(_) => "\x1b[36m".to_string(), + } + )); + + constants.push(format!( + "pub const COLOR_KEY: &'static str = \"{}\";", + match env::var("COLOR_KEY") { + Ok(color) => { + let r = + u32::from_str_radix(&color[1..3], 16).expect("Invalid COLOR_KEY"); + let g = + u32::from_str_radix(&color[3..5], 16).expect("Invalid COLOR_KEY"); + let b = + u32::from_str_radix(&color[5..7], 16).expect("Invalid COLOR_KEY"); + format!("\\x1b[38;2;{r};{g};{b};m") + }, + Err(_) => "\x1b[34m".to_string(), + } + )); + + let val = match env::var("COLOR_VALUE") { + Ok(color) => { + let r = + u32::from_str_radix(&color[1..3], 16).expect("Invalid COLOR_VALUE"); + let g = + u32::from_str_radix(&color[3..5], 16).expect("Invalid COLOR_VALUE"); + let b = + u32::from_str_radix(&color[5..7], 16).expect("Invalid COLOR_VALUE"); + format!("\\x1b[38;2;{r};{g};{b};m") + }, + Err(_) => "".to_string(), + }; + constants.push(format!("pub const COLOR_VAL: &'static str = \"{}\";", val)); + + let out_dir = env::var("OUT_DIR").unwrap(); + let path1 = Path::new(&out_dir).join("colors_config.rs"); + let code = constants.join("\n"); + fs::write(path1, code).unwrap(); + let path2 = Path::new(&out_dir).join("color_helpers.rs"); + fs::write( + path2, + format!("pub const RPAREN: &'static str = \"{})\";\n", val), + ) + .unwrap(); +} diff --git a/crates/lib/src/colors.rs b/crates/lib/src/colors.rs index 9f012f9..9798575 100644 --- a/crates/lib/src/colors.rs +++ b/crates/lib/src/colors.rs @@ -1,3 +1,5 @@ +include!(concat!(env!("OUT_DIR"), "/colors_config.rs")); + use alloc::string::String; /// Color codes for terminal output @@ -9,6 +11,11 @@ pub struct Colors { pub yellow: &'static str, pub red: &'static str, pub magenta: &'static str, + pub l1: &'static str, + pub l2: &'static str, + pub icon: &'static str, + pub key: &'static str, + pub value: &'static str, } impl Colors { @@ -23,6 +30,11 @@ impl Colors { yellow: "", red: "", magenta: "", + l1: "", + l2: "", + icon: "", + key: "", + value: "", } } else { Self { @@ -33,6 +45,11 @@ impl Colors { yellow: "\x1b[33m", red: "\x1b[31m", magenta: "\x1b[35m", + l1: COLOR_LOGO1, + l2: COLOR_LOGO2, + icon: COLOR_ICON, + key: COLOR_KEY, + value: COLOR_VAL, } } } diff --git a/crates/lib/src/lib.rs b/crates/lib/src/lib.rs index d33bee1..9893734 100644 --- a/crates/lib/src/lib.rs +++ b/crates/lib/src/lib.rs @@ -363,19 +363,22 @@ fn print_system_info(fields: &Fields) -> Result<(), Error> { core::fmt::write( &mut w, format_args!( - "\n {b}⠀⠀⠀⠀⠀⠀⢼⣿⣄⠀⠀⠀{cy}⠹⣿⣷⡀⠀⣠⣿⡧⠀⠀⠀⠀⠀⠀{rs} {user_info} ~{rs}\ - \n {b}⠀⠀⠀⠀⠀⠀⠈⢿⣿⣆⠀⠀⠀{cy}⠘⣿⣿⣴⣿⡿⠁⠀⠀⠀⠀⠀⠀{rs} {cy}\u{F313} {b}System{rs} \u{E621} {os_name}\ - \n {b}⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡜{cy}⢿⣿⣟⠀⠀⠀{b}⢀⡄⠀⠀⠀{rs} {cy}\u{E712} {b}Kernel{rs} \u{E621} {kernel_version}\ - \n {b}⠀⠀⠀⠉⠉⠉⠉{cy}⣩⣭⡭{b}⠉⠉⠉⠉⠉{cy}⠈⢿⣿⣆⠀{b}⢠⣿⣿⠂⠀⠀{rs} {cy}\u{F2DB} {b}CPU{rs} \u{E621} {cpu_name}\ - \n {cy}⠀⠀⠀⠀⠀⠀⣼⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⢻⡟{b}⣡⣿⣿⠃⠀⠀⠀{rs} {cy}\u{F4BC} {b}Topology{rs} \u{E621} {cpu_cores}\ - \n {cy}⢸⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀{b}⣰⣿⣿⣿⣿⣿⣿⡇{rs} {cy}\u{E795} {b}Shell{rs} \u{E621} {shell}\ - \n {cy}⠀⠀⠀⢠⣿⣿⢋{b}⣼⣧⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⡟⠀⠀⠀⠀⠀⠀{rs} {cy}\u{F017} {b}Uptime{rs} \u{E621} {uptime}\ - \n {cy}⠀⠀⠠⣿⣿⠃⠀{b}⠹⣿⣷⡀{cy}⣀⣀⣀⣀⣀{b}⣚⣛⣋{cy}⣀⣀⣀⣀⠀⠀⠀{rs} {cy}\u{F2D2} {b}Desktop{rs} \u{E621} {desktop}\ - \n {cy}⠀⠀⠀⠘⠁⠀⠀⠀{b}⣽⣿⣷⡜{cy}⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀{rs} {cy}\u{F035B} {b}Memory{rs} \u{E621} {memory_usage}\ - \n {b}⠀⠀⠀⠀⠀⠀⢀⣾⣿⠟⣿⣿⡄⠀⠀⠀{cy}⠹⣿⣷⡀⠀⠀⠀⠀⠀⠀{rs} {cy}\u{F194E} {b}Storage (/){rs} \u{E621} {storage}\ - \n {b}⠀⠀⠀⠀⠀⠀⢺⣿⠋⠀⠈⢿⣿⣆⠀⠀⠀{cy}⠙⣿⡗⠀⠀⠀⠀⠀⠀{rs} {cy}\u{E22B} {b}Colors{rs} \u{E621} {colors}\n\n", - b = c.blue, - cy = c.cyan, + "\n {l1}⠀⠀⠀⠀⠀⠀⢼⣿⣄⠀⠀⠀{l2}⠹⣿⣷⡀⠀⣠⣿⡧⠀⠀⠀⠀⠀⠀{rs} {user_info} ~{rs}\ + \n {l1}⠀⠀⠀⠀⠀⠀⠈⢿⣿⣆⠀⠀⠀{l2}⠘⣿⣿⣴⣿⡿⠁⠀⠀⠀⠀⠀⠀{rs} {icon}\u{F313} {key}System{rs} \u{E621} {val}{os_name}{rs}\ + \n {l1}⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡜{l2}⢿⣿⣟⠀⠀⠀{l1}⢀⡄⠀⠀⠀{rs} {icon}\u{E712} {key}Kernel{rs} \u{E621} {val}{kernel_version}{rs}\ + \n {l1}⠀⠀⠀⠉⠉⠉⠉{l2}⣩⣭⡭{l1}⠉⠉⠉⠉⠉{l2}⠈⢿⣿⣆⠀{l1}⢠⣿⣿⠂⠀⠀{rs} {icon}\u{F2DB} {key}CPU{rs} \u{E621} {val}{cpu_name}{rs}\ + \n {l2}⠀⠀⠀⠀⠀⠀⣼⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⢻⡟{l1}⣡⣿⣿⠃⠀⠀⠀{rs} {icon}\u{F4BC} {key}Topology{rs} \u{E621} {val}{cpu_cores}{rs}\ + \n {l2}⢸⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀{l1}⣰⣿⣿⣿⣿⣿⣿⡇{rs} {icon}\u{E795} {key}Shell{rs} \u{E621} {val}{shell}{rs}\ + \n {l2}⠀⠀⠀⢠⣿⣿⢋{l1}⣼⣧⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⡟⠀⠀⠀⠀⠀⠀{rs} {icon}\u{F017} {key}Uptime{rs} \u{E621} {val}{uptime}{rs}\ + \n {l2}⠀⠀⠠⣿⣿⠃⠀{l1}⠹⣿⣷⡀{l2}⣀⣀⣀⣀⣀{l1}⣚⣛⣋{l2}⣀⣀⣀⣀⠀⠀⠀{rs} {icon}\u{F2D2} {key}Desktop{rs} \u{E621} {val}{desktop}{rs}\ + \n {l2}⠀⠀⠀⠘⠁⠀⠀⠀{l1}⣽⣿⣷⡜{l2}⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀{rs} {icon}\u{F035B} {key}Memory{rs} \u{E621} {val}{memory_usage}{rs}\ + \n {l1}⠀⠀⠀⠀⠀⠀⢀⣾⣿⠟⣿⣿⡄⠀⠀⠀{l2}⠹⣿⣷⡀⠀⠀⠀⠀⠀⠀{rs} {icon}\u{F194E} {key}Storage (/){rs} \u{E621} {val}{storage}{rs}\ + \n {l1}⠀⠀⠀⠀⠀⠀⢺⣿⠋⠀⠈⢿⣿⣆⠀⠀⠀{l2}⠙⣿⡗⠀⠀⠀⠀⠀⠀{rs} {icon}\u{E22B} {key}Colors{rs} \u{E621} {val}{colors}{rs}\n\n", + l1 = c.l1, + l2 = c.l2, + icon = c.icon, + key = c.key, + val = c.value, rs = c.reset, user_info = user_info, os_name = os_name, diff --git a/crates/lib/src/system.rs b/crates/lib/src/system.rs index 1db8510..5a482e2 100644 --- a/crates/lib/src/system.rs +++ b/crates/lib/src/system.rs @@ -1,3 +1,5 @@ +include!(concat!(env!("OUT_DIR"), "/color_helpers.rs")); + use alloc::string::String; use core::mem::MaybeUninit; @@ -88,11 +90,11 @@ pub fn get_root_disk_usage() -> Result { result.push_str(" GiB / "); write_float(&mut result, total_size, 2); result.push_str(" GiB ("); - result.push_str(colors.cyan); + result.push_str(colors.icon); write_float(&mut result, usage, 0); result.push('%'); result.push_str(colors.reset); - result.push(')'); + result.push_str(RPAREN); Ok(result) } @@ -258,11 +260,11 @@ pub fn get_memory_usage() -> Result { result.push_str(" GiB / "); write_float(&mut result, total_memory, 2); result.push_str(" GiB ("); - result.push_str(colors.cyan); + result.push_str(colors.icon); write_u64(&mut result, percentage_used); result.push('%'); result.push_str(colors.reset); - result.push(')'); + result.push_str(RPAREN); Ok(result) }