|
| 1 | +use super::BarAlignment; |
1 | 2 | use derive_more::{AsRef, Deref, Display, From, Into};
|
2 | 3 | use fmt_iter::repeat;
|
3 | 4 | use std::fmt::{Display, Error, Formatter};
|
@@ -29,37 +30,58 @@ pub struct ProportionBar {
|
29 | 30 | }
|
30 | 31 |
|
31 | 32 | impl ProportionBar {
|
32 |
| - pub fn display_level0(self) -> impl Display { |
| 33 | + fn display_level0(self) -> impl Display { |
33 | 34 | repeat(LEVEL0_BLOCK, self.level0)
|
34 | 35 | }
|
35 | 36 |
|
36 |
| - pub fn display_level1(self) -> impl Display { |
| 37 | + fn display_level1(self) -> impl Display { |
37 | 38 | repeat(LEVEL1_BLOCK, self.level1)
|
38 | 39 | }
|
39 | 40 |
|
40 |
| - pub fn display_level2(self) -> impl Display { |
| 41 | + fn display_level2(self) -> impl Display { |
41 | 42 | repeat(LEVEL2_BLOCK, self.level2)
|
42 | 43 | }
|
43 | 44 |
|
44 |
| - pub fn display_level3(self) -> impl Display { |
| 45 | + fn display_level3(self) -> impl Display { |
45 | 46 | repeat(LEVEL3_BLOCK, self.level3)
|
46 | 47 | }
|
47 | 48 |
|
48 |
| - pub fn display_level4(self) -> impl Display { |
| 49 | + fn display_level4(self) -> impl Display { |
49 | 50 | repeat(LEVEL4_BLOCK, self.level4)
|
50 | 51 | }
|
| 52 | + |
| 53 | + /// Create a [displayable](Display) value. |
| 54 | + pub fn display(self, align: BarAlignment) -> ProportionBarDisplay { |
| 55 | + ProportionBarDisplay { bar: self, align } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Result of [`ProportionBar::display`]. |
| 60 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 61 | +pub struct ProportionBarDisplay { |
| 62 | + pub bar: ProportionBar, |
| 63 | + pub align: BarAlignment, |
51 | 64 | }
|
52 | 65 |
|
53 |
| -impl Display for ProportionBar { |
| 66 | +impl Display for ProportionBarDisplay { |
54 | 67 | fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error> {
|
55 |
| - write!( |
56 |
| - formatter, |
57 |
| - "{level4}{level3}{level2}{level1}{level0}", |
58 |
| - level4 = self.display_level4(), |
59 |
| - level3 = self.display_level3(), |
60 |
| - level2 = self.display_level2(), |
61 |
| - level1 = self.display_level1(), |
62 |
| - level0 = self.display_level0(), |
63 |
| - ) |
| 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 | + } |
64 | 86 | }
|
65 | 87 | }
|
0 commit comments