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
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 PrettyTree
impl PrettyTree
Sourcepub fn pretty(&self, width: usize) -> PrettyFormatter<'_>
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
impl PrettyTree
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 PrettyTree
impl PrettyTree
Source§impl PrettyTree
impl PrettyTree
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_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 + ?Sized,
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
impl PrettyTree
Source§impl PrettyTree
impl PrettyTree
Sourcepub fn append<E>(self, follow: E) -> Selfwhere
E: Into<PrettyTree>,
pub fn append<E>(self, follow: E) -> Selfwhere
E: Into<PrettyTree>,
Append the given document after this document.
Sourcepub fn join<I, T1, T2>(terms: I, joint: T2) -> PrettyTree
pub fn join<I, T1, T2>(terms: I, joint: T2) -> 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
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>,
pub fn union<E>(self, other: E) -> Selfwhere
E: Into<PrettyTree>,
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<T> Add<T> for PrettyTreewhere
T: Into<Self>,
impl<T> Add<T> for PrettyTreewhere
T: Into<Self>,
Source§impl<T> AddAssign<T> for PrettyTreewhere
T: Into<Self>,
impl<T> AddAssign<T> for PrettyTreewhere
T: Into<Self>,
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+= operation. Read moreSource§impl Clone for PrettyTree
impl Clone for PrettyTree
Source§impl Debug for PrettyTree
impl Debug for PrettyTree
Source§impl Default for PrettyTree
impl Default for PrettyTree
Source§impl From<&'static str> for PrettyTree
impl From<&'static str> for PrettyTree
Source§impl From<()> for PrettyTree
impl From<()> for PrettyTree
Source§impl From<PrettySequence> for PrettyTree
impl From<PrettySequence> for PrettyTree
Source§fn from(value: PrettySequence) -> Self
fn from(value: PrettySequence) -> Self
Source§impl From<String> for PrettyTree
impl From<String> for PrettyTree
Source§impl PrettyBuilder for PrettyTree
impl PrettyBuilder for PrettyTree
Source§fn flat_alt<E>(self, flat: E) -> Selfwhere
E: Into<PrettyTree>,
fn flat_alt<E>(self, flat: E) -> Selfwhere
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
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(),
);