use crate::event::Event;
use crate::theme;
use crate::Vec2;
use unicode_width::UnicodeWidthStr;
pub trait Backend {
fn poll_event(&mut self) -> Option<Event>;
fn refresh(&mut self);
fn has_colors(&self) -> bool;
fn screen_size(&self) -> Vec2;
fn print_at(&self, pos: Vec2, text: &str);
fn print_at_rep(&self, pos: Vec2, repetitions: usize, text: &str) {
if repetitions > 0 {
self.print_at(pos, text);
let width = text.width();
let mut pos = pos;
let mut dupes_left = repetitions - 1;
while dupes_left > 0 {
pos = pos.saturating_add((width, 0));
self.print_at(pos, text);
dupes_left -= 1;
}
}
}
fn clear(&self, color: theme::Color);
fn set_color(&self, colors: theme::ColorPair) -> theme::ColorPair;
fn set_effect(&self, effect: theme::Effect);
fn unset_effect(&self, effect: theme::Effect);
fn name(&self) -> &str {
"unknown"
}
}
pub struct Dummy;
impl Dummy {
pub fn init() -> Box<dyn Backend>
where
Self: Sized,
{
Box::new(Dummy)
}
}
impl Backend for Dummy {
fn name(&self) -> &str {
"dummy"
}
fn refresh(&mut self) {}
fn has_colors(&self) -> bool {
false
}
fn screen_size(&self) -> Vec2 {
(1, 1).into()
}
fn poll_event(&mut self) -> Option<Event> {
Some(Event::Exit)
}
fn print_at(&self, _: Vec2, _: &str) {}
fn print_at_rep(&self, _pos: Vec2, _repetitions: usize, _text: &str) {}
fn clear(&self, _: theme::Color) {}
fn set_color(&self, colors: theme::ColorPair) -> theme::ColorPair {
colors
}
fn set_effect(&self, _: theme::Effect) {}
fn unset_effect(&self, _: theme::Effect) {}
}