impl<W> TrackingWriter<W>
where
W: Write,
{
fn new(inner: W) -> Self {
Self { column: 0, inner }
}
}
impl<W> Write for TrackingWriter<W>
where
W: Write,
{
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let written = self.inner.write(buf)?;
let buffer = &buf[..written];
// Extract the last line written to the inner writer
let line = buffer.rsplit_once_str(b"\n").map_or(buffer, |(_, line)| {
// Since we encountered a line separator, reset the column
self.column = 0;
line
});
self.column += line.graphemes().count();
Ok(written)
}
fn flush(&mut self) -> Result<()> {
self.inner.flush()
}
}