1use crate::poll::PollEvents;
2use crate::terminal::{CrosstermTerminal, Terminal};
3use crossbeam::channel::TryRecvError;
4use std::cell::RefCell;
5use std::fmt::{Debug, Formatter};
6use std::io;
7use std::rc::Rc;
8
9pub struct RunConfig<Event, Error>
11where
12 Event: 'static,
13 Error: 'static,
14{
15 pub(crate) manual: bool,
17 pub(crate) term: Rc<RefCell<dyn Terminal<Error>>>,
22 pub(crate) poll: Vec<Box<dyn PollEvents<Event, Error>>>,
26}
27
28impl<Event, Error> Debug for RunConfig<Event, Error>
29where
30 Event: 'static,
31 Error: 'static,
32{
33 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34 f.debug_struct("RunConfig")
35 .field("render", &"...")
36 .field("events", &"...")
37 .finish()
38 }
39}
40
41impl<Event, Error> RunConfig<Event, Error>
42where
43 Event: 'static,
44 Error: 'static + From<io::Error> + From<TryRecvError>,
45{
46 #[allow(clippy::should_implement_trait)]
48 pub fn default() -> Result<Self, Error> {
49 Ok(Self {
50 manual: Default::default(),
51 term: Rc::new(RefCell::new(CrosstermTerminal::new()?)),
52 poll: Default::default(),
53 })
54 }
55
56 pub fn new(term: impl Terminal<Error> + 'static) -> Self {
58 Self {
59 manual: Default::default(),
60 term: Rc::new(RefCell::new(term)),
61 poll: Default::default(),
62 }
63 }
64
65 pub fn manual_mode(mut self) -> Self {
68 self.manual = true;
69 self
70 }
71
72 pub fn poll(mut self, poll: impl PollEvents<Event, Error> + 'static) -> Self {
74 self.poll.push(Box::new(poll));
75 self
76 }
77}