pub fn command(command : char, arguments : Vec<u8>) {
let argument_string = arguments.iter().map(|x|
x.to_string()
).collect::<Vec<String>>().join(";");
print!("\x1b[{}{}", argument_string, command);
}
pub fn move_to(x : u8, y : u8) {
command('H', vec![y, x]);
}
pub const CURSOR_UP : char = 'A';
pub const CURSOR_DOWN : char = 'B';
pub const CURSOR_RIGHT : char = 'C';
pub const CURSOR_LEFT : char = 'D';
pub fn move_by_direction(direction : char, amount : u8) {
command(direction, vec![amount]);
}
pub fn move_by(x : i8, y : i8) {
match (x < 0, x == 0) {
(true, false) => move_by_direction(CURSOR_LEFT, (x * -1) as u8),
(false, false) => move_by_direction(CURSOR_RIGHT, x as u8),
(_, _) => {}
};
match (y < 0, y == 0) {
(true, false) => move_by_direction(CURSOR_DOWN, (y * -1) as u8),
(false, false) => move_by_direction(CURSOR_UP, y as u8),
(_, _) => {}
};
}
pub const ERASE_CURSOR_TO_END : u8 = 0;
pub const ERASE_START_TO_CURSOR : u8 = 1;
pub const ERASE_SCREEN : u8 = 2;
pub const ERASE_CURSOR_TO_END_LINE : u8 = 3;
pub const ERASE_START_TO_CURSOR_LINE : u8 = 4;
pub const ERASE_LINE : u8 = 5;
pub fn erase(method : u8) {
if method > ERASE_SCREEN {
command('K', vec![method-3])
}
command('J', vec![method]);
}
pub const RESET : u8 = 0;
pub const BOLD : u8 = 1;
pub const FAINT : u8 = 2;
pub const ITALIC : u8 = 3;
pub const UNDERLINE : u8 = 4;
pub const SLOW_BLINK : u8 = 5;
pub const FAST_BLINK : u8 = 6;
pub const REVERSE : u8 = 7;
pub fn graphic(method : Vec<u8>) {
for i in method.into_iter() {
command('m', vec![i]);
}
}
pub fn graphic_reset() {
command('m', vec![0]);
}
pub fn color(color : u8, fg : bool) {
let mut fg_int : u8 = 0;
if fg {
fg_int += 10;
}
command('m', vec![38+fg_int, 5, color]);
}
pub fn color_16(color : u8, fg : bool, bright : bool) {
let mut fg_int : u8 = 0;
if fg {
fg_int += 10;
}
if bright {
fg_int += 60;
}
command('m', vec![30+fg_int+color]);
}
pub fn clear() {
graphic(vec![RESET]);
move_to(0, 0);
erase(ERASE_SCREEN);
move_to(0, 0);
}