Commit 2d06ef1cc4
Unsigned
Layout: unified · split
src/global/functions/run_command.rs +12 −20
| @@ -122,27 +122,19 @@ pub fn run_single_command( | ||
| 122 | 122 | // match a command splitted by space to a bunch of avaliable commands |
| 123 | 123 | match command { |
| 124 | 124 | [] => {} |
| 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")) | |
| 144 | 132 | } |
| 145 | } | |
| 133 | None => { | |
| 134 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 135 | Message::Error(format!("Unknown item: {id}")) | |
| 136 | } | |
| 137 | }, | |
| 146 | 138 | ["unmark", id] => { |
| 147 | 139 | let library = framework.data.global.get_mut::<Library>().unwrap(); |
| 148 | 140 | |
src/global/structs/localstore.rs added +45
| @@ -0,0 +1,45 @@ | ||
| 1 | use std::{collections::HashMap, fs, sync::OnceLock}; | |
| 2 | ||
| 3 | use home::home_dir; | |
| 4 | ||
| 5 | use crate::global::structs::Item; | |
| 6 | ||
| 7 | static mut LOCALSTORE: OnceLock<LocalStore> = OnceLock::new(); | |
| 8 | ||
| 9 | /// cached access and write files to ~/.local/share | |
| 10 | #[derive(Default)] | |
| 11 | pub struct LocalStore { | |
| 12 | info: HashMap<String, Item>, | |
| 13 | } | |
| 14 | ||
| 15 | impl 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; | ||
| 4 | 4 | mod item; |
| 5 | 5 | mod keyaction; |
| 6 | 6 | mod library; |
| 7 | mod localstore; | |
| 7 | 8 | mod message; |
| 8 | 9 | #[cfg(feature = "mpv")] |
| 9 | 10 | mod mpv; |
| @@ -20,6 +21,7 @@ pub use history::*; | ||
| 20 | 21 | pub use item::*; |
| 21 | 22 | pub use keyaction::*; |
| 22 | 23 | pub use library::*; |
| 24 | pub use localstore::LocalStore; | |
| 23 | 25 | pub use message::*; |
| 24 | 26 | #[cfg(feature = "mpv")] |
| 25 | 27 | pub use mpv::*; |
src/global/structs/subscriptions.rs +4 −1
| @@ -248,7 +248,10 @@ fn update_channel_cache( | ||
| 248 | 248 | image_index: usize, |
| 249 | 249 | ) -> Result<(), Box<dyn Error>> { |
| 250 | 250 | 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 | )); | |
| 252 | 255 | |
| 253 | 256 | if let Some(parent) = cache_path.parent() { |
| 254 | 257 | fs::create_dir_all(parent)?; |
src/global/traits/collection.rs +4 −1
| @@ -55,7 +55,7 @@ where | ||
| 55 | 55 | |
| 56 | 56 | /// add an item to watch history |
| 57 | 57 | 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/"); | |
| 59 | 59 | |
| 60 | 60 | // removes duplicates and place them on top (if exists) |
| 61 | 61 | let id = item.id().unwrap_or("invalid-dump"); |
| @@ -66,6 +66,8 @@ where | ||
| 66 | 66 | { |
| 67 | 67 | self.items_mut().remove(index); |
| 68 | 68 | } |
| 69 | ||
| 70 | /* | |
| 69 | 71 | let mut file = OpenOptions::new() |
| 70 | 72 | .write(true) |
| 71 | 73 | .create(true) |
| @@ -75,6 +77,7 @@ where | ||
| 75 | 77 | file.write_all(item_string.as_bytes())?; |
| 76 | 78 | |
| 77 | 79 | self.items_mut().push(item); |
| 80 | */ | |
| 78 | 81 | |
| 79 | 82 | Ok(()) |
| 80 | 83 | } |
src/init.rs +1
| @@ -26,6 +26,7 @@ pub fn init( | ||
| 26 | 26 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, |
| 27 | 27 | command: Option<&str>, |
| 28 | 28 | ) -> Result<(), Box<dyn Error>> { |
| 29 | LocalStore::init(); | |
| 29 | 30 | let home_dir = home_dir().unwrap(); |
| 30 | 31 | RUNTIME |
| 31 | 32 | .set(Builder::new_current_thread().enable_all().build().unwrap()) |
src/items/itemlist.rs +76 −80
| @@ -162,88 +162,84 @@ impl ItemList { | ||
| 162 | 162 | |
| 163 | 163 | /// handles select (enter) |
| 164 | 164 | 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 | }, | |
| 188 | 204 | } |
| 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 | }, | |
| 225 | 241 | } |
| 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 | }; | |
| 247 | 243 | |
| 248 | 244 | if let Some(page_to_load) = page_to_load { |
| 249 | 245 | framework |
src/items/singleitem.rs +5 −6
| @@ -745,11 +745,8 @@ impl FrameworkItem for SingleItem { | ||
| 745 | 745 | // gets the item that it needs to load from `data.state.Page` |
| 746 | 746 | let (item, r#type) = match r#type { |
| 747 | 747 | 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 | |
| 753 | 750 | } else { |
| 754 | 751 | load_video(id, mainconfig)? |
| 755 | 752 | }; |
| @@ -765,7 +762,7 @@ impl FrameworkItem for SingleItem { | ||
| 765 | 762 | SingleItemPage::Playlist(id) => { |
| 766 | 763 | let path = home_dir() |
| 767 | 764 | .unwrap() |
| 768 | .join(format!(".cache/youtube-tui/info/{id}.json")); | |
| 765 | .join(format!(".local/share/youtube-tui/info/{id}.json")); | |
| 769 | 766 | |
| 770 | 767 | let playlist = if path.exists() { |
| 771 | 768 | serde_json::from_str(&fs::read_to_string(path)?)? |
| @@ -786,6 +783,8 @@ impl FrameworkItem for SingleItem { | ||
| 786 | 783 | } |
| 787 | 784 | }; |
| 788 | 785 | |
| 786 | LocalStore::set_info(item.id().unwrap().to_string(), item.clone()); | |
| 787 | ||
| 789 | 788 | self.item = Some(item); |
| 790 | 789 | self.r#type = r#type; |
| 791 | 790 | self.iteminfo.item = self.item.clone(); |