#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Color {
Default,
Ansi(u8),
Rgb(u8, u8, u8),
}
impl Color {
pub const BLACK: Color = Color::Ansi(0);
pub const RED: Color = Color::Ansi(1);
pub const GREEN: Color = Color::Ansi(2);
pub const YELLOW: Color = Color::Ansi(3);
pub const BLUE: Color = Color::Ansi(4);
pub const MAGENTA: Color = Color::Ansi(5);
pub const CYAN: Color = Color::Ansi(6);
pub const WHITE: Color = Color::Ansi(7);
pub const GRAY: Color = Color::Ansi(8); pub const BRIGHT_RED: Color = Color::Ansi(9);
pub const BRIGHT_GREEN: Color = Color::Ansi(10);
pub const BRIGHT_YELLOW: Color = Color::Ansi(11);
pub const BRIGHT_BLUE: Color = Color::Ansi(12);
pub const BRIGHT_MAGENTA: Color = Color::Ansi(13);
pub const BRIGHT_CYAN: Color = Color::Ansi(14);
pub const BRIGHT_WHITE: Color = Color::Ansi(15);
pub const ORANGE: Color = Color::Ansi(208);
pub const BG_DIM: Color = Color::Ansi(236);
pub const BG_TOOL: Color = Color::Ansi(237);
pub const BG_WARN: Color = Color::Ansi(17);
}
impl Color {
pub fn fg_escape(&self) -> String {
match self {
Color::Default => "\x1b[39m".to_string(),
Color::Ansi(n) => {
if *n < 8 {
format!("\x1b[{}m", 30 + *n as u16)
} else if *n < 16 {
format!("\x1b[{}m", 90 + (*n - 8) as u16)
} else {
format!("\x1b[38;5;{}m", n)
}
}
Color::Rgb(r, g, b) => format!("\x1b[38;2;{r};{g};{b}m"),
}
}
pub fn bg_escape(&self) -> String {
match self {
Color::Default => "\x1b[49m".to_string(),
Color::Ansi(n) => {
if *n < 8 {
format!("\x1b[{}m", 40 + *n as u16)
} else if *n < 16 {
format!("\x1b[{}m", 100 + (*n - 8) as u16)
} else {
format!("\x1b[48;5;{}m", n)
}
}
Color::Rgb(r, g, b) => format!("\x1b[48;2;{r};{g};{b}m"),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Style {
pub bold: bool,
pub dim: bool,
pub italic: bool,
pub underline: bool,
pub blink: bool,
}
impl Style {
pub fn new() -> Self {
Self::default()
}
pub fn bold() -> Self {
Self {
bold: true,
..Self::default()
}
}
pub fn dim() -> Self {
Self {
dim: true,
..Self::default()
}
}
pub fn bold_dim() -> Self {
Self {
bold: true,
dim: true,
..Self::default()
}
}
pub fn blink() -> Self {
Self {
blink: true,
..Self::default()
}
}
pub fn escape(&self) -> String {
let mut parts = Vec::new();
if self.bold {
parts.push("\x1b[1m");
}
if self.dim {
parts.push("\x1b[2m");
}
if self.italic {
parts.push("\x1b[3m");
}
if self.underline {
parts.push("\x1b[4m");
}
if self.blink {
parts.push("\x1b[5m");
}
parts.join("")
}
pub fn reset() -> &'static str {
"\x1b[0m"
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Cell {
pub char: char,
pub fg: Color,
pub bg: Color,
pub style: Style,
pub wide: bool,
}
impl Default for Cell {
fn default() -> Self {
Cell {
char: ' ',
fg: Color::Default,
bg: Color::Default,
style: Style::default(),
wide: false,
}
}
}
impl Cell {
pub fn char(ch: char) -> Self {
Cell {
char: ch,
..Self::default()
}
}
pub fn styled(ch: char, fg: Color, bg: Color, style: Style) -> Self {
Cell {
char: ch,
fg,
bg,
style,
wide: false,
}
}
pub fn wide_continuation(fg: Color, bg: Color, style: Style) -> Self {
Cell {
char: ' ',
fg,
bg,
style,
wide: true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_color_fg_escape_default() {
assert_eq!(Color::Default.fg_escape(), "\x1b[39m");
}
#[test]
fn test_color_fg_escape_standard() {
assert_eq!(Color::Ansi(0).fg_escape(), "\x1b[30m"); assert_eq!(Color::Ansi(1).fg_escape(), "\x1b[31m"); assert_eq!(Color::Ansi(7).fg_escape(), "\x1b[37m"); }
#[test]
fn test_color_fg_escape_bright() {
assert_eq!(Color::Ansi(8).fg_escape(), "\x1b[90m"); assert_eq!(Color::Ansi(14).fg_escape(), "\x1b[96m"); }
#[test]
fn test_color_fg_escape_256() {
assert_eq!(Color::Ansi(208).fg_escape(), "\x1b[38;5;208m"); assert_eq!(Color::Ansi(236).fg_escape(), "\x1b[38;5;236m"); }
#[test]
fn test_color_fg_escape_rgb() {
assert_eq!(Color::Rgb(255, 0, 128).fg_escape(), "\x1b[38;2;255;0;128m");
}
#[test]
fn test_color_bg_escape_default() {
assert_eq!(Color::Default.bg_escape(), "\x1b[49m");
}
#[test]
fn test_color_bg_escape_standard() {
assert_eq!(Color::Ansi(1).bg_escape(), "\x1b[41m"); }
#[test]
fn test_color_bg_escape_bright() {
assert_eq!(Color::Ansi(8).bg_escape(), "\x1b[100m"); }
#[test]
fn test_color_bg_escape_256() {
assert_eq!(Color::Ansi(237).bg_escape(), "\x1b[48;5;237m");
}
#[test]
fn test_color_bg_escape_rgb() {
assert_eq!(Color::Rgb(10, 20, 30).bg_escape(), "\x1b[48;2;10;20;30m");
}
#[test]
fn test_style_escape() {
let s = Style {
bold: true,
dim: false,
italic: true,
underline: false,
blink: false,
};
assert_eq!(s.escape(), "\x1b[1m\x1b[3m");
}
#[test]
fn test_style_default_is_empty() {
let s = Style::default();
assert_eq!(s.escape(), "");
}
#[test]
fn test_cell_default() {
let c = Cell::default();
assert_eq!(c.char, ' ');
assert_eq!(c.fg, Color::Default);
assert_eq!(c.bg, Color::Default);
}
#[test]
fn test_cell_char() {
let c = Cell::char('X');
assert_eq!(c.char, 'X');
assert_eq!(c.fg, Color::Default);
}
#[test]
fn test_cell_styled() {
let c = Cell::styled('A', Color::RED, Color::BLUE, Style::bold());
assert_eq!(c.char, 'A');
assert_eq!(c.fg, Color::Ansi(1));
assert_eq!(c.bg, Color::Ansi(4));
assert!(c.style.bold);
}
}