use crate::color::{NamedColor, ResolvedColor};
use crate::compositor::layout::cell::CellStyle;
use std::io::{self, Write};
pub const TERMINAL_DEFAULT_SGR: &str = "\x1b[38;2;255;255;255;48;2;0;0;0m";
const BLACK_FG_SGR: &str = "38;2;0;0;0";
const BLACK_BG_SGR: &str = "48;2;0;0;0";
pub fn style_to_sgr(style: &CellStyle) -> String {
let mut params: Vec<String> = Vec::new();
if style.bold {
params.push("1".into());
}
if style.dim {
params.push("2".into());
}
if style.italic {
params.push("3".into());
}
if style.underline {
params.push("4".into());
}
if style.blink {
params.push("5".into());
}
if style.strikethrough {
params.push("9".into());
}
match &style.fg {
Some(fg) => match fg {
ResolvedColor::Named(NamedColor::Black) => {
params.push(BLACK_FG_SGR.into());
}
ResolvedColor::Named(n) => {
params.push(n.fg_sgr().to_string());
}
ResolvedColor::Palette(idx) => {
params.push(format!("38;5;{idx}"));
}
ResolvedColor::Rgb(r, g, b) => {
params.push(format!("38;2;{r};{g};{b}"));
}
},
None => params.push("38;2;255;255;255".into()),
}
match &style.bg {
Some(bg) => match bg {
ResolvedColor::Named(NamedColor::Black) => {
params.push(BLACK_BG_SGR.into());
}
ResolvedColor::Named(n) => {
params.push(n.bg_sgr().to_string());
}
ResolvedColor::Palette(idx) => {
params.push(format!("48;5;{idx}"));
}
ResolvedColor::Rgb(r, g, b) => {
params.push(format!("48;2;{r};{g};{b}"));
}
},
None => params.push("48;2;0;0;0".into()),
}
format!("\x1b[{}m", params.join(";"))
}
pub fn write_style_sgr(out: &mut impl Write, style: &CellStyle) -> io::Result<()> {
out.write_all(b"\x1b[")?;
let mut first = true;
macro_rules! parameter {
($($arg:tt)*) => {{
if !first {
out.write_all(b";")?;
}
first = false;
write!(out, $($arg)*)?;
}};
}
if style.bold {
parameter!("1");
}
if style.dim {
parameter!("2");
}
if style.italic {
parameter!("3");
}
if style.underline {
parameter!("4");
}
if style.blink {
parameter!("5");
}
if style.strikethrough {
parameter!("9");
}
match &style.fg {
Some(ResolvedColor::Named(NamedColor::Black)) => parameter!("{BLACK_FG_SGR}"),
Some(ResolvedColor::Named(color)) => parameter!("{}", color.fg_sgr()),
Some(ResolvedColor::Palette(index)) => parameter!("38;5;{index}"),
Some(ResolvedColor::Rgb(red, green, blue)) => parameter!("38;2;{red};{green};{blue}"),
None => parameter!("38;2;255;255;255"),
}
match &style.bg {
Some(ResolvedColor::Named(NamedColor::Black)) => parameter!("{BLACK_BG_SGR}"),
Some(ResolvedColor::Named(color)) => parameter!("{}", color.bg_sgr()),
Some(ResolvedColor::Palette(index)) => parameter!("48;5;{index}"),
Some(ResolvedColor::Rgb(red, green, blue)) => parameter!("48;2;{red};{green};{blue}"),
None => parameter!("48;2;0;0;0"),
}
debug_assert!(!first);
out.write_all(b"m")
}
pub fn write_cursor(out: &mut impl Write, row: u16, col: u16) -> io::Result<()> {
write!(out, "\x1b[{};{}H", row + 1, col + 1)
}
pub const RESET: &str = "\x1b[0m";
pub fn move_cursor(row: u16, col: u16) -> String {
format!("\x1b[{};{}H", row + 1, col + 1)
}
pub const HIDE_CURSOR: &str = "\x1b[?25l";
pub const SHOW_CURSOR: &str = "\x1b[?25h";
pub const ENTER_ALT_SCREEN: &str = "\x1b[?1049h";
pub const LEAVE_ALT_SCREEN: &str = "\x1b[?1049l";
pub const CLEAR_SCREEN: &str = "\x1b[2J";
pub const CURSOR_HOME: &str = "\x1b[H";
#[cfg(test)]
mod tests {
use super::*;
use crate::color::NamedColor;
#[test]
fn default_style_uses_physical_dark_canvas() {
let style = CellStyle::default();
assert_eq!(style_to_sgr(&style), TERMINAL_DEFAULT_SGR);
}
#[test]
fn bold_only() {
let style = CellStyle {
bold: true,
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[1;38;2;255;255;255;48;2;0;0;0m");
}
#[test]
fn dim_only() {
let style = CellStyle {
dim: true,
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[2;38;2;255;255;255;48;2;0;0;0m");
}
#[test]
fn italic_only() {
let style = CellStyle {
italic: true,
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[3;38;2;255;255;255;48;2;0;0;0m");
}
#[test]
fn underline_only() {
let style = CellStyle {
underline: true,
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[4;38;2;255;255;255;48;2;0;0;0m");
}
#[test]
fn blink_only() {
let style = CellStyle {
blink: true,
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[5;38;2;255;255;255;48;2;0;0;0m");
}
#[test]
fn strikethrough_only() {
let style = CellStyle {
strikethrough: true,
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[9;38;2;255;255;255;48;2;0;0;0m");
}
#[test]
fn named_fg_color() {
let style = CellStyle {
fg: Some(ResolvedColor::Named(NamedColor::Red)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[31;48;2;0;0;0m");
}
#[test]
fn named_bg_color() {
let style = CellStyle {
bg: Some(ResolvedColor::Named(NamedColor::Blue)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[38;2;255;255;255;44m");
}
#[test]
fn bright_fg_color() {
let style = CellStyle {
fg: Some(ResolvedColor::Named(NamedColor::BrightCyan)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[96;48;2;0;0;0m");
}
#[test]
fn palette_fg_color() {
let style = CellStyle {
fg: Some(ResolvedColor::Palette(196)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[38;5;196;48;2;0;0;0m");
}
#[test]
fn palette_bg_color() {
let style = CellStyle {
bg: Some(ResolvedColor::Palette(42)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[38;2;255;255;255;48;5;42m");
}
#[test]
fn rgb_fg_color() {
let style = CellStyle {
fg: Some(ResolvedColor::Rgb(255, 102, 0)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[38;2;255;102;0;48;2;0;0;0m");
}
#[test]
fn rgb_bg_color() {
let style = CellStyle {
bg: Some(ResolvedColor::Rgb(0, 0, 128)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[38;2;255;255;255;48;2;0;0;128m");
}
#[test]
fn combined_styles() {
let style = CellStyle {
fg: Some(ResolvedColor::Named(NamedColor::Red)),
bg: Some(ResolvedColor::Named(NamedColor::Black)),
bold: true,
underline: true,
..Default::default()
};
let sgr = style_to_sgr(&style);
assert!(sgr.starts_with("\x1b["));
assert!(sgr.ends_with('m'));
assert!(sgr.contains("1")); assert!(sgr.contains("4")); assert!(sgr.contains("31")); assert!(sgr.contains("48;2;0;0;0")); assert!(!sgr.contains(";40")); }
#[test]
fn named_black_bg_is_literal_rgb_not_palette_slot_zero() {
let style = CellStyle {
bg: Some(ResolvedColor::Named(NamedColor::Black)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), TERMINAL_DEFAULT_SGR);
}
#[test]
fn named_black_fg_is_literal_rgb() {
let style = CellStyle {
fg: Some(ResolvedColor::Named(NamedColor::Black)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[38;2;0;0;0;48;2;0;0;0m");
}
#[test]
fn bright_black_still_uses_the_palette() {
let style = CellStyle {
fg: Some(ResolvedColor::Named(NamedColor::BrightBlack)),
bg: Some(ResolvedColor::Named(NamedColor::BrightBlack)),
..Default::default()
};
assert_eq!(style_to_sgr(&style), "\x1b[90;100m");
}
#[test]
fn streaming_and_allocating_agree_on_black() {
for style in [
CellStyle {
bg: Some(ResolvedColor::Named(NamedColor::Black)),
..Default::default()
},
CellStyle {
fg: Some(ResolvedColor::Named(NamedColor::Black)),
bg: Some(ResolvedColor::Named(NamedColor::Black)),
bold: true,
..Default::default()
},
] {
let mut direct = Vec::new();
write_style_sgr(&mut direct, &style).unwrap();
assert_eq!(direct, style_to_sgr(&style).as_bytes());
}
}
#[test]
fn all_styles_at_once() {
let style = CellStyle {
fg: Some(ResolvedColor::Rgb(255, 0, 0)),
bg: Some(ResolvedColor::Rgb(0, 0, 255)),
bold: true,
dim: true,
italic: true,
underline: true,
blink: true,
strikethrough: true,
};
let sgr = style_to_sgr(&style);
assert!(sgr.contains("1")); assert!(sgr.contains("2")); assert!(sgr.contains("3")); assert!(sgr.contains("4")); assert!(sgr.contains("5")); assert!(sgr.contains("9")); assert!(sgr.contains("38;2;255;0;0")); assert!(sgr.contains("48;2;0;0;255")); let mut direct = Vec::new();
write_style_sgr(&mut direct, &style).unwrap();
assert_eq!(direct, sgr.as_bytes());
}
#[test]
fn move_cursor_values() {
assert_eq!(move_cursor(0, 0), "\x1b[1;1H");
assert_eq!(move_cursor(5, 10), "\x1b[6;11H");
assert_eq!(move_cursor(23, 79), "\x1b[24;80H");
}
#[test]
fn constants_are_valid_escape_sequences() {
assert!(RESET.starts_with("\x1b["));
assert!(HIDE_CURSOR.starts_with("\x1b["));
assert!(SHOW_CURSOR.starts_with("\x1b["));
assert!(ENTER_ALT_SCREEN.starts_with("\x1b["));
assert!(LEAVE_ALT_SCREEN.starts_with("\x1b["));
assert!(CLEAR_SCREEN.starts_with("\x1b["));
assert!(CURSOR_HOME.starts_with("\x1b["));
}
}