Commit 6b52ccce6c
Unsigned
Layout: unified · split
docs/src/commands.md +8
| @@ -47,6 +47,14 @@ run [command] | |||
| 47 | > | 47 | > |
| 48 | > But for when tasks are stacked up in the *same* event loop and the order of which they are executed matters, this command can be used to force the already stacked up commands to be ran first. | 48 | > But for when tasks are stacked up in the *same* event loop and the order of which they are executed matters, this command can be used to force the already stacked up commands to be ran first. |
| 49 | 49 | ||
| 50 | ## Library | ||
| 51 | |||
| 52 | ```vim | ||
| 53 | bookmark [id] | ||
| 54 | unmark [id] | ||
| 55 | togglemark [id] | ||
| 56 | ``` | ||
| 57 | |||
| 50 | ## Text commands | 58 | ## Text commands |
| 51 | 59 | ||
| 52 | Text commands generates a *text only response* without launching the TUI. | 60 | Text commands generates a *text only response* without launching the TUI. |
src/config/commands.rs +29 −33
| @@ -83,7 +83,7 @@ impl Default for CommandsConfigSerde { | |||
| 83 | // Different pages may contain different `env`s (for example `url` is different in each page) | 83 | // Different pages may contain different `env`s (for example `url` is different in each page) |
| 84 | 84 | ||
| 85 | fn launch_command_default() -> String { | 85 | fn launch_command_default() -> String { |
| 86 | String::from("loadpage popular ;; flush ;; history clear") | 86 | String::from("loadpage watchhistory ;; flush ;; history clear") |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | fn video_default() -> Vec<HashMap<String, String>> { | 89 | fn video_default() -> Vec<HashMap<String, String>> { |
| @@ -111,16 +111,16 @@ fn video_default() -> Vec<HashMap<String, String>> { | |||
| 111 | String::from("run ${browser} '${url}'"), | 111 | String::from("run ${browser} '${url}'"), |
| 112 | )]), | 112 | )]), |
| 113 | HashMap::from([( | 113 | HashMap::from([( |
| 114 | String::from("Download video (webm)"), | 114 | String::from("Toggle bookmark"), |
| 115 | String::from( | 115 | String::from("togglemark ${id}") |
| 116 | "run ${terminal-emulator} ${youtube-downloader} -o ${download-path} '${embed-url}'", | ||
| 117 | ), | ||
| 118 | )]), | 116 | )]), |
| 119 | HashMap::from([( | 117 | HashMap::from([( |
| 120 | String::from("Download audio (opus)"), | 118 | String::from("Save video"), |
| 121 | String::from( | 119 | String::from("bookmark ${id} ;; run rm -rf '${save-path}${id}' ;; run ${terminal-emulator} ${youtube-downloader} ${all-videos} -o '${save-path}${id}/%(id)s'") |
| 122 | "run ${terminal-emulator} ${youtube-downloader} -o ${download-path} '${embed-url}' -x", | 120 | )]), |
| 123 | ), | 121 | HashMap::from([( |
| 122 | String::from("Save audio"), | ||
| 123 | String::from("bookmark ${id} ;; run rm -rf '${save-path}${id}' ;; run ${terminal-emulator} ${youtube-downloader} ${all-videos} -x -o '${save-path}${id}/%(id)s'") | ||
| 124 | )]), | 124 | )]), |
| 125 | HashMap::from([( | 125 | HashMap::from([( |
| 126 | String::from("Mode: ${provider}"), | 126 | String::from("Mode: ${provider}"), |
| @@ -132,6 +132,10 @@ fn video_default() -> Vec<HashMap<String, String>> { | |||
| 132 | fn playlist_default() -> Vec<HashMap<String, String>> { | 132 | fn playlist_default() -> Vec<HashMap<String, String>> { |
| 133 | vec![ | 133 | vec![ |
| 134 | HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]), | 134 | HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]), |
| 135 | HashMap::from([( | ||
| 136 | String::from("Reload as online mode"), | ||
| 137 | String::from("playlist ${id}"), | ||
| 138 | )]), | ||
| 135 | HashMap::from([( | 139 | HashMap::from([( |
| 136 | String::from("Play all videos"), | 140 | String::from("Play all videos"), |
| 137 | String::from("run ${video-player} ${all-videos}"), | 141 | String::from("run ${video-player} ${all-videos}"), |
| @@ -140,10 +144,6 @@ fn playlist_default() -> Vec<HashMap<String, String>> { | |||
| 140 | String::from("Play all audio"), | 144 | String::from("Play all audio"), |
| 141 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video"), | 145 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video"), |
| 142 | )]), | 146 | )]), |
| 143 | HashMap::from([( | ||
| 144 | String::from("Shuffle play all audio"), | ||
| 145 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle"), | ||
| 146 | )]), | ||
| 147 | HashMap::from([( | 147 | HashMap::from([( |
| 148 | String::from("Shuffle play all audio (loop)"), | 148 | String::from("Shuffle play all audio (loop)"), |
| 149 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle --loop-playlist=inf"), | 149 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle --loop-playlist=inf"), |
| @@ -157,12 +157,12 @@ fn playlist_default() -> Vec<HashMap<String, String>> { | |||
| 157 | String::from("run ${browser} '${url}'"), | 157 | String::from("run ${browser} '${url}'"), |
| 158 | )]), | 158 | )]), |
| 159 | HashMap::from([( | 159 | HashMap::from([( |
| 160 | String::from("Download all video (webm)"), | 160 | String::from("Toggle bookmark"), |
| 161 | String::from("run ${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos}") | 161 | String::from("togglemark ${id}") |
| 162 | )]), | 162 | )]), |
| 163 | HashMap::from([( | 163 | HashMap::from([( |
| 164 | String::from("Download all audio (opus)"), | 164 | String::from("Save all audio"), |
| 165 | String::from("run ${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos} -x") | 165 | String::from("bookmark ${id} ;; run rm -rf '${save-path}${id}' ;; run mkdir '${save-path}${id}' ;; run ${terminal-emulator} ${youtube-downloader} ${all-videos} -x -o '${save-path}${id}/%(id)s'") |
| 166 | )]), | 166 | )]), |
| 167 | HashMap::from([( | 167 | HashMap::from([( |
| 168 | String::from("Mode: ${provider}"), | 168 | String::from("Mode: ${provider}"), |
| @@ -200,16 +200,16 @@ fn local_video_default() -> Vec<HashMap<String, String>> { | |||
| 200 | String::from("run ${browser} '${url}'"), | 200 | String::from("run ${browser} '${url}'"), |
| 201 | )]), | 201 | )]), |
| 202 | HashMap::from([( | 202 | HashMap::from([( |
| 203 | String::from("Download video (webm)"), | 203 | String::from("Toggle bookmark"), |
| 204 | String::from( | 204 | String::from("togglemark ${id}") |
| 205 | "run ${terminal-emulator} ${youtube-downloader} -o ${download-path} '${embed-url}'", | ||
| 206 | ), | ||
| 207 | )]), | 205 | )]), |
| 208 | HashMap::from([( | 206 | HashMap::from([( |
| 209 | String::from("Download audio (opus)"), | 207 | String::from("Save video"), |
| 210 | String::from( | 208 | String::from("bookmark ${id} ;; run rm -rf '${save-path}${id}' ;; run ${terminal-emulator} ${youtube-downloader} ${all-videos} -o '${save-path}${id}/%(id)s'") |
| 211 | "run ${terminal-emulator} ${youtube-downloader} -o ${download-path} '${embed-url}' -x", | 209 | )]), |
| 212 | ), | 210 | HashMap::from([( |
| 211 | String::from("Save audio"), | ||
| 212 | String::from("bookmark ${id} ;; run rm -rf '${save-path}${id}' ;; run ${terminal-emulator} ${youtube-downloader} ${all-videos} -x -o '${save-path}${id}/%(id)s'") | ||
| 213 | )]), | 213 | )]), |
| 214 | HashMap::from([( | 214 | HashMap::from([( |
| 215 | String::from("Mode: ${provider}"), | 215 | String::from("Mode: ${provider}"), |
| @@ -233,10 +233,6 @@ fn local_playlist_default() -> Vec<HashMap<String, String>> { | |||
| 233 | String::from("Play all audio"), | 233 | String::from("Play all audio"), |
| 234 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video"), | 234 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video"), |
| 235 | )]), | 235 | )]), |
| 236 | HashMap::from([( | ||
| 237 | String::from("Shuffle play all audio"), | ||
| 238 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle"), | ||
| 239 | )]), | ||
| 240 | HashMap::from([( | 236 | HashMap::from([( |
| 241 | String::from("Shuffle play all audio (loop)"), | 237 | String::from("Shuffle play all audio (loop)"), |
| 242 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle --loop-playlist=inf"), | 238 | String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle --loop-playlist=inf"), |
| @@ -250,12 +246,12 @@ fn local_playlist_default() -> Vec<HashMap<String, String>> { | |||
| 250 | String::from("run ${browser} '${url}'"), | 246 | String::from("run ${browser} '${url}'"), |
| 251 | )]), | 247 | )]), |
| 252 | HashMap::from([( | 248 | HashMap::from([( |
| 253 | String::from("Download all video (webm)"), | 249 | String::from("Toggle bookmark"), |
| 254 | String::from("run ${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos}") | 250 | String::from("togglemark ${id}") |
| 255 | )]), | 251 | )]), |
| 256 | HashMap::from([( | 252 | HashMap::from([( |
| 257 | String::from("Download all audio (opus)"), | 253 | String::from("Save all audio"), |
| 258 | String::from("run ${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos} -x") | 254 | String::from("bookmark ${id} ;; run rm -rf '${save-path}${id}' ;; run mkdir '${save-path}${id}' ;; run ${terminal-emulator} ${youtube-downloader} ${all-videos} -x -o '${save-path}${id}/%(id)s'") |
| 259 | )]), | 255 | )]), |
| 260 | HashMap::from([( | 256 | HashMap::from([( |
| 261 | String::from("Mode: ${provider}"), | 257 | String::from("Mode: ${provider}"), |
src/config/main.rs +5 −1
| @@ -147,7 +147,11 @@ fn default_env() -> HashMap<String, String> { | |||
| 147 | (String::from("youtube-downloader"), String::from("yt-dlp")), | 147 | (String::from("youtube-downloader"), String::from("yt-dlp")), |
| 148 | ( | 148 | ( |
| 149 | String::from("download-path"), | 149 | String::from("download-path"), |
| 150 | String::from("'~/Downloads/%(title)s-%(id)s.%(ext)s'"), | 150 | String::from("~/Downloads/%(title)s-%(id)s.%(ext)s"), |
| 151 | ), | ||
| 152 | ( | ||
| 153 | String::from("save-path"), | ||
| 154 | String::from("~/.local/share/youtube-tui/saved/"), | ||
| 151 | ), | 155 | ), |
| 152 | ]) | 156 | ]) |
| 153 | } | 157 | } |
src/exit.rs +1 −1
| @@ -2,7 +2,7 @@ use home::home_dir; | |||
| 2 | use std::{error::Error, fs}; | 2 | use std::{error::Error, fs}; |
| 3 | use tui_additions::framework::Framework; | 3 | use tui_additions::framework::Framework; |
| 4 | 4 | ||
| 5 | use crate::global::structs::WatchHistory; | 5 | use crate::global::{structs::WatchHistory, traits::Collection}; |
| 6 | 6 | ||
| 7 | /// function to run when the app ends | 7 | /// function to run when the app ends |
| 8 | // exit tasks: | 8 | // exit tasks: |
src/global/functions/download_all_images.rs +1 −1
| @@ -36,7 +36,7 @@ impl From<&Item> for Option<DownloadRequest> { | |||
| 36 | url: fullchannel.thumbnail_url.clone(), | 36 | url: fullchannel.thumbnail_url.clone(), |
| 37 | id: fullchannel.id.clone(), | 37 | id: fullchannel.id.clone(), |
| 38 | }, | 38 | }, |
| 39 | Item::Unknown(_) | Item::Page(_) => return None, | 39 | _ => return None, |
| 40 | }) | 40 | }) |
| 41 | } | 41 | } |
| 42 | } | 42 | } |
src/global/functions/from_url.rs +2 −6
| @@ -10,15 +10,11 @@ pub fn from_channel_url(identifier: &str) -> Result<String, String> { | |||
| 10 | // if there isnt 24 characters after `/channel/`, that url must not have | 10 | // if there isnt 24 characters after `/channel/`, that url must not have |
| 11 | // contained a channel id | 11 | // contained a channel id |
| 12 | if identifier.len() < index + 33 { | 12 | if identifier.len() < index + 33 { |
| 13 | return Err(format!( | 13 | return Err(format!("Cannot find channel id from string `{identifier}`")); |
| 14 | "Cannot find channel id from string `{identifier}`" | ||
| 15 | )); | ||
| 16 | } | 14 | } |
| 17 | index + 9 | 15 | index + 9 |
| 18 | } else { | 16 | } else { |
| 19 | return Err(format!( | 17 | return Err(format!("Cannot find channel id from string `{identifier}`")); |
| 20 | "Cannot find channel id from string `{identifier}`" | ||
| 21 | )); | ||
| 22 | }; | 18 | }; |
| 23 | 19 | ||
| 24 | Ok(identifier[index..index + 24].to_string()) | 20 | Ok(identifier[index..index + 24].to_string()) |
src/global/functions/init_move.rs added +28
| @@ -0,0 +1,28 @@ | |||
| 1 | use std::fs; | ||
| 2 | |||
| 3 | use home::home_dir; | ||
| 4 | |||
| 5 | pub fn init_move() { | ||
| 6 | let home_dir = home_dir().unwrap(); | ||
| 7 | let store_dir = home_dir.join(".local/share/youtube-tui/"); | ||
| 8 | let cache_dir = home_dir.join(".cache/youtube-tui"); | ||
| 9 | |||
| 10 | let store_info_dir = store_dir.join("info/"); | ||
| 11 | let cache_info_dir = cache_dir.join("info/"); | ||
| 12 | fs::read_dir(store_info_dir).unwrap().for_each(|entry| { | ||
| 13 | let entry = entry.unwrap().path(); | ||
| 14 | let _ = fs::rename(&entry, cache_info_dir.join(entry.file_name().unwrap())); | ||
| 15 | }); | ||
| 16 | |||
| 17 | let store_thumbnails_dir = store_dir.join("thumbnails/"); | ||
| 18 | let cache_thumbnails_dir = cache_dir.join("thumbnails/"); | ||
| 19 | fs::read_dir(store_thumbnails_dir) | ||
| 20 | .unwrap() | ||
| 21 | .for_each(|entry| { | ||
| 22 | let entry = entry.unwrap().path(); | ||
| 23 | let _ = fs::rename( | ||
| 24 | &entry, | ||
| 25 | cache_thumbnails_dir.join(entry.file_name().unwrap()), | ||
| 26 | ); | ||
| 27 | }); | ||
| 28 | } | ||
src/global/functions/mod.rs +2
| @@ -23,3 +23,5 @@ mod envs; | |||
| 23 | pub use envs::*; | 23 | pub use envs::*; |
| 24 | mod update_provider; | 24 | mod update_provider; |
| 25 | pub use update_provider::*; | 25 | pub use update_provider::*; |
| 26 | mod init_move; | ||
| 27 | pub use init_move::*; | ||
src/global/functions/run_command.rs +56 −4
| @@ -1,13 +1,17 @@ | |||
| 1 | use crate::{ | 1 | use crate::{ |
| 2 | config::Search, | 2 | config::Search, |
| 3 | global::structs::{ | 3 | global::{ |
| 4 | ChannelDisplayPage, ChannelDisplayPageType, MainMenuPage, Message, Page, SingleItemPage, | 4 | structs::{ |
| 5 | Status, Task, Tasks, | 5 | ChannelDisplayPage, ChannelDisplayPageType, Item, Library, MainMenuPage, Message, Page, |
| 6 | SingleItemPage, Status, Task, Tasks, | ||
| 7 | }, | ||
| 8 | traits::Collection, | ||
| 6 | }, | 9 | }, |
| 7 | load_configs, | 10 | load_configs, |
| 8 | }; | 11 | }; |
| 12 | use home::home_dir; | ||
| 9 | use run_script::ScriptOptions; | 13 | use run_script::ScriptOptions; |
| 10 | use std::{env, io::Stdout, thread}; | 14 | use std::{env, error::Error, fs, io::Stdout, thread}; |
| 11 | use tui::{backend::CrosstermBackend, Terminal}; | 15 | use tui::{backend::CrosstermBackend, Terminal}; |
| 12 | use tui_additions::framework::Framework; | 16 | use tui_additions::framework::Framework; |
| 13 | 17 | ||
| @@ -59,6 +63,49 @@ pub fn run_single_command( | |||
| 59 | // match a command splitted by space to a bunch of avaliable commands | 63 | // match a command splitted by space to a bunch of avaliable commands |
| 60 | match command { | 64 | match command { |
| 61 | [] => {} | 65 | [] => {} |
| 66 | ["bookmark", id] => { | ||
| 67 | match (|| -> Result<Item, Box<dyn Error>> { | ||
| 68 | Ok(serde_json::from_str(&fs::read_to_string( | ||
| 69 | home_dir() | ||
| 70 | .unwrap() | ||
| 71 | .join(format!(".cache/youtube-tui/info/{id}.json")), | ||
| 72 | )?)?) | ||
| 73 | })() { | ||
| 74 | Ok(item) => { | ||
| 75 | let library = framework.data.global.get_mut::<Library>().unwrap(); | ||
| 76 | let _ = library.push(item, None); | ||
| 77 | let _ = library.save(); | ||
| 78 | *framework.data.global.get_mut::<Message>().unwrap() = | ||
| 79 | Message::Success(String::from("Item bookmarked")) | ||
| 80 | } | ||
| 81 | Err(e) => { | ||
| 82 | *framework.data.global.get_mut::<Message>().unwrap() = | ||
| 83 | Message::Error(format!("Unknown item: {e}")) | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | ["unmark", id] => { | ||
| 88 | let library = framework.data.global.get_mut::<Library>().unwrap(); | ||
| 89 | |||
| 90 | if library.remove(id) { | ||
| 91 | let _ = library.save(); | ||
| 92 | *framework.data.global.get_mut::<Message>().unwrap() = | ||
| 93 | Message::Success(String::from("Bookmark removed")) | ||
| 94 | } else { | ||
| 95 | *framework.data.global.get_mut::<Message>().unwrap() = | ||
| 96 | Message::Error(String::from("No item with that ID found")) | ||
| 97 | } | ||
| 98 | } | ||
| 99 | ["togglemark", id] => { | ||
| 100 | let library = framework.data.global.get_mut::<Library>().unwrap(); | ||
| 101 | if library.remove(id) { | ||
| 102 | let _ = library.save(); | ||
| 103 | *framework.data.global.get_mut::<Message>().unwrap() = | ||
| 104 | Message::Success(String::from("Bookmark removed")) | ||
| 105 | } else { | ||
| 106 | run_single_command(&["bookmark", id], framework, terminal); | ||
| 107 | } | ||
| 108 | } | ||
| 62 | ["help"] | ["h"] => { | 109 | ["help"] | ["h"] => { |
| 63 | *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from( | 110 | *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from( |
| 64 | "Avaliable commands can be viewed by running `youtube-tui help` in terminal", | 111 | "Avaliable commands can be viewed by running `youtube-tui help` in terminal", |
| @@ -361,6 +408,11 @@ const HELP_MSG: &str = "\x1b[32mYouTube TUI commands\x1b[0m | |||
| 361 | \x1b[33mrun [command]\x1b[0m Runs a system command (e.g. `run firefox example.com`) | 408 | \x1b[33mrun [command]\x1b[0m Runs a system command (e.g. `run firefox example.com`) |
| 362 | \x1b[33mcopy [text]\x1b[0m Copies text to clipboard | 409 | \x1b[33mcopy [text]\x1b[0m Copies text to clipboard |
| 363 | 410 | ||
| 411 | \x1b[91mLIBRARY:\x1b[0m | ||
| 412 | \x1b[33mbookmark [id]\x1b[0m Bookmark item with ID (item must be already loaded) | ||
| 413 | \x1b[33munmark [id]\x1b[0m Remove bookmark item with ID | ||
| 414 | \x1b[33mtogglemark [id]\x1b[0m Toggle bookmark status | ||
| 415 | |||
| 364 | \x1b[91mALT:\x1b[0m | 416 | \x1b[91mALT:\x1b[0m |
| 365 | \x1b[37malts links back to the original command\x1b[30m | 417 | \x1b[37malts links back to the original command\x1b[30m |
| 366 | \x1b[33m[page] (additional options)\x1b[0m `loadpage [page]` | 418 | \x1b[33m[page] (additional options)\x1b[0m `loadpage [page]` |
src/global/structs/library.rs added +24
| @@ -0,0 +1,24 @@ | |||
| 1 | use crate::global::traits::Collection; | ||
| 2 | |||
| 3 | use super::Item; | ||
| 4 | use serde::{Deserialize, Serialize}; | ||
| 5 | use typemap::Key; | ||
| 6 | |||
| 7 | #[derive(Clone, Default, Serialize, Deserialize)] | ||
| 8 | pub struct Library(pub Vec<Item>); | ||
| 9 | |||
| 10 | impl Key for Library { | ||
| 11 | type Value = Self; | ||
| 12 | } | ||
| 13 | |||
| 14 | impl Collection for Library { | ||
| 15 | const INDEX_PATH: &'static str = ".local/share/youtube-tui/library.json"; | ||
| 16 | |||
| 17 | fn items(&self) -> &Vec<Item> { | ||
| 18 | &self.0 | ||
| 19 | } | ||
| 20 | |||
| 21 | fn items_mut(&mut self) -> &mut Vec<Item> { | ||
| 22 | &mut self.0 | ||
| 23 | } | ||
| 24 | } | ||
src/global/structs/mod.rs +2
| @@ -3,6 +3,7 @@ mod errors; | |||
| 3 | mod invidiousclient; | 3 | mod invidiousclient; |
| 4 | mod item; | 4 | mod item; |
| 5 | mod keyaction; | 5 | mod keyaction; |
| 6 | mod library; | ||
| 6 | mod message; | 7 | mod message; |
| 7 | mod page; | 8 | mod page; |
| 8 | mod state_env; | 9 | mod state_env; |
| @@ -14,6 +15,7 @@ pub use errors::*; | |||
| 14 | pub use invidiousclient::*; | 15 | pub use invidiousclient::*; |
| 15 | pub use item::*; | 16 | pub use item::*; |
| 16 | pub use keyaction::*; | 17 | pub use keyaction::*; |
| 18 | pub use library::*; | ||
| 17 | pub use message::*; | 19 | pub use message::*; |
| 18 | pub use page::*; | 20 | pub use page::*; |
| 19 | pub use state_env::*; | 21 | pub use state_env::*; |
src/global/structs/watchhistory.rs +8 −145
| @@ -1,11 +1,7 @@ | |||
| 1 | use crate::global::traits::Collection; | ||
| 2 | |||
| 1 | use super::Item; | 3 | use super::Item; |
| 2 | use home::home_dir; | ||
| 3 | use serde::{Deserialize, Serialize}; | 4 | use serde::{Deserialize, Serialize}; |
| 4 | use std::{ | ||
| 5 | error::Error, | ||
| 6 | fs::{self, OpenOptions}, | ||
| 7 | io::Write, | ||
| 8 | }; | ||
| 9 | use typemap::Key; | 5 | use typemap::Key; |
| 10 | 6 | ||
| 11 | #[derive(Clone, Default, Serialize, Deserialize)] | 7 | #[derive(Clone, Default, Serialize, Deserialize)] |
| @@ -15,147 +11,14 @@ impl Key for WatchHistory { | |||
| 15 | type Value = Self; | 11 | type Value = Self; |
| 16 | } | 12 | } |
| 17 | 13 | ||
| 18 | impl WatchHistory { | 14 | impl Collection for WatchHistory { |
| 19 | /// saves the current state of watch history into a file | 15 | const INDEX_PATH: &'static str = ".local/share/youtube-tui/watch_history.json"; |
| 20 | pub fn save(&self) -> Result<(), Box<dyn Error>> { | ||
| 21 | let save_string = serde_json::to_string_pretty( | ||
| 22 | &self | ||
| 23 | .0 | ||
| 24 | .iter() | ||
| 25 | .map(|item| item.id().unwrap_or_default()) | ||
| 26 | .filter(|id| !id.is_empty()) | ||
| 27 | .collect::<Vec<&str>>(), | ||
| 28 | )?; | ||
| 29 | let path = home_dir() | ||
| 30 | .unwrap() | ||
| 31 | .join(".local/share/youtube-tui/watch_history/index.json"); | ||
| 32 | |||
| 33 | let mut file = OpenOptions::new() | ||
| 34 | .write(true) | ||
| 35 | .create(true) | ||
| 36 | .truncate(true) | ||
| 37 | .open(path)?; | ||
| 38 | |||
| 39 | file.write_all(save_string.as_bytes())?; | ||
| 40 | |||
| 41 | Ok(()) | ||
| 42 | } | ||
| 43 | |||
| 44 | /// add an item to watch history | ||
| 45 | pub fn push(&mut self, item: Item, max_items: usize) -> Result<(), Box<dyn Error>> { | ||
| 46 | let info = home_dir() | ||
| 47 | .unwrap() | ||
| 48 | .join(".local/share/youtube-tui/watch_history/info/"); | ||
| 49 | |||
| 50 | // removes duplicates and place them on top (if exists) | ||
| 51 | let id = item.id().unwrap_or("invalid-dump"); | ||
| 52 | if let Some(index) = self | ||
| 53 | .0 | ||
| 54 | .iter() | ||
| 55 | .position(|item_in_iter| item_in_iter.id().unwrap_or_default() == id) | ||
| 56 | { | ||
| 57 | let _ = fs::remove_file(info.join(format!("{id}.json"))); | ||
| 58 | self.0.remove(index); | ||
| 59 | } | ||
| 60 | let mut file = OpenOptions::new() | ||
| 61 | .write(true) | ||
| 62 | .create(true) | ||
| 63 | .truncate(true) | ||
| 64 | .open(info.join(format!("{id}.json")))?; | ||
| 65 | let item_string = serde_json::to_string(&item)?; | ||
| 66 | file.write_all(item_string.as_bytes())?; | ||
| 67 | |||
| 68 | self.0.push(item); | ||
| 69 | |||
| 70 | // if length of history exceeds the maximum history length, removes that oldest item | ||
| 71 | if self.0.len() > max_items { | ||
| 72 | let _ = fs::remove_file( | ||
| 73 | info.join(format!("{}.json", self.0[0].id().unwrap_or("invalid-dump"))), | ||
| 74 | ); | ||
| 75 | self.0.remove(0); | ||
| 76 | } | ||
| 77 | 16 | ||
| 78 | Ok(()) | 17 | fn items(&self) -> &Vec<Item> { |
| 18 | &self.0 | ||
| 79 | } | 19 | } |
| 80 | 20 | ||
| 81 | /// loads watch history from file | 21 | fn items_mut(&mut self) -> &mut Vec<Item> { |
| 82 | pub fn load() -> Self { | 22 | &mut self.0 |
| 83 | let path = home_dir() | ||
| 84 | .unwrap() | ||
| 85 | .join(".local/share/youtube-tui/watch_history/index.json"); | ||
| 86 | let res = (|| -> Result<Vec<String>, Box<dyn Error>> { | ||
| 87 | let file_string = fs::read_to_string(&path)?; | ||
| 88 | let deserialized = serde_json::from_str(&file_string)?; | ||
| 89 | Ok(deserialized) | ||
| 90 | })(); | ||
| 91 | |||
| 92 | // if res is err, then the file either doesn't exist of has be altered incorrectly, in | ||
| 93 | // which case returns Self::default() | ||
| 94 | if let Ok(deserialized) = res { | ||
| 95 | let info = home_dir() | ||
| 96 | .unwrap() | ||
| 97 | .join(".local/share/youtube-tui/watch_history/info/"); | ||
| 98 | Self( | ||
| 99 | deserialized | ||
| 100 | .into_iter() | ||
| 101 | .filter_map(|id| fs::read_to_string(info.join(format!("{id}.json"))).ok()) | ||
| 102 | .filter_map(|file_content| serde_json::from_str(&file_content).ok()) | ||
| 103 | .collect(), | ||
| 104 | ) | ||
| 105 | } else { | ||
| 106 | // if the file does exist, back it up | ||
| 107 | // if it doesn't exist, it will throw an error but we dont care | ||
| 108 | let mut new_path = path.clone(); | ||
| 109 | new_path.pop(); | ||
| 110 | new_path.push(format!("index-{}.json", chrono::offset::Local::now(),)); | ||
| 111 | let _ = fs::rename(&path, &new_path); | ||
| 112 | |||
| 113 | Self::default() | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | /// moves all thumbnails from storage to cache when starting | ||
| 118 | pub fn init_move() { | ||
| 119 | let history_thumbnails_path = home_dir() | ||
| 120 | .unwrap() | ||
| 121 | .join(".local/share/youtube-tui/watch_history/thumbnails/"); | ||
| 122 | let cache_thumbnails_path = home_dir().unwrap().join(".cache/youtube-tui/thumbnails/"); | ||
| 123 | |||
| 124 | fs::read_dir(history_thumbnails_path) | ||
| 125 | .unwrap() | ||
| 126 | .into_iter() | ||
| 127 | .for_each(|path| { | ||
| 128 | let path = path.unwrap().path(); | ||
| 129 | let _ = fs::rename( | ||
| 130 | path.clone(), | ||
| 131 | cache_thumbnails_path.join(path.file_name().unwrap()), | ||
| 132 | ); | ||
| 133 | }); | ||
| 134 | } | ||
| 135 | |||
| 136 | /// moves thumbnails of videos in watch history from cache back to storage when exiting, so that thumbnails can be viewed offline | ||
| 137 | pub fn exit_move(&self) { | ||
| 138 | let history_thumbnails_path = home_dir() | ||
| 139 | .unwrap() | ||
| 140 | .join(".local/share/youtube-tui/watch_history/thumbnails/"); | ||
| 141 | let cache_thumbnails_path = home_dir().unwrap().join(".cache/youtube-tui/thumbnails/"); | ||
| 142 | |||
| 143 | self.0.iter().for_each(|item| { | ||
| 144 | let id = item.id().unwrap(); | ||
| 145 | let _ = fs::rename( | ||
| 146 | cache_thumbnails_path.join(id), | ||
| 147 | history_thumbnails_path.join(id), | ||
| 148 | ); | ||
| 149 | |||
| 150 | if let Ok(fullplaylist) = item.fullplaylist() { | ||
| 151 | fullplaylist.videos.iter().for_each(|video| { | ||
| 152 | let id = video.id().unwrap_or("invalid-dump"); | ||
| 153 | let _ = fs::rename( | ||
| 154 | cache_thumbnails_path.join(id), | ||
| 155 | history_thumbnails_path.join(id), | ||
| 156 | ); | ||
| 157 | }) | ||
| 158 | } | ||
| 159 | }) | ||
| 160 | } | 23 | } |
| 161 | } | 24 | } |
src/global/traits/collection.rs added +152
| @@ -0,0 +1,152 @@ | |||
| 1 | use home::home_dir; | ||
| 2 | use serde::{de::DeserializeOwned, Serialize}; | ||
| 3 | use std::{ | ||
| 4 | error::Error, | ||
| 5 | fs::{self, OpenOptions}, | ||
| 6 | io::Write, | ||
| 7 | }; | ||
| 8 | use typemap::Key; | ||
| 9 | |||
| 10 | use crate::global::structs::Item; | ||
| 11 | |||
| 12 | pub trait Collection | ||
| 13 | where | ||
| 14 | Self: Default + Clone + Serialize + DeserializeOwned + Key, | ||
| 15 | { | ||
| 16 | const INDEX_PATH: &'static str; | ||
| 17 | |||
| 18 | fn items(&self) -> &Vec<Item>; | ||
| 19 | fn items_mut(&mut self) -> &mut Vec<Item>; | ||
| 20 | |||
| 21 | /// saves the current state of watch history into a file | ||
| 22 | fn save(&self) -> Result<(), Box<dyn Error>> { | ||
| 23 | let save_string = serde_json::to_string_pretty( | ||
| 24 | &self | ||
| 25 | .items() | ||
| 26 | .iter() | ||
| 27 | .map(|item| item.id().unwrap_or_default()) | ||
| 28 | .filter(|id| !id.is_empty()) | ||
| 29 | .collect::<Vec<&str>>(), | ||
| 30 | )?; | ||
| 31 | let path = home_dir().unwrap().join(Self::INDEX_PATH); | ||
| 32 | |||
| 33 | let mut file = OpenOptions::new() | ||
| 34 | .write(true) | ||
| 35 | .create(true) | ||
| 36 | .truncate(true) | ||
| 37 | .open(path)?; | ||
| 38 | |||
| 39 | file.write_all(save_string.as_bytes())?; | ||
| 40 | |||
| 41 | Ok(()) | ||
| 42 | } | ||
| 43 | |||
| 44 | /// add an item to watch history | ||
| 45 | fn push(&mut self, item: Item, max_length: Option<usize>) -> Result<(), Box<dyn Error>> { | ||
| 46 | let info = home_dir().unwrap().join(".cache/youtube-tui/info/"); | ||
| 47 | |||
| 48 | // removes duplicates and place them on top (if exists) | ||
| 49 | let id = item.id().unwrap_or("invalid-dump"); | ||
| 50 | if let Some(index) = self | ||
| 51 | .items_mut() | ||
| 52 | .iter_mut() | ||
| 53 | .position(|item_in_iter| item_in_iter.id().unwrap_or_default() == id) | ||
| 54 | { | ||
| 55 | self.items_mut().remove(index); | ||
| 56 | } | ||
| 57 | let mut file = OpenOptions::new() | ||
| 58 | .write(true) | ||
| 59 | .create(true) | ||
| 60 | .truncate(true) | ||
| 61 | .open(info.join(format!("{id}.json")))?; | ||
| 62 | let item_string = serde_json::to_string(&item)?; | ||
| 63 | file.write_all(item_string.as_bytes())?; | ||
| 64 | |||
| 65 | self.items_mut().push(item); | ||
| 66 | |||
| 67 | // if length of history exceeds the maximum history length, removes that oldest item | ||
| 68 | if self.items().len() > max_length.unwrap_or(usize::MAX) { | ||
| 69 | self.items_mut().remove(0); | ||
| 70 | } | ||
| 71 | |||
| 72 | Ok(()) | ||
| 73 | } | ||
| 74 | |||
| 75 | /// loads watch history from file | ||
| 76 | fn load() -> Vec<Item> { | ||
| 77 | let path = home_dir().unwrap().join(Self::INDEX_PATH); | ||
| 78 | let res = (|| -> Result<Vec<String>, Box<dyn Error>> { | ||
| 79 | let file_string = fs::read_to_string(&path)?; | ||
| 80 | let deserialized = serde_json::from_str(&file_string)?; | ||
| 81 | Ok(deserialized) | ||
| 82 | })(); | ||
| 83 | |||
| 84 | // if res is err, then the file either doesn't exist of has be altered incorrectly, in | ||
| 85 | // which case returns Self::default() | ||
| 86 | if let Ok(deserialized) = res { | ||
| 87 | let info = home_dir().unwrap().join(".cache/youtube-tui/info/"); | ||
| 88 | deserialized | ||
| 89 | .into_iter() | ||
| 90 | .filter_map(|id| fs::read_to_string(info.join(format!("{id}.json"))).ok()) | ||
| 91 | .filter_map(|file_content| serde_json::from_str(&file_content).ok()) | ||
| 92 | .collect() | ||
| 93 | } else { | ||
| 94 | // if the file does exist, back it up | ||
| 95 | // if it doesn't exist, it will throw an error but we dont care | ||
| 96 | let mut new_path = path.clone(); | ||
| 97 | new_path.pop(); | ||
| 98 | new_path.push(format!("index-{}.json", chrono::offset::Local::now(),)); | ||
| 99 | let _ = fs::rename(&path, &new_path); | ||
| 100 | |||
| 101 | Vec::new() | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | /// moves thumbnails of videos in watch history from cache back to storage when exiting, so that thumbnails can be viewed offline | ||
| 106 | fn exit_move(&self) { | ||
| 107 | let home_dir = home_dir().unwrap(); | ||
| 108 | let store_thumbnails_path = home_dir.join(".local/share/youtube-tui/thumbnails/"); | ||
| 109 | let store_info_path = home_dir.join(".local/share/youtube-tui/info/"); | ||
| 110 | let cache_thumbnails_path = home_dir.join(".cache/youtube-tui/thumbnails/"); | ||
| 111 | let cache_info_path = home_dir.join(".cache/youtube-tui/info"); | ||
| 112 | |||
| 113 | self.items().iter().for_each(|item| { | ||
| 114 | let id = item.id().unwrap_or("invalid-dump"); | ||
| 115 | let _ = fs::rename( | ||
| 116 | cache_thumbnails_path.join(id), | ||
| 117 | store_thumbnails_path.join(id), | ||
| 118 | ); | ||
| 119 | |||
| 120 | let _ = fs::rename( | ||
| 121 | cache_info_path.join(format!("{id}.json")), | ||
| 122 | store_info_path.join(format!("{id}.json")), | ||
| 123 | ); | ||
| 124 | |||
| 125 | if let Item::FullPlaylist(fullplaylist) = item { | ||
| 126 | fullplaylist.videos.iter().for_each(|video| { | ||
| 127 | let id = video.id().unwrap_or("invalid-dump"); | ||
| 128 | let _ = fs::rename( | ||
| 129 | cache_thumbnails_path.join(id), | ||
| 130 | store_thumbnails_path.join(id), | ||
| 131 | ); | ||
| 132 | }) | ||
| 133 | } | ||
| 134 | }) | ||
| 135 | } | ||
| 136 | |||
| 137 | /// remove item based on their id | ||
| 138 | fn remove(&mut self, id: &str) -> bool { | ||
| 139 | let items = self.items_mut(); | ||
| 140 | |||
| 141 | if let Some(found) = items | ||
| 142 | .iter() | ||
| 143 | .enumerate() | ||
| 144 | .find(|(_index, item)| item.id() == Some(id)) | ||
| 145 | { | ||
| 146 | items.remove(found.0); | ||
| 147 | true | ||
| 148 | } else { | ||
| 149 | false | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
src/global/traits/mod.rs +2
| @@ -1,8 +1,10 @@ | |||
| 1 | //! public traits | 1 | //! public traits |
| 2 | mod asurlstring; | 2 | mod asurlstring; |
| 3 | mod collection; | ||
| 3 | mod configtrait; | 4 | mod configtrait; |
| 4 | mod searchfilteritem; | 5 | mod searchfilteritem; |
| 5 | 6 | ||
| 6 | pub use asurlstring::*; | 7 | pub use asurlstring::*; |
| 8 | pub use collection::*; | ||
| 7 | pub use configtrait::*; | 9 | pub use configtrait::*; |
| 8 | pub use searchfilteritem::*; | 10 | pub use searchfilteritem::*; |
src/init.rs +29 −25
| @@ -4,9 +4,11 @@ use crate::{ | |||
| 4 | CommandsConfigSerde, KeyBindingsConfig, MainConfig, MinDimentions, PagesConfig, Search, | 4 | CommandsConfigSerde, KeyBindingsConfig, MainConfig, MinDimentions, PagesConfig, Search, |
| 5 | }, | 5 | }, |
| 6 | global::{ | 6 | global::{ |
| 7 | functions::run_command, | 7 | functions::{init_move, run_command}, |
| 8 | structs::{InvidiousClient, Message, Page, StateEnvs, Status, Tasks, WatchHistory}, | 8 | structs::{ |
| 9 | traits::ConfigTrait, | 9 | InvidiousClient, Library, Message, Page, StateEnvs, Status, Tasks, WatchHistory, |
| 10 | }, | ||
| 11 | traits::{Collection, ConfigTrait}, | ||
| 10 | }, | 12 | }, |
| 11 | }; | 13 | }; |
| 12 | use home::home_dir; | 14 | use home::home_dir; |
| @@ -24,30 +26,36 @@ pub fn init( | |||
| 24 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, | 26 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, |
| 25 | command: Option<&str>, | 27 | command: Option<&str>, |
| 26 | ) -> Result<(), Box<dyn Error>> { | 28 | ) -> Result<(), Box<dyn Error>> { |
| 27 | // creating files | ||
| 28 | let home_dir = home_dir().unwrap(); | 29 | let home_dir = home_dir().unwrap(); |
| 29 | let cache_thumbnails_path = home_dir.join(".cache/youtube-tui/thumbnails/"); | ||
| 30 | let history_thumbnails_path = | ||
| 31 | home_dir.join(".local/share/youtube-tui/watch_history/thumbnails/"); | ||
| 32 | let history_info_path = home_dir.join(".local/share/youtube-tui/watch_history/info/"); | ||
| 33 | |||
| 34 | if !&cache_thumbnails_path.exists() { | ||
| 35 | fs::create_dir_all(&cache_thumbnails_path).unwrap(); | ||
| 36 | } | ||
| 37 | |||
| 38 | if !&history_thumbnails_path.exists() { | ||
| 39 | fs::create_dir_all(&history_thumbnails_path).unwrap(); | ||
| 40 | } | ||
| 41 | 30 | ||
| 42 | if !&history_info_path.exists() { | 31 | // creating files |
| 43 | fs::create_dir_all(&history_info_path).unwrap(); | 32 | [ |
| 44 | } | 33 | ".cache/youtube-tui/thumbnails/", |
| 34 | ".cache/youtube-tui/info/", | ||
| 35 | ".local/share/youtube-tui/thumbnails/", | ||
| 36 | ".local/share/youtube-tui/info/", | ||
| 37 | ".local/share/youtube-tui/saved/", | ||
| 38 | ] | ||
| 39 | .into_iter() | ||
| 40 | .for_each(|s| { | ||
| 41 | let dir = home_dir.join(s); | ||
| 42 | if !dir.exists() { | ||
| 43 | fs::create_dir_all(dir).unwrap(); | ||
| 44 | } | ||
| 45 | }); | ||
| 45 | 46 | ||
| 46 | // watch history init | 47 | init_move(); |
| 47 | WatchHistory::init_move(); | ||
| 48 | 48 | ||
| 49 | load_configs(&mut framework.split_clean().0).ok(); | 49 | load_configs(&mut framework.split_clean().0).ok(); |
| 50 | 50 | ||
| 51 | framework | ||
| 52 | .data | ||
| 53 | .global | ||
| 54 | .insert::<WatchHistory>(WatchHistory(WatchHistory::load())); | ||
| 55 | framework | ||
| 56 | .data | ||
| 57 | .global | ||
| 58 | .insert::<Library>(Library(Library::load())); | ||
| 51 | framework.data.global.insert::<Message>(Message::None); | 59 | framework.data.global.insert::<Message>(Message::None); |
| 52 | framework.data.global.insert::<Status>(Status { | 60 | framework.data.global.insert::<Status>(Status { |
| 53 | provider: framework.data.global.get::<MainConfig>().unwrap().provider, | 61 | provider: framework.data.global.get::<MainConfig>().unwrap().provider, |
| @@ -101,10 +109,6 @@ pub fn load_configs(framework: &mut FrameworkClean) -> Result<(), Box<dyn Error> | |||
| 101 | .data | 109 | .data |
| 102 | .global | 110 | .global |
| 103 | .insert::<CommandsConfig>(CommandsConfig::from(*CommandsConfigSerde::load()?)); | 111 | .insert::<CommandsConfig>(CommandsConfig::from(*CommandsConfigSerde::load()?)); |
| 104 | framework | ||
| 105 | .data | ||
| 106 | .global | ||
| 107 | .insert::<WatchHistory>(WatchHistory::load()); | ||
| 108 | framework | 112 | framework |
| 109 | .data | 113 | .data |
| 110 | .global | 114 | .global |
src/items/itemlist.rs +17 −15
| @@ -1,5 +1,6 @@ | |||
| 1 | use std::error::Error; | 1 | use std::error::Error; |
| 2 | 2 | ||
| 3 | use home::home_dir; | ||
| 3 | use tui::{ | 4 | use tui::{ |
| 4 | layout::{Constraint, Rect}, | 5 | layout::{Constraint, Rect}, |
| 5 | style::Style, | 6 | style::Style, |
| @@ -16,7 +17,7 @@ use crate::{ | |||
| 16 | functions::{download_all_images, set_envs, update_provider}, | 17 | functions::{download_all_images, set_envs, update_provider}, |
| 17 | structs::{ | 18 | structs::{ |
| 18 | ChannelDisplayPage, ChannelDisplayPageType, FullChannelItem, FullPlaylistItem, | 19 | ChannelDisplayPage, ChannelDisplayPageType, FullChannelItem, FullPlaylistItem, |
| 19 | FullVideoItem, InvidiousClient, Item, KeyAction, MainMenuPage, Message, | 20 | FullVideoItem, InvidiousClient, Item, KeyAction, Library, MainMenuPage, Message, |
| 20 | MiniChannelItem, MiniPlaylistItem, MiniVideoItem, Page, SingleItemPage, StateEnvs, | 21 | MiniChannelItem, MiniPlaylistItem, MiniVideoItem, Page, SingleItemPage, StateEnvs, |
| 21 | Status, Task, Tasks, WatchHistory, | 22 | Status, Task, Tasks, WatchHistory, |
| 22 | }, | 23 | }, |
| @@ -40,7 +41,7 @@ impl ItemList { | |||
| 40 | status: &Status, | 41 | status: &Status, |
| 41 | ) -> Vec<(String, String)> { | 42 | ) -> Vec<(String, String)> { |
| 42 | if self.textlist.items.is_empty() { | 43 | if self.textlist.items.is_empty() { |
| 43 | return vec![]; | 44 | return Vec::new(); |
| 44 | } | 45 | } |
| 45 | 46 | ||
| 46 | match &self.items[self.textlist.selected] { | 47 | match &self.items[self.textlist.selected] { |
| @@ -125,8 +126,14 @@ impl ItemList { | |||
| 125 | 126 | ||
| 126 | /// handles select (enter) | 127 | /// handles select (enter) |
| 127 | fn select_at_cursor(&self, framework: &mut FrameworkClean) { | 128 | fn select_at_cursor(&self, framework: &mut FrameworkClean) { |
| 128 | let page = framework.data.state.get::<Page>().unwrap(); | 129 | let page_to_load = if home_dir() |
| 129 | let page_to_load = if *page == Page::MainMenu(MainMenuPage::History) { | 130 | .unwrap() |
| 131 | .join(format!( | ||
| 132 | ".cache/youtube-tui/info/{}.json", | ||
| 133 | self.items[self.textlist.selected].id().unwrap_or_default() | ||
| 134 | )) | ||
| 135 | .exists() | ||
| 136 | { | ||
| 130 | match &self.items[self.textlist.selected] { | 137 | match &self.items[self.textlist.selected] { |
| 131 | Item::MiniVideo(MiniVideoItem { id, .. }) | 138 | Item::MiniVideo(MiniVideoItem { id, .. }) |
| 132 | | Item::FullVideo(FullVideoItem { id, .. }) => { | 139 | | Item::FullVideo(FullVideoItem { id, .. }) => { |
| @@ -312,20 +319,15 @@ impl FrameworkItem for ItemList { | |||
| 312 | .collect(); | 319 | .collect(); |
| 313 | } | 320 | } |
| 314 | Page::MainMenu(MainMenuPage::Subscriptions) => {} | 321 | Page::MainMenu(MainMenuPage::Subscriptions) => {} |
| 315 | Page::MainMenu(MainMenuPage::Library) => {} | 322 | Page::MainMenu(MainMenuPage::Library) => { |
| 323 | let history = framework.data.global.get::<Library>().unwrap(); | ||
| 324 | self.items = history.0.clone().into_iter().rev().collect(); | ||
| 325 | } | ||
| 316 | Page::MainMenu(MainMenuPage::History) => { | 326 | Page::MainMenu(MainMenuPage::History) => { |
| 317 | // the vector needs to be reversed because the latest watch history is pushed to | 327 | // the vector needs to be reversed because the latest watch history is pushed to |
| 318 | // the back, meaning it needs to be reversed so that the latests one are on top | 328 | // the back, meaning it needs to be reversed so that the latests one are on top |
| 319 | self.items = framework | 329 | let history = framework.data.global.get::<WatchHistory>().unwrap(); |
| 320 | .data | 330 | self.items = history.0.clone().into_iter().rev().collect(); |
| 321 | .global | ||
| 322 | .get::<WatchHistory>() | ||
| 323 | .unwrap() | ||
| 324 | .0 | ||
| 325 | .clone() | ||
| 326 | .into_iter() | ||
| 327 | .rev() | ||
| 328 | .collect(); | ||
| 329 | } | 331 | } |
| 330 | Page::Search(search) => { | 332 | Page::Search(search) => { |
| 331 | self.items = client | 333 | self.items = client |
src/items/singleitem.rs +31 −15
| @@ -9,6 +9,7 @@ use crate::{ | |||
| 9 | InvidiousClient, Item, KeyAction, Message, Page, SingleItemPage, StateEnvs, Status, | 9 | InvidiousClient, Item, KeyAction, Message, Page, SingleItemPage, StateEnvs, Status, |
| 10 | Task, Tasks, WatchHistory, | 10 | Task, Tasks, WatchHistory, |
| 11 | }, | 11 | }, |
| 12 | traits::Collection, | ||
| 12 | }, | 13 | }, |
| 13 | }; | 14 | }; |
| 14 | use home::home_dir; | 15 | use home::home_dir; |
| @@ -70,21 +71,25 @@ pub struct SinglePlaylistItem { | |||
| 70 | 71 | ||
| 71 | impl SingleVideoItem { | 72 | impl SingleVideoItem { |
| 72 | pub fn new(commands: &CommandsConfig) -> Self { | 73 | pub fn new(commands: &CommandsConfig) -> Self { |
| 73 | Self::new_with_map(commands | 74 | Self::new_with_map( |
| 75 | commands | ||
| 74 | .video | 76 | .video |
| 75 | .clone() | 77 | .clone() |
| 76 | .into_iter() | 78 | .into_iter() |
| 77 | .map(|(display, command)| (display, command)) | 79 | .map(|(display, command)| (display, command)) |
| 78 | .collect()) | 80 | .collect(), |
| 81 | ) | ||
| 79 | } | 82 | } |
| 80 | 83 | ||
| 81 | pub fn new_local(commands: &CommandsConfig) -> Self { | 84 | pub fn new_local(commands: &CommandsConfig) -> Self { |
| 82 | Self::new_with_map(commands | 85 | Self::new_with_map( |
| 86 | commands | ||
| 83 | .local_video | 87 | .local_video |
| 84 | .clone() | 88 | .clone() |
| 85 | .into_iter() | 89 | .into_iter() |
| 86 | .map(|(display, command)| (display, command)) | 90 | .map(|(display, command)| (display, command)) |
| 87 | .collect()) | 91 | .collect(), |
| 92 | ) | ||
| 88 | } | 93 | } |
| 89 | 94 | ||
| 90 | pub fn new_with_map(commands: Vec<(String, String)>) -> Self { | 95 | pub fn new_with_map(commands: Vec<(String, String)>) -> Self { |
| @@ -182,21 +187,27 @@ impl SingleVideoItem { | |||
| 182 | 187 | ||
| 183 | impl SinglePlaylistItem { | 188 | impl SinglePlaylistItem { |
| 184 | pub fn new(commands: &CommandsConfig, playlist_items: &[Item]) -> Self { | 189 | pub fn new(commands: &CommandsConfig, playlist_items: &[Item]) -> Self { |
| 185 | Self::new_with_map(commands | 190 | Self::new_with_map( |
| 191 | commands | ||
| 186 | .playlist | 192 | .playlist |
| 187 | .clone() | 193 | .clone() |
| 188 | .into_iter() | 194 | .into_iter() |
| 189 | .map(|(display, command)| (display, command)) | 195 | .map(|(display, command)| (display, command)) |
| 190 | .collect(), playlist_items) | 196 | .collect(), |
| 197 | playlist_items, | ||
| 198 | ) | ||
| 191 | } | 199 | } |
| 192 | 200 | ||
| 193 | pub fn new_local(commands: &CommandsConfig, playlist_items: &[Item]) -> Self { | 201 | pub fn new_local(commands: &CommandsConfig, playlist_items: &[Item]) -> Self { |
| 194 | Self::new_with_map(commands | 202 | Self::new_with_map( |
| 203 | commands | ||
| 195 | .local_playlist | 204 | .local_playlist |
| 196 | .clone() | 205 | .clone() |
| 197 | .into_iter() | 206 | .into_iter() |
| 198 | .map(|(display, command)| (display, command)) | 207 | .map(|(display, command)| (display, command)) |
| 199 | .collect(), playlist_items) | 208 | .collect(), |
| 209 | playlist_items, | ||
| 210 | ) | ||
| 200 | } | 211 | } |
| 201 | 212 | ||
| 202 | pub fn new_with_map(commands: Vec<(String, String)>, playlist_items: &[Item]) -> Self { | 213 | pub fn new_with_map(commands: Vec<(String, String)>, playlist_items: &[Item]) -> Self { |
| @@ -659,9 +670,11 @@ impl FrameworkItem for SingleItem { | |||
| 659 | Some(playlist) | 670 | Some(playlist) |
| 660 | } | 671 | } |
| 661 | SingleItemPage::LocalVideo(id) => { | 672 | SingleItemPage::LocalVideo(id) => { |
| 662 | let s = fs::read_to_string(home_dir().unwrap().join(format!( | 673 | let s = fs::read_to_string( |
| 663 | ".local/share/youtube-tui/watch_history/info/{id}.json" | 674 | home_dir() |
| 664 | )))?; | 675 | .unwrap() |
| 676 | .join(format!(".cache/youtube-tui/info/{id}.json")), | ||
| 677 | )?; | ||
| 665 | self.r#type = SingleItemType::Video(SingleVideoItem::new_local( | 678 | self.r#type = SingleItemType::Video(SingleVideoItem::new_local( |
| 666 | framework.data.global.get::<CommandsConfig>().unwrap(), | 679 | framework.data.global.get::<CommandsConfig>().unwrap(), |
| 667 | )); | 680 | )); |
| @@ -670,9 +683,11 @@ impl FrameworkItem for SingleItem { | |||
| 670 | } | 683 | } |
| 671 | 684 | ||
| 672 | SingleItemPage::LocalPlaylist(id) => { | 685 | SingleItemPage::LocalPlaylist(id) => { |
| 673 | let s = fs::read_to_string(home_dir().unwrap().join(format!( | 686 | let s = fs::read_to_string( |
| 674 | ".local/share/youtube-tui/watch_history/info/{id}.json" | 687 | home_dir() |
| 675 | )))?; | 688 | .unwrap() |
| 689 | .join(format!(".cache/youtube-tui/info/{id}.json")), | ||
| 690 | )?; | ||
| 676 | 691 | ||
| 677 | let playlist: Item = serde_json::from_str(&s)?; | 692 | let playlist: Item = serde_json::from_str(&s)?; |
| 678 | let videos = &playlist.fullplaylist()?.videos; | 693 | let videos = &playlist.fullplaylist()?.videos; |
| @@ -704,13 +719,14 @@ impl FrameworkItem for SingleItem { | |||
| 704 | 719 | ||
| 705 | // push to watch history | 720 | // push to watch history |
| 706 | let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap(); | 721 | let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap(); |
| 707 | watch_history.push(item, max_watch_history)?; | 722 | watch_history.push(item, Some(max_watch_history))?; |
| 708 | watch_history.save()?; | 723 | watch_history.save()?; |
| 709 | } | 724 | } |
| 710 | 725 | ||
| 711 | let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); | 726 | let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); |
| 712 | // need to update provider every time the item loads or else it will display `${provider}` | 727 | // need to update provider every time the item loads or else it will display `${provider}` |
| 713 | // instead of the actual provider (e.g. `YouTube`) | 728 | // instead of the actual provider (e.g. `YouTube`) |
| 729 | |||
| 714 | set_envs( | 730 | set_envs( |
| 715 | self.r#type | 731 | self.r#type |
| 716 | .inflate_load( | 732 | .inflate_load( |