work on multibar

This commit is contained in:
2025-11-11 13:29:01 +04:00
parent 15e8bf5e69
commit 50176e5db4
3 changed files with 131 additions and 47 deletions

39
src/multibar.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::fmt;
#[derive(Debug)]
pub struct MultiBar<'s, const N: usize>(pub [(&'s str, u64); N]);
impl<const N: usize> MultiBar<'_, N> {
/// Total length of the bar
pub(crate) fn length(&self) -> u64 {
self.0.iter().map(|(_, len)| *len).sum()
}
/// Transforms the bar to be of target size
pub(crate) fn scale(&self, size: u64) -> Self {
let length = std::cmp::max(self.length(), 1);
let mut prev_prop = 0;
let mut curr_prop = 0;
let inner = self.0.map(|(c, len)| {
curr_prop += len;
let nb_chars = size * curr_prop / length - size * prev_prop / length;
prev_prop = curr_prop;
(c, nb_chars)
});
Self(inner)
}
}
impl<const N: usize> fmt::Display for MultiBar<'_, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for &(c, len) in &self.0 {
for _ in 0..len {
f.write_str(c)?;
}
}
Ok(())
}
}