ecb_rates/
header_description.rs

1use colored::Colorize;
2use std::fmt::Display;
3
4use crate::DEFAULT_WIDTH;
5
6pub struct HeaderDescription<'a> {
7    header_description: [&'a str; 2],
8}
9
10impl<'a> Default for HeaderDescription<'a> {
11    fn default() -> Self {
12        Self::new()
13    }
14}
15
16impl<'a> HeaderDescription<'a> {
17    pub fn new() -> Self {
18        Self {
19            header_description: ["EUR", /*"\u{2217}"*/ "ALL"], // Unicode is ∗
20        }
21    }
22
23    pub fn invert(&mut self) {
24        self.header_description.swap(0, 1);
25    }
26
27    pub fn replace_eur(&mut self, currency: &'a str) {
28        self.header_description[0] = currency;
29    }
30}
31
32impl<'a> Display for HeaderDescription<'a> {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        let width = DEFAULT_WIDTH - 2;
35        let formatted = format!(
36            "{} {} {}",
37            self.header_description[0].purple().bold(),
38            "to".italic(),
39            self.header_description[1].purple().bold()
40        );
41        let unformatted_len =
42            self.header_description[0].len() + self.header_description[1].len() + 4;
43        let left_padding = " ".repeat((width - unformatted_len) / 2);
44
45        let vertical = "═".repeat(width);
46        writeln!(f, " ╔{}╗", &vertical)?;
47        writeln!(
48            f,
49            "  {}{}{} ",
50            &left_padding,
51            formatted,
52            " ".repeat(width - left_padding.len() - unformatted_len)
53        )?;
54        writeln!(f, " ╚{}╝\n", &vertical)?;
55        Ok(())
56    }
57}