pub struct MatrixDisplay<'a, T>{ /* 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>
impl<'a, T> MatrixDisplay<'a, T>
Sourcepub fn new(f: &'a Format, m: &'a mut Matrix<Cell<T>>) -> MatrixDisplay<'a, T>
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?
More examples
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}
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}
Sourcepub fn render(&self, borders: &BordersStyle) -> Vec<ANSIString<'static>> ⓘ
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.
Sourcepub fn print<Out: Write>(&self, out: &mut Out, borders: &BordersStyle)
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?
More examples
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}
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}
Sourcepub fn coordinates_at_cursor_position(
&self,
(x, y): (usize, usize),
) -> (usize, usize)
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
Sourcepub fn cell_at_cursor_position(
&mut self,
cursor: (usize, usize),
) -> &mut Cell<T>
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?
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}