Struct PrintOptions

Source
pub struct PrintOptions {
    pub line_break: LineBreak,
    pub indent_kind: IndentKind,
    pub width: usize,
    pub tab_size: usize,
}
Expand description

Print control options, such as line break and indentation kind.

Fields§

§line_break: LineBreak

Line break for each line. It can be “\n” (LF) or “\r\n” (CRLF).

Default value is LF.

use tiny_pretty::{print, Doc, LineBreak, PrintOptions};

let doc = Doc::list(vec![Doc::text("a"), Doc::hard_line(), Doc::text("b")]);

assert_eq!("a\nb", &print(&doc, &PrintOptions {
    line_break: LineBreak::Lf,
    ..Default::default()
}));

assert_eq!("a\r\nb", &print(&doc, &PrintOptions {
    line_break: LineBreak::Crlf,
    ..Default::default()
}));
§indent_kind: IndentKind

To use space or tab for indentation.

Note that when using tabs and calling nest with offset, it doesn’t mean it will print tabs with the number of offset. Of course, it will print tabs as possible, however if indent % tab_size != 0, it will print tabs first and then fill with spaces to match indentation. Specifically, it prints indent / tab_size times tabs then prints indent % tab_size times spaces. See the documentation and examples of the tab_size option below.

Default value is space.

use tiny_pretty::{print, Doc, IndentKind, PrintOptions};

let doc = Doc::list(vec![Doc::text("a"), Doc::hard_line().nest(2), Doc::text("b")]);

assert_eq!("a\n  b", &print(&doc, &PrintOptions {
    indent_kind: IndentKind::Space,
    ..Default::default()
}));

assert_eq!("a\n\tb", &print(&doc, &PrintOptions {
    indent_kind: IndentKind::Tab,
    ..Default::default()
}));
§width: usize

The limitation that pretty printer should (but not must) avoid columns exceeding. Pretty printer will try its best to keep column width less than this value, but it may exceed for some cases, for example, a very very long single word.

Default value is 80.

use tiny_pretty::{print, Doc, PrintOptions};

let doc = Doc::list(vec![Doc::text("aaaa"), Doc::line_or_space(), Doc::text("bbbb")]).group();
assert_eq!("aaaa\nbbbb", &print(&doc, &PrintOptions {
    width: 5,
    ..Default::default()
}));

assert_eq!("aaaa bbbb", &print(&doc, &PrintOptions {
    width: 20,
    ..Default::default()
}));

let doc = Doc::list(vec![Doc::text("aaaaaaaa"), Doc::line_or_space(), Doc::text("bbbbbbbb")])
    .group();
assert_eq!("aaaaaaaa\nbbbbbbbb", &print(&doc, &PrintOptions {
    width: 5,
    ..Default::default()
}));
§tab_size: usize

Tab size is not indentation size. If indent_kind is set to Tab, when indentation level satisfies the tab_size, it will convert those spaces to tabs.

If you’re implementing a high-level formatter or pretty printer, it’s highly recommended to set this value as same as indentation size of your formatter or pretty printer.

Default value is 2. It can’t be zero. This option will be ignored when indent_kind is Space.

use tiny_pretty::{print, Doc, IndentKind, PrintOptions};

let doc = Doc::list(vec![Doc::text("aaaa"), Doc::hard_line(), Doc::text("bbbb")])
    .group()
    .nest(8);

assert_eq!("aaaa\n\t   bbbb", &print(&doc, &PrintOptions {
    indent_kind: IndentKind::Tab,
    tab_size: 5,
    ..Default::default()
}));

assert_eq!("aaaa\n\t\tbbbb", &print(&doc, &PrintOptions {
    indent_kind: IndentKind::Tab,
    tab_size: 4,
    ..Default::default()
}));

assert_eq!("aaaa\n        bbbb", &print(&doc, &PrintOptions {
    indent_kind: IndentKind::Space,
    tab_size: 5,
    ..Default::default()
}));

Trait Implementations§

Source§

impl Clone for PrintOptions

Source§

fn clone(&self) -> PrintOptions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PrintOptions

Source§

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

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

impl Default for PrintOptions

Source§

fn default() -> Self

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

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.