Expand description
Tree and Table Formatter Module
Provides abstract, reusable formatters for hierarchical data display.
§Features
- Generic
TreeNode: Works with any data type TreeBuilder: Constructs trees from flat data with path-based insertionTreeFormatter: Renders trees with configurable symbols and display optionsTableFormatter: Renders tabular data with borders and alignment- String Output: All formatters return
String, no direct console output
The library supports three interchangeable display formats:
- Table: Horizontal tabular display (standard row-column layout)
- Expanded: Vertical record display (
PostgreSQL\xmode, key-value pairs) - Tree: Hierarchical tree display (outline with box-drawing characters)
§Examples
§Same data in all three formats
use tree_fmt::{ RowBuilder, TableFormatter, ExpandedFormatter, TreeFormatter };
// Create tabular data
let tree = RowBuilder::new( vec![ "Name".into(), "Age".into() ] )
.add_row( vec![ "Alice".into(), "30".into() ] )
.add_row( vec![ "Bob".into(), "25".into() ] )
.build();
// Table format
let table_fmt = TableFormatter::new();
let output = table_fmt.format( &tree );
// Expanded format
let expanded_fmt = ExpandedFormatter::new();
let output = expanded_fmt.format( &tree );
// Tree format (table-shaped tree)
let tree_fmt = TreeFormatter::default();
let output = tree_fmt.format( &tree, Clone::clone );§Expanded format with colored keys
use tree_fmt::{ RowBuilder, ExpandedFormatter, ExpandedConfig };
let tree = RowBuilder::new( vec![ "Name".into(), "Score".into() ] )
.add_row( vec![ "Alice".into(), "95".into() ] )
.build();
// Gray keys for terminal output (PostgreSQL style)
let formatter = ExpandedFormatter::with_config(
ExpandedConfig::new().colorize_keys( true )
);
let output = formatter.format( &tree );§Property list style (colon separator)
use tree_fmt::{ RowBuilder, ExpandedFormatter, ExpandedConfig };
let tree = RowBuilder::new( vec![ "Command".into(), "Status".into() ] )
.add_row( vec![ "build".into(), "success".into() ] )
.build();
// Property list style: no record headers, colon separator
let formatter = ExpandedFormatter::with_config( ExpandedConfig::property_style() );
let output = formatter.format( &tree );
// Output:
// Command: build
// Status: success§Building and formatting a tree
use tree_fmt::{ TreeBuilder, TreeFormatter };
let tree = TreeBuilder::new( "root" )
.insert( &[ "src", "main.rs" ], 150 )
.insert( &[ "src", "lib.rs" ], 300 )
.build();
let formatter = TreeFormatter::new();
let output = formatter.format( &tree, | lines | format!( "{} lines", lines ) );
println!( "{}", output );§Building from items
use tree_fmt::TreeBuilder;
use std::path::PathBuf;
let files = vec![
( PathBuf::from( "src/main.rs" ), 100 ),
( PathBuf::from( "tests/test.rs" ), 50 ),
];
let tree = TreeBuilder::from_items( &files, | ( path, _size ) | {
path.components().map( | c | c.as_os_str().to_string_lossy().to_string() ).collect()
}, | ( path, size ) | ( path.clone(), *size ) );§Formatting a table
use tree_fmt::{ RowBuilder, TableFormatter };
let tree = RowBuilder::new( vec![ "File".into(), "Lines".into() ] )
.add_row( vec![ "main.rs".into(), "100".into() ] )
.add_row( vec![ "lib.rs".into(), "200".into() ] )
.build();
let formatter = TableFormatter::new();
let output = formatter.format( &tree );
println!( "{}", output );Re-exports§
pub use formatters::Format;pub use formatters::FormatError;pub use formatters::TableFormatter;pub use formatters::ExpandedFormatter;pub use formatters::TreeFormatter;pub use formatters::LogfmtFormatter;pub use formatters::TableShapedFormatter;
Modules§
- conversions
- Data conversion utilities between tree representations
- formatters
- Formatting implementations for different display modes
Structs§
- Column
Data - Multi-column data for aligned tree formatting
- Expanded
Config - Formatter parameters for expanded (vertical record) output
- RowBuilder
- Builder for constructing table-shaped trees
- Table
Config - Formatter parameters for table output
- Table
Metadata - Metadata describing table structure and column types
- Table
View - Canonical table data structure for unified formatting
- Tree
Builder - Builder for constructing tree structures from flat data
- Tree
Config - Formatter parameters for tree output
- Tree
Node - Generic tree node that can hold any data type
- Tree
Symbols - Tree symbols used for rendering
Enums§
- Border
Variant - Border rendering variant for tables
- Column
Separator - Column separator parameter
- Data
Type - Data type classification for table columns
- Header
Separator Variant - Header separator line variant
- Padding
Side - Where to place alignment padding in key-value pairs
Traits§
- Table
Shaped View - Trait for working with table-shaped trees
Functions§
- pad_
to_ width - Pad text to target display width while respecting ANSI codes and wide Unicode characters.
- truncate_
cell - Truncate text to maximum visual width with ANSI code preservation
- visual_
len - Calculate the visible character count (char-based, Tier 1).