boxen 0.3.0

A Rust library for creating styled terminal boxes around text with performance optimizations
Documentation

๐Ÿ“ฆ Boxen

Create beautiful boxes in the terminal with Rust

Crates.io Documentation CI Security Audit License Downloads

A Rust implementation of the popular boxen library for creating styled terminal boxes around text.

Features โ€ข Installation โ€ข Quick Start โ€ข Examples โ€ข Documentation


โœจ Features

๐ŸŽจ Styling

  • Multiple border styles (single, double, round, bold, custom)
  • Rich color support (named, hex, RGB)
  • Title support with positioning
  • Dim borders and backgrounds

๐Ÿ“ Layout

  • Flexible text alignment (left, center, right)
  • Precise padding and margins
  • Width and height constraints
  • Fullscreen mode

โšก Performance

  • 30x faster than baseline
  • Thread-local string pooling
  • Optional Unicode width caching
  • Optional terminal size caching

๐Ÿ›ก๏ธ Quality

  • Type-safe API with builder pattern
  • Comprehensive error handling
  • Unicode and ANSI aware
  • 100% backward compatible

๐Ÿ“ฆ Installation

Add boxen to your Cargo.toml:

[dependencies]

boxen = "0.3"

For maximum performance, enable caching features:

[dependencies]

boxen = { version = "0.3", features = ["width-cache", "terminal-cache"] }


๐Ÿš€ Quick Start

use ::boxen::{boxen, builder, BorderStyle, TextAlignment};

fn main() {
    // Simple box with default settings
    let simple = boxen("Hello, World!", None).unwrap();
    println!("{}", simple);
    // โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    // โ”‚Hello, World!โ”‚
    // โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    // Styled box with builder pattern
    let fancy = builder()
        .border_style(BorderStyle::Double)
        .padding(2)
        .text_alignment(TextAlignment::Center)
        .title("Greeting")
        .border_color("blue")
        .render("Hello, World!")
        .unwrap();
    println!("{}", fancy);
}

๐Ÿ“š Examples

Basic Usage

Code:

use ::boxen::boxen;

let result = boxen("Simple box", None)
    .unwrap();
println!("{}", result);

Output:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚Simple boxโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Styled Box

Code:

use ::boxen::{builder, BorderStyle};

let result = builder()
    .border_style(BorderStyle::Round)
    .padding(1)
    .title("Status")
    .border_color("green")
    .render("All systems operational")
    .unwrap();

Output:

โ•ญโ”€โ”€โ”€ Status โ”€โ”€โ”€โ”€โ•ฎ
โ”‚               โ”‚
โ”‚ All systems   โ”‚
โ”‚ operational   โ”‚
โ”‚               โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Convenience Functions

use ::boxen::{simple_box, double_box, round_box};

println!("{}", simple_box("Default style"));
println!("{}", double_box("Double border"));
println!("{}", round_box("Round corners"));

Advanced Styling

use ::boxen::{builder, BorderStyle, TextAlignment, TitleAlignment, Float};

let result = builder()
    .border_style(BorderStyle::Bold)
    .padding((2, 4, 2, 4))  // top, right, bottom, left
    .margin(1)
    .text_alignment(TextAlignment::Center)
    .title_alignment(TitleAlignment::Center)
    .float(Float::Center)
    .width(40)
    .title("๐ŸŽ‰ Celebration")
    .border_color("#ff6b6b")
    .background_color("#ffe66d")
    .render("Congratulations!\nYou've mastered boxen!")
    .unwrap();

๐ŸŽจ Border Styles

Boxen supports various border styles:


๐ŸŒˆ Color Support

Boxen supports multiple color formats:

use ::boxen::builder;

// Named colors (16 standard terminal colors)
builder()
    .border_color("red")
    .background_color("blue");

// Hex colors
builder()
    .border_color("#ff0000")
    .background_color("#0000ff");

// RGB colors
builder()
    .border_color((255, 0, 0))
    .background_color((0, 0, 255));

// Dim borders for subtle styling
builder()
    .border_color("cyan")
    .dim_border(true);

Available named colors: black, red, green, yellow, blue, magenta, cyan, white, bright-black, bright-red, bright-green, bright-yellow, bright-blue, bright-magenta, bright-cyan, bright-white


โšก Performance

Boxen is highly optimized for speed and memory efficiency:

Benchmark Results

Operation Time vs Baseline
Simple box 1.57ฮผs 30x faster โšก
Unicode content 2.93ฮผs 40x faster โšก
Complex styled box 12.2ฮผs -
Large text (1000 chars) 102.75ฮผs 8x faster โšก
Batch (100 boxes) 150ms 30x faster โšก

Core Optimizations

โœ… Thread-local string pooling - Reduces allocations by 24-87%
โœ… Smart buffer management - Pre-allocated buffers with capacity hints
โœ… Efficient ANSI handling - Proper escape sequence processing
โœ… Unicode optimization - Fast width calculations

Optional Performance Features

Enable caching for even better performance:

[dependencies]

boxen = { version = "0.3", features = ["width-cache", "terminal-cache"] }

Feature Benefit Use Case
width-cache 2-3x faster Unicode Apps with CJK text, emoji
terminal-cache 10-20% faster batch Rendering multiple boxes
dhat-heap Memory profiling Development & optimization

Performance gains:

  • 90% cache hit rates for typical workloads

  • Lock-free thread-local caching
  • Automatic cache invalidation on terminal resize (Unix)
  • Configurable cache sizes and TTL

๐Ÿ“– See Performance Guide for detailed information.


๐ŸŽฏ Use Cases

CLI Tools

// Success messages
println!("{}",
    simple_box("โœ“ Build successful!")
);

// Error messages
println!("{}",
    builder()
        .border_color("red")
        .render("โœ— Build failed")
        .unwrap()
);

Status Displays

// System status
println!("{}",
    builder()
        .title("System Status")
        .border_color("green")
        .render("All systems operational")
        .unwrap()
);

Notifications

// User notifications
println!("{}",
    builder()
        .title("๐Ÿ”” Notification")
        .padding(2)
        .render("You have 3 new messages")
        .unwrap()
);

๐Ÿ“– Documentation

Guides

Examples

Run the included examples to see boxen in action:

# Basic usage patterns

cargo run --example main_api_demo


# Color demonstrations

cargo run --example color_demo


# Comprehensive feature showcase

cargo run --example comprehensive_demo


# Performance testing

cargo run --example performance_demo


# Caching features demo

cargo run --example caching_demo --features width-cache,terminal-cache


# Memory profiling

cargo run --example memory_profiling --features dhat-heap


# Error handling patterns

cargo run --example error_handling_demo


# Fullscreen mode

cargo run --example fullscreen_demo


# Interactive clock with spinner

cargo run --example clock_spinner


๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

  1. ๐Ÿ› Report bugs - Open an issue with details
  2. ๐Ÿ’ก Suggest features - Share your ideas
  3. ๐Ÿ“ Improve docs - Help others learn
  4. ๐Ÿ”ง Submit PRs - Fix bugs or add features

Please read our Contributing Guide for details.


๐Ÿ“œ License

This project is licensed under either of:

at your option.


๐Ÿ™ Acknowledgments


โฌ† back to top

Made with ๐Ÿฆ€ Rust โ€ข Report Bug โ€ข Request Feature