Skip to main content

KpiCard

Struct KpiCard 

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

One key number: a label, the value, its change with a direction, an optional sparkline and an optional Status.

╭───────────────────────╮
│ Requests         ✓ ok │
│ 12.4k/s               │
│ ▲ +8.49% vs last week │
│ ▁▂▂▄▆▅▃▆█▇▅▃          │
╰───────────────────────╯
  • The delta is an arrow and a signed number: ▲ +8.5%, ▼ -120, = 0 (^, v and = in ASCII). It is styled chart.delta.good or chart.delta.bad by whether the change is for the better (higher_is_better, on by default), and the arrow and sign carry the direction without colour.
  • The status is a symbol and a word (✓ ok, ! warning, ✗ critical), right-aligned on the label’s line.
  • The card measures to its content, so cards sit side by side in a Columns or a Table grid; width fixes the width and expand fills what it is given, so a row of cards lines up. Every card with the same parts has the same height.
  • Given less width the label is cut, then the other lines; the sparkline resamples. Below 5 cells the border goes.
use rich::Console;
use rich_ext::chart::{Charset, KpiCard, Status};

let console = Console::builder().width(40).color_system(None).build();
let card = KpiCard::new("Errors", 42.0)
    .previous(56.0)
    .higher_is_better(false)
    .caption("vs yesterday")
    .status(Status::Warning);
assert_eq!(
    console.render_export(&card),
    concat!(
        "╭─────────────────────╮\n",
        "│ Errors    ! warning │\n",
        "│ 42                  │\n",
        "│ ▼ -25% vs yesterday │\n",
        "╰─────────────────────╯\n",
    )
);

Implementations§

Source§

impl KpiCard

Source

pub fn new(label: impl Into<String>, value: f64) -> Self

A card called label showing value.

Source

pub fn format(self, format: ValueFormat) -> Self

How the value and an absolute delta are written (default ValueFormat::Compact).

Source

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

Written right after the value, such as %, /s or ms.

Source

pub fn delta(self, delta: f64) -> Self

The change as an amount, written in the value’s format.

Source

pub fn delta_percent(self, percent: f64) -> Self

The change as a percentage.

Source

pub fn previous(self, previous: f64) -> Self

The change from previous to the value, as a percentage (as an amount when previous is 0).

Source

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

Words after the delta, such as vs last week.

Source

pub fn higher_is_better(self, higher: bool) -> Self

Whether a rise is good news (default on); off for errors, latency and costs.

Source

pub fn sparkline(self, sparkline: Sparkline) -> Self

A sparkline under the delta.

Source

pub fn trend(self, values: impl IntoIterator<Item = f64>) -> Self

A sparkline of values under the delta.

Source

pub fn status(self, status: Status) -> Self

The status, right-aligned on the label’s line.

Source

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

Fix the card’s width, border included.

Source

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

Fill the width given (default off: the content’s width).

Source

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

Draw the border (default on).

Source

pub fn charset(self, charset: Charset) -> Self

Glyphs to draw with: Charset::Ascii asks for ASCII; anything else draws box drawing and arrows (ASCII on an ASCII-only console). A sparkline keeps its own charset unless this asks for ASCII.

Trait Implementations§

Source§

impl AccessibleText for KpiCard

KPI Errors: 42, down 25% vs yesterday, better, status warning: the value, its change in words and whether that is for the better, and the status; then the sparkline’s text, if it has one.

Source§

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

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

impl Clone for KpiCard

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 KpiCard

Source§

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

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

impl Draw for KpiCard

Source§

fn draw( &self, console: &Console, options: &ConsoleOptions, screen: &mut Screen, rect: Rect, )

Draw into rect of screen, rendered with options (its width and height are the rectangle’s): the cells screen.write_lines(rect, &console.render_lines(self, options, false)) would give.
Source§

impl PartialEq for KpiCard

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Renderable for KpiCard

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. Port of __rich_measure__. The default is what upstream’s Measurement.get returns for a renderable without one: anything from nothing to the whole width, (0, max_width).
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§

fn print_end_line(&self, _options: &ConsoleOptions) -> bool

Whether a top-level print ends this renderable’s last line. This port’s renderables separate lines, where upstream’s end each line with "\n", so a print adds the final line break. A renderable whose upstream output stops without one (Screen, LiveRender, a Rule whose end has no newline) returns false, and the next output continues its last line. options are the print’s options.
Source§

impl StructuralPartialEq for KpiCard

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