src/main.rs

9aedf4a98dfbe60cee47b4e673a37437e309f462
youtube-tui/src/main.rs history · blame · raw

245 lines · 6246 bytes

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