summaryrefslogtreecommitdiffstats
path: root/src/context/context.rs
blob: bb5a5121d4f56b16e7ebbcb937e49c232a2b3cbb (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::collections::vec_deque::Iter;
use std::collections::VecDeque;
use std::sync::mpsc;
use std::thread;

use crate::config;
use crate::context::{LocalStateContext, TabContext};
use crate::io::{IOWorkerObserver, IOWorkerProgress, IOWorkerThread};
use crate::util::event::{Event, Events};

pub struct JoshutoContext {
    pub exit: bool,
    config: config::JoshutoConfig,
    events: Events,
    tab_context: TabContext,
    local_state: Option<LocalStateContext>,
    search_state: Option<String>,
    message_queue: VecDeque<String>,
    worker_queue: VecDeque<IOWorkerThread>,
    worker: Option<IOWorkerObserver>,
}

impl JoshutoContext {
    pub fn new(config: config::JoshutoConfig) -> Self {
        Self {
            exit: false,
            events: Events::new(),
            tab_context: TabContext::new(),
            local_state: None,
            search_state: None,
            message_queue: VecDeque::with_capacity(4),
            worker_queue: VecDeque::new(),
            worker: None,
            config,
        }
    }

    pub fn config_ref(&self) -> &config::JoshutoConfig {
        &self.config
    }

    pub fn config_mut(&mut self) -> &mut config::JoshutoConfig {
        &mut self.config
    }

    pub fn tab_context_ref(&self) -> &TabContext {
        &self.tab_context
    }
    pub fn tab_context_mut(&mut self) -> &mut TabContext {
        &mut self.tab_context
    }

    pub fn message_queue_ref(&self) -> &VecDeque<String> {
        &self.message_queue
    }
    pub fn push_msg(&mut self, msg: String) {
        self.message_queue.push_back(msg);
    }
    pub fn pop_msg(&mut self) -> Option<String> {
        self.message_queue.pop_front()
    }

    // event related
    pub fn poll_event(&self) -> Result<Event, mpsc::RecvError> {
        self.events.next()
    }
    pub fn get_event_tx(&self) -> mpsc::Sender<Event> {
        self.events.event_tx.clone()
    }
    pub fn flush_event(&self) {
        self.events.flush();
    }

    // local state related
    pub fn set_local_state(&mut self, state: LocalStateContext) {
        self.local_state = Some(state);
    }
    pub fn take_local_state(&mut self) -> Option<LocalStateContext> {
        self.local_state.take()
    }

    pub fn set_search_state(&mut self, pattern: String) {
        self.search_state = Some(pattern);
    }

    pub fn get_search_state(&self) -> Option<&String> {
        self.search_state.as_ref()
    }

    // worker related
    pub fn add_worker(&mut self, thread: IOWorkerThread) {
        self.worker_queue.push_back(thread);
    }
    pub fn worker_is_busy(&self) -> bool {
        self.worker.is_some()
    }
    pub fn worker_is_empty(&self) -> bool {
        self.worker_queue.is_empty()
    }

    pub fn worker_iter(&self) -> Iter<IOWorkerThread> {
        self.worker_queue.iter()
    }

    pub fn worker_ref(&self) -> Option<&IOWorkerObserver> {
        self.worker.as_ref()
    }

    pub fn set_worker_progress(&mut self, res: IOWorkerProgress) {
        if let Some(s) = self.worker.as_mut() {
            s.set_progress(res);
        }
    }

    pub fn update_worker_msg(&mut self) {
        if let Some(s) = self.worker.as_mut() {
            s.update_msg();
        }
    }
    pub fn worker_msg(&self) -> Option<&str> {
        let worker = self.worker.as_ref()?;
        Some(worker.get_msg())
    }

    pub fn start_next_job(&mut self) {
        let tx = self.get_event_tx();

        if let Some(worker) = self.worker_queue.pop_front() {
            let src = worker.paths[0].parent().unwrap().to_path_buf();
            let dest = worker.dest.clone();
            let handle = thread::spawn(move || {
                let (wtx, wrx) = mpsc::channel();
                // start worker
                let worker_handle = thread::spawn(move || worker.start(wtx));
                // relay worker info to event loop
                while let Ok(progress) = wrx.recv() {
                    let _ = tx.send(Event::IOWorkerProgress(progress));
                }
                let result = worker_handle.join();

                match result {
                    Ok(res) => {
                        let _ = tx.send(Event::IOWorkerResult(res));
                    }
                    Err(_) => {
                        let err = std::io::Error::new(std::io::ErrorKind::Other, "Sending Error");
                        let _ = tx.send(Event::IOWorkerResult(Err(err)));
                    }
                }
            });
            let observer = IOWorkerObserver::new(handle, src, dest);
            self.worker = Some(observer);
        }
    }

    pub fn remove_job(&mut self) -> Option<IOWorkerObserver> {
        self.worker.take()
    }
}