use once_cell::sync::Lazy;
use regex::Regex;
static BOX_CHARS: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌╯╰╱╲╳╭╮]").unwrap());
static ANSI_ESCAPES: Lazy<Regex> = Lazy::new(|| Regex::new(r"\x1b\[[0-9;]*[mGKHF]").unwrap());
static TUI_ARTIFACTS: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^\s*[│┃┆┊]\s*|\s*[│┃┆┊]\s*$").unwrap());
pub fn clean_text(input: &str) -> String {
let mut cleaned = input.to_string();
cleaned = ANSI_ESCAPES.replace_all(&cleaned, "").to_string();
cleaned = BOX_CHARS.replace_all(&cleaned, "").to_string();
cleaned = cleaned
.lines()
.map(|line| {
let line = TUI_ARTIFACTS.replace_all(line, "");
line.trim().to_string()
})
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
.join("\n");
cleaned = cleaned.trim().to_string();
let multi_newline = Regex::new(r"\n{3,}").unwrap();
cleaned = multi_newline.replace_all(&cleaned, "\n\n").to_string();
cleaned
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_clean_box_chars() {
let input = "│ Hello World │";
assert_eq!(clean_text(input), "Hello World");
}
#[test]
fn test_clean_ansi() {
let input = "\x1b[1mBold Text\x1b[0m";
assert_eq!(clean_text(input), "Bold Text");
}
#[test]
fn test_clean_complex() {
let input = r#"╭─────────────────╮
│ Test Message │
│ With multiple │
│ lines │
╰─────────────────╯"#;
let expected = "Test Message\nWith multiple\nlines";
assert_eq!(clean_text(input), expected);
}
}