rat_salsa/
run_config.rs

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
9/// Captures some parameters for [crate::run_tui()].
10pub struct RunConfig<Event, Error>
11where
12    Event: 'static,
13    Error: 'static,
14{
15    /// Don't run init/shutdown.
16    pub(crate) manual: bool,
17    /// This is the renderer that connects to the backend, and calls out
18    /// for rendering the application.
19    ///
20    /// Defaults to RenderCrossterm.
21    pub(crate) term: Rc<RefCell<dyn Terminal<Error>>>,
22    /// List of all event-handlers for the application.
23    ///
24    /// Defaults to PollTimers, PollCrossterm, PollTasks. Add yours here.
25    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    /// New configuration with some defaults.
47    #[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    /// Terminal is a rat-salsa::terminal::Terminal not a ratatui::Terminal.
57    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    /// Don't run [Terminal::init] and [Terminal::shutdown].
66    /// You can run your own terminal initialization/shutdown before/after calling run_tui(),
67    pub fn manual_mode(mut self) -> Self {
68        self.manual = true;
69        self
70    }
71
72    /// Add one more poll impl.
73    pub fn poll(mut self, poll: impl PollEvents<Event, Error> + 'static) -> Self {
74        self.poll.push(Box::new(poll));
75        self
76    }
77}