Commit 2d06ef1cc4

2d06ef1cc40f7769039763ae5398c8eaa74fd3ad

parent: 7d217a8cc1

Unsigned

Siriusmart <sirius@siri.ws> · 2025-10-24 08:18 UTC

info caching without file system

Layout: unified · split

src/global/functions/run_command.rs +12 −20
@@ -122,27 +122,19 @@ pub fn run_single_command(
122122 // match a command splitted by space to a bunch of avaliable commands
123123 match command {
124124 [] => {}
125 ["bookmark", id] => {
126 match (|| -> Result<Item, Box<dyn Error>> {
127 Ok(serde_json::from_str(&fs::read_to_string(
128 home_dir()
129 .unwrap()
130 .join(format!(".local/share/youtube-tui/info/{id}.json")),
131 )?)?)
132 })() {
133 Ok(item) => {
134 let library = framework.data.global.get_mut::<Library>().unwrap();
135 let _ = library.push(item);
136 let _ = library.save();
137 *framework.data.global.get_mut::<Message>().unwrap() =
138 Message::Success(String::from("Bookmark added"))
139 }
140 Err(e) => {
141 *framework.data.global.get_mut::<Message>().unwrap() =
142 Message::Error(format!("Unknown item: {e}"))
143 }
125 ["bookmark", id] => match (|| LocalStore::get_info(&id))() {
126 Some(item) => {
127 let library = framework.data.global.get_mut::<Library>().unwrap();
128 let _ = library.push(item);
129 let _ = library.save();
130 *framework.data.global.get_mut::<Message>().unwrap() =
131 Message::Success(String::from("Bookmark added"))
144132 }
145 }
133 None => {
134 *framework.data.global.get_mut::<Message>().unwrap() =
135 Message::Error(format!("Unknown item: {id}"))
136 }
137 },
146138 ["unmark", id] => {
147139 let library = framework.data.global.get_mut::<Library>().unwrap();
148140
src/global/structs/localstore.rs added +45
@@ -0,0 +1,45 @@
1use std::{collections::HashMap, fs, sync::OnceLock};
2
3use home::home_dir;
4
5use crate::global::structs::Item;
6
7static mut LOCALSTORE: OnceLock<LocalStore> = OnceLock::new();
8
9/// cached access and write files to ~/.local/share
10#[derive(Default)]
11pub struct LocalStore {
12 info: HashMap<String, Item>,
13}
14
15impl LocalStore {
16 pub fn init() {
17 unsafe {
18 let _ = LOCALSTORE.set(Self::default());
19 }
20 }
21
22 pub fn get_info(id: &str) -> Option<Item> {
23 let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap();
24
25 match localstore.info.get(id) {
26 Some(item) => Some(item.clone()),
27 None => {
28 let path = home_dir()
29 .unwrap()
30 .join(format!(".local/share/youtube-tui/info/{id}.json"));
31
32 if path.exists() {
33 serde_json::from_str(&fs::read_to_string(path).ok()?).ok()?
34 } else {
35 None
36 }
37 }
38 }
39 }
40
41 pub fn set_info(id: String, item: Item) {
42 let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap();
43 localstore.info.insert(id, item);
44 }
45}
src/global/structs/mod.rs +2
@@ -4,6 +4,7 @@ mod history;
44mod item;
55mod keyaction;
66mod library;
7mod localstore;
78mod message;
89#[cfg(feature = "mpv")]
910mod mpv;
@@ -20,6 +21,7 @@ pub use history::*;
2021pub use item::*;
2122pub use keyaction::*;
2223pub use library::*;
24pub use localstore::LocalStore;
2325pub use message::*;
2426#[cfg(feature = "mpv")]
2527pub use mpv::*;
src/global/structs/subscriptions.rs +4 −1
@@ -248,7 +248,10 @@ fn update_channel_cache(
248248 image_index: usize,
249249) -> Result<(), Box<dyn Error>> {
250250 let home_dir = home_dir().unwrap();
251 let cache_path = home_dir.join(format!(".local/share/youtube-tui/channels/{}.json", channel.id));
251 let cache_path = home_dir.join(format!(
252 ".local/share/youtube-tui/channels/{}.json",
253 channel.id
254 ));
252255
253256 if let Some(parent) = cache_path.parent() {
254257 fs::create_dir_all(parent)?;
src/global/traits/collection.rs +4 −1
@@ -55,7 +55,7 @@ where
5555
5656 /// add an item to watch history
5757 fn push(&mut self, item: T) -> Result<(), Box<dyn Error>> {
58 let info = home_dir().unwrap().join(".local/share/youtube-tui/info/");
58 // let info = home_dir().unwrap().join(".local/share/youtube-tui/info/");
5959
6060 // removes duplicates and place them on top (if exists)
6161 let id = item.id().unwrap_or("invalid-dump");
@@ -66,6 +66,8 @@ where
6666 {
6767 self.items_mut().remove(index);
6868 }
69
70 /*
6971 let mut file = OpenOptions::new()
7072 .write(true)
7173 .create(true)
@@ -75,6 +77,7 @@ where
7577 file.write_all(item_string.as_bytes())?;
7678
7779 self.items_mut().push(item);
80 */
7881
7982 Ok(())
8083 }
src/init.rs +1
@@ -26,6 +26,7 @@ pub fn init(
2626 terminal: &mut Terminal<CrosstermBackend<Stdout>>,
2727 command: Option<&str>,
2828) -> Result<(), Box<dyn Error>> {
29 LocalStore::init();
2930 let home_dir = home_dir().unwrap();
3031 RUNTIME
3132 .set(Builder::new_current_thread().enable_all().build().unwrap())
src/items/itemlist.rs +76 −80
@@ -162,88 +162,84 @@ impl ItemList {
162162
163163 /// handles select (enter)
164164 fn select_at_cursor(&self, framework: &mut FrameworkClean) {
165 let page_to_load = if home_dir()
166 .unwrap()
167 .join(format!(
168 ".local/share/youtube-tui/info/{}.json",
169 self.items[self.textlist.selected].id().unwrap_or_default()
170 ))
171 .exists()
172 {
173 match &self.items[self.textlist.selected] {
174 Item::MiniVideo(MiniVideoItem { id, .. })
175 | Item::FullVideo(FullVideoItem { id, .. }) => {
176 Some(Page::SingleItem(SingleItemPage::Video(id.clone())))
177 }
178 Item::MiniPlaylist(MiniPlaylistItem { id, .. })
179 | Item::FullPlaylist(FullPlaylistItem { id, .. }) => {
180 Some(Page::SingleItem(SingleItemPage::Playlist(id.clone())))
181 }
182 Item::MiniChannel(MiniChannelItem { id, .. })
183 | Item::FullChannel(FullChannelItem { id, .. }) => {
184 Some(Page::ChannelDisplay(ChannelDisplayPage {
185 id: id.to_string(),
186 r#type: ChannelDisplayPageType::Main,
187 }))
165 let page_to_load =
166 if LocalStore::get_info(self.items[self.textlist.selected].id().unwrap_or_default())
167 .is_some()
168 {
169 match &self.items[self.textlist.selected] {
170 Item::MiniVideo(MiniVideoItem { id, .. })
171 | Item::FullVideo(FullVideoItem { id, .. }) => {
172 Some(Page::SingleItem(SingleItemPage::Video(id.clone())))
173 }
174 Item::MiniPlaylist(MiniPlaylistItem { id, .. })
175 | Item::FullPlaylist(FullPlaylistItem { id, .. }) => {
176 Some(Page::SingleItem(SingleItemPage::Playlist(id.clone())))
177 }
178 Item::MiniChannel(MiniChannelItem { id, .. })
179 | Item::FullChannel(FullChannelItem { id, .. }) => {
180 Some(Page::ChannelDisplay(ChannelDisplayPage {
181 id: id.to_string(),
182 r#type: ChannelDisplayPageType::Main,
183 }))
184 }
185 // Item::Unknown(_) => {
186 // *framework.data.global.get_mut::<Message>().unwrap() =
187 // Message::Message(String::from("Unknown item"));
188 // framework
189 // .data
190 // .state
191 // .get_mut::<Tasks>()
192 // .unwrap()
193 // .priority
194 // .push(Task::RenderAll);
195 // None
196 // }
197 Item::Page(b) => match framework.data.state.get::<Page>().unwrap() {
198 Page::Search(search) => Some(Page::Search(Search {
199 page: if *b { search.page + 1 } else { search.page - 1 },
200 ..search.clone()
201 })),
202 _ => unreachable!("Page turners can only be used in search pages"),
203 },
188204 }
189 // Item::Unknown(_) => {
190 // *framework.data.global.get_mut::<Message>().unwrap() =
191 // Message::Message(String::from("Unknown item"));
192 // framework
193 // .data
194 // .state
195 // .get_mut::<Tasks>()
196 // .unwrap()
197 // .priority
198 // .push(Task::RenderAll);
199 // None
200 // }
201 Item::Page(b) => match framework.data.state.get::<Page>().unwrap() {
202 Page::Search(search) => Some(Page::Search(Search {
203 page: if *b { search.page + 1 } else { search.page - 1 },
204 ..search.clone()
205 })),
206 _ => unreachable!("Page turners can only be used in search pages"),
207 },
208 }
209 } else {
210 match &self.items[self.textlist.selected] {
211 Item::MiniVideo(MiniVideoItem { id, .. })
212 | Item::FullVideo(FullVideoItem { id, .. }) => {
213 Some(Page::SingleItem(SingleItemPage::Video(id.clone())))
214 }
215 Item::MiniPlaylist(MiniPlaylistItem { id, .. })
216 | Item::FullPlaylist(FullPlaylistItem { id, .. }) => {
217 Some(Page::SingleItem(SingleItemPage::Playlist(id.clone())))
218 }
219 Item::MiniChannel(MiniChannelItem { id, .. })
220 | Item::FullChannel(FullChannelItem { id, .. }) => {
221 Some(Page::ChannelDisplay(ChannelDisplayPage {
222 id: id.clone(),
223 r#type: ChannelDisplayPageType::Main,
224 }))
205 } else {
206 match &self.items[self.textlist.selected] {
207 Item::MiniVideo(MiniVideoItem { id, .. })
208 | Item::FullVideo(FullVideoItem { id, .. }) => {
209 Some(Page::SingleItem(SingleItemPage::Video(id.clone())))
210 }
211 Item::MiniPlaylist(MiniPlaylistItem { id, .. })
212 | Item::FullPlaylist(FullPlaylistItem { id, .. }) => {
213 Some(Page::SingleItem(SingleItemPage::Playlist(id.clone())))
214 }
215 Item::MiniChannel(MiniChannelItem { id, .. })
216 | Item::FullChannel(FullChannelItem { id, .. }) => {
217 Some(Page::ChannelDisplay(ChannelDisplayPage {
218 id: id.clone(),
219 r#type: ChannelDisplayPageType::Main,
220 }))
221 }
222 // Item::Unknown(_) => {
223 // *framework.data.global.get_mut::<Message>().unwrap() =
224 // Message::Message(String::from("Unknown item"));
225 // framework
226 // .data
227 // .state
228 // .get_mut::<Tasks>()
229 // .unwrap()
230 // .priority
231 // .push(Task::RenderAll);
232 // None
233 // }
234 Item::Page(b) => match framework.data.state.get::<Page>().unwrap() {
235 Page::Search(search) => Some(Page::Search(Search {
236 page: if *b { search.page + 1 } else { search.page - 1 },
237 ..search.clone()
238 })),
239 _ => unreachable!("Page turners can only be used in search pages"),
240 },
225241 }
226 // Item::Unknown(_) => {
227 // *framework.data.global.get_mut::<Message>().unwrap() =
228 // Message::Message(String::from("Unknown item"));
229 // framework
230 // .data
231 // .state
232 // .get_mut::<Tasks>()
233 // .unwrap()
234 // .priority
235 // .push(Task::RenderAll);
236 // None
237 // }
238 Item::Page(b) => match framework.data.state.get::<Page>().unwrap() {
239 Page::Search(search) => Some(Page::Search(Search {
240 page: if *b { search.page + 1 } else { search.page - 1 },
241 ..search.clone()
242 })),
243 _ => unreachable!("Page turners can only be used in search pages"),
244 },
245 }
246 };
242 };
247243
248244 if let Some(page_to_load) = page_to_load {
249245 framework
src/items/singleitem.rs +5 −6
@@ -745,11 +745,8 @@ impl FrameworkItem for SingleItem {
745745 // gets the item that it needs to load from `data.state.Page`
746746 let (item, r#type) = match r#type {
747747 SingleItemPage::Video(id) => {
748 let home_dir = home_dir().unwrap();
749 let path = home_dir.join(format!(".local/youtube-tui/info/{id}.json"));
750
751 let video = if path.exists() {
752 serde_json::from_str(&fs::read_to_string(path)?)?
748 let video = if let Some(item) = LocalStore::get_info(id) {
749 item
753750 } else {
754751 load_video(id, mainconfig)?
755752 };
@@ -765,7 +762,7 @@ impl FrameworkItem for SingleItem {
765762 SingleItemPage::Playlist(id) => {
766763 let path = home_dir()
767764 .unwrap()
768 .join(format!(".cache/youtube-tui/info/{id}.json"));
765 .join(format!(".local/share/youtube-tui/info/{id}.json"));
769766
770767 let playlist = if path.exists() {
771768 serde_json::from_str(&fs::read_to_string(path)?)?
@@ -786,6 +783,8 @@ impl FrameworkItem for SingleItem {
786783 }
787784 };
788785
786 LocalStore::set_info(item.id().unwrap().to_string(), item.clone());
787
789788 self.item = Some(item);
790789 self.r#type = r#type;
791790 self.iteminfo.item = self.item.clone();