use thag_styling::{ColorSupport, Style, TermAttributes, TermBgLuma, Theme};
fn main() {
println!("=== TermAttributes Context Pattern Demo ===\n");
println!("1. Global TermAttributes (auto-detected):");
let global_attrs = TermAttributes::get_or_init();
println!(" Color Support: {:?}", global_attrs.color_support);
println!(" Theme: {}", global_attrs.theme.name);
println!(" Background Luma: {:?}", global_attrs.term_bg_luma);
println!(" How Initialized: {:?}", global_attrs.how_initialized);
println!(" Init Strategy: {:?}", global_attrs.init_strategy);
println!(
" Styled message: {}",
Style::for_role(thag_styling::Role::Success).paint("Global context works!")
);
println!();
println!("2. Creating custom context for testing:");
let custom_theme = Theme::get_builtin("basic_dark").unwrap();
let custom_attrs = TermAttributes::for_testing(
ColorSupport::Basic,
Some([64, 64, 64]),
TermBgLuma::Dark,
custom_theme,
);
println!(" Custom Color Support: {:?}", custom_attrs.color_support);
println!(" Custom Theme: {}", custom_attrs.theme.name);
println!(" Custom Background RGB: {:?}", custom_attrs.term_bg_rgb);
println!("\n3. Running code within custom context:");
custom_attrs.with_context(|| {
let context_attrs = TermAttributes::current();
println!(
" Inside context - Color Support: {:?}",
context_attrs.color_support
);
println!(" Inside context - Theme: {}", context_attrs.theme.name);
println!(
" Inside context - Background RGB: {:?}",
context_attrs.term_bg_rgb
);
println!(
" Styled message: {}",
Style::for_role(thag_styling::Role::Error).paint("Context-specific styling!")
);
println!("\n 3a. Nested context test:");
let nested_theme = Theme::get_builtin("solarized-dark").unwrap();
let nested_attrs = TermAttributes::for_testing(
ColorSupport::TrueColor,
Some([248, 248, 248]),
TermBgLuma::Light,
nested_theme,
);
nested_attrs.with_context(|| {
let nested_context = TermAttributes::current();
println!(
" Nested context - Color Support: {:?}",
nested_context.color_support
);
println!(
" Nested context - Theme: {}",
nested_context.theme.name
);
println!(
" Nested context - Background Luma: {:?}",
nested_context.term_bg_luma
);
println!(
" Styled message: {}",
Style::for_role(thag_styling::Role::Info).paint("Nested context!")
);
});
println!(
" Back to outer context - Theme: {}",
TermAttributes::current().theme.name
);
});
println!("\n4. Back to global context:");
let restored_attrs = TermAttributes::current();
println!(" Color Support: {:?}", restored_attrs.color_support);
println!(" Theme: {}", restored_attrs.theme.name);
println!(" Background Luma: {:?}", restored_attrs.term_bg_luma);
println!(
" Styled message: {}",
Style::for_role(thag_styling::Role::Success).paint("Global context restored!")
);
println!("\n=== Context Demo Complete ===");
}