Commit 238cf89517
Unsigned
Layout: unified · split
src/config/commands.rs +8 −4
| @@ -101,7 +101,7 @@ fn video_default() -> Vec<HashMap<String, String>> { | ||
| 101 | 101 | vec![ |
| 102 | 102 | HashMap::from([( |
| 103 | 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 | 106 | HashMap::from([( |
| 107 | 107 | String::from("Play video"), |
| @@ -154,7 +154,7 @@ fn saved_video_default() -> Vec<HashMap<String, String>> { | ||
| 154 | 154 | vec![ |
| 155 | 155 | HashMap::from([( |
| 156 | 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 | 159 | HashMap::from([( |
| 160 | 160 | String::from("[Offline] Play saved file"), |
| @@ -208,7 +208,7 @@ fn playlist_default() -> Vec<HashMap<String, String>> { | ||
| 208 | 208 | HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]), |
| 209 | 209 | HashMap::from([( |
| 210 | 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 | 213 | HashMap::from([( |
| 214 | 214 | String::from("Play all (videos)"), |
| @@ -262,7 +262,7 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> { | ||
| 262 | 262 | HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]), |
| 263 | 263 | HashMap::from([( |
| 264 | 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 | 267 | HashMap::from([( |
| 268 | 268 | String::from("[Offline] Play all (videos)"), |
| @@ -313,6 +313,10 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> { | ||
| 313 | 313 | |
| 314 | 314 | fn channel_default() -> Vec<HashMap<String, String>> { |
| 315 | 315 | vec![ |
| 316 | HashMap::from([( | |
| 317 | String::from("Reload updated channel"), | |
| 318 | String::from("rmcache ${id} ;; channel ${id}"), | |
| 319 | )]), | |
| 316 | 320 | HashMap::from([( |
| 317 | 321 | String::from("Subscribe to channel"), |
| 318 | 322 | String::from("sync ${id}"), |
src/config/main.rs +4 −3
| @@ -125,7 +125,8 @@ impl Default for Limits { | ||
| 125 | 125 | pub enum Images { |
| 126 | 126 | r#None, |
| 127 | 127 | HalfBlocks, |
| 128 | Sixels, | |
| 128 | #[serde(alias = "Sixels")] | |
| 129 | Sixel, | |
| 129 | 130 | } |
| 130 | 131 | |
| 131 | 132 | impl Images { |
| @@ -134,7 +135,7 @@ impl Images { | ||
| 134 | 135 | } |
| 135 | 136 | |
| 136 | 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 | |
| 215 | 216 | const fn images_default() -> Images { |
| 216 | Images::Sixels | |
| 217 | Images::Sixel | |
| 217 | 218 | } |
| 218 | 219 | |
| 219 | 220 | const 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 | 52 | let cached_before = CACHED_BEFORE.get().unwrap(); |
| 53 | 53 | |
| 54 | 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 | 57 | // remove cache that no longer exists |
| 58 | 58 | for deleted in cached_before |
| @@ -63,6 +63,12 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> { | ||
| 63 | 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 | 72 | LocalStore::save_only(&cached_after); |
| 67 | 73 | |
| 68 | 74 | // let home_dir = home_dir().unwrap(); |
src/global/functions/download_all_images.rs +4 −2
| @@ -1,5 +1,5 @@ | ||
| 1 | use crate::global::structs::Item; | |
| 2 | use std::{error::Error, fs, io::Cursor, path::PathBuf, thread}; | |
| 1 | use crate::global::structs::{Item, LocalStore}; | |
| 2 | use std::{collections::HashSet, error::Error, fs, io::Cursor, path::PathBuf, sync::OnceLock, thread}; | |
| 3 | 3 | |
| 4 | 4 | pub struct DownloadRequest { |
| 5 | 5 | pub url: String, |
| @@ -51,10 +51,12 @@ pub fn download_all_images(downloads: Vec<Option<DownloadRequest>>) { | ||
| 51 | 51 | .join(".local/share/youtube-tui/thumbnails/"); |
| 52 | 52 | |
| 53 | 53 | downloads.into_iter().flatten().for_each(|req| { |
| 54 | LocalStore::add_image(req.id.clone()); | |
| 54 | 55 | let path = path.clone().join(req.id); |
| 55 | 56 | if path.exists() { |
| 56 | 57 | return; |
| 57 | 58 | } |
| 59 | ||
| 58 | 60 | thread::spawn(move || { |
| 59 | 61 | let _ = download_single(&req.url, path); |
| 60 | 62 | }); |
src/global/functions/run_command.rs +7 −5
| @@ -4,13 +4,10 @@ use crate::{ | ||
| 4 | 4 | load_configs, |
| 5 | 5 | }; |
| 6 | 6 | use crossterm::event::{KeyEvent, KeyModifiers}; |
| 7 | use home::home_dir; | |
| 7 | 8 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 8 | 9 | use 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 | |
| 14 | 11 | }; |
| 15 | 12 | use tui_additions::framework::Framework; |
| 16 | 13 | |
| @@ -152,6 +149,11 @@ pub fn run_single_command( | ||
| 152 | 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 | 157 | ["help"] => { |
| 156 | 158 | *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from( |
| 157 | 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 | |
| 1 | 7 | use std::{ |
| 2 | 8 | collections::{HashMap, HashSet}, |
| 3 | 9 | env::home_dir, |
| @@ -19,15 +25,24 @@ pub struct LocalRecord { | ||
| 19 | 25 | #[derive(Default)] |
| 20 | 26 | pub struct LocalStore { |
| 21 | 27 | info: HashMap<String, LocalRecord>, |
| 28 | downloaded_images: HashSet<String>, | |
| 22 | 29 | } |
| 23 | 30 | |
| 24 | 31 | impl LocalStore { |
| 32 | pub fn add_image(id: String) { | |
| 33 | unsafe { LOCALSTORE.get_mut() }.unwrap().downloaded_images.insert(id); | |
| 34 | } | |
| 35 | ||
| 25 | 36 | pub fn init() { |
| 26 | 37 | unsafe { |
| 27 | 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 | 46 | pub fn get_info(id: &str) -> Option<Item> { |
| 32 | 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 | 4 | global::{ |
| 5 | 5 | functions::*, |
| 6 | 6 | structs::*, |
| 7 | traits::{CollectionNoId, SearchProviderWrapper}, | |
| 7 | traits::{Collection, CollectionNoId, SearchProviderWrapper}, | |
| 8 | 8 | }, |
| 9 | 9 | }; |
| 10 | 10 | use ratatui::{ |
| @@ -602,7 +602,14 @@ impl FrameworkItem for ChannelDisplay { | ||
| 602 | 602 | |
| 603 | 603 | match page.r#type { |
| 604 | 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 | 613 | let commands = framework |
| 607 | 614 | .data |
| 608 | 615 | .global |
| @@ -613,7 +620,7 @@ impl FrameworkItem for ChannelDisplay { | ||
| 613 | 620 | |
| 614 | 621 | *self = Self::Main { |
| 615 | 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 | 624 | grid: Grid::new( |
| 618 | 625 | vec![Constraint::Percentage(60), Constraint::Percentage(40)], |
| 619 | 626 | vec![Constraint::Percentage(100)], |
| @@ -622,6 +629,10 @@ impl FrameworkItem for ChannelDisplay { | ||
| 622 | 629 | textlist: Self::new_textlist_with_map(commands.clone()), |
| 623 | 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 | 637 | ChannelDisplayPageType::Videos => { |
| 627 | 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 | 687 | set_envs( |
| 675 | 688 | self.inflate_load(mainconfig, framework.data.global.get::<Status>().unwrap()) |
| 676 | 689 | .into_iter(), |
src/items/singleitem.rs +1 −1
| @@ -797,7 +797,7 @@ impl FrameworkItem for SingleItem { | ||
| 797 | 797 | // push to watch history |
| 798 | 798 | let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap(); |
| 799 | 799 | watch_history.push(item)?; |
| 800 | watch_history.save()?; | |
| 800 | // watch_history.save()?; | |
| 801 | 801 | } |
| 802 | 802 | |
| 803 | 803 | let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); |