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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;

/// Captures some parameters for [crate::run_tui()].
pub struct RunConfig<App, Global, Message, Error>
where
    App: AppWidget<Global, Message, Error>,
    Message: 'static + Send,
    Error: 'static + Send,
{
    /// How many worker threads are wanted?
    /// Most of the time 1 should be sufficient to offload any gui-blocking tasks.
    pub(crate) n_threats: usize,
    /// 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<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>,
{
    /// New configuration with some defaults.
    #[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),
            ],
        })
    }

    /// Number of background-threads.
    /// Default is 1.
    pub fn threads(mut self, n: usize) -> Self {
        self.n_threats = n;
        self
    }

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

    /// Remove default PollEvents.
    pub fn no_poll(mut self) -> Self {
        self.poll.clear();
        self
    }

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