Skip to main content

TabularFormatter

Struct TabularFormatter 

Source
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

Source

pub fn new(spec: &FlatDataSpec, total_width: usize) -> Self

Create a new formatter by resolving widths from the spec.

§Arguments
  • spec - Table specification
  • total_width - Total available width including decorations
Source

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

Source

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.

Source

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.

Source

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);
Source

pub fn total_width(self, width: usize) -> Self

Set the total target width (for anchor gap calculations).

Source

pub fn separator(self, sep: impl Into<String>) -> Self

Set the column separator.

Source

pub fn prefix(self, prefix: impl Into<String>) -> Self

Set the row prefix.

Source

pub fn suffix(self, suffix: impl Into<String>) -> Self

Set the row suffix.

Source

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   ");
Source

pub fn format_rows<S: AsRef<str>>(&self, rows: &[Vec<S>]) -> Vec<String>

Format multiple rows.

Returns a vector of formatted row strings.

Source

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 wraps
Source

pub fn column_width(&self, index: usize) -> Option<usize>

Get the resolved width for a column by index.

Source

pub fn widths(&self) -> &[usize]

Get all resolved column widths.

Source

pub fn num_columns(&self) -> usize

Get the number of columns.

Source

pub fn extract_headers(&self) -> Vec<String>

Extract headers from column specifications.

For each column, uses (in order of preference):

  1. The header field if set
  2. The key field if set
  3. The name field if set
  4. Empty string

This is useful for Table::header_from_columns().

Source

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"));
Source

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.

Source

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);
Source

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

Source§

fn clone(&self) -> TabularFormatter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TabularFormatter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Object for TabularFormatter

Source§

fn get_value(self: &Arc<Self>, key: &Value) -> Option<Value>

Given a key, looks up the associated value.
Source§

fn enumerate(self: &Arc<Self>) -> Enumerator

Enumerates the object. Read more
Source§

fn call_method( self: &Arc<Self>, _state: &State<'_, '_>, name: &str, args: &[Value], ) -> Result<Value, Error>

The engine calls this to invoke a method on the object. Read more
Source§

fn repr(self: &Arc<Self>) -> ObjectRepr

Indicates the natural representation of an object. Read more
Source§

fn enumerator_len(self: &Arc<Self>) -> Option<usize>

Returns the length of the enumerator. Read more
Source§

fn is_true(self: &Arc<Self>) -> bool

Returns true if this object is considered true for if conditions. Read more
Source§

fn call( self: &Arc<Self>, state: &State<'_, '_>, args: &[Value], ) -> Result<Value, Error>

The engine calls this to invoke the object itself. Read more
Source§

fn custom_cmp(self: &Arc<Self>, other: &DynObject) -> Option<Ordering>

Custom comparison of this object against another object of the same type. Read more
Source§

fn render(self: &Arc<Self>, f: &mut Formatter<'_>) -> Result<(), Error>
where Self: Sized + 'static,

Formats the object for stringification. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ObjectExt for T
where T: Object + Send + Sync + 'static,

Source§

fn mapped_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
where F: for<'a> FnOnce(&'a Self) -> Box<dyn Iterator<Item = Value> + Sync + Send + 'a> + Send + Sync + 'static, Self: Sized,

Creates a new iterator enumeration that projects into the given object. Read more
Source§

fn mapped_rev_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
where F: for<'a> FnOnce(&'a Self) -> Box<dyn DoubleEndedIterator<Item = Value> + Sync + Send + 'a> + Send + Sync + 'static, Self: Sized,

Creates a new reversible iterator enumeration that projects into the given object. Read more
Source§

fn try_iter( self: &Arc<Self>, ) -> Option<Box<dyn Iterator<Item = Value> + Sync + Send>>
where Self: 'static,

Iterates over this object. Read more
Source§

fn try_iter_pairs( self: &Arc<Self>, ) -> Option<Box<dyn Iterator<Item = (Value, Value)> + Sync + Send>>

Iterate over key and value at once.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more