src/exit.rs
31 lines · 903 bytes
1use home::home_dir;
2use std::{error::Error, fs};
3use tui_additions::framework::Framework;
4
5use crate::global::{structs::*, traits::Collection};
6
7/// function to run when the app ends
8// exit tasks:
9// - move thumbnails of videos in watch history to `~/.local/share/youtube-tui/watch_history/thumbnails`
10// - remove `~/.cache`
11pub fn exit(framework: &Framework) -> Result<(), Box<dyn Error>> {
12 framework
13 .data
14 .global
15 .get::<WatchHistory>()
16 .unwrap()
17 .exit_move();
18 let subscriptions = framework.data.global.get::<Subscriptions>().unwrap();
19 subscriptions.exit_move();
20 subscriptions.save()?;
21 framework.data.global.get::<Library>().unwrap().exit_move();
22
23 let home_dir = home_dir().unwrap();
24 let cache_path = home_dir.join(".cache/youtube-tui/");
25
26 if cache_path.exists() {
27 fs::remove_dir_all(cache_path)?;
28 }
29
30 Ok(())
31}