From f257931b0b7065bfc123f13d30047f8973a7f655 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 22 Mar 2023 21:02:09 +0800 Subject: fix: mask api_key/organization_id in info text (#93) --- src/utils/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/utils') diff --git a/src/utils/mod.rs b/src/utils/mod.rs index fd178ba..64fc01c 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -27,3 +27,23 @@ pub fn now() -> String { pub fn emphasis(text: &str) -> String { text.stylize().with(Color::White).to_string() } + +pub fn mask_text(text: &str, head: usize, tail: usize) -> String { + if text.len() <= head + tail { + return text.to_string(); + } + format!("{}...{}", &text[0..head], &text[text.len() - tail..]) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mask_text() { + assert_eq!(mask_text("123456", 3, 4), "123456"); + assert_eq!(mask_text("1234567", 3, 4), "1234567"); + assert_eq!(mask_text("12345678", 3, 4), "123...5678"); + assert_eq!(mask_text("12345678", 4, 3), "1234...678"); + } +} -- cgit v1.2.3