1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use crate::{helpers::PrettySequence, render, FmtWrite, PrettyBuilder, RenderAnnotated};
use alloc::{borrow::Cow, rc::Rc, string::String};
use color_ansi::AnsiStyle;
use core::{
fmt::{Debug, Formatter},
ops::{Add, AddAssign},
};
use std::io::Write;
use unicode_segmentation::UnicodeSegmentation;
mod display;
mod into;
/// 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
pub enum PrettyTree {
/// Nothing to show
Nil,
/// A hard line break
Hardline,
/// A dynamic text document, all newlines are hard line breaks
Text(Rc<str>),
/// A static text document, all newlines are hard line breaks
StaticText(&'static str),
/// A document with ansi styles
Annotated {
/// The style to use for the text
style: Rc<AnsiStyle>,
/// The text to display
body: Rc<Self>,
},
/// Concatenates two documents
Append {
/// The first document
lhs: Rc<Self>,
/// The second document
rhs: Rc<Self>,
},
/// Concatenates two documents with a space in between
Group {
/// The first document
items: Rc<Self>,
},
/// Concatenates two documents with a line in between
MaybeInline {
/// The first document
block: Rc<Self>,
/// The second document
inline: Rc<Self>,
},
/// Concatenates two documents with a line in between
Nest {
/// The first document
space: isize,
/// The second document
doc: Rc<Self>,
},
/// Stores the length of a string document that is not just ascii
RenderLength {
/// The length of the string
length: usize,
/// The document
body: Rc<Self>,
},
/// Concatenates two documents with a line in between
Union {
/// The first document
lhs: Rc<Self>,
/// The second document
rhs: Rc<Self>,
},
/// Concatenates two documents with a line in between
Column {
/// The first document
invoke: Rc<dyn Fn(usize) -> Self>,
},
/// Concatenates two documents with a line in between
Nesting {
/// The first document
invoke: Rc<dyn Fn(usize) -> Self>,
},
/// Concatenates two documents with a line in between
Fail,
}
#[allow(non_upper_case_globals)]
impl PrettyTree {
/// A hard line break
pub const Space: Self = PrettyTree::StaticText(" ");
/// A line acts like a `\n` but behaves like `space` if it is grouped on a single line.
#[inline]
pub fn line_or_space() -> Self {
Self::Hardline.flat_alt(Self::Space).into()
}
/// A line acts like `\n` but behaves like `nil` if it is grouped on a single line.
#[inline]
pub fn line_or_comma() -> Self {
Self::Hardline.flat_alt(PrettyTree::StaticText(", ")).into()
}
/// Acts like `line` but behaves like `nil` if grouped on a single line
#[inline]
pub fn line_or_nil() -> Self {
Self::Hardline.flat_alt(Self::Nil).into()
}
}
impl PrettyTree {
/// The given text, which must not contain line breaks.
#[inline]
pub fn text<U: Into<Cow<'static, str>>>(data: U) -> Self {
match data.into() {
Cow::Borrowed(s) => PrettyTree::StaticText(s),
Cow::Owned(s) => PrettyTree::Text(Rc::from(s)),
}
.with_utf8_len()
}
}
impl PrettyTree {
/// Writes a rendered document to a `std::io::Write` object.
#[inline]
#[cfg(feature = "std")]
pub fn render<W>(&self, width: usize, out: &mut W) -> std::io::Result<()>
where
W: ?Sized + std::io::Write,
{
self.render_raw(width, &mut crate::IoWrite::new(out))
}
/// Writes a rendered document to a `std::fmt::Write` object.
#[inline]
pub fn render_fmt<W>(&self, width: usize, out: &mut W) -> core::fmt::Result
where
W: ?Sized + core::fmt::Write,
{
self.render_raw(width, &mut FmtWrite::new(out))
}
/// Writes a rendered document to a `RenderAnnotated<A>` object.
#[inline]
pub fn render_raw<W>(&self, width: usize, out: &mut W) -> Result<(), W::Error>
where
W: RenderAnnotated,
W: ?Sized,
{
render::best(Rc::new(self.clone()), width, out)
}
}
impl PrettyTree {
/// The given text, which must not contain line breaks.
#[inline]
#[cfg(feature = "std")]
pub fn render_colored<W: Write>(&self, width: usize, out: W) -> std::io::Result<()> {
render::best(Rc::new(self.clone()), width, &mut crate::TerminalWriter::new(out))
}
}
impl PrettyBuilder for 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");
/// ```
#[inline]
fn flat_alt<E>(self, flat: E) -> Self
where
E: Into<PrettyTree>,
{
Self::MaybeInline { block: Rc::new(self), inline: Rc::new(flat.into()) }
}
/// 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`
///
/// ```rust
/// 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(),
/// );
/// ```
#[inline]
fn indent(self, adjust: usize) -> Self {
let spaces = {
use crate::render::SPACES;
let mut doc = PrettyTree::Nil;
let mut remaining = adjust;
while remaining != 0 {
let i = SPACES.len().min(remaining);
remaining -= i;
doc = doc.append(PrettyTree::text(&SPACES[..i]))
}
doc
};
spaces.append(self).hang(adjust.try_into().unwrap())
}
/// Increase the indentation level of this document.
#[inline]
fn nest(self, offset: isize) -> Self {
if let Self::Nil = self {
return self;
}
if offset == 0 {
return self;
}
Self::Nest { space: offset, doc: Rc::new(self) }
}
}
impl PrettyTree {
fn with_utf8_len(self) -> Self {
let s = match &self {
Self::Text(s) => s.as_ref(),
Self::StaticText(s) => s,
// Doc::SmallText(s) => s,
_ => return self,
};
if s.is_ascii() {
self
}
else {
let grapheme_len = s.graphemes(true).count();
Self::RenderLength { length: grapheme_len, body: Rc::new(self) }
}
}
/// Append the given document after this document.
#[inline]
pub fn append<E>(self, follow: E) -> Self
where
E: Into<PrettyTree>,
{
let rhs = follow.into();
match (&self, &rhs) {
(Self::Nil, _) => rhs,
(_, Self::Nil) => self,
_ => Self::Append { lhs: Rc::new(self), rhs: Rc::new(rhs) },
}
}
/// 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](https://docs.rs/itertools/0.5.9/itertools/trait.Itertools.html#method.intersperse).
///
/// NOTE: The separator type, `S` may need to be cloned. Consider using cheaply cloneable ptr
/// like `RefDoc` or `RcDoc`
#[inline]
pub fn join<I, T1, T2>(terms: I, joint: T2) -> PrettyTree
where
I: IntoIterator<Item = T1>,
T1: Into<PrettyTree>,
T2: Into<PrettyTree>,
{
let joint = joint.into();
let mut iter = terms.into_iter().map(|s| s.into());
let mut terms = PrettySequence::new(0);
terms += iter.next().unwrap_or(PrettyTree::Nil);
for term in iter {
terms += joint.clone();
terms += term;
}
terms.into()
}
/// Allocate a document that intersperses the given separator `S` between the given documents
pub fn concat<I>(docs: I) -> Self
where
I: IntoIterator,
I::Item: Into<PrettyTree>,
{
let mut head = Self::Nil;
for item in docs.into_iter() {
head += item.into();
}
head
}
/// 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.
#[inline]
pub fn group(self) -> Self {
match self {
Self::Group { .. } | Self::Text(_) | Self::StaticText(_) | Self::Nil => self,
_ => Self::Group { items: Rc::new(self) },
}
}
/// Mark this document as a comment.
#[inline]
pub fn annotate(self, style: Rc<AnsiStyle>) -> Self {
Self::Annotated { style: style, body: Rc::new(self) }
}
/// Mark this document as a hard line break.
#[inline]
pub fn union<E>(self, other: E) -> Self
where
E: Into<PrettyTree>,
{
Self::Union { lhs: Rc::new(self), rhs: Rc::new(other.into()) }
}
/// 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`
///
/// ```rust
/// 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");
/// ```
#[inline]
pub fn align(self) -> Self {
Self::Column {
invoke: Rc::new(move |col| {
let self_ = self.clone();
Self::Nesting { invoke: Rc::new(move |nest| self_.clone().nest(col as isize - nest as isize)) }
}),
}
}
/// 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`
///
/// ```rust
/// 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",
/// );
/// ```
#[inline]
pub fn hang(self, adjust: isize) -> Self {
self.nest(adjust).align()
}
/// 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`
///
/// ```rust
/// 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");
/// ```
#[inline]
pub fn width<F>(self, f: F) -> Self
where
F: Fn(isize) -> Self + Clone + 'static,
{
Self::Column {
invoke: Rc::new(move |start| {
let f = f.clone();
self.clone() + Self::Column { invoke: Rc::new(move |end| f(end as isize - start as isize)) }
}),
}
}
/// Puts `self` between `before` and `after`
#[inline]
pub fn enclose<E, F>(self, before: E, after: F) -> Self
where
E: Into<Self>,
F: Into<Self>,
{
before.into().append(self).append(after.into())
}
/// Puts `self` between `before` and `after` if `cond` is true
pub fn single_quotes(self) -> Self {
self.enclose("'", "'")
}
/// Puts `self` between `before` and `after` if `cond` is true
pub fn double_quotes(self) -> Self {
self.enclose("\"", "\"")
}
/// Puts `self` between `before` and `after` if `cond` is true
pub fn parens(self) -> Self {
self.enclose("(", ")")
}
/// Puts `self` between `before` and `after` if `cond` is true
pub fn angles(self) -> Self {
self.enclose("<", ">")
}
/// Puts `self` between `before` and `after` if `cond` is true
pub fn braces(self) -> Self {
self.enclose("{", "}")
}
/// Puts `self` between `before` and `after` if `cond` is true
pub fn brackets(self) -> Self {
self.enclose("[", "]")
}
}