Boxen
A Rust implementation of the popular boxen library for creating styled terminal boxes around text.
Features
- ๐จ Multiple border styles - Single, double, round, bold, and custom borders
- ๐ Flexible alignment - Left, center, and right text alignment
- ๐ฏ Precise spacing - Fine-grained control over padding and margins
- ๐ Rich colors - Support for named colors, hex codes, and RGB values
- ๐ Title support - Add titles with customizable positioning
- ๐ค Unicode aware - Proper handling of Unicode characters and ANSI escape sequences
- ๐ฑ Responsive - Fullscreen mode and terminal-aware layouts
- โก Performance optimized - Minimal allocations and efficient text processing
- ๐ก๏ธ Type safe - Comprehensive error handling with descriptive messages
Installation
Add this to your Cargo.toml:
[]
= "0.1.3"
Quick Start
use ;
Examples
Basic Usage
use boxen;
let result = boxen.unwrap;
println!;
Output:
โโโโโโโโโโโโ
โSimple boxโ
โโโโโโโโโโโโ
Builder Pattern
use ;
let result = builder
.border_style
.padding
.text_alignment
.width
.title
.border_color
.render
.unwrap;
println!;
Output:
โญโโโ Status โโโโโฎ
โ โ
โ All systems โ
โ operational โ
โ โ
โฐโโโโโโโโโโโโโโโโฏ
Convenience Functions
use ;
println!;
println!;
println!;
Advanced Styling
use ;
let result = builder
.border_style
.padding // top, right, bottom, left
.margin
.text_alignment
.title_alignment
.float
.width
.height
.title
.border_color
.background_color
.render
.unwrap;
println!;
Error Handling
use ;
match builder
.width // Too narrow
.padding // Too much padding
.render
Border Styles
Boxen supports various border styles:
| Style | Preview |
| -------------- | ----------- | --- | --- | --- | ----- |
| Single | โโโโ โโโโ |
| Double | โโโโ โโโโ |
| Round | โญโโฎโ โโฐโโฏ |
| Bold | โโโโ โโโโ |
| SingleDouble | โโโโ โโโโ |
| DoubleSingle | โโโโ โโโโ |
| Classic | +--+ | | | | +--+ |
Color Support
Boxen supports multiple color formats:
use builder;
// Named colors
builder.border_color;
builder.background_color;
// Hex colors
builder.border_color;
builder.background_color;
// RGB colors
builder.border_color;
builder.background_color;
Performance
Boxen is optimized for performance:
- Minimal allocations: Smart string buffer management
- Unicode aware: Efficient width calculation for international text
- ANSI handling: Proper escape sequence processing
- Caching: Terminal dimensions and expensive calculations are cached
Benchmark results on a modern machine:
- Simple box: ~10ฮผs
- Complex styled box: ~50ฮผs
- Large text (1000 lines): ~2ms
Examples
Run the included examples to see boxen in action:
# Basic usage patterns
# Color demonstrations
# Comprehensive feature showcase
# Performance testing
# Error handling patterns
# Fullscreen mode
# Interactive clock with spinner
Documentation
- API Documentation - Complete API reference
- Usage Guide - Detailed usage examples
- Customization Guide - Advanced styling techniques
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
- Inspired by the original boxen TypeScript library by Sindre Sorhus
- Built with โค๏ธ for the Rust community