parallel_disk_usage/visualizer/
proportion_bar.rs1use super::BarAlignment;
2use derive_more::{AsRef, Deref, Display, From, Into};
3use fmt_iter::repeat;
4use std::fmt::{Display, Error, Formatter};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, AsRef, Deref, Display, Into)]
8pub struct ProportionBarBlock(char);
9
10macro_rules! make_const {
11 ($name:ident = $content:literal) => {
12 pub const $name: ProportionBarBlock = ProportionBarBlock($content);
13 };
14}
15
16make_const!(LEVEL0_BLOCK = '█');
17make_const!(LEVEL1_BLOCK = '▓');
18make_const!(LEVEL2_BLOCK = '▒');
19make_const!(LEVEL3_BLOCK = '░');
20make_const!(LEVEL4_BLOCK = ' ');
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, From, Into)]
24pub struct ProportionBar {
25 pub level0: usize,
26 pub level1: usize,
27 pub level2: usize,
28 pub level3: usize,
29 pub level4: usize,
30}
31
32impl ProportionBar {
33 fn display_level0(self) -> impl Display {
34 repeat(LEVEL0_BLOCK, self.level0)
35 }
36
37 fn display_level1(self) -> impl Display {
38 repeat(LEVEL1_BLOCK, self.level1)
39 }
40
41 fn display_level2(self) -> impl Display {
42 repeat(LEVEL2_BLOCK, self.level2)
43 }
44
45 fn display_level3(self) -> impl Display {
46 repeat(LEVEL3_BLOCK, self.level3)
47 }
48
49 fn display_level4(self) -> impl Display {
50 repeat(LEVEL4_BLOCK, self.level4)
51 }
52
53 pub fn display(self, align: BarAlignment) -> ProportionBarDisplay {
55 ProportionBarDisplay { bar: self, align }
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub struct ProportionBarDisplay {
62 pub bar: ProportionBar,
63 pub align: BarAlignment,
64}
65
66impl Display for ProportionBarDisplay {
67 fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error> {
68 let ProportionBarDisplay { bar, align } = self;
69 macro_rules! fmt {
70 ($pattern:literal) => {
71 write!(
72 formatter,
73 $pattern,
74 level0 = bar.display_level0(),
75 level1 = bar.display_level1(),
76 level2 = bar.display_level2(),
77 level3 = bar.display_level3(),
78 level4 = bar.display_level4(),
79 )
80 };
81 }
82 match align {
83 BarAlignment::Left => fmt!("{level0}{level1}{level2}{level3}{level4}"),
84 BarAlignment::Right => fmt!("{level4}{level3}{level2}{level1}{level0}"),
85 }
86 }
87}