pub struct Printer { /* private fields */ }
Expand description

Screen Printer

Screen Printer is a rust crate that will allow you to build and print arrays of data into a grid format.

The purpose of this crate is to make it easier to print rectangular blocks of text to the terminal. Including features like:

  • DynamicPrint, which only prints any characters that changed from any previously printed grid*.
  • PrintingPosition, which allows you to print your string to different places on the terminal, such as the center.

* If the grid changes in size or position it is reprinted in its entirety.

Examples

Using the dynamic print method to print a grid

The core part of this crate is the dynamic_print method. This will take a rectangular grid of characters, and print only the parts of the grid that have changed since the last print.

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_with_printing_position(PrintingPosition::default());

  // 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
Printing Position

Another feature shown in the above example, the PrintingPosition.

This will print the grid in any of the 9 defined positions on the terminal. These are split by the X and Y axes:

  • Left/Top,
  • Middle, and;
  • Right/Bottom.

Implementations§

source§

impl Printer

source

pub fn new() -> Self

Creates a new printer for the dynamic_print() method.

Uses the default PrintingPosition

source

pub fn new_with_printing_position(printing_position: PrintingPosition) -> Self

Creates a new printer for the dynamic_print() method with the given printing position.

PrintingPositons tell the printer where to print any grids passed into it. Refer to PrintingPosition for more information;

source

pub fn replace_printing_position(&mut self, printing_position: PrintingPosition)

source

pub fn replace_x_printing_position( &mut self, new_x_printing_position: XPrintingPosition ) -> Result<(), PrintingError>

Errors
  • There is no defined printing position
source

pub fn replace_y_printing_position( &mut self, new_y_printing_position: YPrintingPosition ) -> Result<(), PrintingError>

Errors
  • There is no defined printing position
source

pub fn get_current_printing_position(&self) -> &PrintingPosition

Returns a reference to the currently stored printing position.

source

pub fn create_grid_from_single_character( character: char, width: usize, height: usize ) -> String

Creates a grid of the given size with the given character.

Example
use screen_printer::printer::*;

let character = 'a';
let expected_grid = "aaa\naaa\naaa";

let grid = Printer::create_grid_from_single_character(character, 3, 3);

assert_eq!(expected_grid, grid);
source

pub fn create_grid_from_full_character_list<T>( characters: &Vec<T>, width: usize, height: usize ) -> Result<String, PrintingError>where T: Display,

Creates a grid of the given size with the given list of characters

Example
use screen_printer::printer::*;

let characters = vec!["a", "b", "c", "d", "e", "f", "g", "h", "i"];
let expected_grid = "abc\ndef\nghi";

let grid = Printer::create_grid_from_full_character_list(&characters, 3, 3).unwrap();

assert_eq!(expected_grid, grid);
Errors
  • When the amount of characters passed in doesn’t fit the expected grid dimensions.
source

pub fn print_over_previous_grid(grid: String, height: usize)

Moves the cursor up by the given height and prints the given grid.

This is for printing over the previously printed grid. It’s recommended to add some whitespace before your first print so the grid doesn’t print into anything that was printed before this method was called.

Example
use screen_printer::printer::*;

let height = 10;
let width = 10;
let grid = Printer::create_grid_from_single_character('a', width, height);

print!("{}", "\n".repeat(height + 5)); // add some space for the grid
Printer::print_over_previous_grid(grid, height);
source

pub fn get_terminal_dimensions() -> Result<(usize, usize), PrintingError>

Returns the current dimensions of the terminal.

Errors
source

pub fn reset(&mut self)

Resets all data for the printer.

source

pub fn reset_and_retain_printing_position(&mut self)

Resets all data for the printer except for the current position.

source

pub fn reset_with_position(&mut self, printing_position: PrintingPosition)

Resets all data for the printer and assigns the given printing position.

Trait Implementations§

source§

impl Debug for Printer

source§

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

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

impl Default for Printer

source§

fn default() -> Printer

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

impl DynamicPrinter for Printer

source§

fn dynamic_print(&mut self, new_grid: String) -> Result<(), PrintingError>

This method will print any grid to the terminal based on the PrintingPosition. Read more
source§

fn clear_grid(&mut self) -> Result<(), PrintingError>

Replaces every character in the grid with whitespace. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.