1use cai_core::{Entry, Result};
4use std::io::Write;
5
6#[derive(Debug, Clone, Default)]
8#[non_exhaustive]
9pub struct FormatterConfig {
10 pub max_width: usize,
12 pub colorize: bool,
14 pub truncate: usize,
16 pub limit: usize,
18}
19
20pub trait Formatter: Send + Sync {
22 fn format<W: Write>(&self, entries: &[Entry], writer: &mut W) -> Result<()>;
24
25 fn format_one<W: Write>(&self, entry: &Entry, writer: &mut W) -> Result<()>;
27
28 fn config(&self) -> &FormatterConfig;
30
31 fn set_config(&mut self, config: FormatterConfig);
33}
34
35pub(crate) trait Truncate {
37 fn truncate_text(&self, text: &str, limit: usize) -> String;
38}
39
40impl Truncate for FormatterConfig {
41 fn truncate_text(&self, text: &str, limit: usize) -> String {
42 if limit == 0 || text.len() <= limit {
43 text.to_string()
44 } else {
45 format!(
46 "{}...",
47 text.chars()
48 .take(limit.saturating_sub(3))
49 .collect::<String>()
50 )
51 }
52 }
53}