src/exit.rs

42 lines · 1472 bytes

 1use home::home_dir;
 2use std::{error::Error, fs};
 3use tui_additions::framework::Framework;
 4
 5use crate::{
 6    config::MainConfig,
 7    global::{
 8        structs::*,
 9        traits::{Collection, CollectionNoId},
10    },
11};
12
13/// function to run when the app ends
14// exit tasks:
15//  - move thumbnails of videos in watch history to `~/.local/share/youtube-tui/watch_history/thumbnails`
16//  - remove `~/.cache`
17pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
18    let limits = framework.data.global.get::<MainConfig>().unwrap().limits;
19    let watchhistory = framework.data.global.get_mut::<WatchHistory>().unwrap();
20    watchhistory.trim(limits.watch_history);
21    watchhistory.exit_move();
22    let _ = watchhistory.save();
23    let subscriptions = framework.data.global.get::<Subscriptions>().unwrap();
24    subscriptions.exit_move();
25    let _ = subscriptions.save();
26    framework.data.global.get::<Library>().unwrap().exit_move();
27    let searchhistory = framework.data.global.get_mut::<SearchHistory>().unwrap();
28    searchhistory.trim(limits.search_history);
29    let _ = searchhistory.save();
30    let commandhistory = framework.data.global.get_mut::<CommandHistory>().unwrap();
31    commandhistory.trim(limits.commands_history);
32    let _ = commandhistory.save();
33
34    let home_dir = home_dir().unwrap();
35    let cache_path = home_dir.join(".cache/youtube-tui/");
36
37    if cache_path.exists() {
38        fs::remove_dir_all(cache_path)?;
39    }
40
41    Ok(())
42}