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
impl Formatter
Sourcepub fn set_table_options(&mut self, table_options: TableOptions)
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())
});
Sourcepub fn get_table_options(&self) -> &TableOptions
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);
Sourcepub fn modify_table_options<F: Fn(&mut TableOptions)>(&mut self, modifier: F)
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);
Sourcepub fn space_split<A: AsRef<str>>(&self, source: A) -> String
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());
Sourcepub 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
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));
Sourcepub 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
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§
impl Freeze for Formatter
impl RefUnwindSafe for Formatter
impl Send for Formatter
impl Sync for Formatter
impl Unpin for Formatter
impl UnwindSafe for Formatter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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