summaryrefslogtreecommitdiffstats
path: root/src/printer.rs
blob: 7091124de29ab0e583d235625f6df7a8aa1f99e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
pub struct Printer<W: std::io::Write> {
    lines: usize,
    buffer: String,
    writer: W,
}

impl<W> From<W> for Printer<W>
where
    W: std::io::Write,
{
    fn from(value: W) -> Self {
        Self {
            lines: 0,
            buffer: String::new(),
            writer: value,
        }
    }
}

impl<W: std::io::Write> Printer<W> {
    pub fn write(&mut self) -> std::io::Result<&mut Self> {
        writeln!(self.writer, "{}", self.buffer)?;
        self.writer.flush()?;

        self.lines = self.buffer.lines().count() + 1;
        self.buffer.clear();

        Ok(self)
    }

    pub fn write_pause(&mut self, prompt: &str) -> std::io::Result<&mut Self> {
        use std::io::Read;

        self.buffer.push('\n');
        self.buffer.push_str(prompt);

        self.write()?;
        let _ = std::io::stdin().read(&mut [0u8])?;
        Ok(self)
    }

    pub fn write_pause_condition(
        &mut self,
        prompt: &str,
        cond: bool,
    ) -> std::io::Result<&mut Self> {
        if cond {
            self.write_pause(prompt)
        } else {
            self.write()
        }
    }

    pub fn write_sleep(&mut self, time: u64) -> std::io::Result<&mut Self> {
        self.write()?;
        std::thread::sleep(std::time::Duration::from_millis(time));

        Ok(self)
    }

    pub fn clear(&mut self) -> &mut Self {
        self.buffer
            .insert_str(0, &"\x1b[1A\x1b[K".repeat(self.lines));
        self.lines = 0;
        self
    }

    pub fn with(&mut self, fmt: std::fmt::Arguments) -> &mut Self {
        self.buffer.push('\n');
        self.buffer.push_str(format!("{}", fmt).as_str());
        self
    }

    pub fn with_grid<T: std::fmt::Display>(&mut self, grid: &[Vec<T>]) -> &mut Self {
        let s = grid
            .iter()
            .flat_map(|row| {
                row.iter()
                    .map(|s| s.to_string())
                    .chain(std::iter::once("\n".to_string()))
            })
            .collect::<String>();

        self.buffer.push('\n');
        self.buffer.push_str(s.as_str());

        self
    }

    pub fn with_grid_path<T: std::fmt::Display>(
        &mut self,
        grid: &[Vec<T>],
        highlight: &[(usize, usize)],
    ) -> &mut Self {
        let s = grid
            .iter()
            .enumerate()
            .flat_map(|(x, row)| {
                row.iter()
                    .enumerate()
                    .map(move |(y, s)| {
                        if highlight.contains(&(x, y)) {
                            format!("\x1b[1;30;42m{s}\x1b[0m")
                        } else {
                            s.to_string()
                        }
                    })
                    .chain(std::iter::once("\n".to_string()))
            })
            .collect::<String>();

        self.buffer.push('\n');
        self.buffer.push_str(s.as_str());

        self
    }
}