#![no_std]
#![warn(missing_docs)]
#![deny(unsafe_code)]
#[cfg(feature = "alloc")]
extern crate alloc;
pub mod decode;
pub mod encode;
#[cfg(test)]
pub mod tests;
use bitflags::bitflags;
pub const START: char = '\x01';
pub const END: char = '\x02';
pub const STYLE_LEN: usize = 5;
pub const DESCRIPTOR_LEN: usize = 3;
#[cfg(feature = "alloc")]
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct Span {
pub text: alloc::string::String,
pub style: Style,
}
#[cfg(feature = "alloc")]
impl Span {
pub const fn new(text: alloc::string::String, style: Style) -> Self {
Self { text, style }
}
pub fn with_text(mut self, text: alloc::string::String) -> Self {
self.text = text;
self
}
pub const fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
}
#[derive(Copy, Clone, Default, Eq, PartialEq, Debug)]
pub struct Style {
pub foreground: Color,
pub background: Color,
pub attributes: Attributes,
}
impl Style {
pub const fn new(foreground: Color, background: Color, attributes: Attributes) -> Self {
Self {
foreground,
background,
attributes,
}
}
pub const fn with_foreground(mut self, foreground: Color) -> Self {
self.foreground = foreground;
self
}
pub const fn with_background(mut self, background: Color) -> Self {
self.background = background;
self
}
pub const fn with_attribute(mut self, attributes: Attributes) -> Self {
self.attributes = attributes;
self
}
}
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
pub enum Color {
#[default]
None = 0,
Gray = 1,
BrightGray = 2,
BrighterGray = 3,
DarkGray = 4,
DarkerGray = 5,
Red = 6,
BrightRed = 7,
BrighterRed = 8,
DarkRed = 9,
DarkerRed = 10,
Green = 11,
BrightGreen = 12,
BrighterGreen = 13,
DarkGreen = 14,
DarkerGreen = 15,
Yellow = 16,
BrightYellow = 17,
BrighterYellow = 18,
DarkYellow = 19,
DarkerYellow = 20,
Blue = 21,
BrightBlue = 22,
BrighterBlue = 23,
DarkBlue = 24,
DarkerBlue = 25,
Purple = 26,
BrightPurple = 27,
BrighterPurple = 28,
DarkPurple = 29,
DarkerPurple = 30,
Cyan = 31,
BrightCyan = 32,
BrighterCyan = 33,
DarkCyan = 34,
DarkerCyan = 35,
}
impl Color {
pub const fn parse(byte: u8) -> Option<Self> {
match byte {
0 => Some(Color::None),
1 => Some(Color::Gray),
2 => Some(Color::BrightGray),
3 => Some(Color::BrighterGray),
4 => Some(Color::DarkGray),
5 => Some(Color::DarkerGray),
6 => Some(Color::Red),
7 => Some(Color::BrightRed),
8 => Some(Color::BrighterRed),
9 => Some(Color::DarkRed),
10 => Some(Color::DarkerRed),
11 => Some(Color::Green),
12 => Some(Color::BrightGreen),
13 => Some(Color::BrighterGreen),
14 => Some(Color::DarkGreen),
15 => Some(Color::DarkerGreen),
16 => Some(Color::Yellow),
17 => Some(Color::BrightYellow),
18 => Some(Color::BrighterYellow),
19 => Some(Color::DarkYellow),
20 => Some(Color::DarkerYellow),
21 => Some(Color::Blue),
22 => Some(Color::BrightBlue),
23 => Some(Color::BrighterBlue),
24 => Some(Color::DarkBlue),
25 => Some(Color::DarkerBlue),
26 => Some(Color::Purple),
27 => Some(Color::BrightPurple),
28 => Some(Color::BrighterPurple),
29 => Some(Color::DarkPurple),
30 => Some(Color::DarkerPurple),
31 => Some(Color::Cyan),
32 => Some(Color::BrightCyan),
33 => Some(Color::BrighterCyan),
34 => Some(Color::DarkCyan),
35 => Some(Color::DarkerCyan),
_ => None,
}
}
pub const fn to_byte(&self) -> u8 {
*self as u8
}
pub const fn to_rgb(&self) -> Option<(u8, u8, u8)> {
match self {
Color::None => None,
Color::Gray => Some((128, 128, 128)),
Color::BrightGray => Some((192, 192, 192)),
Color::BrighterGray => Some((255, 255, 255)),
Color::DarkGray => Some((64, 64, 64)),
Color::DarkerGray => Some((0, 0, 0)),
Color::Red => Some((255, 0, 0)),
Color::BrightRed => Some((255, 64, 64)),
Color::BrighterRed => Some((255, 128, 128)),
Color::DarkRed => Some((128, 0, 0)),
Color::DarkerRed => Some((64, 0, 0)),
Color::Green => Some((0, 255, 0)),
Color::BrightGreen => Some((64, 255, 64)),
Color::BrighterGreen => Some((128, 255, 128)),
Color::DarkGreen => Some((0, 128, 0)),
Color::DarkerGreen => Some((0, 64, 0)),
Color::Yellow => Some((255, 255, 0)),
Color::BrightYellow => Some((255, 255, 64)),
Color::BrighterYellow => Some((255, 255, 128)),
Color::DarkYellow => Some((128, 128, 0)),
Color::DarkerYellow => Some((64, 64, 0)),
Color::Blue => Some((0, 0, 255)),
Color::BrightBlue => Some((64, 64, 255)),
Color::BrighterBlue => Some((128, 128, 255)),
Color::DarkBlue => Some((0, 0, 128)),
Color::DarkerBlue => Some((0, 0, 64)),
Color::Purple => Some((128, 0, 128)),
Color::BrightPurple => Some((192, 64, 192)),
Color::BrighterPurple => Some((224, 128, 224)),
Color::DarkPurple => Some((64, 0, 64)),
Color::DarkerPurple => Some((32, 0, 32)),
Color::Cyan => Some((0, 255, 255)),
Color::BrightCyan => Some((64, 255, 255)),
Color::BrighterCyan => Some((128, 255, 255)),
Color::DarkCyan => Some((0, 128, 128)),
Color::DarkerCyan => Some((0, 64, 64)),
}
}
}
bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct Attributes: u8 {
const BOLD = 1;
const ITALIC = 1 << 1;
const UNDERLINE = 1 << 2;
const STRIKETHROUGH = 1 << 3;
const HIDDEN = 1 << 4;
}
}
impl Attributes {
pub const fn parse(byte: u8) -> Option<Self> {
Self::from_bits(byte)
}
pub const fn to_byte(&self) -> u8 {
self.bits()
}
}