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
impl TableData
Sourcepub fn title(self, title: impl Into<String>) -> Self
pub fn title(self, title: impl Into<String>) -> Self
A centered title above the table (console markup, as the core
Table::title).
Sourcepub fn caption(self, caption: impl Into<String>) -> Self
pub fn caption(self, caption: impl Into<String>) -> Self
A centered caption below the table (console markup).
Sourcepub fn box_set(self, box_set: Box) -> Self
pub fn box_set(self, box_set: Box) -> Self
The box-drawing set (default HEAVY_HEAD, as the core table).
Sourcepub fn without_box(self) -> Self
pub fn without_box(self) -> Self
Draw no borders and no column dividers.
Sourcepub fn border_style(self, style: Style) -> Self
pub fn border_style(self, style: Style) -> Self
Style the box border.
Source§impl TableData
impl TableData
Sourcepub fn new(columns: impl IntoIterator<Item = Column>) -> Self
pub fn new(columns: impl IntoIterator<Item = Column>) -> Self
No rows under columns.
Sourcepub fn push(&mut self, row: impl IntoIterator<Item = Value>) -> &mut Self
pub fn push(&mut self, row: impl IntoIterator<Item = Value>) -> &mut Self
Append a row. Missing cells are Null; extra cells are dropped.
Sourcepub fn extend<R: IntoIterator<Item = Value>>(
&mut self,
rows: impl IntoIterator<Item = R>,
) -> &mut Self
pub fn extend<R: IntoIterator<Item = Value>>( &mut self, rows: impl IntoIterator<Item = R>, ) -> &mut Self
Append several rows.
Sourcepub fn sort_by(self, keys: impl IntoIterator<Item = SortKey>) -> Self
pub fn sort_by(self, keys: impl IntoIterator<Item = SortKey>) -> Self
Sort by keys when rendering (the rows keep their stored order).
Sourcepub fn set_sort(&mut self, keys: impl IntoIterator<Item = SortKey>)
pub fn set_sort(&mut self, keys: impl IntoIterator<Item = SortKey>)
Replace the sort keys; an empty list keeps insertion order.
Sourcepub fn totals(
self,
label: impl Into<String>,
aggregates: impl IntoIterator<Item = Aggregate>,
) -> Self
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.
Sourcepub fn style_rules(self, rules: StyleRules) -> Self
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.
Trait Implementations§
Source§impl AccessibleText for TableData
impl AccessibleText for TableData
Source§fn accessible_text(&self, width: usize) -> String
fn accessible_text(&self, width: usize) -> String
width where layout matters.Source§impl Renderable for TableData
impl Renderable for TableData
fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>
Source§fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement
fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement
(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
fn fit_to_measurement(&self) -> bool
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.