papergrid/estimation/
width_func.rsuse crate::{
util::{string_width_multiline_tab, string_width_tab},
GridConfig,
};
pub trait WidthFunc {
fn width(&self, text: &str) -> usize;
fn width_multiline(&self, text: &str) -> usize;
}
impl<W> WidthFunc for &W
where
W: WidthFunc,
{
fn width(&self, text: &str) -> usize {
W::width(self, text)
}
fn width_multiline(&self, text: &str) -> usize {
W::width_multiline(self, text)
}
}
#[derive(Debug, Default, Clone)]
pub struct CfgWidthFunction {
tab_width: usize,
}
impl CfgWidthFunction {
pub fn from_cfg(cfg: &GridConfig) -> Self {
Self::new(cfg.get_tab_width())
}
pub fn new(tab_size: usize) -> Self {
Self {
tab_width: tab_size,
}
}
}
impl WidthFunc for CfgWidthFunction {
fn width(&self, text: &str) -> usize {
string_width_tab(text, self.tab_width)
}
fn width_multiline(&self, text: &str) -> usize {
string_width_multiline_tab(text, self.tab_width)
}
}