| Crates.io | pprint |
| lib.rs | pprint |
| version | 0.2.2 |
| created_at | 2023-08-08 01:14:58.6552+00 |
| updated_at | 2024-06-27 05:57:44.954048+00 |
| description | Flexible and lightweight pretty printing library for Rust |
| homepage | |
| repository | https://github.com/mkbabb/pprint |
| max_upload_size | |
| id | 938668 |
| size | 27,087 |
pprintA Rust library for pretty ✨ printing using a document model. Automatically derive
Pretty for structs, enums, and primitive types; vector and map types are also
supported by default; very similar to the derive(Debug) macro, just prettier and more
configurable.
use pprint::{Doc, Printer, PRINTER};
let doc = Doc::from(vec![1, 2, 3])
.wrap("[", "]")
.join(", ");
print!("{}", PRINTER.pretty(doc));
// prints:
// [
// 1,
// 2,
// 3
// ]
The document model provides a rich set of building blocks:
concat, join, wrap, groupindent and dedentif_breakhardline, softlineThe Printer handles pretty printing a Doc to a string with configurable options:
max_width - maximum width of each lineindent - number of spaces for each indentation levelbreak_long_text - insert line breaks for long textuse_tabs - use tabs instead of spaces for indentationHalf of the library's development time was spent on the derive macro, allowing for easy pretty printing of essentially any type. Here's a trivial example:
#[derive(Pretty)]
struct Point {
x: f64,
y: f64
}
let point = Point { x: 1.0, y: 2.0 };
print!("{}", Doc::from(point)); // prints "(x: 1, y: 2)"
Pretty supports an additional attribute, pprint, which is used to customize an
object's pretty printing definition. The following options are available:
#[derive(Pretty)]
#[pprint(verbose)]
struct Point {
#[pprint(rename = "x-coordinate")]
x: f64,
#[pprint(rename = "y-coordinate")]
y: f64
#[pprint(skip)]
_skip_me: bool,
}
let point = Point { x: 1.0, y: 2.0, _skip_me: true };
print!("{}", Doc::from(point));
// prints:
// Point {
// x-coordinate: 1,
// y-coordinate: 2
// }
Structures can be arbitrarily nested, & c. & c. More involved examples can be found in the tests file.
This library was partway created as a means by which to learn more about Rust's procedural macros, and partway because I just love pretty printing. It's a work in progress, but I'm fairly pleased with it hitherto. If you have any suggestions, please feel free to open an issue or a pull request.
Also of note is the implementation of the text_justify function, which was based off the wonderful series of lectures (specifically No. 20) from MIT's 6.006 course, "Introduction to Algorithms". It's a simple and rather elegant algorithm that I've found to be quite effective in practice.