d_stuff/
message.rs

1use super::*;
2
3pub struct Message {
4    title: Option<Text>,
5    message: Text,
6}
7
8impl Message {
9    pub fn new(title: Option<Text>, message: Text) -> Self {
10        Self { title, message }
11    }
12
13    pub fn padding() -> usize {
14        1
15    }
16
17    fn print_sep() {
18        print!(
19            "{}{}│{}",
20            Pretty::border_style(),
21            Pretty::border_color(),
22            termion::style::Reset,
23        );
24    }
25
26    fn print_padding() {
27        for _ in 0..Self::padding() {
28            print!(" ");
29        }
30    }
31    fn print_message(&self, text: &str) {
32        print!(
33            "{}{}{}{}",
34            self.message.style(),
35            self.message.color(),
36            text,
37            termion::style::Reset
38        );
39    }
40    fn fill(n: usize) {
41        for _ in 0..n {
42            print!(" ");
43        }
44    }
45
46    pub fn print(&self, width: usize) {
47        let mut text = "".to_string();
48
49        if let Some(title) = &self.title {
50            text.push_str(&format!("{}: ", title.text()));
51        }
52        text.push_str(self.message.text());
53
54        let w = width - 2 - 2 * Self::padding();
55        let mut lines = textwrap::wrap(&text, w);
56
57        if let Some(title) = &self.title {
58            let first = lines.first().unwrap()[title.len() + 2..].to_string();
59
60            Self::print_sep();
61            Self::print_padding();
62            title.print();
63            print!(": ");
64            self.print_message(first.as_str());
65            Self::print_padding();
66            Self::fill(w - title.len() - 2 - first.len());
67            Self::print_sep();
68
69            lines.remove(0);
70        }
71
72        for line in lines {
73            let line = line.to_string();
74            let n = if w > line.len() { w - line.len() } else { 0 };
75            if !line.is_empty() {
76                Self::print_sep();
77                Self::print_padding();
78                self.print_message(line.as_str());
79                Self::print_padding();
80                Self::fill(n);
81                Self::print_sep();
82            }
83        }
84    }
85}