}
}
Ok(())
}
}
struct Indented<'a, D> {
inner: &'a mut D,
ind: Option<usize>,
started: bool,
}
impl<'a, D> Indented<'a, D> {
fn numbered(inner: &'a mut D, ind: usize) -> Self {
Self {
inner,
ind: Some(ind),
started: false,
}
}
fn new(inner: &'a mut D) -> Self {
Self {
inner,
ind: None,
started: false,
}
}
}
impl<T> fmt::Write for Indented<'_, T>
where
T: fmt::Write,
{
fn write_str(&mut self, s: &str) -> fmt::Result {
for (ind, mut line) in s.split('\n').enumerate() {
if !self.started {
// trim first line to ensure it lines up with the number nicely
line = line.trim();
// Don't render the first line unless its actually got text on it
if line.is_empty() {
continue;
}
self.started = true;
match self.ind {
Some(ind) => self.inner.write_fmt(format_args!("{: >5}: ", ind))?,
None => self.inner.write_fmt(format_args!(" "))?,
}
} else if ind > 0 {
self.inner.write_char('\n')?;
if self.ind.is_some() {
self.inner.write_fmt(format_args!(" "))?;
} else {
self.inner.write_fmt(format_args!(" "))?;
}