use std::{future::ready, ops::ControlFlow, time::Duration}; use crossterm::event::Event; use futures::{Stream, StreamExt}; use ratatui::{ Terminal, backend::Backend, buffer::{Buffer, CellDiffOption}, style::Modifier, }; use crate::{ boot_model::{BootChange, BootState, boot_origin::BootOrigin}, boot_view::{BootView, PasswordPrompt}, screen_settings::{color_scheme::Colors, config_file::Timing, font_glyphs::Glyphs}, systemd_link::{link_error::Error, monotonic_clock::monotonic_now}, }; const INPUT_BATCH: usize = 64; pub enum ScreenInput { Boot(Vec), Reconnected, Key(Event), Frame, FullRepaint, PasswordRequests, Terminate, Failed(Error), } pub enum WatchEnding { BootCompleted, ViewerLeft, Failed(Error), } pub trait ConsoleDesk { fn glyphs(&self) -> &Glyphs; fn drops_bold(&self) -> bool; fn prompt(&self, now: Duration) -> Option>; fn press_key(&mut self, event: &Event, now: Duration) -> ControlFlow; fn frame_passed(&mut self, state: &mut BootState, now: Duration) -> ControlFlow; fn password_requests_changed(&mut self) {} fn keep_console_up(&mut self) {} fn take_clear_request(&mut self) -> bool { false } fn refreshed_origin(&self) -> Option { None } } pub struct WatchScreen { pub state: BootState, pub desk: Desk, origin: BootOrigin, full_repaint_due: bool, colors: Colors, timing: Timing, epoch: Duration, } impl WatchScreen { pub fn new( state: BootState, desk: Desk, origin: BootOrigin, colors: Colors, timing: Timing, epoch: Duration, ) -> Self { Self { state, desk, origin, full_repaint_due: false, colors, timing, epoch, } } #[must_use] pub fn now(&self) -> Duration { monotonic_now().saturating_sub(self.epoch) } fn absorb(&mut self, input: ScreenInput) -> ControlFlow { let now = self.now(); match input { ScreenInput::Boot(changes) => self.apply_boot_changes(changes), ScreenInput::Reconnected => { self.full_repaint_due = true; ControlFlow::Continue(()) } ScreenInput::Key(event) => self.desk.press_key(&event, now), ScreenInput::Frame => self.desk.frame_passed(&mut self.state, now), ScreenInput::FullRepaint => { self.desk.keep_console_up(); self.full_repaint_due = true; ControlFlow::Continue(()) } ScreenInput::PasswordRequests => { self.desk.password_requests_changed(); ControlFlow::Continue(()) } ScreenInput::Terminate => ControlFlow::Break(WatchEnding::ViewerLeft), ScreenInput::Failed(error) => ControlFlow::Break(WatchEnding::Failed(error)), } } fn apply_boot_changes(&mut self, changes: Vec) -> ControlFlow { let phase_before = self.state.phase; changes .into_iter() .for_each(|change| self.state.apply(change)); if self.state.phase != phase_before && let Some(origin) = self.desk.refreshed_origin() { self.origin = origin; } if !self.state.is_complete() { return ControlFlow::Continue(()); } eprintln!( "bootvine: {} reached, handing the console over", self.state.root.unit().unwrap_or("boot") ); ControlFlow::Break(WatchEnding::BootCompleted) } fn absorb_batch( &mut self, batch: Vec, terminal: &mut Terminal, ) -> ControlFlow where Error: From, { batch.into_iter().try_for_each(|input| self.absorb(input))?; match self.draw(terminal) { Ok(()) => ControlFlow::Continue(()), Err(error) => ControlFlow::Break(WatchEnding::Failed(error)), } } fn draw(&mut self, terminal: &mut Terminal) -> Result<(), Error> where Error: From, { if self.desk.take_clear_request() { terminal.clear()?; } let now = self.now(); let completed = terminal.draw(|canvas| { canvas.render_widget( BootView { state: &self.state, colors: &self.colors, timing: &self.timing, glyphs: self.desk.glyphs(), now, origin: &self.origin, password_prompt: self.desk.prompt(now), }, canvas.area(), ); if self.desk.drops_bold() { drop_bold(canvas.buffer_mut()); } })?; if self.full_repaint_due { let painted = completed.buffer.clone(); overwrite_every_cell(terminal.backend_mut(), &painted)?; self.full_repaint_due = false; } Ok(()) } } pub async fn watch_until_ending( screen: &mut WatchScreen, terminal: &mut Terminal, inputs: impl Stream, ) -> WatchEnding where Error: From, { inputs .ready_chunks(INPUT_BATCH) .filter_map(|batch| ready(screen.absorb_batch(batch, terminal).break_value())) .take(1) .fold(WatchEnding::ViewerLeft, |_, ending| ready(ending)) .await } fn overwrite_every_cell(backend: &mut B, frame: &Buffer) -> Result<(), B::Error> { backend.draw( frame .content .iter() .enumerate() .filter(|(_, cell)| cell.diff_option != CellDiffOption::Skip) .map(|(index, cell)| { let (x, y) = frame.pos_of(index); (x, y, cell) }), )?; backend.flush() } fn drop_bold(frame: &mut Buffer) { frame .content .iter_mut() .for_each(|cell| cell.modifier.remove(Modifier::BOLD)); }