Crate tree_fmt

Crate tree_fmt 

Source
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 insertion
  • TreeFormatter: Renders trees with configurable symbols and display options
  • TableFormatter: 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 \x mode, 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§

ColumnData
Multi-column data for aligned tree formatting
ExpandedConfig
Formatter parameters for expanded (vertical record) output
RowBuilder
Builder for constructing table-shaped trees
TableConfig
Formatter parameters for table output
TableMetadata
Metadata describing table structure and column types
TableView
Canonical table data structure for unified formatting
TreeBuilder
Builder for constructing tree structures from flat data
TreeConfig
Formatter parameters for tree output
TreeNode
Generic tree node that can hold any data type
TreeSymbols
Tree symbols used for rendering

Enums§

BorderVariant
Border rendering variant for tables
ColumnSeparator
Column separator parameter
DataType
Data type classification for table columns
HeaderSeparatorVariant
Header separator line variant
PaddingSide
Where to place alignment padding in key-value pairs

Traits§

TableShapedView
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).