use cli_boxes::{BorderStyle, BoxChars};
use std::collections::HashMap;
fn main() {
println!("🎨 CLI Boxes - Comprehensive Example\n");
showcase_all_styles();
demonstrate_custom_boxes();
demonstrate_string_parsing();
demonstrate_real_world_usage();
demonstrate_performance_features();
}
fn showcase_all_styles() {
println!("📦 All Available Box Styles:");
println!("{:-<60}", "");
let descriptions = [
(BorderStyle::None, "Invisible borders (spacing only)"),
(BorderStyle::Single, "Clean, professional single-line"),
(BorderStyle::Double, "Bold emphasis with double-line"),
(BorderStyle::Round, "Modern rounded corners"),
(BorderStyle::Bold, "Maximum emphasis with thick lines"),
(
BorderStyle::SingleDouble,
"Mixed: single horizontal, double vertical",
),
(
BorderStyle::DoubleSingle,
"Mixed: double horizontal, single vertical",
),
(BorderStyle::Classic, "ASCII-compatible for legacy systems"),
(BorderStyle::Arrow, "Decorative arrows for special effects"),
];
for (style, description) in &descriptions {
let chars = style.chars();
println!("{:12} | {} | {}", style.to_string(), chars, description);
}
println!();
}
fn demonstrate_custom_boxes() {
println!("🔧 Custom Box Creation:");
println!("{:-<60}", "");
let uniform = BoxChars::builder()
.corners('●')
.horizontal('═')
.vertical('║')
.build();
println!("Uniform style: {}", uniform);
let mixed = BoxChars::builder()
.corners('●')
.horizontal('═')
.vertical('║')
.top_left('╔')
.build();
println!("Selective override: {}", mixed);
let asymmetric = BoxChars::builder()
.top_left('╭')
.top('─')
.top_right('╮')
.left('│')
.right('│')
.bottom_left('└')
.bottom('─')
.bottom_right('┘')
.build();
println!("Asymmetric design: {}", asymmetric);
let direct = BoxChars::new('*', '-', '*', '|', '*', '-', '*', '|');
println!("Direct constructor: {}", direct);
println!();
}
fn demonstrate_string_parsing() {
println!("📝 String Parsing & Error Handling:");
println!("{:-<60}", "");
let test_inputs = [
"single", "DOUBLE", "single-double", "single_double", "Round", "invalid_style", "", ];
for input in &test_inputs {
match input.parse::<BorderStyle>() {
Ok(style) => {
let chars = style.chars();
println!("✓ '{}' -> {} ({})", input, style, chars);
}
Err(e) => {
println!("✗ '{}' -> {}", input, e);
}
}
}
println!();
}
fn demonstrate_real_world_usage() {
println!("🌍 Real-World Usage Patterns:");
println!("{:-<60}", "");
let config_styles: HashMap<&str, BorderStyle> = [
("info", BorderStyle::Single),
("warning", BorderStyle::Bold),
("error", BorderStyle::Double),
("success", BorderStyle::Round),
]
.iter()
.cloned()
.collect();
println!("Configuration-driven styles:");
for (level, style) in &config_styles {
let chars = style.chars();
println!(" {}: {} ({})", level, style, chars);
}
println!("\nSample message boxes:");
create_message_box(
"Information",
"This is an info message",
BorderStyle::Single,
);
create_message_box("Warning", "This is a warning!", BorderStyle::Bold);
create_message_box("Error", "Something went wrong!", BorderStyle::Double);
println!("\nDynamic width adjustment:");
let messages = [
"Short",
"Medium length message",
"This is a very long message that needs more space",
];
for msg in &messages {
let width = msg.len() + 4; create_simple_box(msg, width, BorderStyle::Round);
}
println!();
}
fn demonstrate_performance_features() {
println!("⚡ Performance Features:");
println!("{:-<60}", "");
println!("✓ Compile-time constants (zero runtime cost)");
const COMPILE_TIME_BOX: BoxChars = BoxChars::SINGLE;
println!(" Const box: {}", COMPILE_TIME_BOX);
println!("✓ Zero-allocation string parsing");
let parse_count = 1000;
let start = std::time::Instant::now();
for _ in 0..parse_count {
let _ = "single".parse::<BorderStyle>().unwrap();
let _ = "double".parse::<BorderStyle>().unwrap();
let _ = "single-double".parse::<BorderStyle>().unwrap();
}
let duration = start.elapsed();
println!(
" Parsed {} styles in {:?} (avg: {:?} per parse)",
parse_count * 3,
duration,
duration / (parse_count * 3)
);
println!("✓ Memory efficiency");
println!(" BoxChars size: {} bytes", std::mem::size_of::<BoxChars>());
println!(
" BorderStyle size: {} bytes",
std::mem::size_of::<BorderStyle>()
);
println!("✓ Efficient iteration over all styles");
let start = std::time::Instant::now();
let mut count = 0;
for _ in 0..10000 {
for style in BorderStyle::all() {
let _chars = style.chars();
count += 1;
}
}
let duration = start.elapsed();
println!(" Processed {} style conversions in {:?}", count, duration);
println!();
}
fn create_message_box(title: &str, message: &str, style: BorderStyle) {
let chars = style.chars();
let width = std::cmp::max(title.len(), message.len()) + 4;
print!("{}", chars.top_left);
print!("{}", chars.top.to_string().repeat(width));
println!("{}", chars.top_right);
let title_padding = width - title.len() - 2;
let left_pad = title_padding / 2;
let right_pad = title_padding - left_pad;
print!("{}", chars.left);
print!("{}", " ".repeat(left_pad));
print!("{}", title);
print!("{}", " ".repeat(right_pad));
println!("{}", chars.right);
if title != message {
print!("{}", chars.left);
print!("{}", chars.top.to_string().repeat(width));
println!("{}", chars.right);
}
let msg_padding = width - message.len() - 2;
let left_pad = msg_padding / 2;
let right_pad = msg_padding - left_pad;
print!("{}", chars.left);
print!("{}", " ".repeat(left_pad));
print!("{}", message);
print!("{}", " ".repeat(right_pad));
println!("{}", chars.right);
print!("{}", chars.bottom_left);
print!("{}", chars.bottom.to_string().repeat(width));
println!("{}", chars.bottom_right);
println!();
}
fn create_simple_box(content: &str, width: usize, style: BorderStyle) {
let chars = style.chars();
let content_width = width.saturating_sub(2);
let padding = content_width.saturating_sub(content.len());
let left_pad = padding / 2;
let right_pad = padding - left_pad;
print!("{}", chars.top_left);
print!("{}", chars.top.to_string().repeat(content_width));
println!("{}", chars.top_right);
print!("{}", chars.left);
print!("{}", " ".repeat(left_pad));
print!("{}", content);
print!("{}", " ".repeat(right_pad));
println!("{}", chars.right);
print!("{}", chars.bottom_left);
print!("{}", chars.bottom.to_string().repeat(content_width));
println!("{}", chars.bottom_right);
}