#![no_std]
use core::fmt::{self, Formatter, Write};
pub struct PadAdapter<'a, 'b: 'a, 'c> {
fmt: &'a mut Formatter<'b>,
padding: &'c str,
state: State,
}
impl<'a, 'b: 'a, 'c> PadAdapter<'a, 'b, 'c> {
pub fn new(fmt: &'a mut Formatter<'b>) -> Self {
Self {
fmt,
padding: " ",
state: Default::default(),
}
}
pub fn with_padding(fmt: &'a mut Formatter<'b>, padding: &'c str) -> Self {
Self {
fmt,
padding,
state: Default::default(),
}
}
}
impl Write for PadAdapter<'_, '_, '_> {
fn write_str(&mut self, mut s: &str) -> fmt::Result {
while !s.is_empty() {
if self.state.on_newline {
self.fmt.write_str(self.padding)?;
}
let split = match s.find('\n') {
Some(pos) => {
self.state.on_newline = true;
pos + 1
}
None => {
self.state.on_newline = false;
s.len()
}
};
self.fmt.write_str(&s[..split])?;
s = &s[split..];
}
Ok(())
}
}
struct State {
on_newline: bool,
}
impl Default for State {
fn default() -> Self {
Self { on_newline: true }
}
}