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