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
pub mod attribute;
pub mod color;

use std::ops::BitAnd;
use std::ops::Not;
use std::fmt;
use std::mem;
use std::char;

use ::libc;

use self::attribute::Attribute;

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Character {
    /// Attribute.
    attribute: libc::c_uchar,
    /// Text color.
    foreground: [libc::c_uchar; 3],
    /// Background color.
    background: [libc::c_uchar; 3],
    /// Glyph.
    glyph: libc::c_uint,
}

impl Character {
    pub fn is_enter(&self) -> bool { self.glyph.eq(&10) }

    pub fn is_space(&self) -> bool { self.glyph.eq(&32) }

    pub fn is_null(&self) -> bool {
        self.glyph.eq(&0)
    }

    pub fn is_bold(&self) -> bool {
        (self.attribute & Attribute::Bold as u8).eq(&0).not()
    }

    pub fn is_dim(&self) -> bool {
        (self.attribute & Attribute::Dim as u8).eq(&0).not()
    }

    pub fn is_italic(&self) -> bool {
        (self.attribute & Attribute::Italic as u8).eq(&0).not()
    }

    pub fn is_underline(&self) -> bool {
        (self.attribute & Attribute::Underline as u8).eq(&0).not()
    }

    pub fn is_blink(&self) -> bool {
        (self.attribute & Attribute::Blink as u8).eq(&0).not()
    }
 
    pub fn is_reverse(&self) -> bool {
        (self.attribute & Attribute::Reverse as u8).eq(&0).not()
    }

    pub fn is_hidden(&self) -> bool {
        (self.attribute & Attribute::Hidden as u8).eq(&0).not()
    }

    pub fn get_attribute(&self) -> u8 {
        self.attribute
    }

    pub fn get_foreground(&self) -> [libc::c_uchar; 3] {
        self.foreground
    }

    pub fn get_background(&self) -> [libc::c_uchar; 3] {
        self.background
    }

    pub fn get_glyph(&self) -> char {
        unsafe {
            mem::transmute::<libc::c_uint, char>(self.glyph)
        }
    }

    pub fn set_foreground(&mut self, fore: [libc::c_uchar; 3]) {
        self.foreground = fore;
    }

    pub fn set_background(&mut self, back: [libc::c_uchar; 3]) {
        self.background = back;
    }

    pub fn add_attribute(&mut self, attr: Attribute) {
        self.attribute |= attr as u8;
    }

    pub fn sub_attribute(&mut self, attr: Attribute) {
        self.attribute &= (attr as u8).not();
    }

    pub fn set_attribute(&mut self, attr: Attribute) {
        self.attribute = attr as u8;
    }

    pub fn set_attribute_from_u8(&mut self, attr: u8) {
        self.attribute = attr;
    }

    pub fn set_glyph(&mut self, glyph: char) {
        self.glyph = glyph as libc::c_uint;
    }

    /// The method `clear` resets the term character.
    pub fn clear(&mut self) {
        *self = Self::default();
    }
}

impl fmt::Display for Character {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(format!("\x1B[m").fmt(f));
        if self.attribute.eq(&0)
        .bitand(self.foreground.eq(&color::DEFAULT_FOREGROUND)
        .bitand(self.background.eq(&color::DEFAULT_BACKGROUND)))
        .not() {
            if self.attribute.gt(&0)
            { try!("\x1B[".fmt(f));
              if self.is_bold() {
                try!(Attribute::Bold.fmt(f))
              }
              if self.is_dim() {
                try!(Attribute::Dim.fmt(f))
              }
              if self.is_italic() {
                try!(Attribute::Italic.fmt(f))
              }
              if self.is_underline() {
                try!(Attribute::Underline.fmt(f))
              }
              if self.is_blink() {
                try!(Attribute::Blink.fmt(f))
              }
              if self.is_reverse() {
                try!(Attribute::Reverse.fmt(f))
              }
              if self.is_hidden() {
                try!(Attribute::Hidden.fmt(f))
              }
              try!(format!("m").fmt(f)) }
            if self.foreground.eq(&[0, 0, 0]).not()
            { try!(format!("\x1B[38;2;{};{};{}m",
                         self.foreground[0],
                         self.foreground[1],
                         self.foreground[2]).fmt(f)) }
            if self.background.eq(&[255, 255, 255]).not()
            { try!(format!("\x1B[48;2;{};{};{}m",
                         self.background[0],
                         self.background[1],
                         self.background[2]).fmt(f)) }
        }
        unsafe {
            try!(format!("{}", char::from_u32_unchecked(self.glyph)).fmt(f))
        }
        Ok(())
    }
}

impl From<char> for Character {
    fn from(glyph: char) -> Character {
        Character {
           attribute: 0,
           foreground: color::BLACK,
           background: color::WHITE,
           glyph: glyph as libc::c_uint,
        }
    }
}

impl Default for Character {
    fn default() -> Self {
        Character::from(' ')
    }
}