termimad/fit/
filling.rs

1use {
2    crate::{
3        crossterm::{
4            style::Print,
5            QueueableCommand,
6        },
7        *,
8    },
9    std::io::Write,
10};
11
12const FILLING_STRING_CHAR_LEN: usize = 1000;
13
14/// something to fill with
15pub struct Filling {
16    filling_string: String,
17    char_size: usize,
18}
19
20impl Filling {
21    pub fn from_char(filling_char: char) -> Self {
22        let char_size = String::from(filling_char).len();
23        let mut filling_string = String::with_capacity(char_size * FILLING_STRING_CHAR_LEN);
24        for _ in 0..FILLING_STRING_CHAR_LEN {
25            filling_string.push(filling_char);
26        }
27        Self {
28            filling_string,
29            char_size,
30        }
31    }
32    pub fn queue_unstyled<W: Write>(&self, w: &mut W, mut len: usize) -> Result<(), Error> {
33        while len > 0 {
34            let sl = len.min(FILLING_STRING_CHAR_LEN);
35            w.queue(Print(&self.filling_string[0..sl * self.char_size]))?;
36            len -= sl;
37        }
38        Ok(())
39    }
40    pub fn queue_styled<W: Write>(
41        &self,
42        w: &mut W,
43        cs: &CompoundStyle,
44        mut len: usize,
45    ) -> Result<(), Error> {
46        while len > 0 {
47            let sl = len.min(FILLING_STRING_CHAR_LEN);
48            cs.queue_str(w, &self.filling_string[0..sl * self.char_size])?;
49            len -= sl;
50        }
51        Ok(())
52    }
53}