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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use ansi_term::Style;
use std::io::{self, Read, Write};
use termion::event::Event;
use termion::input::Events;
use termion::raw::RawTerminal;

pub type Location = (u16, u16); // width, height

pub type Size = (u16, u16); // x, y

pub trait Renderable {
    fn position(&self) -> &Location;
    fn set_position(&mut self, location: &Location);
    fn size(&self) -> &Size;
    fn ascii_for(&self, location: &Location) -> char;
    fn style_for(&self, location: &Location) -> Style;
    fn on_event(&mut self, event: Event) -> io::Result<()>;

    fn center(&self) -> Location {
        let position = self.position();
        let size = self.size();

        (position.0 + size.0 / 2, position.1 + size.1 / 2)
    }

    fn is_center(&self, location: &Location) -> bool {
        location == &self.center()
    }

    fn set_center(&mut self, location: &Location) {
        let &(mut x, mut y) = location;
        let &(w, h) = self.size();

        x -= w / 2;
        y -= h / 2;

        self.set_position(&(x, y));
    }

    fn top_left_corner(&self) -> Location {
        let &(x, y) = self.position();
        (x, y)
    }

    fn is_top_left_corner(&self, location: &Location) -> bool {
        location == &self.top_left_corner()
    }

    fn bottom_left_corner(&self) -> Location {
        let &(x, y) = self.position();
        let &(_, height) = self.size();

        (x, y + height)
    }

    fn is_bottom_left_corner(&self, location: &Location) -> bool {
        location == &self.bottom_left_corner()
    }

    fn top_right_corner(&self) -> Location {
        let &(x, y) = self.position();
        let &(width, _) = self.size();

        (x + width, y)
    }

    fn is_top_right_corner(&self, location: &Location) -> bool {
        location == &self.top_right_corner()
    }

    fn bottom_right_corner(&self) -> Location {
        let &(x, y) = self.position();
        let &(width, height) = self.size();

        (x + height, y + width)
    }

    fn is_bottom_right_corner(&self, location: &Location) -> bool {
        location == &self.bottom_right_corner()
    }

    fn is_corner(&self, location: &Location) -> bool {
        self.is_top_right_corner(location)
            || self.is_top_left_corner(location)
            || self.is_bottom_right_corner(location)
            || self.is_bottom_left_corner(location)
    }

    fn left_boundary(&self) -> u16 {
        self.position().0
    }

    fn is_left_boundary(&self, location: &Location) -> bool {
        location.0 == self.left_boundary()
    }

    fn top_boundary(&self) -> u16 {
        self.position().1
    }

    fn is_top_boundary(&self, location: &Location) -> bool {
        location.1 == self.top_boundary()
    }

    fn bottom_boundary(&self) -> u16 {
        self.position().1 + self.size().1
    }

    fn is_bottom_boundary(&self, location: &Location) -> bool {
        location.1 == self.bottom_boundary()
    }

    fn right_boundary(&self) -> u16 {
        self.position().0 + self.size().0
    }

    fn is_right_boundary(&self, location: &Location) -> bool {
        location.0 == self.right_boundary()
    }

    fn is_boundary(&self, location: &Location) -> bool {
        self.is_right_boundary(location)
            || self.is_left_boundary(location)
            || self.is_top_boundary(location)
            || self.is_bottom_boundary(location)
    }

    fn covers(&self, location: &Location) -> bool {
        let &(x, y) = location;
        x >= self.left_boundary()
            && x <= self.right_boundary()
            && y >= self.top_boundary()
            && y <= self.bottom_boundary()
    }

    fn paint(&self, stdout: &mut RawTerminal<io::Stdout>, location: Location) -> io::Result<()> {
        let ch = self.ascii_for(&location);
        let style = self.style_for(&location);

        write!(
            stdout,
            "{}{}",
            termion::cursor::Goto(location.0, location.1),
            style.paint(ch.to_string())
        )
    }

    fn paint_all(&self, stdout: &mut RawTerminal<io::Stdout>) -> io::Result<()> {
        for y in self.top_boundary()..(self.bottom_boundary() + 1) {
            for x in self.left_boundary()..(self.right_boundary() + 1) {
                self.paint(stdout, (x, y))?;
            }
        }
        write!(stdout, "{}", termion::cursor::Show)?;
        stdout.flush()
    }

    fn clear(&self, stdout: &mut RawTerminal<io::Stdout>, location: Location) -> io::Result<()> {
        write!(stdout, "{} ", termion::cursor::Goto(location.0, location.1))
    }

    fn clear_all(&self, stdout: &mut RawTerminal<io::Stdout>) -> io::Result<()> {
        for y in self.top_boundary()..(self.bottom_boundary() + 1) {
            for x in self.left_boundary()..(self.right_boundary() + 1) {
                self.clear(stdout, (x, y))?;
            }
        }
        write!(stdout, "{}", termion::cursor::Show)?;
        stdout.flush()
    }

    fn render<R: Read>(
        &mut self,
        stdout: &mut RawTerminal<io::Stdout>,
        events: &mut Events<R>,
    ) -> io::Result<()> {
        loop {
            self.paint_all(stdout)?;
            if let Some(result) = events.next() {
                let event = result?;
                if let Err(err) = self.on_event(event) {
                    match err.kind() {
                        io::ErrorKind::Interrupted => {
                            break self.clear_all(stdout);
                        }
                        _ => {
                            self.clear_all(stdout)?;
                            break Err(err);
                        }
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}