Commit 95700f4c6f
95700f4c6f2323693a22b08930f4bcc80f62c7f5
parent: dba33247c8
Unsigned
Siriusmart <sirius@siri.ws> · 2025-11-08 11:12 UTC
removed outdated cache and stuff?
Layout: unified · split
src/exit.rs
+34 −7
| @@ -1,13 +1,12 @@ |
| 1 | 1 | use home::home_dir; |
| 2 | | use std::{error::Error, fs}; |
| 2 | use std::{collections::HashSet, error::Error, fs}; |
| 3 | 3 | use tui_additions::framework::Framework; |
| 4 | 4 | |
| 5 | 5 | use crate::{ |
| 6 | | config::MainConfig, |
| 7 | | global::{ |
| 6 | CACHED_BEFORE, config::MainConfig, global::{ |
| 8 | 7 | structs::*, |
| 9 | 8 | traits::{Collection, CollectionNoId}, |
| 10 | | }, |
| 9 | } |
| 11 | 10 | }; |
| 12 | 11 | |
| 13 | 12 | /// function to run when the app ends |
| @@ -15,8 +14,8 @@ use crate::{ |
| 15 | 14 | // - move thumbnails of videos in watch history to `~/.local/share/youtube-tui/watch_history/thumbnails` |
| 16 | 15 | // - remove `~/.cache` |
| 17 | 16 | pub 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(); |
| 17 | let mut limits = framework.data.global.remove::<MainConfig>().unwrap().limits; |
| 18 | let mut watchhistory = framework.data.global.remove::<WatchHistory>().unwrap(); |
| 20 | 19 | watchhistory.trim(limits.watch_history); |
| 21 | 20 | // watchhistory.exit_move(); |
| 22 | 21 | let _ = watchhistory.save(); |
| @@ -27,9 +26,37 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> { |
| 27 | 26 | let searchhistory = framework.data.global.get_mut::<SearchHistory>().unwrap(); |
| 28 | 27 | searchhistory.trim(limits.search_history); |
| 29 | 28 | let _ = searchhistory.save(); |
| 30 | | let commandhistory = framework.data.global.get_mut::<CommandHistory>().unwrap(); |
| 29 | let mut commandhistory = framework.data.global.remove::<CommandHistory>().unwrap(); |
| 31 | 30 | commandhistory.trim(limits.commands_history); |
| 32 | 31 | let _ = commandhistory.save(); |
| 32 | let library = framework.data.global.get_mut::<Library>().unwrap(); |
| 33 | let _ = library.save(); |
| 34 | |
| 35 | let cached_after: HashSet<String> = HashSet::from_iter( |
| 36 | library |
| 37 | .items() |
| 38 | .iter() |
| 39 | .filter_map(|item| item.id()) |
| 40 | .map(str::to_string) |
| 41 | .chain( |
| 42 | watchhistory |
| 43 | .items() |
| 44 | .iter() |
| 45 | .filter_map(|item| item.id()) |
| 46 | .map(str::to_string), |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | let cached_before = CACHED_BEFORE.get().unwrap(); |
| 51 | |
| 52 | let info_path = home_dir().unwrap().join(".local/share/youtube-tui/info/"); |
| 53 | let thumbnail_path = home_dir().unwrap().join(".local/share/youtube-tui/info/"); |
| 54 | |
| 55 | // remove cache that no longer exists |
| 56 | for deleted in cached_before.iter().filter(|id| !cached_after.contains(*id)) { |
| 57 | let _ = fs::remove_file(info_path.join(deleted).with_extension("json")); |
| 58 | let _ = fs::remove_file(thumbnail_path.join(deleted)); |
| 59 | } |
| 33 | 60 | |
| 34 | 61 | // let home_dir = home_dir().unwrap(); |
| 35 | 62 | // let cache_path = home_dir.join(".cache/youtube-tui/"); |
src/global/functions/run_command.rs
−3
| @@ -126,7 +126,6 @@ pub fn run_single_command( |
| 126 | 126 | Some(item) => { |
| 127 | 127 | let library = framework.data.global.get_mut::<Library>().unwrap(); |
| 128 | 128 | let _ = library.push(item); |
| 129 | | let _ = library.save(); |
| 130 | 129 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 131 | 130 | Message::Success(String::from("Bookmark added")) |
| 132 | 131 | } |
| @@ -139,7 +138,6 @@ pub fn run_single_command( |
| 139 | 138 | let library = framework.data.global.get_mut::<Library>().unwrap(); |
| 140 | 139 | |
| 141 | 140 | if library.remove(id) { |
| 142 | | let _ = library.save(); |
| 143 | 141 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 144 | 142 | Message::Success(String::from("Bookmark removed")) |
| 145 | 143 | } else { |
| @@ -150,7 +148,6 @@ pub fn run_single_command( |
| 150 | 148 | ["togglemark", id] => { |
| 151 | 149 | let library = framework.data.global.get_mut::<Library>().unwrap(); |
| 152 | 150 | if library.remove(id) { |
| 153 | | let _ = library.save(); |
| 154 | 151 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 155 | 152 | Message::Success(String::from("Bookmark removed")) |
| 156 | 153 | } else { |
src/global/structs/tasks.rs
+10 −10
| @@ -194,16 +194,16 @@ impl TaskQueue { |
| 194 | 194 | |
| 195 | 195 | Self::render_force_clear(framework, terminal)?; |
| 196 | 196 | // reload simply runs `.load()` on all items |
| 197 | | *framework.data.global.get_mut::<Message>().unwrap() = |
| 198 | | Message::Message(String::from("Reloading page...")); |
| 199 | | Self::render_force_clear(framework, terminal)?; |
| 200 | | *framework.data.global.get_mut::<Message>().unwrap() = |
| 201 | | if let Err(e) = framework.load() { |
| 202 | | Message::Error(e.to_string()) |
| 203 | | } else { |
| 204 | | Message::None |
| 205 | | }; |
| 206 | | self.render = RenderTask::All; |
| 197 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 198 | Message::Message(String::from("Reloading page...")); |
| 199 | Self::render_force_clear(framework, terminal)?; |
| 200 | *framework.data.global.get_mut::<Message>().unwrap() = if let Err(e) = framework.load() |
| 201 | { |
| 202 | Message::Error(e.to_string()) |
| 203 | } else { |
| 204 | Message::None |
| 205 | }; |
| 206 | self.render = RenderTask::All; |
| 207 | 207 | |
| 208 | 208 | let status = framework.data.global.get_mut::<Status>().unwrap(); |
| 209 | 209 | status.provider_updated = true; |
src/global/traits/collection.rs
+5 −2
| @@ -58,10 +58,13 @@ where |
| 58 | 58 | // removes duplicates and place them on top (if exists) |
| 59 | 59 | let id = match item.id() { |
| 60 | 60 | Some(t) => t, |
| 61 | | None => return Ok(()) // TODO make error message |
| 61 | None => return Ok(()), // TODO make error message |
| 62 | 62 | }; |
| 63 | 63 | |
| 64 | | let info = home_dir().unwrap().join(".local/share/youtube-tui/info/").join(format!("{id}.json")); |
| 64 | let info = home_dir() |
| 65 | .unwrap() |
| 66 | .join(".local/share/youtube-tui/info/") |
| 67 | .join(format!("{id}.json")); |
| 65 | 68 | |
| 66 | 69 | if let Some(index) = self |
| 67 | 70 | .items_mut() |
src/init.rs
+23 −5
| @@ -5,6 +5,7 @@ use crate::{ |
| 5 | 5 | use home::home_dir; |
| 6 | 6 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 7 | 7 | use std::{ |
| 8 | collections::HashSet, |
| 8 | 9 | error::Error, |
| 9 | 10 | fs::{self}, |
| 10 | 11 | io::Stdout, |
| @@ -15,6 +16,7 @@ use tui_additions::framework::{Framework, FrameworkClean}; |
| 15 | 16 | |
| 16 | 17 | pub static mut MAIN_CONFIG: OnceLock<MainConfig> = OnceLock::new(); |
| 17 | 18 | pub static RUNTIME: OnceLock<Runtime> = OnceLock::new(); |
| 19 | pub static CACHED_BEFORE: OnceLock<HashSet<String>> = OnceLock::new(); |
| 18 | 20 | |
| 19 | 21 | /// app to run before the app starts |
| 20 | 22 | // init tasks: |
| @@ -53,6 +55,25 @@ pub fn init( |
| 53 | 55 | |
| 54 | 56 | load_configs(&mut framework.split_clean().0)?; |
| 55 | 57 | |
| 58 | let library = Library::load(); |
| 59 | let watchhistory = WatchHistory::load(); |
| 60 | |
| 61 | let cached_set: HashSet<String> = HashSet::from_iter( |
| 62 | library |
| 63 | .items() |
| 64 | .iter() |
| 65 | .filter_map(|item| item.id()) |
| 66 | .map(str::to_string) |
| 67 | .chain( |
| 68 | watchhistory |
| 69 | .items() |
| 70 | .iter() |
| 71 | .filter_map(|item| item.id()) |
| 72 | .map(str::to_string), |
| 73 | ), |
| 74 | ); |
| 75 | CACHED_BEFORE.set(cached_set).unwrap(); |
| 76 | |
| 56 | 77 | framework |
| 57 | 78 | .data |
| 58 | 79 | .global |
| @@ -61,10 +82,7 @@ pub fn init( |
| 61 | 82 | .data |
| 62 | 83 | .global |
| 63 | 84 | .insert::<SearchHistory>(SearchHistory::load()); |
| 64 | | framework |
| 65 | | .data |
| 66 | | .global |
| 67 | | .insert::<WatchHistory>(WatchHistory::load()); |
| 85 | framework.data.global.insert::<WatchHistory>(watchhistory); |
| 68 | 86 | framework |
| 69 | 87 | .data |
| 70 | 88 | .global |
| @@ -73,7 +91,7 @@ pub fn init( |
| 73 | 91 | .data |
| 74 | 92 | .global |
| 75 | 93 | .insert::<Subscriptions>(Subscriptions::load()); |
| 76 | | framework.data.global.insert::<Library>(Library::load()); |
| 94 | framework.data.global.insert::<Library>(library); |
| 77 | 95 | framework.data.global.insert::<Message>(Message::None); |
| 78 | 96 | |
| 79 | 97 | framework.data.global.insert::<Status>(Status { |