From 3ff47bd7d45c45371cb904e6a6c41c86be98772e Mon Sep 17 00:00:00 2001 From: Sebastian Speitel Date: Sun, 5 May 2024 16:48:30 +0200 Subject: [PATCH] feat(format): clamp single value data --- src/data/format.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/data/format.rs b/src/data/format.rs index 9e61f4b..3e46281 100644 --- a/src/data/format.rs +++ b/src/data/format.rs @@ -36,6 +36,10 @@ pub trait Verbosity { fn show_id(&self) -> bool { false } + #[inline] + fn ellipsis_threshold(&self) -> Option { + 1024.try_into().ok() + } } impl Verbosity for i8 { @@ -55,6 +59,14 @@ impl Verbosity for i8 { fn show_id(&self) -> bool { *self >= 1 } + #[inline] + fn ellipsis_threshold(&self) -> Option { + match *self { + 1.. => None, + 0 => 1024.try_into().ok(), + ..=-1 => 25.try_into().ok(), + } + } } pub trait Format { @@ -95,7 +107,16 @@ pub trait Format { } if let Some(val) = values.single() { - f.write_fmt(format_args!("{{{val}}}"))?; + if let Some(t) = Self::verbosity().ellipsis_threshold() { + let formatted = val.to_string(); + if formatted.len() > t.get() { + write!(f, "{{{}…}}", &formatted[..t.get()])?; + } else { + write!(f, "{{{formatted}}}")?; + } + } else { + write!(f, "{{{val}}}")?; + } return Ok(()); }