edit multibar

This commit is contained in:
2025-11-11 21:41:36 +00:00
parent 966c7d5656
commit ac377a9c9c
2 changed files with 18 additions and 10 deletions

View File

@@ -88,9 +88,14 @@ fn main() -> Result<(), color_eyre::Report> {
// pb.finish_and_clear();
let term = Term::stdout();
let empty = style("-").blue().to_string();
let bar =
MultiBar([("#", 5), (empty.as_str(), 2), (" ", 3)]).scale(u64::from(term.size().0) / 3);
println!("Bar: [{}]", bar);
let bar = MultiBar([
("=", 50, false),
(">", 1, true),
(empty.as_str(), 3, false),
(" ", 30, false),
])
.scale(u64::from(term.size().1 - 2));
println!("[{}]", bar);
return Ok(());
let lines = std::fs::read_to_string("build.log")?;

View File

@@ -1,12 +1,12 @@
use std::fmt;
#[derive(Debug)]
pub struct MultiBar<'s, const N: usize>(pub [(&'s str, u64); N]);
pub struct MultiBar<'s, const N: usize>(pub [(&'s str, u64, bool); 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()
self.0.iter().map(|(_, len, _)| *len).sum::<u64>()
}
/// Transforms the bar to be of target size
@@ -15,20 +15,23 @@ impl<const N: usize> MultiBar<'_, N> {
let mut prev_prop = 0;
let mut curr_prop = 0;
let inner = self.0.map(|(c, len)| {
let inner = self.0.map(|(c, len, const_len)| {
if const_len {
return (c, len, const_len);
}
curr_prop += len;
let nb_chars = size * curr_prop / length - size * prev_prop / length;
prev_prop = curr_prop;
(c, nb_chars)
});
prev_prop = curr_prop;
(c, nb_chars, const_len)
});
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 &(c, len, _) in &self.0 {
for _ in 0..len {
f.write_str(c)?;
}