CIO - Console Input/Output for Rust
CIO brings Python-like simplicity to Rust console operations with powerful procedural macros that enhance println! and provide type-safe input!. Experience advanced table formatting, ANSI colors, mathematical matrices, and seamless JSON integration.
β¨ Key Features
- π¨ Rich ANSI Colors: Easy
@(color, style)syntax - π Advanced Table Formatting: Professional layouts with Unicode borders
- π’ Mathematical Matrices: Beautiful matrix display with
:mand:dformats - π₯ Type-Safe Input: Automatic parsing with
input!macro - π Dynamic Separators: Custom output control with
$(...) - π JSON Native Support: Seamless
serde_jsonintegration - π Multiple Format Specifiers:
:t,:m,:d,:a,:c,:j
π Quick Start
Installation
[]
= "0.5.0"
= { = "1.0", = ["derive"] }
= "1.0"
Basic Usage
use ;
use json;
π₯ Type-Safe Input with input!
The input! macro provides automatic type parsing with elegant error handling:
use input;
// Basic types with automatic parsing
let name: String = input!;
let age: i32 = input!;
let height: f64 = input!;
let married: bool = input!;
let favorite_letter: char = input!;
Features:
- β Automatic Type Conversion: Handles all standard Rust types
- β
Color Support in Prompts: Use
@(color)syntax in input prompts - β Error Resilience: Automatically retries on invalid input
- β Zero Runtime Cost: All parsing happens at compile-time
π¨ Enhanced println! with ANSI Colors
Color Syntax
Use @(color, style) for rich terminal output:
use println;
// Basic colors
println!;
println!;
// Multiple styles
println!;
// Reset with @()
println!;
Available Colors
Standard Colors: black, red, green, yellow, blue, magenta, cyan, white
Bright Colors: bright_red, bright_green, bright_blue, bright_cyan, bright_magenta, bright_yellow, bright_white, bright_black
Styles: bold, italic, underline, dimmed, blink, reversed, hidden, strikethrough
Dynamic Colors
let color_style = "bright_blue,bold,italic";
let temp = 18.7;
println!;
π Dynamic Separators with $(...)
Control output flow with dynamic separators:
use println;
// Progress indicators without newlines
for i in 1..10
println!; // Final item with newline
// Output: 1 β 2 β 3 β 4 β 5 β 6 β 7 β 8 β 9 β 10
Separator Options:
$( β )- Custom separator string$(\n)- Explicit newline$(sep_variable)- Dynamic separator from variable
π Advanced Table Formatting
Smart Table Format (:t)
CIO automatically detects data structure and creates professional tables:
use println;
use json;
// JSON object β Key-value table
let capitals = json!;
println!;
Output:
βββββββββββ¬ββββββββββ
β Country β Capital β
βββββββββββΌββββββββββ€
β France β Paris β
βββββββββββΌββββββββββ€
β Germany β Berlin β
βββββββββββΌββββββββββ€
β Italy β Rome β
βββββββββββΌββββββββββ€
β Spain β Madrid β
βββββββββββ΄ββββββββββ
Complex 3D Structures
CIO handles deeply nested JSON with hierarchical headers:
let class_grades = json!;
println!;
Output:
ββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ
β 6A β 6B β
βββββββββββββββββΌβββββββ¬βββββββ¬βββββββ¬βββββββ¬βββββββΌβββββββ¬βββββββ¬βββββββ¬βββββββ¬βββββββ€
β FranΓ§ais β 16.0 β 13.0 β 18.0 β 15.0 β 17.0 β 14.0 β 12.0 β 15.0 β 13.0 β 14.0 β
βββββββββββββββββΌβββββββΌβββββββΌβββββββΌβββββββΌβββββββΌβββββββΌβββββββΌβββββββΌβββββββΌβββββββ€
β MathΓ©matiques β 15.0 β 11.0 β 16.0 β 14.0 β 15.0 β 12.0 β 11.0 β 14.0 β 13.0 β 11.0 β
βββββββββββββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ
Native Rust Collections
CIO works seamlessly with standard Rust collections:
use ;
// HashMap
let mut capitals = new;
capitals.insert;
capitals.insert;
println!;
// BTreeMap (automatically sorted)
let mut btree_map = new;
btree_map.insert;
btree_map.insert;
btree_map.insert;
println!;
// HashSet
let mut hash_set = new;
hash_set.insert;
hash_set.insert;
println!;
// VecDeque
let mut vecdeque = new;
vecdeque.push_back;
vecdeque.push_front;
println!;
π’ Mathematical Matrix Formatting
Matrix Format (:m)
Display matrices with proper mathematical notation:
let matrix_2d = vec!;
println!;
Output:
β 1 2 3 β
β 4 5 6 β
β 7 8 9 β
Determinant Format (:d)
Display determinants with vertical bars:
let matrix_small = vec!;
println!;
Output:
β 4 3 β
β 2 1 β
Matrix Operations
let matrix = vec!;
let sum: i32 = matrix.iter.flatten.sum;
println!;
println!;
π Complete Format Specifier Reference
| Format | Description | Best For |
|---|---|---|
:t |
Smart table formatting | Data display, JSON objects, collections |
:t(Col1, Col2) |
Table with custom headers | Structured data with specific column names |
:m |
Mathematical matrix notation | 2D arrays, mathematical computations |
:d |
Determinant notation | Square matrices, mathematical expressions |
:a |
Array format with indentation | Nested data structures, debug output |
:c |
Compact single-line format | Dense data, debugging |
:j |
JSON pretty-print format | Complex objects, configuration files |
Format Comparison Example
let demo_matrix = json!;
let demo_native = vec!;
println!;
println!;
println!;
println!;
println!;
π‘ Advanced Examples
Employee Data Analysis
use println;
use HashMap;
let dataset = vec!;
// Advanced data analysis with turbofish
let mut dept_count = new;
let mut dept_ages = new;
for in &dataset
println!;
for in &dept_count
Sales Data Analysis
let sales_data = vec!;
// Multi-dimensional analysis
let quarterly_summary: =
sales_data.iter
.fold;
println!;
for in &quarterly_summary
π― Best Practices
When to Use JSON vs Native Collections
Use JSON (json!) for:
- β Static data structures and demonstrations
- β Clean, readable data declarations
- β Complex nested structures for display
- β Table formatting examples
// Perfect for JSON
let config = json!;
println!;
Use Native Collections for:
- β Data analysis and computation
- β
Operations like
.iter(),.get(),.insert() - β Performance-critical code
- β Type safety and compile-time checks
// Perfect for Native
let mut sales = new;
sales.insert;
sales.insert;
let total: f64 = sales.values.sum; // Native operations
println!;
println!;
Color Usage Guidelines
// System messages
println!;
println!;
println!;
println!;
// Data highlighting
println!;
println!;
// Progress indicators
for i in 1..=5
Expression Integration
let first_name = "John";
let last_name = "Doe";
let age = 30;
let height = 1.85;
// Direct expressions in placeholders
println!;
println!;
println!;
// Complex expressions
let letter = 'A';
let category = if letter.is_alphabetic else ;
println!;
π§ Technical Details
Performance Characteristics
- Zero Runtime Overhead: All formatting decisions made at compile-time
- Memory Efficient: Optimized string building with capacity pre-allocation
- ANSI Optimized: Efficient color code generation and reset management
- Unicode Safe: Proper handling of multi-byte characters in alignment
Compatibility
- Rust Version: 1.70+ required for procedural macro features
- Terminal Support: Works with all ANSI-compatible terminals
- Platform Support: Cross-platform (Windows, macOS, Linux)
- Fallback Handling: Graceful degradation on unsupported terminals
Error Handling
// Input macro handles errors gracefully
let number: i32 = input!; // Retries automatically on invalid input
// Println macro provides safe defaults
let data = problematic_data;
println!; // Falls back to Debug format if JSON fails
π Examples Repository
All examples from this README are available in a complete demonstration:
# Add the complete main.rs example from the repository
π€ Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Roadmap
- Custom color themes
- More mathematical formats (vectors, tensors)
- Export formats (CSV, JSON, HTML)
- Interactive table editing
- Graph/chart ASCII rendering
- Custom format specifier plugins