PrettyTree

Enum PrettyTree 

Source
pub enum PrettyTree {
Show 14 variants Nil, Hardline, Text(Rc<str>), StaticText(&'static str), Annotated { style: Rc<AnsiStyle>, body: Rc<Self>, }, Append { lhs: Rc<Self>, rhs: Rc<Self>, }, Group { items: Rc<Self>, }, MaybeInline { block: Rc<Self>, inline: Rc<Self>, }, Nest { space: isize, doc: Rc<Self>, }, RenderLength { length: usize, body: Rc<Self>, }, Union { lhs: Rc<Self>, rhs: Rc<Self>, }, Column { invoke: Rc<dyn Fn(usize) -> Self>, }, Nesting { invoke: Rc<dyn Fn(usize) -> Self>, }, Fail,
}
Expand description

The concrete document type. This type is not meant to be used directly. Instead use the static functions on Doc or the methods on an DocAllocator.

The T parameter is used to abstract over pointers to Doc. See RefDoc and BoxDoc for how it is used

Variants§

§

Nil

Nothing to show

§

Hardline

A hard line break

§

Text(Rc<str>)

A dynamic text document, all newlines are hard line breaks

§

StaticText(&'static str)

A static text document, all newlines are hard line breaks

§

Annotated

A document with ansi styles

Fields

§style: Rc<AnsiStyle>

The style to use for the text

§body: Rc<Self>

The text to display

§

Append

Concatenates two documents

Fields

§lhs: Rc<Self>

The first document

§rhs: Rc<Self>

The second document

§

Group

Concatenates two documents with a space in between

Fields

§items: Rc<Self>

The first document

§

MaybeInline

Concatenates two documents with a line in between

Fields

§block: Rc<Self>

The first document

§inline: Rc<Self>

The second document

§

Nest

Concatenates two documents with a line in between

Fields

§space: isize

The first document

§doc: Rc<Self>

The second document

§

RenderLength

Stores the length of a string document that is not just ascii

Fields

§length: usize

The length of the string

§body: Rc<Self>

The document

§

Union

Concatenates two documents with a line in between

Fields

§lhs: Rc<Self>

The first document

§rhs: Rc<Self>

The second document

§

Column

Concatenates two documents with a line in between

Fields

§invoke: Rc<dyn Fn(usize) -> Self>

The first document

§

Nesting

Concatenates two documents with a line in between

Fields

§invoke: Rc<dyn Fn(usize) -> Self>

The first document

§

Fail

Concatenates two documents with a line in between

Implementations§

Source§

impl PrettyTree

Source

pub fn pretty(&self, width: usize) -> PrettyFormatter<'_>

Returns a value which implements std::fmt::Display

use pretty::{BoxDoc, Doc};
let doc =
    BoxDoc::<()>::group(BoxDoc::text("hello").append(Doc::line()).append(Doc::text("world")));
assert_eq!(format!("{}", doc.pretty(80)), "hello world");
Source§

impl PrettyTree

Source

pub const Space: Self

A hard line break

Source

pub fn line_or_space() -> Self

A line acts like a \n but behaves like space if it is grouped on a single line.

Source

pub fn line_or_comma() -> Self

A line acts like \n but behaves like nil if it is grouped on a single line.

Source

pub fn line_or_nil() -> Self

Acts like line but behaves like nil if grouped on a single line

Source§

impl PrettyTree

Source

pub fn text<U: Into<Cow<'static, str>>>(data: U) -> Self

The given text, which must not contain line breaks.

Source§

impl PrettyTree

Source

pub fn render<W>(&self, width: usize, out: &mut W) -> Result<()>
where W: ?Sized + Write,

Writes a rendered document to a std::io::Write object.

Source

pub fn render_fmt<W>(&self, width: usize, out: &mut W) -> Result
where W: ?Sized + Write,

Writes a rendered document to a std::fmt::Write object.

Source

pub fn render_raw<W>(&self, width: usize, out: &mut W) -> Result<(), W::Error>
where W: RenderAnnotated + ?Sized,

Writes a rendered document to a RenderAnnotated<A> object.

Source§

impl PrettyTree

Source

pub fn render_colored<W: Write>(&self, width: usize, out: W) -> Result<()>

The given text, which must not contain line breaks.

Source§

impl PrettyTree

Source

pub fn append<E>(self, follow: E) -> Self
where E: Into<PrettyTree>,

Append the given document after this document.

Source

pub fn join<I, T1, T2>(terms: I, joint: T2) -> PrettyTree
where I: IntoIterator<Item = T1>, T1: Into<PrettyTree>, T2: Into<PrettyTree>,

Allocate a document that intersperses the given separator S between the given documents [A, B, C, ..., Z], yielding [A, S, B, S, C, S, ..., S, Z].

Compare the intersperse method from the itertools crate.

NOTE: The separator type, S may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

Source

pub fn concat<I>(docs: I) -> Self

Allocate a document that intersperses the given separator S between the given documents

Source

pub fn group(self) -> Self

Mark this document as a group.

Groups are layed out on a single line if possible. Within a group, all basic documents with several possible layouts are assigned the same layout, that is, they are all layed out horizontally and combined into a one single line, or they are each layed out on their own line.

Source

pub fn annotate(self, style: Rc<AnsiStyle>) -> Self

Mark this document as a comment.

Source

pub fn union<E>(self, other: E) -> Self
where E: Into<PrettyTree>,

Mark this document as a hard line break.

Source

pub fn align(self) -> Self

Lays out self so with the nesting level set to the current column

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::{docs, DocAllocator};

let arena = &pretty::Arena::<()>::new();
let doc = docs![
    arena,
    "lorem",
    " ",
    arena.intersperse(["ipsum", "dolor"].iter().cloned(), arena.line_()).align(),
    arena.hardline(),
    "next",
];
assert_eq!(doc.1.pretty(80).to_string(), "lorem ipsum\n      dolor\nnext");
Source

pub fn hang(self, adjust: isize) -> Self

Lays out self with a nesting level set to the current level plus adjust.

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena
    .text("prefix")
    .append(arena.text(" "))
    .append(arena.reflow("Indenting these words with nest").hang(4));
assert_eq!(
    doc.1.pretty(24).to_string(),
    "prefix Indenting these\n           words with\n           nest",
);
Source

pub fn width<F>(self, f: F) -> Self
where F: Fn(isize) -> Self + Clone + 'static,

Lays out self and provides the column width of it available to f

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena
    .text("prefix ")
    .append(arena.column(|l| arena.text("| <- column ").append(arena.as_string(l)).into_doc()));
assert_eq!(doc.1.pretty(80).to_string(), "prefix | <- column 7");
Source

pub fn enclose<E, F>(self, before: E, after: F) -> Self
where E: Into<Self>, F: Into<Self>,

Puts self between before and after

Source

pub fn single_quotes(self) -> Self

Puts self between before and after if cond is true

Source

pub fn double_quotes(self) -> Self

Puts self between before and after if cond is true

Source

pub fn parens(self) -> Self

Puts self between before and after if cond is true

Source

pub fn angles(self) -> Self

Puts self between before and after if cond is true

Source

pub fn braces(self) -> Self

Puts self between before and after if cond is true

Source

pub fn brackets(self) -> Self

Puts self between before and after if cond is true

Trait Implementations§

Source§

impl<T> Add<T> for PrettyTree
where T: Into<Self>,

Source§

type Output = PrettyTree

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> AddAssign<T> for PrettyTree
where T: Into<Self>,

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl Clone for PrettyTree

Source§

fn clone(&self) -> Self

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 PrettyTree

Source§

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

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

impl Default for PrettyTree

Source§

fn default() -> Self

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

impl From<&'static str> for PrettyTree

Source§

fn from(s: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<()> for PrettyTree

Source§

fn from(_: ()) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Option<T>> for PrettyTree
where Self: From<T>,

Source§

fn from(x: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<PrettySequence> for PrettyTree

Source§

fn from(value: PrettySequence) -> Self

Converts to this type from the input type.
Source§

impl From<String> for PrettyTree

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl PrettyBuilder for PrettyTree

Source§

fn flat_alt<E>(self, flat: E) -> Self
where E: Into<PrettyTree>,

Acts as self when laid out on multiple lines and acts as that when laid out on a single line.

use pretty::{Arena, DocAllocator};

let arena = Arena::<()>::new();
let body = arena.line().append("x");
let doc = arena
    .text("let")
    .append(arena.line())
    .append("x")
    .group()
    .append(body.clone().flat_alt(arena.line().append("in").append(body)))
    .group();

assert_eq!(doc.1.pretty(100).to_string(), "let x in x");
assert_eq!(doc.1.pretty(8).to_string(), "let x\nx");
Source§

fn indent(self, adjust: usize) -> Self

Indents self by adjust spaces from the current cursor position

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena
    .text("prefix")
    .append(arena.text(" "))
    .append(arena.reflow("The indent function indents these words!").indent(4));
assert_eq!(
    doc.1.pretty(24).to_string(),
    "
prefix     The indent
           function
           indents these
           words!"
        .trim_start(),
);
Source§

fn nest(self, offset: isize) -> Self

Increase the indentation level of this document.

Source§

impl PrettyPrint for PrettyTree

Source§

fn pretty(&self, _: &PrettyProvider) -> PrettyTree

Build a pretty tree for this type.
Source§

fn pretty_string(&self, theme: &PrettyProvider) -> String

Get a pretty string for this type.
Source§

fn pretty_colorful(&self, theme: &PrettyProvider) -> String

Print a pretty string for this type.

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.