Commit 238cf89517

238cf895170930ad8736c8b39071e7e98f5a9895

parent: 5ad5070bed

Unsigned

Siriusmart <sirius@siri.ws> · 2025-11-08 12:13 UTC

correctly removing thumbnilas that are no longer needed

Layout: unified · split

src/config/commands.rs +8 −4
@@ -101,7 +101,7 @@ fn video_default() -> Vec<HashMap<String, String>> {
101101 vec![
102102 HashMap::from([(
103103 String::from("Reload updated video"),
104 String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
104 String::from("rmcache ${id} ;; video ${id}"),
105105 )]),
106106 HashMap::from([(
107107 String::from("Play video"),
@@ -154,7 +154,7 @@ fn saved_video_default() -> Vec<HashMap<String, String>> {
154154 vec![
155155 HashMap::from([(
156156 String::from("Reload updated video"),
157 String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
157 String::from("rmcache ${id} ;; video ${id}"),
158158 )]),
159159 HashMap::from([(
160160 String::from("[Offline] Play saved file"),
@@ -208,7 +208,7 @@ fn playlist_default() -> Vec<HashMap<String, String>> {
208208 HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
209209 HashMap::from([(
210210 String::from("Reload updated playlist"),
211 String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
211 String::from("rmcache ${id} ;; playlist ${id}"),
212212 )]),
213213 HashMap::from([(
214214 String::from("Play all (videos)"),
@@ -262,7 +262,7 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> {
262262 HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
263263 HashMap::from([(
264264 String::from("Reload updated playlist"),
265 String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
265 String::from("rmcache ${id} ;; playlist ${id}"),
266266 )]),
267267 HashMap::from([(
268268 String::from("[Offline] Play all (videos)"),
@@ -313,6 +313,10 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> {
313313
314314fn channel_default() -> Vec<HashMap<String, String>> {
315315 vec![
316 HashMap::from([(
317 String::from("Reload updated channel"),
318 String::from("rmcache ${id} ;; channel ${id}"),
319 )]),
316320 HashMap::from([(
317321 String::from("Subscribe to channel"),
318322 String::from("sync ${id}"),
src/config/main.rs +4 −3
@@ -125,7 +125,8 @@ impl Default for Limits {
125125pub enum Images {
126126 r#None,
127127 HalfBlocks,
128 Sixels,
128 #[serde(alias = "Sixels")]
129 Sixel,
129130}
130131
131132impl Images {
@@ -134,7 +135,7 @@ impl Images {
134135 }
135136
136137 pub fn use_sixels(&self) -> bool {
137 self == &Self::Sixels
138 self == &Self::Sixel
138139 }
139140}
140141
@@ -213,7 +214,7 @@ fn message_bar_default_default() -> String {
213214}
214215
215216const fn images_default() -> Images {
216 Images::Sixels
217 Images::Sixel
217218}
218219
219220const fn image_index_default() -> usize {
src/exit.rs +7 −1
@@ -52,7 +52,7 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
5252 let cached_before = CACHED_BEFORE.get().unwrap();
5353
5454 let info_path = home_dir().unwrap().join(".local/share/youtube-tui/info/");
55 let thumbnail_path = home_dir().unwrap().join(".local/share/youtube-tui/info/");
55 let thumbnail_path = home_dir().unwrap().join(".local/share/youtube-tui/thumbnails/");
5656
5757 // remove cache that no longer exists
5858 for deleted in cached_before
@@ -63,6 +63,12 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
6363 let _ = fs::remove_file(thumbnail_path.join(deleted));
6464 }
6565
66 for image_id in LocalStore::list_downloaded_images() {
67 if !cached_after.contains(image_id) {
68 let _ = fs::remove_file(thumbnail_path.join(image_id));
69 }
70 }
71
6672 LocalStore::save_only(&cached_after);
6773
6874 // let home_dir = home_dir().unwrap();
src/global/functions/download_all_images.rs +4 −2
@@ -1,5 +1,5 @@
1use crate::global::structs::Item;
2use std::{error::Error, fs, io::Cursor, path::PathBuf, thread};
1use crate::global::structs::{Item, LocalStore};
2use std::{collections::HashSet, error::Error, fs, io::Cursor, path::PathBuf, sync::OnceLock, thread};
33
44pub struct DownloadRequest {
55 pub url: String,
@@ -51,10 +51,12 @@ pub fn download_all_images(downloads: Vec<Option<DownloadRequest>>) {
5151 .join(".local/share/youtube-tui/thumbnails/");
5252
5353 downloads.into_iter().flatten().for_each(|req| {
54 LocalStore::add_image(req.id.clone());
5455 let path = path.clone().join(req.id);
5556 if path.exists() {
5657 return;
5758 }
59
5860 thread::spawn(move || {
5961 let _ = download_single(&req.url, path);
6062 });
src/global/functions/run_command.rs +7 −5
@@ -4,13 +4,10 @@ use crate::{
44 load_configs,
55};
66use crossterm::event::{KeyEvent, KeyModifiers};
7use home::home_dir;
78use ratatui::{backend::CrosstermBackend, Terminal};
89use std::{
9 env,
10 error::Error,
11 io::Stdout,
12 process::{Command, Stdio},
13 sync::Arc,
10 env, error::Error, fs, io::Stdout, process::{Command, Stdio}, sync::Arc
1411};
1512use tui_additions::framework::Framework;
1613
@@ -152,6 +149,11 @@ pub fn run_single_command(
152149 run_single_command(&["bookmark", id], framework, terminal);
153150 }
154151 }
152 ["rmcache", id] => {
153 LocalStore::rm_cache(id);
154 let _ = fs::remove_file(home_dir().unwrap().join(".local/share/youtube-tui/info/").join(id).with_extension("json"));
155 let _ = fs::remove_file(home_dir().unwrap().join(".local/share/youtube-tui/thumbnails/").join(id));
156 }
155157 ["help"] => {
156158 *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from(
157159 "Avaliable commands can be viewed by running `youtube-tui help` in terminal",
src/global/structs/localstore.rs +19
@@ -1,3 +1,9 @@
1// currently 3 copies of the same item is stored in ram
2// - in localstore
3// - in watch history
4// - in the actual state
5//
6// this needs to change
17use std::{
28 collections::{HashMap, HashSet},
39 env::home_dir,
@@ -19,15 +25,24 @@ pub struct LocalRecord {
1925#[derive(Default)]
2026pub struct LocalStore {
2127 info: HashMap<String, LocalRecord>,
28 downloaded_images: HashSet<String>,
2229}
2330
2431impl LocalStore {
32 pub fn add_image(id: String) {
33 unsafe { LOCALSTORE.get_mut() }.unwrap().downloaded_images.insert(id);
34 }
35
2536 pub fn init() {
2637 unsafe {
2738 let _ = LOCALSTORE.set(Self::default());
2839 }
2940 }
3041
42 pub fn rm_cache(id: &str) {
43 unsafe { LOCALSTORE.get_mut() }.unwrap().info.remove(id);
44 }
45
3146 pub fn get_info(id: &str) -> Option<Item> {
3247 let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap();
3348
@@ -72,4 +87,8 @@ impl LocalStore {
7287 }
7388 }
7489 }
90
91 pub fn list_downloaded_images() -> &'static HashSet<String> {
92 &unsafe { LOCALSTORE.get() }.unwrap().downloaded_images
93 }
7594}
src/items/channeldisplay.rs +16 −3
@@ -4,7 +4,7 @@ use crate::{
44 global::{
55 functions::*,
66 structs::*,
7 traits::{CollectionNoId, SearchProviderWrapper},
7 traits::{Collection, CollectionNoId, SearchProviderWrapper},
88 },
99};
1010use ratatui::{
@@ -602,7 +602,14 @@ impl FrameworkItem for ChannelDisplay {
602602
603603 match page.r#type {
604604 ChannelDisplayPageType::Main => {
605 let channel = load_channel(&page.id, mainconfig)?;
605 let (channel, is_new) = if let Some(item) = LocalStore::get_info(&page.id) {
606 (item, false)
607 } else {
608 (load_channel(&page.id, mainconfig)?, true)
609 };
610
611 LocalStore::set_info(page_id.clone(), channel.clone(), is_new);
612
606613 let commands = framework
607614 .data
608615 .global
@@ -613,7 +620,7 @@ impl FrameworkItem for ChannelDisplay {
613620
614621 *self = Self::Main {
615622 iteminfo: Box::new(ItemInfo::new(Some(channel.clone()))),
616 channel: Box::new(channel),
623 channel: Box::new(channel.clone()), // TODO the clone seem rather wasteful here
617624 grid: Grid::new(
618625 vec![Constraint::Percentage(60), Constraint::Percentage(40)],
619626 vec![Constraint::Percentage(100)],
@@ -622,6 +629,10 @@ impl FrameworkItem for ChannelDisplay {
622629 textlist: Self::new_textlist_with_map(commands.clone()),
623630 commands,
624631 };
632
633 let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap();
634 watch_history.push(channel)?;
635
625636 }
626637 ChannelDisplayPageType::Videos => {
627638 let videos = SearchProviderWrapper::channel_videos(&page.id)?
@@ -671,6 +682,8 @@ impl FrameworkItem for ChannelDisplay {
671682 }
672683 }
673684
685 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
686
674687 set_envs(
675688 self.inflate_load(mainconfig, framework.data.global.get::<Status>().unwrap())
676689 .into_iter(),
src/items/singleitem.rs +1 −1
@@ -797,7 +797,7 @@ impl FrameworkItem for SingleItem {
797797 // push to watch history
798798 let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap();
799799 watch_history.push(item)?;
800 watch_history.save()?;
800 // watch_history.save()?;
801801 }
802802
803803 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();