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