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