use cai_core::{Entry, Result};
use std::io::Write;
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct FormatterConfig {
pub max_width: usize,
pub colorize: bool,
pub truncate: usize,
pub limit: usize,
}
pub trait Formatter: Send + Sync {
fn format<W: Write>(&self, entries: &[Entry], writer: &mut W) -> Result<()>;
fn format_one<W: Write>(&self, entry: &Entry, writer: &mut W) -> Result<()>;
fn config(&self) -> &FormatterConfig;
fn set_config(&mut self, config: FormatterConfig);
}
pub(crate) trait Truncate {
fn truncate_text(&self, text: &str, limit: usize) -> String;
}
impl Truncate for FormatterConfig {
fn truncate_text(&self, text: &str, limit: usize) -> String {
if limit == 0 || text.len() <= limit {
text.to_string()
} else {
format!(
"{}...",
text.chars()
.take(limit.saturating_sub(3))
.collect::<String>()
)
}
}
}