src/run.rs
302 lines · 12392 bytes
1use crossterm::event::{self, Event, MouseButton, MouseEventKind};
2use std::{error::Error, io::Stdout};
3use tui::{backend::CrosstermBackend, Terminal};
4use tui_additions::{
5 framework::{Framework, FrameworkDirection},
6 widgets::TextField,
7};
8
9use crate::{
10 config::*,
11 global::{functions::*, structs::*},
12};
13
14/// the main event loop of the program
15pub fn run(
16 terminal: &mut Terminal<CrosstermBackend<Stdout>>,
17 framework: &mut Framework,
18) -> Result<(), Box<dyn Error>> {
19 loop {
20 // repeat forever until all tasks are ran (and Tasks is cleared)
21 if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() {
22 tasks.run(framework, terminal)?;
23 continue;
24 }
25
26 framework
27 .data
28 .global
29 .get_mut::<Status>()
30 .unwrap()
31 .storage
32 .clear();
33
34 // exits the function is `.exit` is true - a way for items/commands to exit the program
35 if framework.data.global.get::<Status>().unwrap().exit {
36 break;
37 }
38 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
39
40 match event::read()? {
41 Event::Mouse(mouse)
42 if framework
43 .data
44 .global
45 .get::<MainConfig>()
46 .unwrap()
47 .mouse_support =>
48 {
49 if mouse.kind != MouseEventKind::Down(MouseButton::Left) {
50 continue;
51 }
52
53 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
54
55 let status = framework.data.global.get_mut::<Status>().unwrap();
56 status.command_capture = None;
57
58 // check if the search filter popup is clicked
59 let mut searchfilter_clicked = false;
60 if status.search_filter_opened {
61 let (mut frameworkclean, state) = framework.split_clean();
62 for row in state.0.iter_mut() {
63 for item in row.items.iter_mut() {
64 if item.item.r#type()
65 == "youtube_tui::items::searchfilters::SearchFilter"
66 && item.item.mouse_event(
67 &mut frameworkclean,
68 0,
69 0,
70 mouse.column,
71 mouse.row,
72 )
73 {
74 searchfilter_clicked = true;
75 break;
76 }
77 }
78
79 if searchfilter_clicked {
80 break;
81 }
82 }
83 }
84
85 let updated = if searchfilter_clicked {
86 true
87 } else {
88 framework.mouse_event(mouse.column, mouse.row)
89 };
90
91 if updated {
92 framework
93 .data
94 .state
95 .get_mut::<Tasks>()
96 .unwrap()
97 .priority
98 .push(Task::RenderAll);
99 }
100 }
101 Event::Key(key) => {
102 // check if key binds to any commands
103 let command_to_run = framework
104 .data
105 .global
106 .get::<CommandBindings>()
107 .unwrap()
108 .get_command(&key, framework.data.state.get::<Page>().unwrap());
109 if !command_to_run.is_empty() {
110 run_command(&apply_envs(command_to_run), framework, terminal);
111 framework
112 .data
113 .state
114 .get_mut::<Tasks>()
115 .unwrap()
116 .priority
117 .push(Task::RenderAll);
118 }
119
120 // 1. get the corresponding action
121 // 2. check if action is deselect, if yes, deselect
122 // 3. check is anything is selected, if yes, run `.key_event()` with the key
123 // 4. if nothing is selected, do stuff like moving the cursor or exiting
124
125 let action = framework
126 .data
127 .global
128 .get::<KeyBindingsConfig>()
129 .unwrap()
130 .get(key);
131
132 if action == Some(KeyAction::Deselect) {
133 let _ = framework.deselect();
134 framework
135 .data
136 .state
137 .get_mut::<Tasks>()
138 .unwrap()
139 .priority
140 .push(Task::RenderAll);
141 framework
142 .data
143 .global
144 .get_mut::<Status>()
145 .unwrap()
146 .command_capture = None;
147 continue;
148 }
149
150 // if something is selected, pass the key input into that item
151 // if nothing is selected, the following big chunk of code handles to movement of
152 // cursor and stuff
153 if framework.is_selected() {
154 if let Err(e) = framework.key_input(key) {
155 *framework.data.global.get_mut::<Message>().unwrap() =
156 Message::Error(e.to_string());
157 };
158 continue;
159 }
160
161 // the first part check for if the keys should be captured for entering commands
162 if framework
163 .data
164 .global
165 .get::<Status>()
166 .unwrap()
167 .command_capture
168 .is_some()
169 // the second part runs the command, and see if the returned boolean
170 // `is_updated` is true, if so update the screen
171 && command_capture(&mut framework.split_clean().0, key)
172 {
173 framework
174 .data
175 .state
176 .get_mut::<Tasks>()
177 .unwrap()
178 .priority
179 .push(Task::RenderAll);
180 continue;
181 }
182
183 if let Some(action) = action {
184 let mut render = true;
185 match action {
186 KeyAction::StartCommandCapture => {
187 framework
188 .data
189 .global
190 .get_mut::<Status>()
191 .unwrap()
192 .command_capture = Some(TextField::default());
193 }
194 KeyAction::Exit => break,
195 KeyAction::MoveUp => framework.r#move(FrameworkDirection::Up)?,
196 KeyAction::MoveDown => framework.r#move(FrameworkDirection::Down)?,
197 KeyAction::MoveLeft => framework.r#move(FrameworkDirection::Left)?,
198 KeyAction::MoveRight => framework.r#move(FrameworkDirection::Right)?,
199 KeyAction::Reload => framework
200 .data
201 .state
202 .get_mut::<Tasks>()
203 .unwrap()
204 .priority
205 .push(Task::Reload),
206 KeyAction::Back => {
207 if !framework.history.is_empty() {
208 clear_envs(
209 &mut framework.data.state.get::<StateEnvs>().unwrap().0.clone(),
210 );
211 }
212 if framework.revert_last_history().is_err() {
213 *framework.data.global.get_mut::<Message>().unwrap() =
214 Message::Error(String::from(
215 "This is already the beginning of history",
216 ))
217 } else {
218 let status = framework.data.global.get_mut::<Status>().unwrap();
219 status.render_image = true;
220
221 framework
222 .data
223 .state
224 .get_mut::<Tasks>()
225 .unwrap()
226 .priority
227 .push(Task::ClearPage);
228 let state_envs =
229 framework.data.state.get_mut::<StateEnvs>().unwrap();
230 set_envs(state_envs.clone().0.into_iter(), &mut state_envs.0);
231 update_provider(&mut framework.data);
232 }
233 }
234 KeyAction::FirstHistory => {
235 if framework.revert_history(0).is_err() {
236 *framework.data.global.get_mut::<Message>().unwrap() =
237 Message::Error(String::from(
238 "This is already the beginning of history",
239 ))
240 } else {
241 let status = framework.data.global.get_mut::<Status>().unwrap();
242 status.render_image = true;
243
244 update_provider(&mut framework.data);
245 framework
246 .data
247 .state
248 .get_mut::<Tasks>()
249 .unwrap()
250 .priority
251 .push(Task::ClearPage);
252 }
253 }
254 KeyAction::ClearHistory => {
255 if framework.history.is_empty() {
256 *framework.data.global.get_mut::<Message>().unwrap() =
257 Message::Error(String::from(
258 "This is already the beginning of history",
259 ))
260 } else {
261 framework.clear_history();
262 *framework.data.global.get_mut::<Message>().unwrap() =
263 Message::Success(String::from("History cleared!"))
264 }
265 }
266 KeyAction::Select => {
267 let _ = framework.select();
268 }
269 _ => render = false,
270 }
271 if render {
272 framework
273 .data
274 .state
275 .get_mut::<Tasks>()
276 .unwrap()
277 .priority
278 .push(Task::RenderAll);
279 }
280 }
281 }
282 // always render if there is a screen resize event
283 Event::Resize(_, _) => {
284 framework
285 .data
286 .state
287 .get_mut::<Tasks>()
288 .unwrap()
289 .priority
290 .push(Task::RenderAll);
291 framework
292 .data
293 .global
294 .get_mut::<Status>()
295 .unwrap()
296 .render_image = true;
297 }
298 _ => {}
299 }
300 }
301 Ok(())
302}