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
use std::{
    collections::{BTreeMap, HashMap},
    io::Write,
    iter::Iterator,
};

use crossterm::{
    cursor, queue,
    style::{self, ContentStyle},
};

use super::{Canvas, Coord, Drawable, Pixel, Pixels, Rect, Style};

pub struct Buffer<S: Style> {
    pub width: u16,
    pub height: u16,
    pixels: Vec<Pixel<S>>,
    pub changes: BTreeMap<usize, Pixel<S>>,
    pub styles: HashMap<S, ContentStyle>,
}

impl<S: Style> Default for Buffer<S> {
    fn default() -> Self {
        Self {
            width: 0,
            height: 0,
            pixels: Vec::new(),
            changes: BTreeMap::new(),
            styles: HashMap::new(),
        }
    }
}

impl<S: Style> Buffer<S> {
    pub fn canvas<'a>(&'a mut self, frame: Rect) -> Canvas<'a, S> {
        Canvas::new(self, frame)
    }

    pub fn set_styles(&mut self, styles: HashMap<S, ContentStyle>) {
        self.styles = styles;
    }

    pub fn resize(&mut self, width: u16, height: u16) {
        self.width = width;
        self.height = height;
        self.pixels = (0..width * height).map(|_| Pixel::default()).collect();
    }

    pub fn draw_changes<W: Write>(&mut self, stdout: &mut W) -> Result<(), crossterm::ErrorKind> {
        let mut last_style = None;
        let mut last_coord = None;
        let mut text = "".to_string();

        for (k, v) in self.changes.iter() {
            if let Some(pixel) = self.pixels.get(*k) {
                if *pixel != *v {
                    self.pixels[*k] = *v;
                } else {
                    continue;
                }
            } else {
                continue;
            }

            if last_style != Some(v.style) || *k == 0 || last_coord != Some(*k - 1) {
                if !text.is_empty() {
                    let style = match self.styles.get(&last_style.unwrap()) {
                        Some(s) => *s,
                        None => ContentStyle::default(),
                    };
                    queue!(stdout, style::PrintStyledContent(style.apply(text)))?;
                }

                let coord = Coord::from_index(*k, self.width);
                queue!(stdout, cursor::MoveTo(coord.x, coord.y))?;

                text = v.text.unwrap_or(' ').to_string();
                last_style = Some(v.style);
            } else {
                text = format!("{}{}", text, v.text.unwrap_or(' '));
            }

            last_coord = Some(*k);
        }

        if !text.is_empty() {
            let style = match self.styles.get(&last_style.unwrap()) {
                Some(s) => *s,
                None => ContentStyle::default(),
            };

            queue!(stdout, style::PrintStyledContent(style.apply(text)))?;
        }

        self.changes.clear();

        stdout.flush()?;

        Ok(())
    }

    pub fn get_pixel(&self, x: u16, y: u16) -> Option<&Pixel<S>> {
        let index = Coord::from((x, y)).as_index(self.width);

        self.pixels.get(index)
    }
}

impl<S: Style> Drawable<S> for Buffer<S> {
    fn fill(&mut self, text: char, style: S) {
        self.rect(Rect::new(0, 0, self.width, self.height), text, style);
    }
    fn rect(&mut self, rect: Rect, text: char, style: S) {
        let text: String = (0..rect.width).map(|_| text).collect();
        for y in 0..rect.height {
            self.string(rect.x, rect.y + y, text.clone(), style);
        }
    }

    fn string(&mut self, x: u16, y: u16, text: String, style: S) {
        let index = Coord::from((x, y)).as_index(self.width);

        for (i, c) in text.chars().enumerate() {
            if i + index >= self.pixels.len() {
                break;
            }

            self.pixel_at_index(
                index + i,
                Pixel {
                    text: Some(c),
                    style,
                },
            );
        }
    }

    fn pixels(&mut self, x: u16, y: u16, pixels: &Pixels<S>) {
        let index = Coord::from((x, y)).as_index(self.width);

        for (i, p) in pixels.iter().enumerate() {
            if i + index >= self.pixels.len() {
                break;
            }

            self.pixel_at_index(index + i, *p);
        }
    }

    fn pixel(&mut self, x: u16, y: u16, pixel: Pixel<S>) {
        let index = Coord::from((x, y)).as_index(self.width);
        self.pixel_at_index(index, pixel);
    }

    fn pixel_at_index(&mut self, coord: usize, pixel: Pixel<S>) {
        if self.pixels[coord] != pixel {
            self.changes.insert(coord, pixel);
        } else {
            self.changes.remove(&coord);
        }
    }
}