rat_salsa/
run_config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::poll_events::PollEvents;
use crate::terminal::{CrosstermTerminal, Terminal};
use crossbeam::channel::TryRecvError;
use std::fmt::{Debug, Formatter};
use std::io;

/// Captures some parameters for [crate::run_tui()].
pub struct RunConfig<Event, Error>
where
    Event: 'static + Send,
    Error: 'static + Send,
{
    /// This is the renderer that connects to the backend, and calls out
    /// for rendering the application.
    ///
    /// Defaults to RenderCrossterm.
    pub(crate) term: Box<dyn Terminal<Error>>,
    /// List of all event-handlers for the application.
    ///
    /// Defaults to PollTimers, PollCrossterm, PollTasks. Add yours here.
    pub(crate) poll: Vec<Box<dyn PollEvents<Event, Error>>>,
}

impl<Event, Error> Debug for RunConfig<Event, Error>
where
    Event: 'static + Send,
    Error: 'static + Send,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RunConfig")
            .field("render", &"...")
            .field("events", &"...")
            .finish()
    }
}

impl<Event, Error> RunConfig<Event, Error>
where
    Event: 'static + Send,
    Error: 'static + Send + From<io::Error> + From<TryRecvError>,
{
    /// New configuration with some defaults.
    #[allow(clippy::should_implement_trait)]
    pub fn default() -> Result<Self, Error> {
        Ok(Self {
            term: Box::new(CrosstermTerminal::new()?),
            poll: Default::default(),
        })
    }

    /// Terminal is a rat-salsa::terminal::Terminal not a ratatui::Terminal.
    pub fn new(term: impl Terminal<Error> + 'static) -> Self {
        Self {
            term: Box::new(term),
            poll: Default::default(),
        }
    }

    /// Add one more poll impl.
    pub fn poll(mut self, poll: impl PollEvents<Event, Error> + 'static) -> Self {
        self.poll.push(Box::new(poll));
        self
    }
}