diff options
| author | sigoden <sigoden@gmail.com> | 2023-03-22 21:02:09 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-22 21:02:09 +0800 |
| commit | f257931b0b7065bfc123f13d30047f8973a7f655 (patch) | |
| tree | c9e40facb3b10a87f5a5ca78b6b2d184e13573f4 /src/utils/mod.rs | |
| parent | c36eab543377c52e230d5be08d675b330f59d566 (diff) | |
| download | aichat-f257931b0b7065bfc123f13d30047f8973a7f655.tar.gz | |
fix: mask api_key/organization_id in info text (#93)
Diffstat (limited to 'src/utils/mod.rs')
| -rw-r--r-- | src/utils/mod.rs | 20 |
1 files changed, 20 insertions, 0 deletions
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"); + } +} |
