pub trait DynamicPrinter {
// Required methods
fn dynamic_print(&mut self, new_grid: String) -> Result<(), PrintingError>;
fn clear_grid(&mut self) -> Result<(), PrintingError>;
}Required Methods§
sourcefn dynamic_print(&mut self, new_grid: String) -> Result<(), PrintingError>
fn dynamic_print(&mut self, new_grid: String) -> Result<(), PrintingError>
This method will print any grid to the terminal based on the PrintingPosition.
When printing a new grid to the screen, it’ll compare every character from the previous one, and only print the characters that have changed.
Errors
- The given grid wasn’t rectangular in shape.
- The string for the grid is empty.
- The given grid is larger than the current dimensions of the terminal.
Example
use screen_printer::printer::*;
const WIDTH: usize = 3;
const HEIGHT: usize = 3;
fn main() {
print!("\u{1b}[2J"); // Clear all text on the terminal
// The default printing position is the bottom left of the terminal
let mut printer = Printer::new();
// Create the first grid to be printed.
let grid_1 = "abc\n123\nxyz".to_string();
// print the first grid.
printer.dynamic_print(grid_1).unwrap();
// Wait before printing the second grid.
std::thread::sleep(std::time::Duration::from_millis(500));
// Create the second grid to be printed.
let grid_2 = "abc\n789\nxyz".to_string();
// Print the second grid.
// This will only end up printing the difference between the two grids/
printer.dynamic_print(grid_2).unwrap();
}This will result in
abc
123
xyz
Into
abc
789 < only line that was actually printed
xyz
For more information about using the printer, refer to the example on github
sourcefn clear_grid(&mut self) -> Result<(), PrintingError>
fn clear_grid(&mut self) -> Result<(), PrintingError>
Replaces every character in the grid with whitespace.
Errors
- Grid dimensions weren’t defined.
- Origin wasn’t defined.