use super::width_string::WidthString;
use std::fmt::{Debug, Display, Formatter};
#[derive(Clone, Default)]
pub struct Row(pub(crate) Vec<WidthString>);
impl Row {
pub fn new() -> Self {
Row(Vec::new())
}
#[must_use]
pub fn with_cell<S: Display>(mut self, value: S) -> Self {
self.add_cell(value);
self
}
pub fn add_cell<S: Display>(&mut self, value: S) -> &mut Self {
self.0.push(WidthString::new(value));
self
}
#[must_use]
#[cfg(feature = "ansi-cell")]
pub fn with_ansi_cell<S: Display>(mut self, value: S) -> Self {
self.add_ansi_cell(value);
self
}
#[cfg(feature = "ansi-cell")]
pub fn add_ansi_cell<S: Display>(&mut self, value: S) -> &mut Self {
self.0.push(WidthString::new_ansi(value));
self
}
#[must_use]
pub fn with_custom_width_cell<S: Display>(mut self, value: S, width: usize) -> Self {
self.add_custom_width_cell(value, width);
self
}
pub fn add_custom_width_cell<S: Display>(&mut self, value: S, width: usize) -> &mut Self {
self.0.push(WidthString::custom_width(value, width));
self
}
pub fn from_cells<S, I>(values: I) -> Self
where
S: Into<String>,
I: IntoIterator<Item = S>,
{
Row(values
.into_iter()
.map(Into::into)
.map(WidthString::new)
.collect())
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl Debug for Row {
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
write!(f, "Row::from_cells(vec!{:?})", self.0)
}
}
#[derive(Clone, Debug)]
pub enum InternalRow {
Cells(Vec<WidthString>),
Heading(String),
}