use crate::cli::fmt::{self, ansi};
pub(crate) struct FormatBuffer {
width: usize,
curr: usize,
buffer: String,
gray_color: &'static str,
text_color: &'static str,
}
impl FormatBuffer {
pub fn new() -> Self {
Self {
width: fmt::term_width_or_max(),
curr: 0,
buffer: String::new(),
gray_color: "",
text_color: "",
}
}
pub fn as_str(&self) -> &str {
self.buffer.as_str()
}
#[allow(unused)] pub fn take(&mut self) -> String {
std::mem::take(&mut self.buffer)
}
pub fn reset(&mut self, gray_color: &'static str, text_color: &'static str) {
self.curr = 0;
self.buffer.clear();
self.width = fmt::term_width_or_max();
self.gray_color = gray_color;
self.text_color = text_color;
}
pub fn push_lf(&mut self) {
self.buffer.push('\n');
}
pub fn push_control(&mut self, x: &str) {
self.buffer.push_str(x)
}
pub fn push_str(&mut self, x: &str) {
for (c, w) in ansi::with_width(x.chars()) {
self.push(c, w);
}
}
pub fn push(&mut self, c: char, w: usize) {
if c == '\n' {
self.new_line();
return;
}
if self.width < 5 {
self.buffer.push(c);
return;
}
if w < self.width && self.curr > self.width - w {
self.new_line();
}
self.buffer.push(c);
self.curr += w;
}
pub fn new_line(&mut self) {
self.buffer.push('\n');
self.buffer.push_str(self.gray_color);
self.buffer.push_str(" | ");
self.buffer.push_str(self.text_color);
self.curr = 3;
}
}