Struct Formatter

Source
pub struct Formatter { /* private fields */ }
Expand description

Helper structure to format text

The Formatter structure helps create some simple shapes through text, like tables and just formatted text. By default, for tables a header division pattern of - and no pattern for bridging columns will be used. This can be modified by using either set_table_options or by modify_table_options.

Implementations§

Source§

impl Formatter

Source

pub fn new(width: u8) -> Formatter

Creates a new formatter with a default width

Source

pub fn set_table_options(&mut self, table_options: TableOptions)

Sets a new set of table options

To modify just one parameter in a simpler way, check the modify_table_options method.

let mut formatter = Formatter::new(20);
formatter.set_table_options(TableOptions {
    header_division_pattern: Some(".-".into()),
    join_columns_pattern: Some(".".into())
});
Source

pub fn get_table_options(&self) -> &TableOptions

Gives back a reference to the active table options

let mut formatter = Formatter::new(20);
assert_eq!(Some("-".to_string()), formatter.get_table_options().header_division_pattern);
Source

pub fn modify_table_options<F: Fn(&mut TableOptions)>(&mut self, modifier: F)

Modify the table options through a callback

Allows table options modification with a function or closure. Sometimes it may come in hand.

let mut formatter = Formatter::new(20);
formatter.modify_table_options(|table_options| {
    table_options.header_division_pattern = Some("=".to_string());
});
assert_eq!(Some("=".to_string()), formatter.get_table_options().header_division_pattern);
Source

pub fn space_split<A: AsRef<str>>(&self, source: A) -> String

Splits a string by whitespaces, according to the given width

Notice that the final line will not contain a new line at the end.

use escpos_rs::Formatter;
 
let formatter = Formatter::new(16);
let res = formatter.space_split("Sentence with two lines.");
assert_eq!("Sentence with\ntwo lines.", res.as_str());
Source

pub fn duo_table<A: Into<String>, B: Into<String>, C: IntoIterator<Item = (D, E)>, D: Into<String>, E: Into<String>>( &self, header: (A, B), rows: C, ) -> String

Creates a table with two columns

In case the headers do not fit with at least one space between, priority will be given to the second header, and the last remaining character from the first header will be replaced by a dot. If the second header would need to be shortened to less than 3 characters, then the first header will now also be truncated, with the same dot replacing the last charcater from the remaining part of the first header.

let formatter = Formatter::new(20);
let header = ("Product", "Price");
let rows = vec![
    ("Milk", "5.00"),
    ("Cereal", "10.00")
];

// We use trim_start just to show the table nicer in this example.
let target = r#"
Product        Price
--------------------
Milk            5.00
Cereal         10.00
"#.trim_start();
 
assert_eq!(target, formatter.duo_table(header, rows));
Source

pub fn trio_table<A: Into<String>, B: Into<String>, C: Into<String>, D: IntoIterator<Item = (E, F, G)>, E: Into<String>, F: Into<String>, G: Into<String>>( &self, header: (A, B, C), rows: D, ) -> String

Creates a table with three columns

In case the headers do not fit with at least one space between, priority will be given to the first header, and the last remaining character from the second header will be replaced by a dot. If the second header would need to be shortened to less than 3 characters, then the first header will now also be truncated, with the same dot replacing the last charcater from the remaining part of the first header.

let formatter = Formatter::new(20);
let header = ("Product", "Price", "Qty.");
let rows = vec![
    ("Milk", "5.00", "3"),
    ("Cereal", "10.00", "1")
];

// We use trim_start just to show the table nicer in this example.
let target = r#"
Product  Price  Qty.
--------------------
Milk     5.00      3
Cereal   10.00     1
"#.trim_start();
 
assert_eq!(target, formatter.trio_table(header, rows));

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.