src/run.rs

db597a03bc7a1786d5261e8a55628da30193c775
youtube-tui/src/run.rs history · blame · raw

115 lines · 4066 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::{
  9        keyaction::KeyAction,
 10        message::Message,
 11        page::{MainMenuPage, Page},
 12        tasks::{Task, Tasks},
 13    },
 14};
 15
 16// the main event loop of the program
 17pub fn run(
 18    terminal: &mut Terminal<CrosstermBackend<Stdout>>,
 19    mut framework: Framework,
 20) -> Result<(), Box<dyn Error>> {
 21    // push the initial page load
 22    framework
 23        .data
 24        .state
 25        .get_mut::<Tasks>()
 26        .unwrap()
 27        .priority
 28        .push(Task::LoadPage(Page::MainMenu(MainMenuPage::Trending)));
 29
 30    loop {
 31        // repeat forever until all tasks are ran (and Tasks is cleared)
 32        if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() {
 33            tasks.run(&mut framework, terminal)?;
 34            continue;
 35        }
 36
 37        match event::read()? {
 38            Event::Key(key) => {
 39                // 1. get the corresponding action
 40                // 2. check if action is deselect, if yes, deselect
 41                // 3. check is anything is selected, if yes, run `.key_event()` with the key
 42                // 4. if nothing is selected, do stuff like moving the cursor or exiting
 43
 44                let action = if let Some(keyactions) = framework
 45                    .data
 46                    .global
 47                    .get::<KeyBindingsConfig>()
 48                    .unwrap()
 49                    .0
 50                    .get(&key.code)
 51                {
 52                    if let Some(action) = keyactions.get(&key.modifiers.bits()) {
 53                        Some(*action)
 54                    } else {
 55                        None
 56                    }
 57                } else {
 58                    None
 59                };
 60
 61                if action == Some(KeyAction::Deselect) {
 62                    let _ = framework.deselect();
 63                    framework
 64                        .data
 65                        .state
 66                        .get_mut::<Tasks>()
 67                        .unwrap()
 68                        .priority
 69                        .push(Task::RenderAll);
 70                }
 71
 72                if framework.is_selected() {
 73                    if let Err(e) = framework.key_input(key) {
 74                        *framework.data.global.get_mut::<Message>().unwrap() =
 75                            Message::Error(format!("{}", e));
 76                    };
 77                } else if let Some(action) = action {
 78                    let mut render = true;
 79                    match action {
 80                        KeyAction::Exit => break,
 81                        KeyAction::MoveUp => framework.r#move(FrameworkDirection::Up)?,
 82                        KeyAction::MoveDown => framework.r#move(FrameworkDirection::Down)?,
 83                        KeyAction::MoveLeft => framework.r#move(FrameworkDirection::Left)?,
 84                        KeyAction::MoveRight => framework.r#move(FrameworkDirection::Right)?,
 85                        KeyAction::Select => {
 86                            let _ = framework.select();
 87                        }
 88                        _ => render = false,
 89                    }
 90                    if render {
 91                        framework
 92                            .data
 93                            .state
 94                            .get_mut::<Tasks>()
 95                            .unwrap()
 96                            .priority
 97                            .push(Task::RenderAll);
 98                    }
 99                }
100            }
101            // always render if there is a screen resize event
102            Event::Resize(_, _) => {
103                framework
104                    .data
105                    .state
106                    .get_mut::<Tasks>()
107                    .unwrap()
108                    .priority
109                    .push(Task::RenderAll);
110            }
111            _ => {}
112        }
113    }
114    Ok(())
115}