Commit 2d06ef1cc4
Unsigned
Layout: unified · split
src/global/functions/run_command.rs +12 −20
| @@ -122,27 +122,19 @@ pub fn run_single_command( | |||
| 122 | // match a command splitted by space to a bunch of avaliable commands | 122 | // match a command splitted by space to a bunch of avaliable commands |
| 123 | match command { | 123 | match command { |
| 124 | [] => {} | 124 | [] => {} |
| 125 | ["bookmark", id] => { | 125 | ["bookmark", id] => match (|| LocalStore::get_info(&id))() { |
| 126 | match (|| -> Result<Item, Box<dyn Error>> { | 126 | Some(item) => { |
| 127 | Ok(serde_json::from_str(&fs::read_to_string( | 127 | let library = framework.data.global.get_mut::<Library>().unwrap(); |
| 128 | home_dir() | 128 | let _ = library.push(item); |
| 129 | .unwrap() | 129 | let _ = library.save(); |
| 130 | .join(format!(".local/share/youtube-tui/info/{id}.json")), | 130 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 131 | )?)?) | 131 | Message::Success(String::from("Bookmark added")) |
| 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 | } | ||
| 144 | } | 132 | } |
| 145 | } | 133 | None => { |
| 134 | *framework.data.global.get_mut::<Message>().unwrap() = | ||
| 135 | Message::Error(format!("Unknown item: {id}")) | ||
| 136 | } | ||
| 137 | }, | ||
| 146 | ["unmark", id] => { | 138 | ["unmark", id] => { |
| 147 | let library = framework.data.global.get_mut::<Library>().unwrap(); | 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 | mod item; | 4 | mod item; |
| 5 | mod keyaction; | 5 | mod keyaction; |
| 6 | mod library; | 6 | mod library; |
| 7 | mod localstore; | ||
| 7 | mod message; | 8 | mod message; |
| 8 | #[cfg(feature = "mpv")] | 9 | #[cfg(feature = "mpv")] |
| 9 | mod mpv; | 10 | mod mpv; |
| @@ -20,6 +21,7 @@ pub use history::*; | |||
| 20 | pub use item::*; | 21 | pub use item::*; |
| 21 | pub use keyaction::*; | 22 | pub use keyaction::*; |
| 22 | pub use library::*; | 23 | pub use library::*; |
| 24 | pub use localstore::LocalStore; | ||
| 23 | pub use message::*; | 25 | pub use message::*; |
| 24 | #[cfg(feature = "mpv")] | 26 | #[cfg(feature = "mpv")] |
| 25 | pub use mpv::*; | 27 | pub use mpv::*; |
src/global/structs/subscriptions.rs +4 −1
| @@ -248,7 +248,10 @@ fn update_channel_cache( | |||
| 248 | image_index: usize, | 248 | image_index: usize, |
| 249 | ) -> Result<(), Box<dyn Error>> { | 249 | ) -> Result<(), Box<dyn Error>> { |
| 250 | let home_dir = home_dir().unwrap(); | 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 | if let Some(parent) = cache_path.parent() { | 256 | if let Some(parent) = cache_path.parent() { |
| 254 | fs::create_dir_all(parent)?; | 257 | fs::create_dir_all(parent)?; |
src/global/traits/collection.rs +4 −1
| @@ -55,7 +55,7 @@ where | |||
| 55 | 55 | ||
| 56 | /// add an item to watch history | 56 | /// add an item to watch history |
| 57 | fn push(&mut self, item: T) -> Result<(), Box<dyn Error>> { | 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 | // removes duplicates and place them on top (if exists) | 60 | // removes duplicates and place them on top (if exists) |
| 61 | let id = item.id().unwrap_or("invalid-dump"); | 61 | let id = item.id().unwrap_or("invalid-dump"); |
| @@ -66,6 +66,8 @@ where | |||
| 66 | { | 66 | { |
| 67 | self.items_mut().remove(index); | 67 | self.items_mut().remove(index); |
| 68 | } | 68 | } |
| 69 | |||
| 70 | /* | ||
| 69 | let mut file = OpenOptions::new() | 71 | let mut file = OpenOptions::new() |
| 70 | .write(true) | 72 | .write(true) |
| 71 | .create(true) | 73 | .create(true) |
| @@ -75,6 +77,7 @@ where | |||
| 75 | file.write_all(item_string.as_bytes())?; | 77 | file.write_all(item_string.as_bytes())?; |
| 76 | 78 | ||
| 77 | self.items_mut().push(item); | 79 | self.items_mut().push(item); |
| 80 | */ | ||
| 78 | 81 | ||
| 79 | Ok(()) | 82 | Ok(()) |
| 80 | } | 83 | } |
src/init.rs +1
| @@ -26,6 +26,7 @@ pub fn init( | |||
| 26 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, | 26 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, |
| 27 | command: Option<&str>, | 27 | command: Option<&str>, |
| 28 | ) -> Result<(), Box<dyn Error>> { | 28 | ) -> Result<(), Box<dyn Error>> { |
| 29 | LocalStore::init(); | ||
| 29 | let home_dir = home_dir().unwrap(); | 30 | let home_dir = home_dir().unwrap(); |
| 30 | RUNTIME | 31 | RUNTIME |
| 31 | .set(Builder::new_current_thread().enable_all().build().unwrap()) | 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 | /// handles select (enter) | 163 | /// handles select (enter) |
| 164 | fn select_at_cursor(&self, framework: &mut FrameworkClean) { | 164 | fn select_at_cursor(&self, framework: &mut FrameworkClean) { |
| 165 | let page_to_load = if home_dir() | 165 | let page_to_load = |
| 166 | .unwrap() | 166 | if LocalStore::get_info(self.items[self.textlist.selected].id().unwrap_or_default()) |
| 167 | .join(format!( | 167 | .is_some() |
| 168 | ".local/share/youtube-tui/info/{}.json", | 168 | { |
| 169 | self.items[self.textlist.selected].id().unwrap_or_default() | 169 | match &self.items[self.textlist.selected] { |
| 170 | )) | 170 | Item::MiniVideo(MiniVideoItem { id, .. }) |
| 171 | .exists() | 171 | | Item::FullVideo(FullVideoItem { id, .. }) => { |
| 172 | { | 172 | Some(Page::SingleItem(SingleItemPage::Video(id.clone()))) |
| 173 | match &self.items[self.textlist.selected] { | 173 | } |
| 174 | Item::MiniVideo(MiniVideoItem { id, .. }) | 174 | Item::MiniPlaylist(MiniPlaylistItem { id, .. }) |
| 175 | | Item::FullVideo(FullVideoItem { id, .. }) => { | 175 | | Item::FullPlaylist(FullPlaylistItem { id, .. }) => { |
| 176 | Some(Page::SingleItem(SingleItemPage::Video(id.clone()))) | 176 | Some(Page::SingleItem(SingleItemPage::Playlist(id.clone()))) |
| 177 | } | 177 | } |
| 178 | Item::MiniPlaylist(MiniPlaylistItem { id, .. }) | 178 | Item::MiniChannel(MiniChannelItem { id, .. }) |
| 179 | | Item::FullPlaylist(FullPlaylistItem { id, .. }) => { | 179 | | Item::FullChannel(FullChannelItem { id, .. }) => { |
| 180 | Some(Page::SingleItem(SingleItemPage::Playlist(id.clone()))) | 180 | Some(Page::ChannelDisplay(ChannelDisplayPage { |
| 181 | } | 181 | id: id.to_string(), |
| 182 | Item::MiniChannel(MiniChannelItem { id, .. }) | 182 | r#type: ChannelDisplayPageType::Main, |
| 183 | | Item::FullChannel(FullChannelItem { id, .. }) => { | 183 | })) |
| 184 | Some(Page::ChannelDisplay(ChannelDisplayPage { | 184 | } |
| 185 | id: id.to_string(), | 185 | // Item::Unknown(_) => { |
| 186 | r#type: ChannelDisplayPageType::Main, | 186 | // *framework.data.global.get_mut::<Message>().unwrap() = |
| 187 | })) | 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(_) => { | 205 | } else { |
| 190 | // *framework.data.global.get_mut::<Message>().unwrap() = | 206 | match &self.items[self.textlist.selected] { |
| 191 | // Message::Message(String::from("Unknown item")); | 207 | Item::MiniVideo(MiniVideoItem { id, .. }) |
| 192 | // framework | 208 | | Item::FullVideo(FullVideoItem { id, .. }) => { |
| 193 | // .data | 209 | Some(Page::SingleItem(SingleItemPage::Video(id.clone()))) |
| 194 | // .state | 210 | } |
| 195 | // .get_mut::<Tasks>() | 211 | Item::MiniPlaylist(MiniPlaylistItem { id, .. }) |
| 196 | // .unwrap() | 212 | | Item::FullPlaylist(FullPlaylistItem { id, .. }) => { |
| 197 | // .priority | 213 | Some(Page::SingleItem(SingleItemPage::Playlist(id.clone()))) |
| 198 | // .push(Task::RenderAll); | 214 | } |
| 199 | // None | 215 | Item::MiniChannel(MiniChannelItem { id, .. }) |
| 200 | // } | 216 | | Item::FullChannel(FullChannelItem { id, .. }) => { |
| 201 | Item::Page(b) => match framework.data.state.get::<Page>().unwrap() { | 217 | Some(Page::ChannelDisplay(ChannelDisplayPage { |
| 202 | Page::Search(search) => Some(Page::Search(Search { | 218 | id: id.clone(), |
| 203 | page: if *b { search.page + 1 } else { search.page - 1 }, | 219 | r#type: ChannelDisplayPageType::Main, |
| 204 | ..search.clone() | 220 | })) |
| 205 | })), | 221 | } |
| 206 | _ => unreachable!("Page turners can only be used in search pages"), | 222 | // Item::Unknown(_) => { |
| 207 | }, | 223 | // *framework.data.global.get_mut::<Message>().unwrap() = |
| 208 | } | 224 | // Message::Message(String::from("Unknown item")); |
| 209 | } else { | 225 | // framework |
| 210 | match &self.items[self.textlist.selected] { | 226 | // .data |
| 211 | Item::MiniVideo(MiniVideoItem { id, .. }) | 227 | // .state |
| 212 | | Item::FullVideo(FullVideoItem { id, .. }) => { | 228 | // .get_mut::<Tasks>() |
| 213 | Some(Page::SingleItem(SingleItemPage::Video(id.clone()))) | 229 | // .unwrap() |
| 214 | } | 230 | // .priority |
| 215 | Item::MiniPlaylist(MiniPlaylistItem { id, .. }) | 231 | // .push(Task::RenderAll); |
| 216 | | Item::FullPlaylist(FullPlaylistItem { id, .. }) => { | 232 | // None |
| 217 | Some(Page::SingleItem(SingleItemPage::Playlist(id.clone()))) | 233 | // } |
| 218 | } | 234 | Item::Page(b) => match framework.data.state.get::<Page>().unwrap() { |
| 219 | Item::MiniChannel(MiniChannelItem { id, .. }) | 235 | Page::Search(search) => Some(Page::Search(Search { |
| 220 | | Item::FullChannel(FullChannelItem { id, .. }) => { | 236 | page: if *b { search.page + 1 } else { search.page - 1 }, |
| 221 | Some(Page::ChannelDisplay(ChannelDisplayPage { | 237 | ..search.clone() |
| 222 | id: id.clone(), | 238 | })), |
| 223 | r#type: ChannelDisplayPageType::Main, | 239 | _ => unreachable!("Page turners can only be used in search pages"), |
| 224 | })) | 240 | }, |
| 225 | } | 241 | } |
| 226 | // Item::Unknown(_) => { | 242 | }; |
| 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 | }; | ||
| 247 | 243 | ||
| 248 | if let Some(page_to_load) = page_to_load { | 244 | if let Some(page_to_load) = page_to_load { |
| 249 | framework | 245 | framework |
src/items/singleitem.rs +5 −6
| @@ -745,11 +745,8 @@ impl FrameworkItem for SingleItem { | |||
| 745 | // gets the item that it needs to load from `data.state.Page` | 745 | // gets the item that it needs to load from `data.state.Page` |
| 746 | let (item, r#type) = match r#type { | 746 | let (item, r#type) = match r#type { |
| 747 | SingleItemPage::Video(id) => { | 747 | SingleItemPage::Video(id) => { |
| 748 | let home_dir = home_dir().unwrap(); | 748 | let video = if let Some(item) = LocalStore::get_info(id) { |
| 749 | let path = home_dir.join(format!(".local/youtube-tui/info/{id}.json")); | 749 | item |
| 750 | |||
| 751 | let video = if path.exists() { | ||
| 752 | serde_json::from_str(&fs::read_to_string(path)?)? | ||
| 753 | } else { | 750 | } else { |
| 754 | load_video(id, mainconfig)? | 751 | load_video(id, mainconfig)? |
| 755 | }; | 752 | }; |
| @@ -765,7 +762,7 @@ impl FrameworkItem for SingleItem { | |||
| 765 | SingleItemPage::Playlist(id) => { | 762 | SingleItemPage::Playlist(id) => { |
| 766 | let path = home_dir() | 763 | let path = home_dir() |
| 767 | .unwrap() | 764 | .unwrap() |
| 768 | .join(format!(".cache/youtube-tui/info/{id}.json")); | 765 | .join(format!(".local/share/youtube-tui/info/{id}.json")); |
| 769 | 766 | ||
| 770 | let playlist = if path.exists() { | 767 | let playlist = if path.exists() { |
| 771 | serde_json::from_str(&fs::read_to_string(path)?)? | 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 | self.item = Some(item); | 788 | self.item = Some(item); |
| 790 | self.r#type = r#type; | 789 | self.r#type = r#type; |
| 791 | self.iteminfo.item = self.item.clone(); | 790 | self.iteminfo.item = self.item.clone(); |