Skip to main content

TableData

Struct TableData 

Source
pub struct TableData { /* private fields */ }
Expand description

Rows of Values under Columns, optionally sorted, grouped and totalled, rendered as one core Table.

Sorting is stable and puts empty cells last (sort); the sorted columns get a ▲/▼ header indicator (^/v on an ASCII-only console). With a GroupBy, each group renders as a header row with the group’s key in the first column (prefixed by the grouped column’s header when that is not the first column), the group’s rows, and — when the grouping has aggregates — a summary row. totals adds a final summary over every row.

use rich::{Console, Justify};
use rich_ext::table::{Aggregate, Column, GroupBy, SortKey, TableData, Value};

let mut data = TableData::new([
    Column::new("team"),
    Column::new("service"),
    Column::new("pods").justify(Justify::Right),
]);
data.push(["core".into(), "api".into(), Value::Int(3)]);
data.push(["edge".into(), "web".into(), Value::Int(5)]);
data.push(["core".into(), "db".into(), Value::Int(1)]);
let data = data
    .sort_by([SortKey::asc(0), SortKey::asc(1)])
    .group_by(GroupBy::new(0).aggregate(Aggregate::sum(2)));

let out = Console::builder().width(40).build().render_export(&data);
assert_eq!(
    out,
    "\
┏━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━┓
┃ team ▲1  ┃ service ▲2 ┃ pods ┃
┡━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━┩
│ core     │            │      │
│ core     │ api        │    3 │
│ core     │ db         │    1 │
│ subtotal │            │    4 │
│ edge     │            │      │
│ edge     │ web        │    5 │
│ subtotal │            │    5 │
└──────────┴────────────┴──────┘
"
);

Implementations§

Source§

impl TableData

Source

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

A centered title above the table (console markup, as the core Table::title).

Source

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

A centered caption below the table (console markup).

Source

pub fn box_set(self, box_set: Box) -> Self

The box-drawing set (default HEAVY_HEAD, as the core table).

Source

pub fn without_box(self) -> Self

Draw no borders and no column dividers.

Source

pub fn show_edge(self, show: bool) -> Self

Draw the outer edges (default on).

Source

pub fn expand(self, expand: bool) -> Self

Expand to the full available width.

Source

pub fn border_style(self, style: Style) -> Self

Style the box border.

Source§

impl TableData

Source

pub fn new(columns: impl IntoIterator<Item = Column>) -> Self

No rows under columns.

Source

pub fn push(&mut self, row: impl IntoIterator<Item = Value>) -> &mut Self

Append a row. Missing cells are Null; extra cells are dropped.

Source

pub fn extend<R: IntoIterator<Item = Value>>( &mut self, rows: impl IntoIterator<Item = R>, ) -> &mut Self

Append several rows.

Source

pub fn sort_by(self, keys: impl IntoIterator<Item = SortKey>) -> Self

Sort by keys when rendering (the rows keep their stored order).

Source

pub fn set_sort(&mut self, keys: impl IntoIterator<Item = SortKey>)

Replace the sort keys; an empty list keeps insertion order.

Source

pub fn group_by(self, group: GroupBy) -> Self

Group the (sorted) rows.

Source

pub fn totals( self, label: impl Into<String>, aggregates: impl IntoIterator<Item = Aggregate>, ) -> Self

Add a final summary row over every row. label fills the first cell, followed by the first column’s own aggregate if it has one (total: 6); an empty label leaves the aggregate alone.

Source

pub fn style_rules(self, rules: StyleRules) -> Self

Style cells, rows and columns by value (rules). Rules restyle the rows; group headers and summary rows keep their own styles.

Source

pub fn columns(&self) -> &[Column]

The columns.

Source

pub fn rows(&self) -> &[Vec<Value>]

The rows, in the order they were added.

Source

pub fn sort_keys(&self) -> &[SortKey]

The sort keys.

Source

pub fn order(&self) -> Vec<usize>

Row indices in display order (stable sort by the keys).

Source

pub fn to_table(&self, console: &Console) -> Table

The core table: headers with sort indicators, then the sorted rows, grouped and totalled as configured.

Trait Implementations§

Source§

impl AccessibleText for TableData

Source§

fn accessible_text(&self, width: usize) -> String

The content as plain lines, laid out for width where layout matters.
Source§

impl Clone for TableData

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TableData

Source§

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

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

impl Renderable for TableData

Source§

fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>

Source§

fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement

The (minimum, maximum) cell width this renderable wants. The default assumes the renderable fills the available width (e.g. Panel, Table); Text overrides it with its content width so the top-level print path can shrink to fit. Port of __rich_measure__ / Measurement.get.
Source§

fn fit_to_measurement(&self) -> bool

Whether a top-level Console::print shrinks this renderable to its measured width. Upstream renders every top-level renderable at the full console width, so the default is false; an extension may opt in.
Source§

fn vertical(&self) -> Option<VerticalAlign>

The vertical alignment a Table cell holding this renderable uses in place of its column’s. Upstream reads getattr(renderable, "vertical", None); Align sets it.
Source§

impl Transform<TableData> for Sort

Source§

impl Transform<TableData> for Group

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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