pub enum PrettyTree<'a, T = Cow<'a, str>> {
Show 13 variants
Nil,
Hardline,
Text(T),
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 + 'a>,
},
Nesting {
invoke: Rc<dyn Fn(usize) -> Self + 'a>,
},
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(T)
A dynamic text document, all newlines are hard line breaks
Annotated
A document with ansi styles
Append
Concatenates two documents
Group
Concatenates two documents with a space in between
MaybeInline
Concatenates two documents with a line in between
Nest
Concatenates two documents with a line in between
RenderLength
Stores the length of a string document that is not just ascii
Union
Concatenates two documents with a line in between
Column
Concatenates two documents with a line in between
Nesting
Concatenates two documents with a line in between
Fail
Concatenates two documents with a line in between
Implementations§
Source§impl<'a, T> PrettyTree<'a, T>
impl<'a, T> PrettyTree<'a, T>
Sourcepub fn pretty(&self, width: usize) -> PrettyFormatter<'_, 'a, T>
pub fn pretty(&self, width: usize) -> PrettyFormatter<'_, 'a, T>
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<'a, T: Text<'a>> PrettyTree<'a, T>
impl<'a, T: Text<'a>> PrettyTree<'a, T>
Sourcepub fn line_or_space() -> Self
pub fn line_or_space() -> Self
A line acts like a \n but behaves like space if it is grouped on a single line.
Sourcepub fn line_or_comma() -> Self
pub fn line_or_comma() -> Self
A line acts like \n but behaves like nil if it is grouped on a single line.
Sourcepub fn line_or_nil() -> Self
pub fn line_or_nil() -> Self
Acts like line but behaves like nil if grouped on a single line
Source§impl<'a, T> PrettyTree<'a, T>
impl<'a, T> PrettyTree<'a, T>
Source§impl<'a, T: Text<'a>> PrettyTree<'a, T>
impl<'a, T: Text<'a>> PrettyTree<'a, T>
Sourcepub fn render<W>(&self, width: usize, out: &mut W) -> Result<()>
pub fn render<W>(&self, width: usize, out: &mut W) -> Result<()>
Writes a rendered document to a std::io::Write object.
Sourcepub fn render_vec(
&self,
width: usize,
out: &mut Vec<T>,
) -> Result<(), &'static str>
pub fn render_vec( &self, width: usize, out: &mut Vec<T>, ) -> Result<(), &'static str>
Writes a rendered document to a std::io::Write object.
Sourcepub fn render_fmt<W>(&self, width: usize, out: &mut W) -> Result
pub fn render_fmt<W>(&self, width: usize, out: &mut W) -> Result
Writes a rendered document to a std::fmt::Write object.
Sourcepub fn render_raw<W>(&self, width: usize, out: &mut W) -> Result<(), W::Error>where
W: RenderAnnotated<'a, T> + ?Sized,
pub fn render_raw<W>(&self, width: usize, out: &mut W) -> Result<(), W::Error>where
W: RenderAnnotated<'a, T> + ?Sized,
Writes a rendered document to a RenderAnnotated<A> object.
Source§impl<'a, T: Text<'a>> PrettyTree<'a, T>
impl<'a, T: Text<'a>> PrettyTree<'a, T>
Source§impl<'a, T: Text<'a>> PrettyTree<'a, T>
impl<'a, T: Text<'a>> PrettyTree<'a, T>
Sourcepub fn append<E>(self, follow: E) -> Selfwhere
E: Into<PrettyTree<'a, T>>,
pub fn append<E>(self, follow: E) -> Selfwhere
E: Into<PrettyTree<'a, T>>,
Append the given document after this document.
Sourcepub fn join<I, T1, T2>(terms: I, joint: T2) -> PrettyTree<'a, T>
pub fn join<I, T1, T2>(terms: I, joint: T2) -> PrettyTree<'a, T>
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
Sourcepub fn concat<I>(docs: I) -> Self
pub fn concat<I>(docs: I) -> Self
Allocate a document that intersperses the given separator S between the given documents
Sourcepub fn group(self) -> Self
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.
Sourcepub fn union<E>(self, other: E) -> Selfwhere
E: Into<PrettyTree<'a, T>>,
pub fn union<E>(self, other: E) -> Selfwhere
E: Into<PrettyTree<'a, T>>,
Mark this document as a hard line break.
Sourcepub fn align(self) -> Self
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");Sourcepub fn hang(self, adjust: isize) -> Self
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",
);Sourcepub fn width<F>(self, f: F) -> Self
pub fn width<F>(self, f: F) -> Self
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");Sourcepub fn single_quotes(self) -> Self
pub fn single_quotes(self) -> Self
Puts self between before and after if cond is true
Sourcepub fn double_quotes(self) -> Self
pub fn double_quotes(self) -> Self
Puts self between before and after if cond is true
Trait Implementations§
Source§impl<'a, U, T: Text<'a>> AddAssign<U> for PrettyTree<'a, T>where
U: Into<Self>,
impl<'a, U, T: Text<'a>> AddAssign<U> for PrettyTree<'a, T>where
U: Into<Self>,
Source§fn add_assign(&mut self, rhs: U)
fn add_assign(&mut self, rhs: U)
+= operation. Read moreSource§impl<'a, T: Clone> Clone for PrettyTree<'a, T>
impl<'a, T: Clone> Clone for PrettyTree<'a, T>
Source§impl<'a, T> Debug for PrettyTree<'a, T>
impl<'a, T> Debug for PrettyTree<'a, T>
Source§impl<'a, T> Default for PrettyTree<'a, T>
impl<'a, T> Default for PrettyTree<'a, T>
Source§fn default() -> PrettyTree<'a, T>
fn default() -> PrettyTree<'a, T>
Source§impl<'a, T> From<()> for PrettyTree<'a, T>
impl<'a, T> From<()> for PrettyTree<'a, T>
Source§impl<'a, T: Text<'a>> From<PrettySequence<'a, T>> for PrettyTree<'a, T>
impl<'a, T: Text<'a>> From<PrettySequence<'a, T>> for PrettyTree<'a, T>
Source§fn from(value: PrettySequence<'a, T>) -> Self
fn from(value: PrettySequence<'a, T>) -> Self
Source§impl<'a, T: Text<'a>> From<T> for PrettyTree<'a, T>
impl<'a, T: Text<'a>> From<T> for PrettyTree<'a, T>
Source§impl<'a, T: Text<'a>> PrettyBuilder<'a, T> for PrettyTree<'a, T>
impl<'a, T: Text<'a>> PrettyBuilder<'a, T> for PrettyTree<'a, T>
Source§fn flat_alt<E>(self, flat: E) -> Selfwhere
E: Into<PrettyTree<'a, T>>,
fn flat_alt<E>(self, flat: E) -> Selfwhere
E: Into<PrettyTree<'a, T>>,
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
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(),
);