#![deny(missing_docs)]
#[derive(Debug, Clone)]
pub struct Flusher {
buf: String,
max_pending_chars: usize,
}
impl Flusher {
pub fn new(max_pending_chars: usize) -> Self {
Self {
buf: String::new(),
max_pending_chars,
}
}
pub fn push(&mut self, chunk: &str) -> Option<String> {
self.buf.push_str(chunk);
if let Some(last_nl) = self.buf.rfind('\n') {
let split = last_nl + 1;
let out: String = self.buf.drain(..split).collect();
return Some(out);
}
if self.buf.chars().count() > self.max_pending_chars {
return Some(self.flush());
}
None
}
pub fn flush(&mut self) -> String {
std::mem::take(&mut self.buf)
}
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
}