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
use crate::{core::Printer, render::Render, string::CowString};
#[cfg(not(feature = "unicode-width"))]
fn text_len(text: &str) -> usize {
text.chars().count()
}
#[cfg(feature = "unicode-width")]
fn text_len(text: &str) -> usize {
unicode_width::UnicodeWidthStr::width(text)
}
impl<'a, S: AsRef<str>, R: Render, E> Printer<'a, R, S, E> {
/// Write a text element.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.text("Hello, world!")?;
/// assert_eq!(pp.finish()?, "Hello, world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn text(&mut self, text: &'a (impl AsRef<str> + ?Sized)) -> Result<(), R::Error> {
let text = text.as_ref();
let width = text_len(text);
self.scan_text(CowString::Borrowed(text), width)
}
/// Write a text element.
///
/// This method is similar to [`Printer::text`], but it takes an owned
/// string.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.text_owned("Hello, world!".to_string())?;
/// assert_eq!(pp.finish()?, "Hello, world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn text_owned(&mut self, text: impl Into<S>) -> Result<(), R::Error> {
let text = text.into();
let width = text_len(text.as_ref());
self.scan_text(CowString::Owned(text), width)
}
/// Write a hard line break.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.text("Hello,")?;
/// pp.hard_break()?;
/// pp.text("world!")?;
/// assert_eq!(pp.finish()?, "Hello,\nworld!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn hard_break(&mut self) -> Result<(), R::Error> {
self.scan_break(Self::MAX_WIDTH, 0)
}
/// Write a zero-width line break.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.text("Hello,")?;
/// pp.zero_break()?;
/// pp.text("world!")?;
/// assert_eq!(pp.finish()?, "Hello,world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn zero_break(&mut self) -> Result<(), R::Error> {
self.scan_break(0, 0)
}
/// Write a number of spaces.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.text("Hello,")?;
/// pp.spaces(2)?;
/// pp.text("world!")?;
/// assert_eq!(pp.finish()?, "Hello, world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn spaces(&mut self, n: usize) -> Result<(), R::Error> {
self.scan_break(n, 0)
}
/// Write a space (soft line break).
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.text("Hello,")?;
/// pp.space()?;
/// pp.text("world!")?;
/// assert_eq!(pp.finish()?, "Hello, world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn space(&mut self) -> Result<(), R::Error> {
self.scan_break(1, 0)
}
/// Write a group.
///
/// The `consistent` parameter controls whether the group is
/// rendered with consistent indentation or not. See [`Printer::cgroup`]
/// and [`Printer::igroup`] for details.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.group(2, true, |pp| {
/// pp.text("Hello,")?;
/// pp.hard_break()?;
/// pp.text("world!")?;
/// Ok(())
/// })?;
/// assert_eq!(pp.finish()?, "Hello,\n world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn group(
&mut self,
indent: isize,
consistent: bool,
f: impl FnOnce(&mut Self) -> Result<(), R::Error>,
) -> Result<(), R::Error> {
self.scan_begin(indent, consistent);
f(self)?;
self.scan_end()
}
/// Write a consistent indented group.
///
/// Once the group cannot be fit in the current line, all the breakable
/// elements will be written on a new line.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.cgroup(2, |pp| {
/// pp.text("foo")?;
/// pp.hard_break()?;
/// pp.text("Hello,")?;
/// pp.space()?;
/// pp.text("world!")?;
/// Ok(())
/// })?;
/// assert_eq!(pp.finish()?, "foo\n Hello,\n world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn cgroup(
&mut self,
indent: isize,
f: impl FnOnce(&mut Self) -> Result<(), R::Error>,
) -> Result<(), R::Error> {
self.group(indent, true, f)
}
/// Write an inconsistent indented group.
///
/// Even if after the first line break, the printer still attempts to flow
/// the rest of the elements onto as few lines as possible.
///
/// ```
/// # use elegance::Printer;
/// let mut pp = Printer::new(String::new(), 40);
/// pp.igroup(2, |pp| {
/// pp.text("foo")?;
/// pp.hard_break()?;
/// pp.text("Hello,")?;
/// pp.space()?;
/// pp.text("world!")?;
/// Ok(())
/// })?;
/// assert_eq!(pp.finish()?, "foo\n Hello, world!");
/// # Ok::<(), std::convert::Infallible>(())
/// ```
#[inline]
pub fn igroup(
&mut self,
indent: isize,
f: impl FnOnce(&mut Self) -> Result<(), R::Error>,
) -> Result<(), R::Error> {
self.group(indent, false, f)
}
}