Commit 1361b33d10
1361b33d10e308e9c67165418f8dfa9761b0b2a0
parent: 238cf89517
Unsigned
Siriusmart <sirius@siri.ws> · 2025-11-08 12:30 UTC
channel caching done
Layout: unified · split
docs/src/config/commands.md
+1 −1
| @@ -7,7 +7,7 @@ The commands config file decides what options (that will run a certain command o |
| 7 | ```yaml |
7 | ```yaml |
| 8 | launch_command: loadpage library ;; flush ;; history clear ;; key Esc 0 ;; key Up 0 ;; key Up 0 ;; key Left 0 ;; key Enter 0 # the key commands select the searchbar on launch |
8 | launch_command: loadpage library ;; flush ;; history clear ;; key Esc 0 ;; key Up 0 ;; key Up 0 ;; key Left 0 ;; key Enter 0 # the key commands select the searchbar on launch |
| 9 | video: |
9 | video: |
| 10 | - Reload updated video: run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id} # remove the cached info first, then reload the page |
10 | - Reload updated video: rmcache ${id} ;; video ${id} # remove the cached info first, then reload the page |
| 11 | - Play video: parrun ${video-player} '${embed-url}' |
11 | - Play video: parrun ${video-player} '${embed-url}' |
| 12 | - Play audio: mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile '${embed-url}' ;; echo mpv Player started |
12 | - Play audio: mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile '${embed-url}' ;; echo mpv Player started |
| 13 | - Play audio (loop): mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile '${embed-url}' ;; echo mpv Player started |
13 | - Play audio (loop): mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile '${embed-url}' ;; echo mpv Player started |
src/exit.rs
+3 −1
| @@ -52,7 +52,9 @@ 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/thumbnails/"); |
55 | let thumbnail_path = home_dir() |
| |
56 | .unwrap() |
| |
57 | .join(".local/share/youtube-tui/thumbnails/"); |
| 56 | |
58 | |
| 57 | // remove cache that no longer exists |
59 | // remove cache that no longer exists |
| 58 | for deleted in cached_before |
60 | for deleted in cached_before |
src/global/functions/download_all_images.rs
+1 −1
| @@ -1,5 +1,5 @@ |
| 1 | use crate::global::structs::{Item, LocalStore}; |
1 | use crate::global::structs::{Item, LocalStore}; |
| 2 | use std::{collections::HashSet, error::Error, fs, io::Cursor, path::PathBuf, sync::OnceLock, thread}; |
2 | use std::{error::Error, fs, io::Cursor, path::PathBuf, thread}; |
| 3 | |
3 | |
| 4 | pub struct DownloadRequest { |
4 | pub struct DownloadRequest { |
| 5 | pub url: String, |
5 | pub url: String, |
src/global/functions/run_command.rs
+27 −4
| @@ -7,7 +7,12 @@ use crossterm::event::{KeyEvent, KeyModifiers}; |
| 7 | use home::home_dir; |
7 | use home::home_dir; |
| 8 | use ratatui::{backend::CrosstermBackend, Terminal}; |
8 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 9 | use std::{ |
9 | use std::{ |
| 10 | env, error::Error, fs, io::Stdout, process::{Command, Stdio}, sync::Arc |
10 | env, |
| |
11 | error::Error, |
| |
12 | fs, |
| |
13 | io::Stdout, |
| |
14 | process::{Command, Stdio}, |
| |
15 | sync::Arc, |
| 11 | }; |
16 | }; |
| 12 | use tui_additions::framework::Framework; |
17 | use tui_additions::framework::Framework; |
| 13 | |
18 | |
| @@ -150,9 +155,27 @@ pub fn run_single_command( |
| 150 | } |
155 | } |
| 151 | } |
156 | } |
| 152 | ["rmcache", id] => { |
157 | ["rmcache", id] => { |
| 153 | LocalStore::rm_cache(id); |
158 | let res = LocalStore::rm_cache(id); |
| 154 | let _ = fs::remove_file(home_dir().unwrap().join(".local/share/youtube-tui/info/").join(id).with_extension("json")); |
159 | let _ = fs::remove_file( |
| 155 | let _ = fs::remove_file(home_dir().unwrap().join(".local/share/youtube-tui/thumbnails/").join(id)); |
160 | home_dir() |
| |
161 | .unwrap() |
| |
162 | .join(".local/share/youtube-tui/info/") |
| |
163 | .join(id) |
| |
164 | .with_extension("json"), |
| |
165 | ); |
| |
166 | let _ = fs::remove_file( |
| |
167 | home_dir() |
| |
168 | .unwrap() |
| |
169 | .join(".local/share/youtube-tui/thumbnails/") |
| |
170 | .join(id), |
| |
171 | ); |
| |
172 | if res { |
| |
173 | *framework.data.global.get_mut::<Message>().unwrap() = |
| |
174 | Message::Success(String::from("Cache cleared")) |
| |
175 | } else { |
| |
176 | *framework.data.global.get_mut::<Message>().unwrap() = |
| |
177 | Message::Error(String::from("Item not in cache, nothing changed")) |
| |
178 | } |
| 156 | } |
179 | } |
| 157 | ["help"] => { |
180 | ["help"] => { |
| 158 | *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from( |
181 | *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from( |
src/global/functions/singleitem_load.rs
+1 −18
| @@ -2,8 +2,7 @@ use crate::{ |
| 2 | config::MainConfig, |
2 | config::MainConfig, |
| 3 | global::{functions::download_all_images, structs::Item, traits::SearchProviderWrapper}, |
3 | global::{functions::download_all_images, structs::Item, traits::SearchProviderWrapper}, |
| 4 | }; |
4 | }; |
| 5 | use home::home_dir; |
5 | use std::error::Error; |
| 6 | use std::{error::Error, fs}; |
| |
| 7 | |
6 | |
| 8 | pub fn load_playlist(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> { |
7 | pub fn load_playlist(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> { |
| 9 | let playlist = |
8 | let playlist = |
| @@ -31,25 +30,9 @@ pub fn load_video(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Err |
| 31 | } |
30 | } |
| 32 | |
31 | |
| 33 | pub fn load_channel(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> { |
32 | pub fn load_channel(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> { |
| 34 | let home_dir = home_dir().unwrap(); |
| |
| 35 | let cache_path = home_dir.join(format!(".local/share/youtube-tui/channels/{id}.json")); |
| |
| 36 | |
| |
| 37 | if cache_path.exists() { |
| |
| 38 | if let Ok(content) = fs::read_to_string(&cache_path) { |
| |
| 39 | if let Ok(item) = serde_json::from_str::<Item>(&content) { |
| |
| 40 | return Ok(item); |
| |
| 41 | } |
| |
| 42 | } |
| |
| 43 | } |
| |
| 44 | |
| |
| 45 | let channel = |
33 | let channel = |
| 46 | Item::from_full_channel(SearchProviderWrapper::channel(id)?, mainconfig.image_index); |
34 | Item::from_full_channel(SearchProviderWrapper::channel(id)?, mainconfig.image_index); |
| 47 | |
35 | |
| 48 | if let Some(parent) = cache_path.parent() { |
| |
| 49 | fs::create_dir_all(parent)?; |
| |
| 50 | } |
| |
| 51 | fs::write(&cache_path, serde_json::to_string(&channel)?)?; |
| |
| 52 | |
| |
| 53 | if mainconfig.images.display() { |
36 | if mainconfig.images.display() { |
| 54 | download_all_images(vec![(&channel).into()]); |
37 | download_all_images(vec![(&channel).into()]); |
| 55 | } |
38 | } |
src/global/structs/history.rs
+2
| @@ -74,6 +74,7 @@ impl CollectionNoId<String> for CommandHistory { |
| 74 | } |
74 | } |
| 75 | } |
75 | } |
| 76 | |
76 | |
| |
77 | /* |
| 77 | #[derive(Clone, Default, Serialize, Deserialize)] |
78 | #[derive(Clone, Default, Serialize, Deserialize)] |
| 78 | pub struct ChannelHistory(pub Vec<String>); |
79 | pub struct ChannelHistory(pub Vec<String>); |
| 79 | |
80 | |
| @@ -96,3 +97,4 @@ impl CollectionNoId<String> for ChannelHistory { |
| 96 | Self(items) |
97 | Self(items) |
| 97 | } |
98 | } |
| 98 | } |
99 | } |
| |
100 | */ |
src/global/structs/localstore.rs
+18 −3
| @@ -30,7 +30,10 @@ pub struct LocalStore { |
| 30 | |
30 | |
| 31 | impl LocalStore { |
31 | impl LocalStore { |
| 32 | pub fn add_image(id: String) { |
32 | pub fn add_image(id: String) { |
| 33 | unsafe { LOCALSTORE.get_mut() }.unwrap().downloaded_images.insert(id); |
33 | unsafe { LOCALSTORE.get_mut() } |
| |
34 | .unwrap() |
| |
35 | .downloaded_images |
| |
36 | .insert(id); |
| 34 | } |
37 | } |
| 35 | |
38 | |
| 36 | pub fn init() { |
39 | pub fn init() { |
| @@ -39,8 +42,20 @@ impl LocalStore { |
| 39 | } |
42 | } |
| 40 | } |
43 | } |
| 41 | |
44 | |
| 42 | pub fn rm_cache(id: &str) { |
45 | pub fn rm_cache(id: &str) -> bool { |
| 43 | unsafe { LOCALSTORE.get_mut() }.unwrap().info.remove(id); |
46 | let res = unsafe { LOCALSTORE.get_mut() }.unwrap().info.remove(id); |
| |
47 | unsafe { LOCALSTORE.get_mut() } |
| |
48 | .unwrap() |
| |
49 | .downloaded_images |
| |
50 | .remove(id); |
| |
51 | let info_path = home_dir().unwrap().join(".local/share/youtube-tui/info/"); |
| |
52 | let thumbnail_path = home_dir() |
| |
53 | .unwrap() |
| |
54 | .join(".local/share/youtube-tui/thumbnails/"); |
| |
55 | let _ = fs::remove_file(info_path.join(id).with_extension("json")); |
| |
56 | let _ = fs::remove_file(thumbnail_path.join(id)); |
| |
57 | |
| |
58 | res.is_some() |
| 44 | } |
59 | } |
| 45 | |
60 | |
| 46 | pub fn get_info(id: &str) -> Option<Item> { |
61 | pub fn get_info(id: &str) -> Option<Item> { |
src/init.rs
+2
| @@ -83,10 +83,12 @@ pub fn init( |
| 83 | .global |
83 | .global |
| 84 | .insert::<SearchHistory>(SearchHistory::load()); |
84 | .insert::<SearchHistory>(SearchHistory::load()); |
| 85 | framework.data.global.insert::<WatchHistory>(watchhistory); |
85 | framework.data.global.insert::<WatchHistory>(watchhistory); |
| |
86 | /* |
| 86 | framework |
87 | framework |
| 87 | .data |
88 | .data |
| 88 | .global |
89 | .global |
| 89 | .insert::<ChannelHistory>(ChannelHistory::load()); |
90 | .insert::<ChannelHistory>(ChannelHistory::load()); |
| |
91 | */ |
| 90 | framework |
92 | framework |
| 91 | .data |
93 | .data |
| 92 | .global |
94 | .global |
src/items/channeldisplay.rs
+4 −3
| @@ -4,7 +4,7 @@ use crate::{ |
| 4 | global::{ |
4 | global::{ |
| 5 | functions::*, |
5 | functions::*, |
| 6 | structs::*, |
6 | structs::*, |
| 7 | traits::{Collection, CollectionNoId, SearchProviderWrapper}, |
7 | traits::{Collection, SearchProviderWrapper}, |
| 8 | }, |
8 | }, |
| 9 | }; |
9 | }; |
| 10 | use ratatui::{ |
10 | use ratatui::{ |
| @@ -598,7 +598,7 @@ impl FrameworkItem for ChannelDisplay { |
| 598 | let appearance = framework.data.global.get::<AppearanceConfig>().unwrap(); |
598 | let appearance = framework.data.global.get::<AppearanceConfig>().unwrap(); |
| 599 | let page = framework.data.state.get::<Page>().unwrap().channeldisplay(); |
599 | let page = framework.data.state.get::<Page>().unwrap().channeldisplay(); |
| 600 | let page_id = page.id.clone(); |
600 | let page_id = page.id.clone(); |
| 601 | let is_main_page = matches!(page.r#type, ChannelDisplayPageType::Main); |
601 | // let is_main_page = matches!(page.r#type, ChannelDisplayPageType::Main); |
| 602 | |
602 | |
| 603 | match page.r#type { |
603 | match page.r#type { |
| 604 | ChannelDisplayPageType::Main => { |
604 | ChannelDisplayPageType::Main => { |
| @@ -632,7 +632,6 @@ impl FrameworkItem for ChannelDisplay { |
| 632 | |
632 | |
| 633 | let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap(); |
633 | let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap(); |
| 634 | watch_history.push(channel)?; |
634 | watch_history.push(channel)?; |
| 635 | |
| |
| 636 | } |
635 | } |
| 637 | ChannelDisplayPageType::Videos => { |
636 | ChannelDisplayPageType::Videos => { |
| 638 | let videos = SearchProviderWrapper::channel_videos(&page.id)? |
637 | let videos = SearchProviderWrapper::channel_videos(&page.id)? |
| @@ -696,6 +695,7 @@ impl FrameworkItem for ChannelDisplay { |
| 696 | &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0, |
695 | &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0, |
| 697 | ); |
696 | ); |
| 698 | |
697 | |
| |
698 | /* |
| 699 | if is_main_page { |
699 | if is_main_page { |
| 700 | let channel_history = framework.data.global.get_mut::<ChannelHistory>().unwrap(); |
700 | let channel_history = framework.data.global.get_mut::<ChannelHistory>().unwrap(); |
| 701 | if !channel_history.0.contains(&page_id) { |
701 | if !channel_history.0.contains(&page_id) { |
| @@ -703,6 +703,7 @@ impl FrameworkItem for ChannelDisplay { |
| 703 | let _ = channel_history.save(); |
703 | let _ = channel_history.save(); |
| 704 | } |
704 | } |
| 705 | } |
705 | } |
| |
706 | */ |
| 706 | |
707 | |
| 707 | Ok(()) |
708 | Ok(()) |
| 708 | } |
709 | } |