edit multibar
This commit is contained in:
@@ -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)?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user