Crate cli_boxes

Crate cli_boxes 

Source
Expand description

ยงCLI Boxes

Crates.io Documentation License

A high-performance Rust library providing Unicode box-drawing characters for creating beautiful CLI interfaces. Perfect for terminal applications, CLI tools, and text-based user interfaces.

ยง๐ŸŽฏ Features

  • 9 Beautiful Box Styles: From elegant single-line to decorative arrows
  • Unicode & ASCII Support: Full Unicode compliance with ASCII fallback
  • Zero-Cost Abstractions: Compile-time constants and zero-allocation parsing
  • Type Safety: Strong typing prevents incorrect usage
  • Ergonomic Builder API: Fluent interface for custom box creation
  • String Parsing: Parse styles from configuration files or user input
  • Serde Integration: Optional serialization support (feature-gated)
  • Comprehensive Testing: 100% test coverage with extensive edge case handling

ยง๐Ÿ“ฆ Box Styles

StylePreviewUse Case
SINGLEโ”Œโ”€โ”โ”‚โ”˜โ”€โ””โ”‚General purpose, clean appearance
DOUBLEโ•”โ•โ•—โ•‘โ•โ•โ•šโ•‘Emphasis, important content
ROUNDโ•ญโ”€โ•ฎโ”‚โ•ฏโ”€โ•ฐโ”‚Modern, soft appearance
BOLDโ”โ”โ”“โ”ƒโ”›โ”โ”—โ”ƒStrong emphasis, headers
SINGLE_DOUBLEโ•“โ”€โ•–โ•‘โ•œโ”€โ•™โ•‘Mixed style, unique look
DOUBLE_SINGLEโ•’โ•โ••โ”‚โ•›โ•โ•˜โ”‚Alternative mixed style
CLASSIC+โ”€+|+โ”€+|ASCII compatibility, legacy systems
ARROWโ†˜โ†“โ†™โ†โ†–โ†‘โ†—โ†’Decorative, special effects
NONE Invisible borders, spacing

ยง๐Ÿš€ Quick Start

Add to your Cargo.toml:

[dependencies]
cli-boxes = "0.1.0"

ยงBasic Usage

use cli_boxes::{BoxChars, BorderStyle};

// Create a simple box
let box_chars = BoxChars::SINGLE;
println!("{}{}{}",
    box_chars.top_left,
    box_chars.top.to_string().repeat(10),
    box_chars.top_right
);
println!("{}          {}", box_chars.left, box_chars.right);
println!("{}{}{}",
    box_chars.bottom_left,
    box_chars.bottom.to_string().repeat(10),
    box_chars.bottom_right
);
// Output:
// โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
// โ”‚          โ”‚
// โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

ยงAdvanced Usage with Builder Pattern

use cli_boxes::BoxChars;

// Create custom box with builder pattern
let custom = BoxChars::builder()
    .corners('โ—')
    .horizontal('โ•')
    .vertical('โ•‘')
    .top_left('โ•”')  // Override specific corner
    .build();

// Asymmetric design
let fancy = BoxChars::builder()
    .top_left('โ•ญ').top('โ”€').top_right('โ•ฎ')
    .left('โ”‚').right('โ”‚')
    .bottom_left('โ””').bottom('โ”€').bottom_right('โ”˜')
    .build();

ยงDynamic Style Selection

use cli_boxes::{BorderStyle, BoxChars};

// Parse from configuration
let style: BorderStyle = "double".parse().unwrap();
let box_chars = style.chars();

// Iterate through all styles
for style in BorderStyle::all() {
    let chars = style.chars();
    println!("{}: {}", style, chars);
}

ยง๐ŸŽจ Real-World Examples

ยงCreating a Status Box

use cli_boxes::BoxChars;

fn create_status_box(message: &str, width: usize) -> String {
    let box_chars = BoxChars::DOUBLE;
    let padding = width.saturating_sub(message.len() + 2);
    let left_pad = padding / 2;
    let right_pad = padding - left_pad;
     
    format!(
        "{}{}{}\n{}{}{}{}{}\n{}{}{}",
        box_chars.top_left,
        box_chars.top.to_string().repeat(width),
        box_chars.top_right,
        box_chars.left,
        " ".repeat(left_pad),
        message,
        " ".repeat(right_pad),
        box_chars.right,
        box_chars.bottom_left,
        box_chars.bottom.to_string().repeat(width),
        box_chars.bottom_right
    )
}

ยงConfiguration-Driven Boxes

use cli_boxes::{BorderStyle, BoxChars};
use std::collections::HashMap;

fn get_box_for_level(level: &str) -> BoxChars {
    let styles: HashMap<&str, BorderStyle> = [
        ("info", BorderStyle::Single),
        ("warning", BorderStyle::Bold),
        ("error", BorderStyle::Double),
        ("success", BorderStyle::Round),
    ].iter().cloned().collect();
     
    styles.get(level)
        .map(|s| s.chars())
        .unwrap_or(BoxChars::CLASSIC)
}

ยงโšก Performance

This library is designed for maximum performance:

  • Zero Allocations: String parsing uses byte-level comparison without heap allocation
  • Compile-Time Constants: All predefined styles are const and computed at compile time
  • Minimal Dependencies: Only strum for enum iteration, optional serde for serialization
  • Small Memory Footprint: BoxChars is only 32 bytes (8 ร— 4-byte chars)

ยง๐Ÿ”ง Optional Features

ยงSerde Support

Enable serialization support:

[dependencies]
cli-boxes = { version = "0.1.0", features = ["serde"] }
use cli_boxes::{BoxChars, BorderStyle};
use serde_json;

let style = BorderStyle::Double;
let json = serde_json::to_string(&style).unwrap();
let deserialized: BorderStyle = serde_json::from_str(&json).unwrap();

ยง๐Ÿ›ก๏ธ Error Handling

The library provides helpful error messages for invalid input:

use cli_boxes::BorderStyle;

match "invalid_style".parse::<BorderStyle>() {
    Ok(style) => println!("Parsed: {}", style),
    Err(e) => println!("{}", e),
    // Output: Invalid border style: 'invalid_style'.
    // Did you mean one of: none, single, double, round, bold,
    // single_double, double_single, classic, arrow?
}
  • tui - Terminal user interface library
  • crossterm - Cross-platform terminal manipulation
  • console - Terminal and console abstraction

ยง๐Ÿ“„ License

This project is licensed under either of

at your option.

Structsยง

BorderStyleIter
An iterator over the variants of BorderStyle
BoxChars
A collection of Unicode characters used for drawing boxes in CLI applications.
BoxCharsBuilder
Builder for creating custom BoxChars with a fluent, type-safe API.

Enumsยง

BorderStyle
Available box drawing styles with semantic meaning and use cases.
ParseBorderStyleError
Error type for parsing BorderStyle from string.