pub struct TabularFormatter { /* private fields */ }Expand description
Formats table rows according to a specification.
The formatter holds resolved column widths and produces formatted rows. It supports row-by-row formatting for interleaved output patterns.
§Example
use standout_render::tabular::{FlatDataSpec, Column, Width, TabularFormatter};
let spec = FlatDataSpec::builder()
.column(Column::new(Width::Fixed(8)))
.column(Column::new(Width::Fill))
.column(Column::new(Width::Fixed(10)))
.separator(" ")
.build();
let formatter = TabularFormatter::new(&spec, 80);
// Format rows one at a time (enables interleaved output)
let row1 = formatter.format_row(&["abc123", "path/to/file.rs", "pending"]);
println!("{}", row1);
println!(" └─ Note: needs review"); // Interleaved content
let row2 = formatter.format_row(&["def456", "src/lib.rs", "done"]);
println!("{}", row2);Implementations§
Source§impl TabularFormatter
impl TabularFormatter
Sourcepub fn new(spec: &FlatDataSpec, total_width: usize) -> Self
pub fn new(spec: &FlatDataSpec, total_width: usize) -> Self
Create a new formatter by resolving widths from the spec.
§Arguments
spec- Table specificationtotal_width- Total available width including decorations
Sourcepub fn from_resolved(spec: &FlatDataSpec, resolved: ResolvedWidths) -> Self
pub fn from_resolved(spec: &FlatDataSpec, resolved: ResolvedWidths) -> Self
Create a formatter with pre-resolved widths.
Use this when you’ve already calculated widths (e.g., from data).
Sourcepub fn from_resolved_with_width(
spec: &FlatDataSpec,
resolved: ResolvedWidths,
total_width: usize,
) -> Self
pub fn from_resolved_with_width( spec: &FlatDataSpec, resolved: ResolvedWidths, total_width: usize, ) -> Self
Create a formatter with pre-resolved widths and explicit total width.
Sourcepub fn with_widths(columns: Vec<Column>, widths: Vec<usize>) -> Self
pub fn with_widths(columns: Vec<Column>, widths: Vec<usize>) -> Self
Create a formatter from explicit widths and columns.
This is useful for direct construction without a full FlatDataSpec.
Sourcepub fn from_type<T: Tabular>(total_width: usize) -> Self
pub fn from_type<T: Tabular>(total_width: usize) -> Self
Create a formatter from a type that implements Tabular.
This constructor uses the TabularSpec generated by the #[derive(Tabular)]
macro to configure the formatter.
§Example
use standout_render::tabular::{Tabular, TabularFormatter};
use serde::Serialize;
#[derive(Serialize, Tabular)]
struct Task {
#[col(width = 8)]
id: String,
#[col(width = "fill")]
title: String,
}
let formatter = TabularFormatter::from_type::<Task>(80);Sourcepub fn total_width(self, width: usize) -> Self
pub fn total_width(self, width: usize) -> Self
Set the total target width (for anchor gap calculations).
Sourcepub fn format_row<S: AsRef<str>>(&self, values: &[S]) -> String
pub fn format_row<S: AsRef<str>>(&self, values: &[S]) -> String
Format a single row of values.
Values are truncated/padded according to the column specifications. Missing values use the column’s null representation.
§Arguments
values- Slice of cell values (strings)
§Example
use standout_render::tabular::{FlatDataSpec, Column, Width, TabularFormatter};
let spec = FlatDataSpec::builder()
.column(Column::new(Width::Fixed(10)))
.column(Column::new(Width::Fixed(8)))
.separator(" | ")
.build();
let formatter = TabularFormatter::new(&spec, 80);
let output = formatter.format_row(&["Hello", "World"]);
assert_eq!(output, "Hello | World ");Sourcepub fn format_rows<S: AsRef<str>>(&self, rows: &[Vec<S>]) -> Vec<String>
pub fn format_rows<S: AsRef<str>>(&self, rows: &[Vec<S>]) -> Vec<String>
Format multiple rows.
Returns a vector of formatted row strings.
Sourcepub fn format_row_lines<S: AsRef<str>>(&self, values: &[S]) -> Vec<String>
pub fn format_row_lines<S: AsRef<str>>(&self, values: &[S]) -> Vec<String>
Format a row that may produce multiple output lines (due to wrapping).
If any cell wraps to multiple lines, the output contains multiple lines with proper vertical alignment. Cells are top-aligned.
§Example
use standout_render::tabular::{FlatDataSpec, Column, Width, Overflow, TabularFormatter};
let spec = FlatDataSpec::builder()
.column(Column::new(Width::Fixed(10)).wrap())
.column(Column::new(Width::Fixed(8)))
.separator(" ")
.build();
let formatter = TabularFormatter::new(&spec, 80);
let lines = formatter.format_row_lines(&["This is a long text", "Short"]);
// Returns multiple lines if the first column wrapsSourcepub fn column_width(&self, index: usize) -> Option<usize>
pub fn column_width(&self, index: usize) -> Option<usize>
Get the resolved width for a column by index.
Sourcepub fn num_columns(&self) -> usize
pub fn num_columns(&self) -> usize
Get the number of columns.
Sourcepub fn extract_headers(&self) -> Vec<String>
pub fn extract_headers(&self) -> Vec<String>
Extract headers from column specifications.
For each column, uses (in order of preference):
- The
headerfield if set - The
keyfield if set - The
namefield if set - Empty string
This is useful for Table::header_from_columns().
Sourcepub fn row_from<T: Serialize>(&self, value: &T) -> String
pub fn row_from<T: Serialize>(&self, value: &T) -> String
Format a row by extracting values from a serializable struct.
This method extracts field values from the struct based on each column’s
key or name field. Supports dot notation for nested field access
(e.g., “user.email”).
§Arguments
value- Any serializable value to extract fields from
§Example
use standout_render::tabular::{FlatDataSpec, Column, Width, TabularFormatter};
use serde::Serialize;
#[derive(Serialize)]
struct Record {
name: String,
status: String,
count: u32,
}
let spec = FlatDataSpec::builder()
.column(Column::new(Width::Fixed(20)).key("name"))
.column(Column::new(Width::Fixed(10)).key("status"))
.column(Column::new(Width::Fixed(5)).key("count"))
.separator(" ")
.build();
let formatter = TabularFormatter::new(&spec, 80);
let record = Record {
name: "example".to_string(),
status: "active".to_string(),
count: 42,
};
let row = formatter.row_from(&record);
assert!(row.contains("example"));
assert!(row.contains("active"));
assert!(row.contains("42"));Sourcepub fn row_lines_from<T: Serialize>(&self, value: &T) -> Vec<String>
pub fn row_lines_from<T: Serialize>(&self, value: &T) -> Vec<String>
Format a row with potential multi-line output from a serializable struct.
Same as row_from but handles word-wrap columns that may produce
multiple output lines.
Sourcepub fn row_from_trait<T: TabularRow>(&self, value: &T) -> String
pub fn row_from_trait<T: TabularRow>(&self, value: &T) -> String
Format a row using the TabularRow trait.
This method uses the optimized to_row() implementation generated by
#[derive(TabularRow)], avoiding JSON serialization overhead.
§Example
use standout_render::tabular::{TabularRow, TabularFormatter};
#[derive(TabularRow)]
struct Task {
id: String,
title: String,
}
let task = Task {
id: "TSK-001".to_string(),
title: "Implement feature".to_string(),
};
let formatter = TabularFormatter::from_type::<Task>(80);
let row = formatter.row_from_trait(&task);Sourcepub fn row_lines_from_trait<T: TabularRow>(&self, value: &T) -> Vec<String>
pub fn row_lines_from_trait<T: TabularRow>(&self, value: &T) -> Vec<String>
Format a row with potential multi-line output using the TabularRow trait.
Same as row_from_trait but handles word-wrap columns that may produce
multiple output lines.
Trait Implementations§
Source§impl Clone for TabularFormatter
impl Clone for TabularFormatter
Source§fn clone(&self) -> TabularFormatter
fn clone(&self) -> TabularFormatter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TabularFormatter
impl Debug for TabularFormatter
Source§impl Object for TabularFormatter
impl Object for TabularFormatter
Source§fn get_value(self: &Arc<Self>, key: &Value) -> Option<Value>
fn get_value(self: &Arc<Self>, key: &Value) -> Option<Value>
Source§fn call_method(
self: &Arc<Self>,
_state: &State<'_, '_>,
name: &str,
args: &[Value],
) -> Result<Value, Error>
fn call_method( self: &Arc<Self>, _state: &State<'_, '_>, name: &str, args: &[Value], ) -> Result<Value, Error>
Source§fn repr(self: &Arc<Self>) -> ObjectRepr
fn repr(self: &Arc<Self>) -> ObjectRepr
Source§fn enumerator_len(self: &Arc<Self>) -> Option<usize>
fn enumerator_len(self: &Arc<Self>) -> Option<usize>
Source§fn is_true(self: &Arc<Self>) -> bool
fn is_true(self: &Arc<Self>) -> bool
true if this object is considered true for if conditions. Read more