use crate::poll::{PollCrossterm, PollEvents, PollTasks, PollTimers};
use crate::terminal::{CrosstermTerminal, Terminal};
use crate::AppWidget;
use crossbeam::channel::TryRecvError;
use std::fmt::{Debug, Formatter};
use std::io;
pub struct RunConfig<App, Global, Message, Error>
where
App: AppWidget<Global, Message, Error>,
Message: 'static + Send,
Error: 'static + Send,
{
pub(crate) n_threats: usize,
pub(crate) term: Box<dyn Terminal<Error>>,
pub(crate) poll: Vec<Box<dyn PollEvents<Global, App::State, Message, Error>>>,
}
impl<App, Global, Message, Error> Debug for RunConfig<App, Global, Message, Error>
where
App: AppWidget<Global, Message, Error>,
Message: 'static + Send,
Error: 'static + Send,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RunConfig")
.field("n_threads", &self.n_threats)
.field("render", &"...")
.field("events", &"...")
.finish()
}
}
impl<App, Global, Message, Error> RunConfig<App, Global, Message, Error>
where
App: AppWidget<Global, Message, Error>,
Message: 'static + Send,
Error: 'static + Send + From<io::Error> + From<TryRecvError>,
{
#[allow(clippy::should_implement_trait)]
pub fn default() -> Result<Self, Error> {
Ok(Self {
n_threats: 1,
term: Box::new(CrosstermTerminal::new()?),
poll: vec![
Box::new(PollTimers),
Box::new(PollCrossterm),
Box::new(PollTasks),
],
})
}
pub fn threads(mut self, n: usize) -> Self {
self.n_threats = n;
self
}
pub fn term(mut self, term: impl Terminal<Error> + 'static) -> Self {
self.term = Box::new(term);
self
}
pub fn no_poll(mut self) -> Self {
self.poll.clear();
self
}
pub fn poll(
mut self,
poll: impl PollEvents<Global, App::State, Message, Error> + 'static,
) -> Self {
self.poll.push(Box::new(poll));
self
}
}