src/run.rs
285 lines · 11922 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::{clear_envs, command_capture, set_envs},
13 structs::{KeyAction, Message, StateEnvs, 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.history.is_empty() {
187 clear_envs(
188 &mut framework.data.state.get::<StateEnvs>().unwrap().0.clone(),
189 );
190 }
191 if framework.revert_last_history().is_err() {
192 *framework.data.global.get_mut::<Message>().unwrap() =
193 Message::Error(String::from(
194 "This is already the beginning of history",
195 ))
196 } else {
197 framework
198 .data
199 .global
200 .get_mut::<Status>()
201 .unwrap()
202 .render_image = true;
203 framework
204 .data
205 .state
206 .get_mut::<Tasks>()
207 .unwrap()
208 .priority
209 .push(Task::ClearPage);
210 let state_envs =
211 framework.data.state.get_mut::<StateEnvs>().unwrap();
212 set_envs(state_envs.clone().0.into_iter(), &mut state_envs.0);
213 }
214 }
215 KeyAction::FirstHistory => {
216 if framework.revert_history(0).is_err() {
217 *framework.data.global.get_mut::<Message>().unwrap() =
218 Message::Error(String::from(
219 "This is already the beginning of history",
220 ))
221 } else {
222 framework
223 .data
224 .global
225 .get_mut::<Status>()
226 .unwrap()
227 .render_image = true;
228 framework
229 .data
230 .state
231 .get_mut::<Tasks>()
232 .unwrap()
233 .priority
234 .push(Task::ClearPage);
235 }
236 }
237 KeyAction::ClearHistory => {
238 if framework.history.is_empty() {
239 *framework.data.global.get_mut::<Message>().unwrap() =
240 Message::Error(String::from(
241 "This is already the beginning of history",
242 ))
243 } else {
244 framework.clear_history();
245 *framework.data.global.get_mut::<Message>().unwrap() =
246 Message::Success(String::from("History cleared!"))
247 }
248 }
249 KeyAction::Select => {
250 let _ = framework.select();
251 }
252 _ => render = false,
253 }
254 if render {
255 framework
256 .data
257 .state
258 .get_mut::<Tasks>()
259 .unwrap()
260 .priority
261 .push(Task::RenderAll);
262 }
263 }
264 }
265 // always render if there is a screen resize event
266 Event::Resize(_, _) => {
267 framework
268 .data
269 .state
270 .get_mut::<Tasks>()
271 .unwrap()
272 .priority
273 .push(Task::RenderAll);
274 framework
275 .data
276 .global
277 .get_mut::<Status>()
278 .unwrap()
279 .render_image = true;
280 }
281 _ => {}
282 }
283 }
284 Ok(())
285}