use thag_styling::{ColorInitStrategy, TermAttributes};
struct TestStyledString {
content: String,
style_codes: String,
}
impl TestStyledString {
fn new(style_codes: &str) -> Self {
Self {
content: String::new(),
style_codes: style_codes.to_string(),
}
}
fn has_ansi_code(ansi_string: &str, codes: &[u8]) -> bool {
let mut pos = 0;
while let Some(esc_start) = ansi_string[pos..].find("\x1b[") {
let abs_start = pos + esc_start + 2; if let Some(m_pos) = ansi_string[abs_start..].find('m') {
let codes_str = &ansi_string[abs_start..abs_start + m_pos];
if Self::contains_text_attributes(codes_str, codes) {
return true;
}
pos = abs_start + m_pos + 1;
} else {
break;
}
}
false
}
fn contains_text_attributes(codes_str: &str, target_codes: &[u8]) -> bool {
let parts: Vec<&str> = codes_str.split(';').collect();
let mut i = 0;
while i < parts.len() {
if let Ok(code) = parts[i].parse::<u8>() {
match code {
38 | 48 => {
if i + 1 < parts.len() {
if let Ok(color_type) = parts[i + 1].parse::<u8>() {
match color_type {
2 => {
i += 5; }
5 => {
i += 3; }
_ => i += 1,
}
} else {
i += 1;
}
} else {
i += 1;
}
}
_ => {
if target_codes.contains(&code) {
return true;
}
i += 1;
}
}
} else {
i += 1;
}
}
false
}
fn test_has_bold(&self) -> bool {
Self::has_ansi_code(&self.style_codes, &[1, 2])
}
fn test_has_italic(&self) -> bool {
Self::has_ansi_code(&self.style_codes, &[3])
}
fn test_has_underline(&self) -> bool {
Self::has_ansi_code(&self.style_codes, &[4])
}
}
fn main() {
TermAttributes::get_or_init_with_strategy(&ColorInitStrategy::Match);
println!("=== ANSI Parsing Logic Test ===\n");
println!("1. RGB Color Tests:");
let rgb_color = TestStyledString::new("\x1b[38;2;202;97;101m");
println!(" RGB color code: {:?}", rgb_color.style_codes);
println!(
" Has bold: {} (should be false)",
rgb_color.test_has_bold()
);
println!(
" Has italic: {} (should be false)",
rgb_color.test_has_italic()
);
println!(
" Has underline: {} (should be false)",
rgb_color.test_has_underline()
);
println!();
println!("2. Simple Text Attribute Tests:");
let bold_only = TestStyledString::new("\x1b[1m");
println!(" Bold code: {:?}", bold_only.style_codes);
println!(
" Has bold: {} (should be true)",
bold_only.test_has_bold()
);
println!(
" Has italic: {} (should be false)",
bold_only.test_has_italic()
);
println!(
" Has underline: {} (should be false)",
bold_only.test_has_underline()
);
println!();
let italic_only = TestStyledString::new("\x1b[3m");
println!(" Italic code: {:?}", italic_only.style_codes);
println!(
" Has bold: {} (should be false)",
italic_only.test_has_bold()
);
println!(
" Has italic: {} (should be true)",
italic_only.test_has_italic()
);
println!(
" Has underline: {} (should be false)",
italic_only.test_has_underline()
);
println!();
println!("3. Combined Attribute Tests:");
let combined = TestStyledString::new("\x1b[1;3;4m");
println!(" Combined code: {:?}", combined.style_codes);
println!(" Has bold: {} (should be true)", combined.test_has_bold());
println!(
" Has italic: {} (should be true)",
combined.test_has_italic()
);
println!(
" Has underline: {} (should be true)",
combined.test_has_underline()
);
println!();
println!("4. RGB Color + Attribute Tests:");
let rgb_plus_bold = TestStyledString::new("\x1b[38;2;255;0;0m\x1b[1m");
println!(" RGB + Bold codes: {:?}", rgb_plus_bold.style_codes);
println!(
" Has bold: {} (should be true)",
rgb_plus_bold.test_has_bold()
);
println!(
" Has italic: {} (should be false)",
rgb_plus_bold.test_has_italic()
);
println!(
" Has underline: {} (should be false)",
rgb_plus_bold.test_has_underline()
);
println!();
println!("5. Mixed Sequence Tests:");
let mixed = TestStyledString::new("\x1b[38;2;100;200;50;1;3m");
println!(" Mixed sequence: {:?}", mixed.style_codes);
println!(" Has bold: {} (should be true)", mixed.test_has_bold());
println!(
" Has italic: {} (should be true)",
mixed.test_has_italic()
);
println!(
" Has underline: {} (should be false)",
mixed.test_has_underline()
);
println!();
println!("6. Problematic Case:");
let problematic = TestStyledString::new("\x1b[38;2;0;188;188m");
println!(" Cyan RGB: {:?}", problematic.style_codes);
println!(
" Has bold: {} (should be false - this was the bug!)",
problematic.test_has_bold()
);
println!(
" Has italic: {} (should be false)",
problematic.test_has_italic()
);
println!(
" Has underline: {} (should be false)",
problematic.test_has_underline()
);
println!();
println!("=== Test Summary ===");
println!("✅ RGB color codes should not be mistaken for text attributes");
println!("✅ Text attributes (1-4) should be correctly detected");
println!("✅ Combined sequences should work properly");
println!("✅ The parsing logic should handle complex ANSI sequences");
}