Struct textcanvas::textcanvas::TextCanvas

source ·
pub struct TextCanvas {
    pub output: Surface,
    pub screen: Surface,
    pub buffer: PixelBuffer,
    pub color_buffer: ColorBuffer,
    pub text_buffer: TextBuffer,
    /* private fields */
}
Expand description

Draw to the terminal like an HTML Canvas.

§Examples

use textcanvas::textcanvas::TextCanvas;

let mut canvas = TextCanvas::new(15, 5).unwrap();

assert_eq!(
    canvas.repr(),
    "Canvas(output=(15×5), screen=(30×20)))"
);

assert_eq!(
    (canvas.w(), canvas.h(), canvas.cx(), canvas.cy()),
    (29, 19, 15, 10),
);

canvas.stroke_line(0, 0, canvas.w(), canvas.h());
canvas.draw_text(1, 2, "hello, world");
assert_eq!(
    canvas.to_string(),
    "\
⠑⠢⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠑⠢⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀hello,⠢world⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠢⢄⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠢⢄
"
)

§Todo

  • move_to(), line_to(), stroke() and other JS Canvas primitives.

Fields§

§output: Surface

Properties of the output surface, whose size is given as parameter to the constructor. One unit in width and in height represents exactly one character on the terminal.

§screen: Surface

Properties of the virtual output surface. This surface benefits from the increase in resolution. One unit in width and in height represents one Braille dot. There are 2 dots per character in width, and 4 per character in height. So this surface is 2× wider and 4× higher than the output surface.

§buffer: PixelBuffer

The in-memory pixel buffer. This maps 1-to-1 to the virtual screen. Each pixel is this buffer is either on or off.

§color_buffer: ColorBuffer

The in-memory color buffer. This maps 1-to-1 to the output buffer (and so to the physical screen). This contains color data for characters. Screen dots, being part of one output character, cannot be colored individually. Color is thus less precise than screen pixels. Note that a call to set_color() is valid for both pixels and text, but text embeds color in its own buffer, and does not use this buffer at all. Note also that this buffer is empty until the first call to set_color(). The first call to set_color() initializes the buffer and sets is_colorized() to true.

§text_buffer: TextBuffer

The in-memory text buffer. This maps 1-to-1 to the output buffer (and so to the physical screen). This contains regular text characters. Text is drawn on top of the pixel buffer on a separate layer. Drawing text does not affect pixels. Pixels and text do not share the same color buffer either. Color info is embedded in the text buffer with each character directly. Note also that this buffer is empty until the first call to draw_text(). The first call to draw_text() initializes the buffer and sets is_textual() to True.

Implementations§

source§

impl TextCanvas

source

pub fn new(width: i32, height: i32) -> Result<Self, &'static str>

Create new TextCanvas.

§Errors

If width and height of canvas are < 1×1.

Also, if the pixel resolution of width or height is larger than 65_535 (realistically, should not happen). This is because we let the user interact with the canvas with i32s. i32 is a more likely user-type (maths, etc.), and allows for negative values. Even though they are never displayed, allowing negative values makes for a nicer API. We handle the complexity for the user.

source

pub fn repr(&self) -> String

High-level string representation of the canvas.

§Examples
use textcanvas::textcanvas::TextCanvas;

let mut canvas = TextCanvas::new(15, 5).unwrap();

assert_eq!(
    canvas.repr(),
    "Canvas(output=(15×5), screen=(30×20)))"
);
source

pub fn w(&self) -> i32

Shortcut for width of pixel screen (index of last column).

source

pub fn h(&self) -> i32

Shortcut for height of pixel screen (index of last row).

source

pub fn cx(&self) -> i32

Shortcut for center-X of pixel screen.

source

pub fn cy(&self) -> i32

Shortcut for center-Y of pixel screen.

source

pub fn clear(&mut self)

Turn all pixels off and remove color and text.

Note: This method does not drop the color and text buffers, it only clears them. No memory is freed, and all references remain valid (buffers are cleared in-place, not replaced).

source

pub fn is_colorized(&self) -> bool

Whether the canvas can contain colors.

Note: This does not mean that any colors are displayed. This only means the color buffer is active.

§Examples
use textcanvas::textcanvas::{Color, TextCanvas};

let mut canvas = TextCanvas::new(15, 5).unwrap();

assert!(!canvas.is_colorized());

canvas.set_color(&Color::new().to_owned());  // Buffer is initialized.
assert!(canvas.is_colorized());
source

pub fn is_textual(&self) -> bool

Whether the canvas can contain text.

Note: This does not mean that any text is displayed. This only means the text buffer is active.

§Examples
use textcanvas::textcanvas::TextCanvas;

let mut canvas = TextCanvas::new(15, 5).unwrap();

assert!(!canvas.is_textual());

canvas.draw_text(0, 0, "");  // Buffer is initialized.
assert!(canvas.is_textual());
source

pub fn set_color(&mut self, color: &Color)

Set context color.

§Examples
use textcanvas::textcanvas::{Color, TextCanvas};

let mut canvas = TextCanvas::new(3, 1).unwrap();
let green = Color::new().bright_green().to_owned();

canvas.set_color(&green);
assert!(canvas.is_colorized());

canvas.draw_text(0, 0, "foo");
assert_eq!(
    canvas.to_string(),
    "\x1b[0;92mf\x1b[0m\x1b[0;92mo\x1b[0m\x1b[0;92mo\x1b[0m\n"
);
source

pub fn get_pixel(&self, x: i32, y: i32) -> Option<bool>

Get the state of a screen pixel.

Some(true) if the pixel is turned on, Some(false) if it is turned off, and None if the coordinates are outside the bounds of the buffer.

§Arguments
  • x - Screen X (high resolution).
  • y - Screen Y (high resolution).
source

pub fn set_pixel(&mut self, x: i32, y: i32, state: bool)

Set the state of a screen pixel.

Note: Coordinates outside the screen bounds are ignored.

Note: Turning a pixel off also removes color. This side effect does not affect text, as text has a separate color buffer.

§Arguments
  • x - Screen X (high resolution).
  • y - Screen Y (high resolution).
  • state - true means on, false means off.
source

pub fn draw_text(&mut self, x: i32, y: i32, text: &str)

Draw text onto the canvas.

Note: Coordinates outside the screen bounds are ignored.

Note: Text is rendered on top of pixels, as a separate layer.

Note: set_color() works for text as well, but text does not share its color buffer with pixels. Rather, color data is stored with the text characters themselves.

§Arguments
  • x - Output X (true resolution).
  • y - Output Y (true resolution).
  • text - The text to draw. Spaces are transparent (you see pixels through), and can be used to erase previously drawn characters.
source

pub fn iter_buffer(&self) -> IterPixelBuffer

Iterate over all cells of the pixel buffer.

Yields all X/Y coordinates, left-right, top-bottom.

source

pub fn stroke_line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32)

Stroke line using Bresenham’s line algorithm.

Trait Implementations§

source§

impl Debug for TextCanvas

source§

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

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

impl Default for TextCanvas

source§

fn default() -> Self

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

impl Display for TextCanvas

source§

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

Formats the value using the given formatter. Read more

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.