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:xgrows to the right andyupwards. They default to0..1; empty, reversed or infinite bounds draw nothing. - It fills the height it is given (a
Layoutregion, an intuiTUIve node), or isheightrows when that is set, or 10 rows; and it is as wide as the width it is given (orwidth). backgroundstyles the cells nothing drew in, and goes under what is drawn.paintruns 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
impl Canvas
Sourcepub fn height(self, rows: usize) -> Self
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.
Sourcepub fn background(self, style: impl Into<String>) -> Self
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").
Sourcepub fn marker(self, marker: Marker) -> Self
pub fn marker(self, marker: Marker) -> Self
The marker the first layer draws with (default
Marker::Braille); CanvasPainter::marker changes it.
Sourcepub fn paint(self, draw: impl FnOnce(&mut CanvasPainter)) -> Self
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.
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
fn accessible_text(&self, _width: usize) -> String
width where layout matters.Source§impl Renderable for Canvas
impl Renderable for Canvas
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. 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
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.Source§fn vertical(&self) -> Option<VerticalAlign>
fn vertical(&self) -> Option<VerticalAlign>
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
fn print_end_line(&self, _options: &ConsoleOptions) -> bool
"\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.