1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-11 23:00:05 +08:00

Compare commits

..

5 Commits

Author SHA1 Message Date
254585bb90 feat: v0.6.51 2026-01-04 23:16:31 +08:00
e13b2a3db4 feat: v0.6.50 2025-08-24 22:57:45 +08:00
e2b258ca09 feat: add env_var 2025-08-24 16:11:03 +08:00
4b596db8de feat: update cname 2025-07-26 10:45:54 +08:00
af4b91d4e9 feat: v0.5.48 2025-07-26 08:52:31 +08:00
6 changed files with 76 additions and 5 deletions

2
CNAME
View File

@@ -1 +1 @@
rust-util.hatter.cc
rust-util.ruststack.org

View File

@@ -1,6 +1,6 @@
[package]
name = "rust_util"
version = "0.6.47"
version = "0.6.51"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
description = "Hatter's Rust Util"

View File

@@ -1,10 +1,22 @@
#[macro_use] extern crate rust_util;
use std::sync::mpsc::channel;
use std::thread;
use rust_util::{XResult, SimpleError};
use rust_util::util_msg::set_logger_std_out;
use rust_util::util_msg::{set_logger_sender, set_logger_std_out};
// cargo run --example log
fn main() -> XResult<()> {
let (sender, receiver) = channel::<String>();
set_logger_sender(sender);
let _t = thread::spawn(move || {
loop {
let msg = receiver.recv();
println!("[RECV]: {:?}", msg)
}
});
std::env::set_var("LOGGER_LEVEL", "*");
println!(r##"env LOGGER_LEVEL set to:
debug or *

View File

@@ -1,11 +1,11 @@
use std::env;
pub fn is_env_on(var: &str) -> bool {
env::var(var).ok().map(|val| is_on(&val)).unwrap_or(false)
env_var(var).map(|val| is_on(&val)).unwrap_or(false)
}
pub fn is_env_off(var: &str) -> bool {
env::var(var).ok().map(|val| is_off(&val)).unwrap_or(false)
env_var(var).map(|val| is_off(&val)).unwrap_or(false)
}
pub fn is_on(val: &str) -> bool {
@@ -17,3 +17,15 @@ pub fn is_off(val: &str) -> bool {
let lower_val = val.to_lowercase();
["false", "no", "0"].iter().any(|x| *x == lower_val)
}
pub fn env_var(var: &str) -> Option<String> {
let var_from_env = env::var(var).ok();
if var_from_env.is_some() {
return var_from_env;
}
let var_content = crate::util_file::read_file_content(&format!("~/.config/envs/{}", var));
if let Ok(var_content) = var_content {
return Some(var_content.trim().to_string());
}
None
}

View File

@@ -1,14 +1,21 @@
use std::env;
use std::io::{self, Write};
use std::sync::{Arc, Mutex, RwLock};
use std::sync::mpsc::Sender;
lazy_static! {
pub static ref IS_ATTY: bool = is_atty();
static ref LOGGER_LEVEL: MessageType = get_logger_level();
static ref LOGGER_SENDER: Arc<RwLock<Option<Sender<String>>>> = Arc::new(RwLock::new(None));
static ref LOGGER_TO_STDOUT: Arc<RwLock<bool>> = Arc::new(RwLock::new(true));
static ref PRINT_MESSAGE_LOCK: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
}
pub fn set_logger_sender(sender: Sender<String>) {
let mut logger_sender_opt = LOGGER_SENDER.write().unwrap();
logger_sender_opt.replace(sender);
}
pub fn set_logger_std_out(is_std_out: bool) {
let mut std_out = LOGGER_TO_STDOUT.write().unwrap();
*std_out = is_std_out;
@@ -105,6 +112,13 @@ pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m
}
pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &str) {
{
let logger_sender_opt = LOGGER_SENDER.read().unwrap();
if let Some(logger_sender) = &*logger_sender_opt {
logger_sender.send(format!("{} {}", h, message)).ok();
return;
}
}
let is_std_out = get_logger_std_out();
let mut lock = PRINT_MESSAGE_LOCK.lock().unwrap();
print_color(is_std_out, color, true, h);

View File

@@ -2,9 +2,42 @@ use std::io::{self, Write};
use crate::util_msg;
pub const STD_BLACK: &str = "\x1B[30m";
pub const STD_RED: &str = "\x1B[31m";
pub const STD_GREEN: &str = "\x1B[32m";
pub const STD_YELLOW: &str = "\x1B[33m";
pub const STD_BLUE: &str = "\x1B[34m";
pub const STD_MAGENTA: &str = "\x1B[35m"; // 品红色/洋红
pub const STD_CYAN: &str = "\x1B[36m"; // 青色
pub const STD_WHITE: &str = "\x1B[37m";
pub const BLACK: &str = "\x1B[90m";
pub const RED: &str = "\x1B[91m";
pub const GREEN: &str = "\x1B[92m";
pub const YELLOW: &str = "\x1B[93m";
pub const BLUE: &str = "\x1B[94m";
pub const MAGENTA: &str = "\x1B[95m";
pub const CYAN: &str = "\x1B[96m";
pub const WHITE: &str = "\x1B[97m";
pub const BG_STD_BLACK: &str = "\x1B[40m";
pub const BG_STD_RED: &str = "\x1B[41m";
pub const BG_STD_GREEN: &str = "\x1B[42m";
pub const BG_STD_YELLOW: &str = "\x1B[43m";
pub const BG_STD_BLUE: &str = "\x1B[44m";
pub const BG_STD_MAGENTA: &str = "\x1B[45m";
pub const BG_STD_CYAN: &str = "\x1B[46m";
pub const BG_STD_WHITE: &str = "\x1B[47m";
pub const BG_BLACK: &str = "\x1B[100m";
pub const BG_RED: &str = "\x1B[101m";
pub const BG_GREEN: &str = "\x1B[102m";
pub const BG_YELLOW: &str = "\x1B[103m";
pub const BG_BLUE: &str = "\x1B[104m";
pub const BG_MAGENTA: &str = "\x1B[105m";
pub const BG_CYAN: &str = "\x1B[106m";
pub const BG_WHITE: &str = "\x1B[107m";
pub const BOLD: &str = "\x1B[1m";
pub const UNDER: &str = "\x1B[4m";
pub const END: &str = "\x1B[0m";