use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Alignment {
Left,
Center,
Right,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell {
pub content: String,
pub column_span: usize,
pub alignment: Alignment,
pub has_padding: bool,
}
impl Cell {
pub fn new(content: impl ToString) -> Self {
Cell {
content: content.to_string(),
column_span: 1,
alignment: Alignment::Left,
has_padding: true,
}
}
pub fn with_column_span(mut self, column_span: usize) -> Self {
self.column_span = column_span;
self
}
pub fn with_alignment(mut self, alignment: Alignment) -> Self {
self.alignment = alignment;
self
}
pub fn without_padding(mut self) -> Self {
self.has_padding = false;
self
}
pub(crate) fn width(&self) -> usize {
crate::visible_width(&self.content) + if self.has_padding { 2 } else { 0 }
}
pub(crate) fn wrapped_content(&self, max_width: usize) -> Vec<String> {
let hidden: HashSet<usize> = crate::ANSI_REGEX
.find_iter(&self.content)
.flat_map(|m| m.start()..m.end())
.collect();
let mut res: Vec<String> = Vec::new();
let mut buf = String::new();
let mut byte_index = 0;
for c in self.content.chars() {
if !hidden.contains(&byte_index)
&& (crate::visible_width(&buf) >= max_width || c == '\n')
{
res.push(buf);
buf = String::new();
if c == '\n' {
byte_index += 1;
continue;
}
}
byte_index += c.len_utf8();
buf.push(c);
}
res.push(buf);
res
}
}