src/main.rs

46e7ed5b32a7417aabe4491e60a597344597d330
youtube-tui/src/main.rs history · blame · raw

258 lines · 6971 bytes

  1use crossterm::{
  2    event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode},
  3    execute,
  4    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
  5};
  6use std::{error::Error, fs, io};
  7use tui::{
  8    backend::{Backend, CrosstermBackend},
  9    Terminal,
 10};
 11use youtube_tui::{
 12    app::{
 13        app::App,
 14        pages::{item_info::ItemInfo, main_menu::MainMenu},
 15    },
 16    structs::{Item, Page, Row, RowItem},
 17    traits::LoadItem,
 18};
 19
 20fn main() -> Result<(), Box<dyn Error>> {
 21    init()?;
 22
 23    let app = App::default();
 24
 25    enable_raw_mode()?;
 26    let mut stdout = io::stdout();
 27    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
 28    let backend = CrosstermBackend::new(stdout);
 29    let mut terminal = Terminal::new(backend)?;
 30
 31    let res = run_app(&mut terminal, app);
 32
 33    execute!(
 34        terminal.backend_mut(),
 35        LeaveAlternateScreen,
 36        DisableMouseCapture
 37    )?;
 38    disable_raw_mode()?;
 39
 40    res?;
 41    exit()?;
 42
 43    Ok(())
 44}
 45
 46fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(), Box<dyn Error>> {
 47    loop {
 48        if app.render {
 49            ui(&mut terminal, &mut app)?;
 50        }
 51
 52        if app.load {
 53            *app.message.lock().unwrap() = Some(match app.page {
 54                Page::MainMenu(_) => MainMenu::message(),
 55                Page::ItemDisplay(_) => ItemInfo::message(),
 56            });
 57            terminal.clear()?;
 58            ui(&mut terminal, &mut app)?;
 59            let mut new_state = Vec::new();
 60            for row in app.state.iter() {
 61                let mut row_vec = Vec::new();
 62                for row_item in row.items.iter() {
 63                    match &row_item.item {
 64                        Item::MainMenu(item) => match item.load_item(&app) {
 65                            Ok(new) => {
 66                                row_vec.push(RowItem {
 67                                    item: Item::MainMenu(*new),
 68                                    ..*row_item
 69                                });
 70                            }
 71                            Err(e) => {
 72                                row_vec.push(RowItem {
 73                                    item: Item::MainMenu(item.clone()),
 74                                    ..*row_item
 75                                });
 76                                *app.message.lock().unwrap() = Some(e.to_string());
 77                            }
 78                        },
 79                        Item::ItemInfo(item) => match item.load_item(&app) {
 80                            Ok(new) => {
 81                                row_vec.push(RowItem {
 82                                    item: Item::ItemInfo(*new),
 83                                    ..*row_item
 84                                });
 85                            }
 86                            Err(e) => {
 87                                row_vec.push(RowItem {
 88                                    item: Item::ItemInfo(item.clone()),
 89                                    ..*row_item
 90                                });
 91                                *app.message.lock().unwrap() = Some(e.to_string());
 92                            }
 93                        },
 94                        _ => {
 95                            row_vec.push(RowItem {
 96                                item: row_item.item.clone(),
 97                                ..*row_item
 98                            });
 99                        }
100                    }
101                }
102
103                new_state.push(Row {
104                    items: row_vec,
105                    ..*row
106                });
107            }
108
109            app.state = new_state;
110
111            app.load = false;
112            terminal.clear()?;
113            ui(&mut terminal, &mut app)?;
114        } else {
115            match event::read()? {
116                event::Event::Key(key) => {
117                    if app.selected.is_none() {
118                        match key.code {
119                            KeyCode::Char('q') => {
120                                return Ok(());
121                            }
122                            KeyCode::Backspace => {
123                                let holder = app.pop();
124                                app = holder.0;
125                                if holder.1 {
126                                    terminal.clear()?;
127                                }
128                                app.render = true;
129                                continue;
130                            }
131                            KeyCode::End => {
132                                app.history = Vec::new();
133                                continue;
134                            }
135                            KeyCode::Home => {
136                                terminal.clear()?;
137                                app.home();
138                                continue;
139                            }
140                            _ => {}
141                        }
142                    }
143
144                    app = app.key_input(key.code);
145                    app.render = true;
146                }
147                event::Event::Resize(_, _) => {
148                    app.render = true;
149                }
150                _ => {}
151            }
152        }
153    }
154}
155
156fn ui<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> Result<(), Box<dyn Error>> {
157    terminal.draw(|mut frame| {
158        app.render(&mut frame);
159    })?;
160
161    *app.message.lock().unwrap() = None;
162    app.render = false;
163
164    Ok(())
165}
166
167fn init() -> Result<(), Box<dyn Error>> {
168    let home_dir = home::home_dir().expect("Cannot get your home directory");
169
170    let mut dir = home_dir.clone();
171
172    dir.push(".cache");
173    if !dir.exists() {
174        fs::create_dir(&dir)?;
175    }
176
177    dir.push("youtube-tui");
178    if !dir.exists() {
179        fs::create_dir(&dir)?;
180    }
181
182    dir.push("thumbnails");
183
184    if !dir.exists() {
185        fs::create_dir(&dir)?;
186    }
187
188    dir = home_dir.clone();
189
190    dir.push(".config");
191
192    if !dir.exists() {
193        fs::create_dir(&dir)?;
194    }
195
196    dir.push("youtube-tui");
197
198    if !dir.exists() {
199        fs::create_dir(&dir)?;
200    }
201
202    dir = home_dir;
203
204    dir.push(".local");
205
206    if !dir.exists() {
207        fs::create_dir(&dir)?;
208    }
209
210    dir.push("share");
211
212    if !dir.exists() {
213        fs::create_dir(&dir)?;
214    }
215
216    dir.push("youtube-tui");
217
218    if !dir.exists() {
219        fs::create_dir(&dir)?;
220    }
221
222    dir.push("watch_history");
223
224    if !dir.exists() {
225        fs::create_dir(&dir)?;
226    }
227
228    dir.push("thumbnails");
229
230    if !dir.exists() {
231        fs::create_dir(&dir)?;
232    }
233
234    dir.pop();
235
236    dir.push("info");
237
238    if !dir.exists() {
239        fs::create_dir(&dir)?;
240    }
241
242
243    Ok(())
244}
245
246fn exit() -> Result<(), Box<dyn Error>> {
247    let mut dir = home::home_dir().expect("Cannot get your home directory");
248
249    dir.push(".cache");
250    dir.push("youtube-tui");
251    dir.push("cache");
252
253    if dir.exists() {
254        fs::remove_dir_all(&dir)?;
255    }
256
257    Ok(())
258}