use eye_declare_engine::Engine;
use eye_declare_engine::escape::CursorStyle;
use eye_declare_engine::frame::Frame;
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::Rect;
use crate::element::Element;
pub struct Timeline {
engine: Engine,
}
impl Timeline {
pub fn new(width: u16, terminal_height: u16) -> Self {
Self {
engine: Engine::new(width, terminal_height),
}
}
pub fn width(&self) -> u16 {
self.engine.width()
}
pub fn set_terminal_height(&mut self, height: u16) {
self.engine.set_terminal_height(height);
}
pub fn set_cursor_style(&mut self, style: CursorStyle) {
self.engine.set_cursor_style(style);
}
pub fn reset_screen(&mut self, new_width: u16) -> Vec<u8> {
self.engine.reset(new_width)
}
pub fn push(&mut self, block: impl Element) -> Vec<u8> {
let buf = render_to_buffer(&block, self.engine.width());
self.engine.commit(&buf)
}
pub fn present(&mut self, tail: &impl Element) -> Vec<u8> {
let width = self.engine.width();
let height = tail.height(width);
let area = Rect::new(0, 0, width, height);
let mut buf = Buffer::empty(area);
tail.render(area, &mut buf);
let cursor = tail.cursor(area);
self.engine.present(Frame::new(buf), cursor)
}
pub fn resize(&mut self, new_width: u16) -> Vec<u8> {
self.engine.reset_region(new_width)
}
pub fn resize_anchored(&mut self, new_width: u16, cursor: (u16, u16)) -> Vec<u8> {
self.engine.reset_region_anchored(new_width, cursor)
}
pub fn finalize(&mut self) -> Vec<u8> {
self.engine.finalize()
}
}
fn render_to_buffer(el: &impl Element, width: u16) -> Buffer {
let height = el.height(width);
let area = Rect::new(0, 0, width, height);
let mut buf = Buffer::empty(area);
if height > 0 {
el.render(area, &mut buf);
}
buf
}