Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

71 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ฆ 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
  • Dynamic width/height with closures
  • Fixed width/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.4"

For maximum performance, enable caching features:

[dependencies]
boxen = { version = "0.4", 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();

Dynamic Sizing

Boxen supports both fixed and dynamic width/height using closures that adapt to available terminal space:

use boxen::builder;

// Fixed width
let fixed = builder()
    .width(50)
    .render("Fixed width box")
    .unwrap();

// Dynamic width - 80% of terminal, minimum 30 columns
let dynamic = builder()
    .width(|available| (available * 4 / 5).max(30))
    .render("Dynamic width adapts to terminal size")
    .unwrap();

Markdown Rendering

Transform markdown syntax into beautifully styled terminal output:

Code:

use boxen::builder;

let markdown = r#"
# Commands

**create** - Create a new item
**delete** - Remove an item

Use `--help` for more info
"#;

let result = builder()
    .title("Help")
    .markdown()  // Enable markdown
    .render(markdown)
    .unwrap();
println!("{}", result);

Features:

  • Headers (H1-H6) with colors
  • Bold, italic, strikethrough
  • Inline code styling
  • Code blocks
  • Lists (ordered & unordered)
  • Links with display options
  • Horizontal rules
  • Blockquotes

Custom Markdown Styling:

use boxen::{builder, Color, markdown::{MarkdownStyle, LinkStyle, ItalicStyle}};

let style = MarkdownStyle {
    h1_color: Color::Named("magenta".to_string()),
    bold_color: Some(Color::Named("yellow".to_string())),
    link_style: LinkStyle::ShowUrl,
    italic_style: ItalicStyle::Underline,
    ..Default::default()
};

let result = builder()
    .markdown_with_style(style)
    .render("# Custom **styling** for [links](https://example.com)")
    .unwrap();

Quick Markdown Box:

use boxen::markdown_box;

// One-line markdown rendering
println!("{}", markdown_box("# Quick\n**Easy** markdown!"));

// Fixed width (traditional approach) let result = builder() .width(50) .render("Fixed width box") .unwrap();

// Dynamic width - use 80% of available terminal width let result = builder() .width(|available: usize| (available * 4 / 5).max(30)) .render("This box adapts to terminal width") .unwrap();

// Dynamic height - use 50% of available terminal height let result = builder() .height(|available: usize| (available / 2).max(10)) .render("Multi\nLine\nContent") .unwrap();

// Both dynamic - fully responsive box let result = builder() .width(|available: usize| (available * 3 / 4).max(40)) .height(|available: usize| (available / 3).max(8)) .render("Fully responsive box") .unwrap();

// Mix fixed and dynamic let result = builder() .width(|available: usize| available.min(60)) // Cap at 60 columns .height(15) // Fixed height .render("Dynamic width, fixed height") .unwrap();


**Key features:**

- ๐ŸŽฏ **Unified API** - Same `.width()` and `.height()` methods accept both fixed values and closures
- ๐Ÿ“ **Terminal-aware** - Closures receive available terminal space as parameter
- ๐Ÿ”„ **100% backward compatible** - Existing code using `.width(50)` continues to work
- ๐ŸŽจ **Flexible** - Mix fixed and dynamic dimensions as needed

---

## ๐ŸŽจ Border Styles

Boxen supports various border styles:

<table>
<tr>
<th>Style</th>
<th>Preview</th>
<th>Description</th>
</tr>
<tr>
<td><code>Single</code></td>
<td><pre>โ”Œโ”€โ”
โ”‚ โ”‚
โ””โ”€โ”˜</pre></td>
<td>Clean single-line borders</td>
</tr>
<tr>
<td><code>Double</code></td>
<td><pre>โ•”โ•โ•—
โ•‘ โ•‘
โ•šโ•โ•</pre></td>
<td>Bold double-line borders</td>
</tr>
<tr>
<td><code>Round</code></td>
<td><pre>โ•ญโ”€โ•ฎ
โ”‚ โ”‚
โ•ฐโ”€โ•ฏ</pre></td>
<td>Smooth rounded corners</td>
</tr>
<tr>
<td><code>Bold</code></td>
<td><pre>โ”โ”โ”“
โ”ƒ โ”ƒ
โ”—โ”โ”›</pre></td>
<td>Heavy bold borders</td>
</tr>
<tr>
<td><code>SingleDouble</code></td>
<td><pre>โ•“โ”€โ•–
โ•‘ โ•‘
โ•™โ”€โ•œ</pre></td>
<td>Single horizontal, double vertical</td>
</tr>
<tr>
<td><code>DoubleSingle</code></td>
<td><pre>โ•’โ•โ••
โ”‚ โ”‚
โ•˜โ•โ•›</pre></td>
<td>Double horizontal, single vertical</td>
</tr>
<tr>
<td><code>Classic</code></td>
<td><pre>+--+
|  |
+--+</pre></td>
<td>ASCII-compatible classic style</td>
</tr>
</table>

---

## ๐ŸŒˆ Color Support

Boxen supports multiple color formats:

```rust
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));

// Title colors (independent from border color)
builder()
    .title("Status")
    .title_color("green")
    .border_color("blue");

// 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

# Dynamic width/height sizing
cargo run --example dynamic_sizing_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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages