pub const ESC: char = '\x1b';
pub const CSI: &str = "\x1b[";
pub const DCS: &str = "\x1bP";
pub const OCS: &str = "\x1b]";
#[macro_export]
macro_rules! csi {
($i:literal, $($a:expr),+) => {
$crate::seq!("\x1b[", $i, $($a),+)
};
}
#[macro_export]
macro_rules! seq {
($sq:literal, $i:literal, $f:literal, $($a:literal),*) => {
concat!($sq, $f $(, ';', $a)*, $i)
};
($sq:literal, $i:literal, $f:expr $(,$a:expr)*) => {
$crate::seq!($sq, $i, $f, $(";{}"; $a),*)
};
($sq:literal, $i:literal, $f:expr, $($l:literal; $e:expr),*) => {
format!(concat!($sq, "{}" $(,$l)*, $i), $f $(,$e)*)
}
}
pub const BELL: char = '\x07';
pub const BACKSPACE: char = '\x08';
pub const HTAB: char = '\t';
pub const NEWLINE: char = '\n';
pub const VTAB: char = '\x0b';
pub const FORMFEED: char = '\x0c';
pub const CARRIAGE_RETURN: char = '\r';
pub const DELETE: char = '\x7f';
#[macro_export]
macro_rules! move_to {
($x:expr, $y:expr) => {
$crate::csi!('H', $y, $x)
};
}
#[macro_export]
macro_rules! move_up {
($n:expr) => {
$crate::csi!('A', $n)
};
}
#[macro_export]
macro_rules! move_down {
($n:expr) => {
$crate::csi!('B', $n)
};
}
#[macro_export]
macro_rules! move_right {
($n:expr) => {
$crate::csi!('C', $n)
};
}
#[macro_export]
macro_rules! move_left {
($n:expr) => {
$crate::csi!('D', $n)
};
}
#[macro_export]
macro_rules! set_down {
($n:expr) => {
$crate::csi!('E', $n)
};
}
#[macro_export]
macro_rules! set_up {
($n:expr) => {
$crate::csi!('F', $n)
};
}
#[macro_export]
macro_rules! column {
($n:expr) => {
$crate::csi!('G', $n)
};
}
pub const UP_SCRL: &str = "\x1bM";
pub const CUR_SAVE: &str = "\x1b7";
pub const CUR_LOAD: &str = "\x1b8";
pub const ERASE_TO_END: &str = "\x1b[J";
pub const ERASE_FROM_START: &str = "\x1b[1J";
pub const ERASE_SCREEN: &str = "\x1b[2J";
pub const ERASE_ALL: &str = "\x1b[3J";
pub const ERASE_TO_LN_END: &str = "\x1b[K";
pub const ERASE_FROM_LN_START: &str = "\x1b[1K";
pub const ERASE_LINE: &str = "\x1b[2K";
pub const RESET: &str = "\x1b[0m";
pub const BOLD: &str = "\x1b[1m";
pub const FAINT: &str = "\x1b[2m";
pub const ITALIC: &str = "\x1b[3m";
pub const UNDERLINE: &str = "\x1b[4m";
pub const BLINKING: &str = "\x1b[5m";
pub const INVERSE: &str = "\x1b[7m";
pub const INVISIBLE: &str = "\x1b[8m";
pub const STRIKETROUGH: &str = "\x1b[9m";
pub const DOUBLE_UNDERLINE: &str = "\x1b[21";
pub const RESET_BOLD: &str = "\x1b[22m";
pub const RESET_ITALIC: &str = "\x1b[23m";
pub const RESET_UNDERLINE: &str = "\x1b[24m";
pub const RESET_BLINKING: &str = "\x1b[25m";
pub const RESET_INVERSE: &str = "\x1b[27m";
pub const RESET_INVISIBLE: &str = "\x1b[28m";
pub const RESET_STRIKETROUGH: &str = "\x1b[29m";
pub const BLACK_FG: &str = "\x1b[30m";
pub const WHITE_FG: &str = "\x1b[97m";
pub const GRAY_FG: &str = "\x1b[90m";
pub const GRAY_BRIGHT_FG: &str = "\x1b[37m";
pub const RED_FG: &str = "\x1b[91m";
pub const GREEN_FG: &str = "\x1b[92m";
pub const YELLOW_FG: &str = "\x1b[93m";
pub const BLUE_FG: &str = "\x1b[94m";
pub const MAGENTA_FG: &str = "\x1b[95m";
pub const CYAN_FG: &str = "\x1b[96m";
pub const RED_DARK_FG: &str = "\x1b[31m";
pub const GREEN_DARK_FG: &str = "\x1b[32m";
pub const YELLOW_DARK_FG: &str = "\x1b[33m";
pub const BLUE_DARK_FG: &str = "\x1b[34m";
pub const MAGENTA_DARK_FG: &str = "\x1b[35m";
pub const CYAN_DARK_FG: &str = "\x1b[36m";
pub const RESET_FG: &str = "\x1b[39m";
pub const BLACK_BG: &str = "\x1b[40m";
pub const WHITE_BG: &str = "\x1b[107m";
pub const GRAY_BG: &str = "\x1b[100m";
pub const GRAY_BRIGHT_BG: &str = "\x1b[47m";
pub const RED_BG: &str = "\x1b[101m";
pub const GREEN_BG: &str = "\x1b[102m";
pub const YELLOW_BG: &str = "\x1b[103m";
pub const BLUE_BG: &str = "\x1b[104m";
pub const MAGENTA_BG: &str = "\x1b[105m";
pub const CYAN_BG: &str = "\x1b[106m";
pub const RED_DARK_BG: &str = "\x1b[41m";
pub const GREEN_DARK_BG: &str = "\x1b[42m";
pub const YELLOW_DARK_BG: &str = "\x1b[43m";
pub const BLUE_DARK_BG: &str = "\x1b[44m";
pub const MAGENTA_DARK_BG: &str = "\x1b[45m";
pub const CYAN_DARK_BG: &str = "\x1b[46m";
pub const RESET_BG: &str = "\x1b[49m";
#[macro_export]
macro_rules! fg256 {
($c:expr) => {
$crate::csi!('m', 38, 5, $c)
};
}
#[macro_export]
macro_rules! bg256 {
($c:expr) => {
$crate::csi!('m', 48, 5, $c)
};
}
#[macro_export]
macro_rules! fg {
($r:expr, $g:expr, $b:expr) => {
$crate::csi!('m', 38, 2, $r, $g, $b)
};
}
#[macro_export]
macro_rules! bg {
($r:expr, $g:expr, $b:expr) => {
$crate::csi!('m', 48, 2, $r, $g, $b)
};
}
pub const ENABLE_LINE_WRAP: &str = "\x1b[=7h";
pub const DISABLE_LINE_WRAP: &str = "\x1b[=7l";
pub const HIDE_CURSOR: &str = "\x1b[?25l";
pub const SHOW_CURSOR: &str = "\x1b[?25h";
pub const SAVE_SCREEN: &str = "\x1b[?47l";
pub const LOAD_SCREEN: &str = "\x1b[?47h";
pub const ENABLE_ALTERNATIVE_BUFFER: &str = "\x1b[?1049h";
pub const DISABLE_ALTERNATIVE_BUFFER: &str = "\x1b[?1049l";
pub trait GetString {
fn get_string(self) -> String;
}
impl GetString for &str {
fn get_string(self) -> String {
self.to_owned()
}
}
impl GetString for String {
fn get_string(self) -> String {
self
}
}
#[cfg(test)]
mod tests {
use std::any::TypeId;
fn type_id_of<T: 'static>(_: T) -> TypeId {
TypeId::of::<T>()
}
use super::*;
#[test]
fn test_macros() {
assert_eq!(csi!('a', 1, 2, 3, 4, 5), "\x1b[1;2;3;4;5a");
assert_eq!(csi!('a', 1 + 0, 2, 3, 4, 5), "\x1b[1;2;3;4;5a");
assert_eq!(type_id_of(csi!('a', 1, 2, 3, 4, 5)), TypeId::of::<&str>());
assert_eq!(
type_id_of(csi!('a', 1 + 0, 2, 3, 4, 5)),
TypeId::of::<String>()
);
}
}