Files
LLeMbas-TTY/src/cell.rs
T
2018-07-20 06:59:32 -07:00

242 lines
7.3 KiB
Rust

//! Model a cell in the terminal display
use color::ColorAttribute;
pub use escape::osc::Hyperlink;
use std::mem;
use std::rc::Rc;
/// Holds the attributes for a cell.
/// Most style attributes are stored internally as part of a bitfield
/// to reduce per-cell overhead.
/// The setter methods return a mutable self reference so that they can
/// be chained together.
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct CellAttributes {
attributes: u16,
/// The foreground color
pub foreground: ColorAttribute,
/// The background color
pub background: ColorAttribute,
/// The hyperlink content, if any
pub hyperlink: Option<Rc<Hyperlink>>,
}
/// Define getter and setter for the attributes bitfield.
/// The first form is for a simple boolean value stored in
/// a single bit. The $bitnum parameter specifies which bit.
/// The second form is for an integer value that occupies a range
/// of bits. The $bitmask and $bitshift parameters define how
/// to transform from the stored bit value to the consumable
/// value.
macro_rules! bitfield {
($getter:ident, $setter:ident, $bitnum:expr) => {
#[inline]
pub fn $getter(&self) -> bool {
(self.attributes & (1 << $bitnum)) == (1 << $bitnum)
}
#[inline]
pub fn $setter(&mut self, value: bool) -> &mut Self {
let attr_value = if value { 1 << $bitnum } else { 0 };
self.attributes = (self.attributes & !(1 << $bitnum)) | attr_value;
self
}
};
($getter:ident, $setter:ident, $bitmask:expr, $bitshift:expr) => {
#[inline]
pub fn $getter(&self) -> u16 {
(self.attributes >> $bitshift) & $bitmask
}
#[inline]
pub fn $setter(&mut self, value: u16) -> &mut Self {
let clear = !($bitmask << $bitshift);
let attr_value = (value & $bitmask) << $bitshift;
self.attributes = (self.attributes & clear) | attr_value;
self
}
};
($getter:ident, $setter:ident, $enum:ident, $bitmask:expr, $bitshift:expr) => {
#[inline]
pub fn $getter(&self) -> $enum {
unsafe { mem::transmute(((self.attributes >> $bitshift) & $bitmask) as u16)}
}
#[inline]
pub fn $setter(&mut self, value: $enum) -> &mut Self {
let value = value as u16;
let clear = !($bitmask << $bitshift);
let attr_value = (value & $bitmask) << $bitshift;
self.attributes = (self.attributes & clear) | attr_value;
self
}
};
}
/// The `Intensity` of a cell describes its boldness. Most terminals
/// implement `Intensity::Bold` by either using a bold font or by simply
/// using an alternative color. Some terminals implement `Intensity::Half`
/// as a dimmer color variant.
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
#[repr(u16)]
pub enum Intensity {
Normal = 0,
Bold = 1,
Half = 2,
}
/// Specify just how underlined you want your `Cell` to be
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
#[repr(u16)]
pub enum Underline {
/// The cell is not underlined
None = 0,
/// The cell is underlined with a single line
Single = 1,
/// The cell is underlined with two lines
Double = 2,
}
/// Allow converting to boolean; true means some kind of
/// underline, false means none. This is used in some
/// generic code to determine whether to enable underline.
impl Into<bool> for Underline {
fn into(self) -> bool {
self != Underline::None
}
}
/// Specify whether you want to slowly or rapidly annoy your users
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
#[repr(u16)]
pub enum Blink {
None = 0,
Slow = 1,
Rapid = 2,
}
/// Allow converting to boolean; true means some kind of
/// blink, false means none. This is used in some
/// generic code to determine whether to enable blink.
impl Into<bool> for Blink {
fn into(self) -> bool {
self != Blink::None
}
}
impl CellAttributes {
bitfield!(intensity, set_intensity, Intensity, 0b11, 0);
bitfield!(underline, set_underline, Underline, 0b11, 2);
bitfield!(blink, set_blink, Blink, 0b11, 4);
bitfield!(italic, set_italic, 6);
bitfield!(reverse, set_reverse, 7);
bitfield!(strikethrough, set_strikethrough, 8);
bitfield!(invisible, set_invisible, 9);
/// Returns true if the attribute bits in both objects are equal.
/// This can be used to cheaply test whether the styles of the two
/// cells are the same, and is used by some `Renderer` implementations.
pub fn attribute_bits_equal(&self, other: &Self) -> bool {
self.attributes == other.attributes
}
/// Set the foreground color for the cell to that specified
pub fn set_foreground<C: Into<ColorAttribute>>(&mut self, foreground: C) -> &mut Self {
self.foreground = foreground.into();
self
}
pub fn set_background<C: Into<ColorAttribute>>(&mut self, background: C) -> &mut Self {
self.background = background.into();
self
}
/// Clone the attributes, but exclude fancy extras such
/// as hyperlinks or future sprite things
pub fn clone_sgr_only(&self) -> Self {
Self {
attributes: self.attributes,
foreground: self.foreground,
background: self.background,
hyperlink: None,
}
}
}
/// Models the contents of a cell on the terminal display
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Cell {
text: char,
attrs: CellAttributes,
}
impl Default for Cell {
fn default() -> Self {
Cell::new(' ', CellAttributes::default())
}
}
impl Cell {
/// De-fang the input character such that it has no special meaning
/// to a terminal. All control and movement characters are rewritten
/// as a space.
pub(crate) fn nerf_control_char(text: char) -> char {
if text < 0x20 as char || text == 0x7f as char {
' '
} else {
text
}
}
/// Create a new cell holding the specified character and with the
/// specified cell attributes.
/// All control and movement characters are rewritten as a space.
pub fn new(text: char, attrs: CellAttributes) -> Self {
Self {
text: Self::nerf_control_char(text),
attrs,
}
}
/// Returns the textual content of the cell
pub fn char(&self) -> char {
self.text
}
/// Returns the attributes of the cell
pub fn attrs(&self) -> &CellAttributes {
&self.attrs
}
}
/// Models a change in the attributes of a cell in a stream of changes.
/// Each variant specifies one of the possible attributes; the corresponding
/// value holds the new value to be used for that attribute.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum AttributeChange {
Intensity(Intensity),
Underline(Underline),
Italic(bool),
Blink(Blink),
Reverse(bool),
StrikeThrough(bool),
Invisible(bool),
Foreground(ColorAttribute),
Background(ColorAttribute),
Hyperlink(Option<Rc<Hyperlink>>),
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn nerf_special() {
for c in " \n\r\t".chars() {
let cell = Cell::new(c, CellAttributes::default());
assert_eq!(cell.char(), ' ');
}
}
}