src/run.rs
161 lines · 6885 bytes
1use crossterm::event::{self, Event};
2use std::{error::Error, io::Stdout};
3use tui::{backend::CrosstermBackend, Terminal};
4use tui_additions::framework::{Framework, FrameworkDirection};
5
6use crate::{
7 config::KeyBindingsConfig,
8 global::structs::{KeyAction, MainMenuPage, Message, Page, Task, Tasks},
9};
10
11/// the main event loop of the program
12pub fn run(
13 terminal: &mut Terminal<CrosstermBackend<Stdout>>,
14 framework: &mut Framework,
15) -> Result<(), Box<dyn Error>> {
16 // push the initial page load
17 let tasks = framework.data.state.get_mut::<Tasks>().unwrap();
18 tasks
19 .priority
20 .push(Task::LoadPage(Page::MainMenu(MainMenuPage::Popular)));
21 tasks.pop().unwrap().run(framework, terminal)?;
22 framework.clear_history();
23
24 loop {
25 // repeat forever until all tasks are ran (and Tasks is cleared)
26 if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() {
27 tasks.run(framework, terminal)?;
28 continue;
29 }
30
31 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
32
33 match event::read()? {
34 Event::Key(key) => {
35 // 1. get the corresponding action
36 // 2. check if action is deselect, if yes, deselect
37 // 3. check is anything is selected, if yes, run `.key_event()` with the key
38 // 4. if nothing is selected, do stuff like moving the cursor or exiting
39
40 let action = if let Some(keyactions) = framework
41 .data
42 .global
43 .get::<KeyBindingsConfig>()
44 .unwrap()
45 .0
46 .get(&key.code)
47 {
48 keyactions.get(&key.modifiers.bits()).copied()
49 } else {
50 None
51 };
52
53 if action == Some(KeyAction::Deselect) {
54 let _ = framework.deselect();
55 framework
56 .data
57 .state
58 .get_mut::<Tasks>()
59 .unwrap()
60 .priority
61 .push(Task::RenderAll);
62 }
63
64 // if something is selected, pass the key input into that item
65 // if nothing is selected, the following big chunk of code handles to movement of
66 // cursor and stuff
67 if framework.is_selected() {
68 if let Err(e) = framework.key_input(key) {
69 *framework.data.global.get_mut::<Message>().unwrap() =
70 Message::Error(format!("{}", e));
71 };
72 } else if let Some(action) = action {
73 let mut render = true;
74 match action {
75 KeyAction::Exit => break,
76 KeyAction::MoveUp => framework.r#move(FrameworkDirection::Up)?,
77 KeyAction::MoveDown => framework.r#move(FrameworkDirection::Down)?,
78 KeyAction::MoveLeft => framework.r#move(FrameworkDirection::Left)?,
79 KeyAction::MoveRight => framework.r#move(FrameworkDirection::Right)?,
80 KeyAction::Reload => framework
81 .data
82 .state
83 .get_mut::<Tasks>()
84 .unwrap()
85 .priority
86 .push(Task::Reload),
87 KeyAction::Back => {
88 if framework.revert_last_history().is_err() {
89 *framework.data.global.get_mut::<Message>().unwrap() =
90 Message::Error(String::from(
91 "This is already the beginning of history",
92 ))
93 } else {
94 framework
95 .data
96 .state
97 .get_mut::<Tasks>()
98 .unwrap()
99 .priority
100 .push(Task::ClearPage);
101 }
102 }
103 KeyAction::FirstHistory => {
104 if framework.revert_history(0).is_err() {
105 *framework.data.global.get_mut::<Message>().unwrap() =
106 Message::Error(String::from(
107 "This is already the beginning of history",
108 ))
109 } else {
110 framework
111 .data
112 .state
113 .get_mut::<Tasks>()
114 .unwrap()
115 .priority
116 .push(Task::ClearPage);
117 }
118 }
119 KeyAction::ClearHistory => {
120 if framework.history.is_empty() {
121 *framework.data.global.get_mut::<Message>().unwrap() =
122 Message::Error(String::from(
123 "This is already the beginning of history",
124 ))
125 } else {
126 framework.clear_history();
127 *framework.data.global.get_mut::<Message>().unwrap() =
128 Message::Success(String::from("History cleared!"))
129 }
130 }
131 KeyAction::Select => {
132 let _ = framework.select();
133 }
134 _ => render = false,
135 }
136 if render {
137 framework
138 .data
139 .state
140 .get_mut::<Tasks>()
141 .unwrap()
142 .priority
143 .push(Task::RenderAll);
144 }
145 }
146 }
147 // always render if there is a screen resize event
148 Event::Resize(_, _) => {
149 framework
150 .data
151 .state
152 .get_mut::<Tasks>()
153 .unwrap()
154 .priority
155 .push(Task::RenderAll);
156 }
157 _ => {}
158 }
159 }
160 Ok(())
161}