mod justify;
mod min_width;
mod truncate;
mod wrap;
use crate::measurment::Measurment;
pub use self::{
justify::Justify,
min_width::MinWidth,
truncate::{SuffixLimit, Truncate},
wrap::Wrap,
};
use papergrid::{records::Records, width::WidthEstimator, Estimate, GridConfig};
pub(crate) use wrap::wrap_text;
#[derive(Debug)]
pub struct Width;
impl Width {
pub fn wrap<W>(width: W) -> Wrap<W>
where
W: Measurment<Width>,
{
Wrap::new(width)
}
pub fn truncate<W>(width: W) -> Truncate<'static, W>
where
W: Measurment<Width>,
{
Truncate::new(width)
}
pub fn increase<W>(width: W) -> MinWidth<W>
where
W: Measurment<Width>,
{
MinWidth::new(width)
}
pub fn justify<W>(width: W) -> Justify<W>
where
W: Measurment<Width>,
{
Justify::new(width)
}
}
pub(crate) fn get_table_widths<R>(records: R, cfg: &GridConfig) -> Vec<usize>
where
R: Records,
{
let mut evaluator = WidthEstimator::default();
evaluator.estimate(records, cfg);
evaluator.into()
}
pub(crate) fn count_borders(
cfg: &GridConfig,
start: usize,
end: usize,
count_columns: usize,
) -> usize {
(start..end)
.skip(1)
.filter(|&i| cfg.has_vertical(i, count_columns))
.count()
}
pub(crate) fn get_table_total_width<W, R>(records: R, cfg: &GridConfig, ctrl: &W) -> usize
where
W: Estimate<R>,
R: Records,
{
ctrl.total()
+ cfg.count_vertical(records.count_columns())
+ cfg.get_margin().left.size
+ cfg.get_margin().right.size
}
pub(crate) fn get_table_widths_with_total<R>(records: R, cfg: &GridConfig) -> (Vec<usize>, usize)
where
R: Records,
{
let mut evaluator = WidthEstimator::default();
evaluator.estimate(&records, cfg);
let total_width = get_table_total_width(&records, cfg, &evaluator);
let widths = evaluator.into();
(widths, total_width)
}