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>> {
101 vec![ 101 vec![
102 HashMap::from([( 102 HashMap::from([(
103 String::from("Reload updated video"), 103 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}"),
105 )]), 105 )]),
106 HashMap::from([( 106 HashMap::from([(
107 String::from("Play video"), 107 String::from("Play video"),
@@ -154,7 +154,7 @@ fn saved_video_default() -> Vec<HashMap<String, String>> {
154 vec![ 154 vec![
155 HashMap::from([( 155 HashMap::from([(
156 String::from("Reload updated video"), 156 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}"),
158 )]), 158 )]),
159 HashMap::from([( 159 HashMap::from([(
160 String::from("[Offline] Play saved file"), 160 String::from("[Offline] Play saved file"),
@@ -208,7 +208,7 @@ fn playlist_default() -> Vec<HashMap<String, String>> {
208 HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]), 208 HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
209 HashMap::from([( 209 HashMap::from([(
210 String::from("Reload updated playlist"), 210 String::from("Reload updated playlist"),
211 String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"), 211 String::from("rmcache ${id} ;; playlist ${id}"),
212 )]), 212 )]),
213 HashMap::from([( 213 HashMap::from([(
214 String::from("Play all (videos)"), 214 String::from("Play all (videos)"),
@@ -262,7 +262,7 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> {
262 HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]), 262 HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
263 HashMap::from([( 263 HashMap::from([(
264 String::from("Reload updated playlist"), 264 String::from("Reload updated playlist"),
265 String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"), 265 String::from("rmcache ${id} ;; playlist ${id}"),
266 )]), 266 )]),
267 HashMap::from([( 267 HashMap::from([(
268 String::from("[Offline] Play all (videos)"), 268 String::from("[Offline] Play all (videos)"),
@@ -313,6 +313,10 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> {
313 313
314fn channel_default() -> Vec<HashMap<String, String>> { 314fn channel_default() -> Vec<HashMap<String, String>> {
315 vec![ 315 vec![
316 HashMap::from([(
317 String::from("Reload updated channel"),
318 String::from("rmcache ${id} ;; channel ${id}"),
319 )]),
316 HashMap::from([( 320 HashMap::from([(
317 String::from("Subscribe to channel"), 321 String::from("Subscribe to channel"),
318 String::from("sync ${id}"), 322 String::from("sync ${id}"),
src/config/main.rs +4 −3
@@ -125,7 +125,8 @@ impl Default for Limits {
125pub enum Images { 125pub enum Images {
126 r#None, 126 r#None,
127 HalfBlocks, 127 HalfBlocks,
128 Sixels, 128 #[serde(alias = "Sixels")]
129 Sixel,
129} 130}
130 131
131impl Images { 132impl Images {
@@ -134,7 +135,7 @@ impl Images {
134 } 135 }
135 136
136 pub fn use_sixels(&self) -> bool { 137 pub fn use_sixels(&self) -> bool {
137 self == &Self::Sixels 138 self == &Self::Sixel
138 } 139 }
139} 140}
140 141
@@ -213,7 +214,7 @@ fn message_bar_default_default() -> String {
213} 214}
214 215
215const fn images_default() -> Images { 216const fn images_default() -> Images {
216 Images::Sixels 217 Images::Sixel
217} 218}
218 219
219const fn image_index_default() -> usize { 220const fn image_index_default() -> usize {
src/exit.rs +7 −1
@@ -52,7 +52,7 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
52 let cached_before = CACHED_BEFORE.get().unwrap(); 52 let cached_before = CACHED_BEFORE.get().unwrap();
53 53
54 let info_path = home_dir().unwrap().join(".local/share/youtube-tui/info/"); 54 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/");
56 56
57 // remove cache that no longer exists 57 // remove cache that no longer exists
58 for deleted in cached_before 58 for deleted in cached_before
@@ -63,6 +63,12 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
63 let _ = fs::remove_file(thumbnail_path.join(deleted)); 63 let _ = fs::remove_file(thumbnail_path.join(deleted));
64 } 64 }
65 65
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
66 LocalStore::save_only(&cached_after); 72 LocalStore::save_only(&cached_after);
67 73
68 // let home_dir = home_dir().unwrap(); 74 // let home_dir = home_dir().unwrap();
src/global/functions/download_all_images.rs +4 −2
@@ -1,5 +1,5 @@
1use crate::global::structs::Item; 1use crate::global::structs::{Item, LocalStore};
2use std::{error::Error, fs, io::Cursor, path::PathBuf, thread}; 2use std::{collections::HashSet, error::Error, fs, io::Cursor, path::PathBuf, sync::OnceLock, thread};
3 3
4pub struct DownloadRequest { 4pub struct DownloadRequest {
5 pub url: String, 5 pub url: String,
@@ -51,10 +51,12 @@ pub fn download_all_images(downloads: Vec<Option<DownloadRequest>>) {
51 .join(".local/share/youtube-tui/thumbnails/"); 51 .join(".local/share/youtube-tui/thumbnails/");
52 52
53 downloads.into_iter().flatten().for_each(|req| { 53 downloads.into_iter().flatten().for_each(|req| {
54 LocalStore::add_image(req.id.clone());
54 let path = path.clone().join(req.id); 55 let path = path.clone().join(req.id);
55 if path.exists() { 56 if path.exists() {
56 return; 57 return;
57 } 58 }
59
58 thread::spawn(move || { 60 thread::spawn(move || {
59 let _ = download_single(&req.url, path); 61 let _ = download_single(&req.url, path);
60 }); 62 });
src/global/functions/run_command.rs +7 −5
@@ -4,13 +4,10 @@ use crate::{
4 load_configs, 4 load_configs,
5}; 5};
6use crossterm::event::{KeyEvent, KeyModifiers}; 6use crossterm::event::{KeyEvent, KeyModifiers};
7use home::home_dir;
7use ratatui::{backend::CrosstermBackend, Terminal}; 8use ratatui::{backend::CrosstermBackend, Terminal};
8use std::{ 9use std::{
9 env, 10 env, error::Error, fs, io::Stdout, process::{Command, Stdio}, sync::Arc
10 error::Error,
11 io::Stdout,
12 process::{Command, Stdio},
13 sync::Arc,
14}; 11};
15use tui_additions::framework::Framework; 12use tui_additions::framework::Framework;
16 13
@@ -152,6 +149,11 @@ pub fn run_single_command(
152 run_single_command(&["bookmark", id], framework, terminal); 149 run_single_command(&["bookmark", id], framework, terminal);
153 } 150 }
154 } 151 }
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 }
155 ["help"] => { 157 ["help"] => {
156 *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from( 158 *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from(
157 "Avaliable commands can be viewed by running `youtube-tui help` in terminal", 159 "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
1use std::{ 7use std::{
2 collections::{HashMap, HashSet}, 8 collections::{HashMap, HashSet},
3 env::home_dir, 9 env::home_dir,
@@ -19,15 +25,24 @@ pub struct LocalRecord {
19#[derive(Default)] 25#[derive(Default)]
20pub struct LocalStore { 26pub struct LocalStore {
21 info: HashMap<String, LocalRecord>, 27 info: HashMap<String, LocalRecord>,
28 downloaded_images: HashSet<String>,
22} 29}
23 30
24impl LocalStore { 31impl LocalStore {
32 pub fn add_image(id: String) {
33 unsafe { LOCALSTORE.get_mut() }.unwrap().downloaded_images.insert(id);
34 }
35
25 pub fn init() { 36 pub fn init() {
26 unsafe { 37 unsafe {
27 let _ = LOCALSTORE.set(Self::default()); 38 let _ = LOCALSTORE.set(Self::default());
28 } 39 }
29 } 40 }
30 41
42 pub fn rm_cache(id: &str) {
43 unsafe { LOCALSTORE.get_mut() }.unwrap().info.remove(id);
44 }
45
31 pub fn get_info(id: &str) -> Option<Item> { 46 pub fn get_info(id: &str) -> Option<Item> {
32 let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap(); 47 let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap();
33 48
@@ -72,4 +87,8 @@ impl LocalStore {
72 } 87 }
73 } 88 }
74 } 89 }
90
91 pub fn list_downloaded_images() -> &'static HashSet<String> {
92 &unsafe { LOCALSTORE.get() }.unwrap().downloaded_images
93 }
75} 94}
src/items/channeldisplay.rs +16 −3
@@ -4,7 +4,7 @@ use crate::{
4 global::{ 4 global::{
5 functions::*, 5 functions::*,
6 structs::*, 6 structs::*,
7 traits::{CollectionNoId, SearchProviderWrapper}, 7 traits::{Collection, CollectionNoId, SearchProviderWrapper},
8 }, 8 },
9}; 9};
10use ratatui::{ 10use ratatui::{
@@ -602,7 +602,14 @@ impl FrameworkItem for ChannelDisplay {
602 602
603 match page.r#type { 603 match page.r#type {
604 ChannelDisplayPageType::Main => { 604 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
606 let commands = framework 613 let commands = framework
607 .data 614 .data
608 .global 615 .global
@@ -613,7 +620,7 @@ impl FrameworkItem for ChannelDisplay {
613 620
614 *self = Self::Main { 621 *self = Self::Main {
615 iteminfo: Box::new(ItemInfo::new(Some(channel.clone()))), 622 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
617 grid: Grid::new( 624 grid: Grid::new(
618 vec![Constraint::Percentage(60), Constraint::Percentage(40)], 625 vec![Constraint::Percentage(60), Constraint::Percentage(40)],
619 vec![Constraint::Percentage(100)], 626 vec![Constraint::Percentage(100)],
@@ -622,6 +629,10 @@ impl FrameworkItem for ChannelDisplay {
622 textlist: Self::new_textlist_with_map(commands.clone()), 629 textlist: Self::new_textlist_with_map(commands.clone()),
623 commands, 630 commands,
624 }; 631 };
632
633 let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap();
634 watch_history.push(channel)?;
635
625 } 636 }
626 ChannelDisplayPageType::Videos => { 637 ChannelDisplayPageType::Videos => {
627 let videos = SearchProviderWrapper::channel_videos(&page.id)? 638 let videos = SearchProviderWrapper::channel_videos(&page.id)?
@@ -671,6 +682,8 @@ impl FrameworkItem for ChannelDisplay {
671 } 682 }
672 } 683 }
673 684
685 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
686
674 set_envs( 687 set_envs(
675 self.inflate_load(mainconfig, framework.data.global.get::<Status>().unwrap()) 688 self.inflate_load(mainconfig, framework.data.global.get::<Status>().unwrap())
676 .into_iter(), 689 .into_iter(),
src/items/singleitem.rs +1 −1
@@ -797,7 +797,7 @@ impl FrameworkItem for SingleItem {
797 // push to watch history 797 // push to watch history
798 let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap(); 798 let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap();
799 watch_history.push(item)?; 799 watch_history.push(item)?;
800 watch_history.save()?; 800 // watch_history.save()?;
801 } 801 }
802 802
803 let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); 803 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();