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
///! `Cell` is a cell of the terminal.
///! It has a display character and an attribute (fg and bg color, effects).
use crate::attr::{Attr, Color, Effect};

const EMPTY_CHAR: char = '\0';

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Cell {
    pub ch: char,
    pub attr: Attr,
}

impl Default for Cell {
    fn default() -> Self {
        Self {
            ch: ' ',
            attr: Attr::default(),
        }
    }
}

impl Cell {
    pub fn empty() -> Self {
        Self::default().ch(EMPTY_CHAR)
    }

    pub fn ch(mut self, ch: char) -> Self {
        self.ch = ch;
        self
    }

    pub fn fg(mut self, fg: Color) -> Self {
        self.attr.fg = fg;
        self
    }

    pub fn bg(mut self, bg: Color) -> Self {
        self.attr.bg = bg;
        self
    }

    pub fn effect(mut self, effect: Effect) -> Self {
        self.attr.effect = effect;
        self
    }

    pub fn attribute(mut self, attr: Attr) -> Self {
        self.attr = attr;
        self
    }

    /// check if a cell is empty
    pub fn is_empty(self) -> bool {
        self.ch == EMPTY_CHAR && self.attr == Attr::default()
    }
}

impl From<char> for Cell {
    fn from(ch: char) -> Self {
        Cell {
            ch,
            attr: Attr::default(),
        }
    }
}