Struct MatrixDisplay

Source
pub struct MatrixDisplay<'a, T>
where T: Clone + ToString + 'a,
{ /* private fields */ }
Expand description

Stores a matrix of data and offers a way to pretty print it

#Example: visualising a 256 colors palette:

use matrix_display::*;
let format = Format::new(5,1);
let board = (0..256)
       .map(|x| cell::Cell::new(x, 0, x as u8))
    .collect::<Vec<_>>();
let mut data = matrix::Matrix::new(8, board);
let display = MatrixDisplay::new(&format, &mut data);
display.print(&mut std::io::stdout(), &style::BordersStyle::Light);

Implementations§

Source§

impl<'a, T> MatrixDisplay<'a, T>
where T: Clone + ToString + 'a,

Source

pub fn new(f: &'a Format, m: &'a mut Matrix<Cell<T>>) -> MatrixDisplay<'a, T>

Construct a matrix display

f: the format of a cell (width, height) m: a reference to the data (&Matrix)

Examples found in repository?
examples/palette.rs (line 10)
4fn main() {
5    let format = Format::new(5, 1);
6    let board = (0..256)
7        .map(|x| cell::Cell::new(x, 0, x as u8))
8        .collect::<Vec<_>>();
9    let mut data = matrix::Matrix::new(8, board);
10    let display = MatrixDisplay::new(&format, &mut data);
11    display.print(&mut std::io::stdout(), &style::BordersStyle::Light);
12}
More examples
Hide additional examples
examples/2048.rs (line 19)
4fn main() {
5    let format = Format::new(7, 3);
6    let colour_theme = vec![
7        247, 78, 222, 220, 214, 208, 202, 196, 162, 160, 126, 90, 88, 54, 53, 52,
8    ];
9    let board = (0..16)
10        .map(|x| {
11            cell::Cell::new(
12                2_f64.powi(x + 1),
13                7,
14                *colour_theme.get(x as usize).unwrap() as u8,
15            )
16        })
17        .collect::<Vec<_>>();
18    let mut data = matrix::Matrix::new(4, board);
19    let display = MatrixDisplay::new(&format, &mut data);
20    display.print(&mut std::io::stdout(), &style::BordersStyle::Heavy);
21}
examples/chess.rs (line 27)
4fn main() {
5    let format = Format::new(7, 3);
6    #[cfg_attr(rustfmt, rustfmt_skip)]
7    let board = vec!['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜',
8	                 '♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟',
9					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
10                     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
11					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
12					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
13					 '♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖',
14					 '♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙']
15        .iter()
16        .enumerate()
17        .map(|(i, x)| {
18            let ansi_fg = 33;
19            let mut ansi_bg = 0;
20            if i % 2 + (i / 8) % 2 == 1 {
21                ansi_bg = 7;
22            }
23            cell::Cell::new(x.clone(), ansi_fg, ansi_bg)
24        })
25        .collect::<Vec<_>>();
26    let mut data = matrix::Matrix::new(8, board);
27    let mut display = MatrixDisplay::new(&format, &mut data);
28    display.cell_at_cursor_position((13, 6)).color.bg = 10;
29    display.print(&mut std::io::stdout(), &style::BordersStyle::None);
30}
Source

pub fn width(&self) -> usize

The matrix’s width in number of characters

Source

pub fn height(&self) -> usize

The matrix’s height in number of characters

Source

pub fn render(&self, borders: &BordersStyle) -> Vec<ANSIString<'static>>

Render a matrix into a Vec.

Pick a BorderStyle, an output that implements the Write trait and you’re good to go! This approach allows the user to customize how to display the matrix.

Source

pub fn print<Out: Write>(&self, out: &mut Out, borders: &BordersStyle)

Print a matrix. This is the most important method of this library

Pick a BorderStyle, an output that implements the Write trait and you’re good to go!

Examples found in repository?
examples/palette.rs (line 11)
4fn main() {
5    let format = Format::new(5, 1);
6    let board = (0..256)
7        .map(|x| cell::Cell::new(x, 0, x as u8))
8        .collect::<Vec<_>>();
9    let mut data = matrix::Matrix::new(8, board);
10    let display = MatrixDisplay::new(&format, &mut data);
11    display.print(&mut std::io::stdout(), &style::BordersStyle::Light);
12}
More examples
Hide additional examples
examples/2048.rs (line 20)
4fn main() {
5    let format = Format::new(7, 3);
6    let colour_theme = vec![
7        247, 78, 222, 220, 214, 208, 202, 196, 162, 160, 126, 90, 88, 54, 53, 52,
8    ];
9    let board = (0..16)
10        .map(|x| {
11            cell::Cell::new(
12                2_f64.powi(x + 1),
13                7,
14                *colour_theme.get(x as usize).unwrap() as u8,
15            )
16        })
17        .collect::<Vec<_>>();
18    let mut data = matrix::Matrix::new(4, board);
19    let display = MatrixDisplay::new(&format, &mut data);
20    display.print(&mut std::io::stdout(), &style::BordersStyle::Heavy);
21}
examples/chess.rs (line 29)
4fn main() {
5    let format = Format::new(7, 3);
6    #[cfg_attr(rustfmt, rustfmt_skip)]
7    let board = vec!['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜',
8	                 '♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟',
9					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
10                     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
11					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
12					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
13					 '♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖',
14					 '♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙']
15        .iter()
16        .enumerate()
17        .map(|(i, x)| {
18            let ansi_fg = 33;
19            let mut ansi_bg = 0;
20            if i % 2 + (i / 8) % 2 == 1 {
21                ansi_bg = 7;
22            }
23            cell::Cell::new(x.clone(), ansi_fg, ansi_bg)
24        })
25        .collect::<Vec<_>>();
26    let mut data = matrix::Matrix::new(8, board);
27    let mut display = MatrixDisplay::new(&format, &mut data);
28    display.cell_at_cursor_position((13, 6)).color.bg = 10;
29    display.print(&mut std::io::stdout(), &style::BordersStyle::None);
30}
Source

pub fn coordinates_at_cursor_position( &self, (x, y): (usize, usize), ) -> (usize, usize)

Takes a cursor position in (usize, usize) and returns the coordinates of the cell under the cursor

Source

pub fn cell_at_cursor_position( &mut self, cursor: (usize, usize), ) -> &mut Cell<T>

Takes a cursor position in characters (x, y) and returns a mutable reference to the corresponding cell

This is used to modify a cell that was clicked

Examples found in repository?
examples/chess.rs (line 28)
4fn main() {
5    let format = Format::new(7, 3);
6    #[cfg_attr(rustfmt, rustfmt_skip)]
7    let board = vec!['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜',
8	                 '♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟',
9					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
10                     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
11					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
12					 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
13					 '♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖',
14					 '♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙']
15        .iter()
16        .enumerate()
17        .map(|(i, x)| {
18            let ansi_fg = 33;
19            let mut ansi_bg = 0;
20            if i % 2 + (i / 8) % 2 == 1 {
21                ansi_bg = 7;
22            }
23            cell::Cell::new(x.clone(), ansi_fg, ansi_bg)
24        })
25        .collect::<Vec<_>>();
26    let mut data = matrix::Matrix::new(8, board);
27    let mut display = MatrixDisplay::new(&format, &mut data);
28    display.cell_at_cursor_position((13, 6)).color.bg = 10;
29    display.print(&mut std::io::stdout(), &style::BordersStyle::None);
30}

Auto Trait Implementations§

§

impl<'a, T> Freeze for MatrixDisplay<'a, T>

§

impl<'a, T> RefUnwindSafe for MatrixDisplay<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for MatrixDisplay<'a, T>
where T: Send,

§

impl<'a, T> Sync for MatrixDisplay<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for MatrixDisplay<'a, T>

§

impl<'a, T> !UnwindSafe for MatrixDisplay<'a, T>

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

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.