use super::*;
use std::io::{Read, Stdout};
mod cross;
use cross::Crossterminal;
mod vt52;
use vt52::Vt52;
pub(crate) trait Term {
fn set_attrs(
&mut self,
style: Style,
fg: Option<Color>,
bg: Option<Color>,
) -> LifeOrDeath;
fn reset_attrs(&mut self) -> LifeOrDeath;
fn print(&mut self, text: &str) -> LifeOrDeath;
fn print_char(&mut self, char: char) -> LifeOrDeath;
fn print_spaces(&mut self, spaces: usize) -> LifeOrDeath;
fn move_cursor_up(&mut self, amt: u32) -> LifeOrDeath;
fn move_cursor_down(&mut self, amt: u32) -> LifeOrDeath;
fn move_cursor_left(&mut self, amt: u32) -> LifeOrDeath;
fn move_cursor_right(&mut self, amt: u32) -> LifeOrDeath;
fn newline(&mut self) -> LifeOrDeath;
fn carriage_return(&mut self) -> LifeOrDeath;
fn bell(&mut self) -> LifeOrDeath;
fn clear_all_and_reset(&mut self) -> LifeOrDeath;
fn clear_forward_and_reset(&mut self) -> LifeOrDeath;
fn clear_to_end_of_line(&mut self) -> LifeOrDeath;
fn hide_cursor(&mut self) -> LifeOrDeath;
fn show_cursor(&mut self) -> LifeOrDeath;
fn get_width(&mut self) -> u32;
fn cur_style(&self) -> Style;
fn flush(&mut self) -> LifeOrDeath;
fn suspend(&mut self) -> LifeOrDeath;
fn unsuspend(&mut self) -> LifeOrDeath;
fn cleanup(&mut self) -> LifeOrDeath;
}
pub(crate) fn new_term(
req_tx: &std_mpsc::Sender<Request>,
) -> Result<Box<dyn Term>, DummyError> {
if let Ok(term) = std::env::var("TERM") {
let main = term.split('-').next().unwrap_or("");
match main {
"st52" | "tw52" | "tt52" | "at" | "atari" | "atarist"
| "atari_st" | "vt52" | "stv52" | "stv52pc" => {
let monochrome = main == "vt52" || term.ends_with("-m");
let num_colors = if monochrome {
2
} else if main.contains("st") {
match crossterm::terminal::size().unwrap_or((80, 25)) {
(80, 50) => 2,
(80, 25) => 4,
(40, 25) => 16,
_ => {
eprintln!(
"Your terminal is configured \
incorrectly. Assuming monochrome."
);
2
}
}
} else {
16
};
return Ok(Box::new(Vt52::new(req_tx.clone(), num_colors)?));
}
_ => (), }
}
Ok(Box::new(Crossterminal::new(req_tx.clone())?))
}