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

Compare commits

...

3 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
3 changed files with 48 additions and 3 deletions

View File

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

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

@@ -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";