Skip to main content

Canvas

Struct Canvas 

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

Shapes and text drawn in world coordinates, as ratatui’s Canvas.

  • The bounds (x_bounds, y_bounds) are the world’s edges: x grows to the right and y upwards. They default to 0..1; empty, reversed or infinite bounds draw nothing.
  • It fills the height it is given (a Layout region, an intuiTUIve node), or is height rows when that is set, or 10 rows; and it is as wide as the width it is given (or width).
  • background styles the cells nothing drew in, and goes under what is drawn.
  • paint runs a closure once and records what it draws, so the canvas renders the same every time and can be cloned.
  • Marker::Braille (the default) draws 2×4 dots a cell. An ASCII-only console gets one glyph a cell instead.
use rich::Console;
use rich_ext::chart::{Canvas, Marker};

let console = Console::builder().width(12).color_system(None).build();
let canvas = Canvas::new()
    .x_bounds(0.0, 11.0)
    .y_bounds(0.0, 3.0)
    .height(4)
    .marker(Marker::Ascii('#'))
    .paint(|p| {
        p.rect(0.0, 0.0, 11.0, 3.0, "blue");
        p.line(0.0, 0.0, 11.0, 3.0, "red");
        p.print(2.0, 2.0, "hi", "bold");
    });
assert_eq!(
    console.render_export(&canvas),
    concat!(
        "############\n",
        "# hi  #### #\n",
        "# ####     #\n",
        "############\n",
    )
);

// Braille: 2×4 dots a cell.
let braille = Canvas::new()
    .x_bounds(0.0, 3.0)
    .y_bounds(0.0, 3.0)
    .height(1)
    .width(2)
    .paint(|p| {
        p.line(0.0, 0.0, 3.0, 3.0, "");
    });
assert_eq!(console.render_export(&braille), "⡠⠊\n");

Implementations§

Source§

impl Canvas

Source

pub fn new() -> Self

A blank canvas over 0..1 × 0..1.

Source

pub fn x_bounds(self, left: f64, right: f64) -> Self

The world’s left and right edges.

Source

pub fn y_bounds(self, bottom: f64, top: f64) -> Self

The world’s bottom and top edges.

Source

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

Width in cells (default: the width it is given).

Source

pub fn height(self, rows: usize) -> Self

Height in rows (at most 65535), whatever height it is given. Without it, the canvas fills the height it is given, or takes 10 rows.

Source

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

The style of the cells nothing drew in, and under what is drawn: a theme key or a definition. A style with only a foreground colour ("navy_blue") is taken as that background ("on navy_blue").

Source

pub fn marker(self, marker: Marker) -> Self

The marker the first layer draws with (default Marker::Braille); CanvasPainter::marker changes it.

Source

pub fn paint(self, draw: impl FnOnce(&mut CanvasPainter)) -> Self

Run draw once and keep what it draws. Each call adds to what earlier calls drew, in a new layer.

Trait Implementations§

Source§

impl AccessibleText for Canvas

Canvas, x from -180 to 180, y from -90 to 90: a world map, 4 points, 3 lines, then the text printed on it (Text: London, Paris). A canvas holds shapes rather than data, so it says what it drew.

Source§

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

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

impl Clone for Canvas

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 Canvas

Source§

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

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

impl Default for Canvas

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Draw for Canvas

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 Canvas

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 Canvas

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 Canvas

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