CLI Boxes
A Rust library providing Unicode box-drawing characters for creating beautiful CLI interfaces.
Features
- 9 Different Box Styles: From simple single-line to decorative arrows (including invisible)
- Unicode Support: Proper Unicode box-drawing characters
- ASCII Fallback: Classic ASCII style for maximum compatibility
- Ergonomic API: Both low-level
BoxCharsand high-levelBorderStyleenum - Builder Pattern: Fluent API for creating custom box characters
- Constructor Methods: Direct instantiation with
new()method - String Conversion: Parse styles from strings and convert to strings
- Serde Support: Serialize and deserialize box character sets (feature-gated)
- Type Safety: Strong typing ensures correct usage
- Iterator Support: Enumerate all available styles
- Performance Optimized: Zero-allocation string parsing and const evaluation
Box Styles
| Style | Example | Description |
|---|---|---|
NONE |
|
Invisible borders (all spaces) |
SINGLE |
┌─┐│┘─└│ |
Clean single-line borders |
DOUBLE |
╔═╗║╝═╚║ |
Bold double-line borders |
ROUND |
╭─╮│╯─╰│ |
Soft rounded corners |
BOLD |
┏━┓┃┛━┗┃ |
Thick, bold lines |
SINGLE_DOUBLE |
╓─╖║╜─╙║ |
Single horizontal, double vertical |
DOUBLE_SINGLE |
╒═╕│╛═╘│ |
Double horizontal, single vertical |
CLASSIC |
+─+|+─+| |
ASCII-compatible characters |
ARROW |
↘↓↙←↖↑↗→ |
Decorative arrow style |
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
# Enable serde support (optional)
= { = "0.1.0", = ["serde"] }
Usage
Using BorderStyle Enum (Recommended)
use ;
// Use the ergonomic BorderStyle enum
let style = Single;
let box_chars = style.chars;
// Or convert directly
let box_chars = from;
// Parse from string (case-insensitive, supports kebab-case)
let style: BorderStyle = "single-double".parse.unwrap;
let box_chars = style.chars;
// Convert to string
println!; // Output: "round"
Using BoxChars Directly
use BoxChars;
// Create a simple box with single-line characters
let box_chars = SINGLE;
println!;
println!;
println!;
Output:
┌──────────┐
│ │
└──────────┘
Iterating Through All Styles
use BorderStyle;
// Iterate through all available styles
for style in all
String Parsing
The BorderStyle enum supports parsing from strings with flexible formatting:
use FromStr;
use BorderStyle;
// Case-insensitive parsing
let style1 = "SINGLE"..unwrap;
let style2 = "double"..unwrap;
// Supports both snake_case and kebab-case
let style3 = "single_double"..unwrap;
let style4 = "single-double"..unwrap;
// Error handling with helpful suggestions
match "invalid_style".
Custom Box Characters
You can create custom box character sets in several ways:
Using the Constructor
use BoxChars;
let custom = new;
Using the Builder Pattern (Recommended)
use BoxChars;
// Set all corners and sides uniformly
let uniform = builder
.corners
.horizontal
.vertical
.build;
// Mix and match for asymmetric designs
let asymmetric = builder
.top_left
.top_right
.bottom_left
.bottom_right
.horizontal
.vertical
.build;
// Override specific characters
let mixed = builder
.corners
.horizontal
.vertical
.top_left // Override just the top-left corner
.build;
Using Struct Literals
use BoxChars;
let custom = BoxChars ;
API Reference
BorderStyle Methods
chars()- Get theBoxCharsfor this styleall()- Iterator over all available stylesto_string()- Convert to lowercase string representationparse()- Parse from string (viaFromStr)
BoxChars Methods
new()- Constructor for creating custom box charactersbuilder()- Returns aBoxCharsBuilderfor fluent API constructiondefault()- ReturnsBoxChars::SINGLE
BoxChars Constants
BoxChars::NONE- Invisible borders (spaces)BoxChars::SINGLE- Single-line Unicode bordersBoxChars::DOUBLE- Double-line Unicode bordersBoxChars::ROUND- Rounded corner bordersBoxChars::BOLD- Bold/thick line bordersBoxChars::SINGLE_DOUBLE- Mixed single/double bordersBoxChars::DOUBLE_SINGLE- Mixed double/single bordersBoxChars::CLASSIC- ASCII-compatible bordersBoxChars::ARROW- Decorative arrow borders
BoxCharsBuilder Methods
corners(char)- Set all four corner charactershorizontal(char)- Set top and bottom border charactersvertical(char)- Set left and right border characterstop_left(char),top(char),top_right(char)- Set individual charactersright(char),bottom_right(char),bottom(char)- Set individual charactersbottom_left(char),left(char)- Set individual charactersbuild()- Construct the finalBoxChars
Requirements
- Rust 2024 edition or later
- No additional runtime dependencies (except optional
serdeandstrum)
Performance
This library is designed for performance:
- Zero Allocations: String parsing avoids heap allocations through optimized byte-level comparison
- Const Evaluation: All predefined box characters are compile-time constants
- Efficient Parsing: Case-insensitive parsing with hyphen/underscore normalization without string conversion
Serde Support
When the serde feature is enabled, all types can be serialized and deserialized:
[]
= { = "0.1.0", = ["serde"] }
= "1.0" # For JSON serialization (not included with cli-boxes)
use ;
use serde_json;
// Serialize a BorderStyle
let style = Double;
let json = to_string.unwrap;
// Serialize BoxChars
let chars = ROUND;
let json = to_string.unwrap;
License
This project is licensed under the MIT OR Apache-2.0 License - see the LICENSE file for details.