Commit 2737f15310
Unsigned
Layout: unified · split
Cargo.toml +1 −1
| @@ -29,4 +29,4 @@ serde_yaml = "0.8" | |||
| 29 | serde_json = "1" | 29 | serde_json = "1" |
| 30 | 30 | ||
| 31 | [profile.dev.package."*"] | 31 | [profile.dev.package."*"] |
| 32 | opt-level = 3 | 32 | opt-level = 3\ No newline at end of file |
README.md +3 −3
| @@ -1,9 +1,9 @@ | |||
| 1 | ## Warning: | 1 | ## Warning: |
| 2 | * This app is incomplete and is missing many features | 2 | * This app is incomplete but is basically usable |
| 3 | * This app is only for *\*nix* systems | 3 | * This app is only for *\*nix* systems |
| 4 | 4 | ||
| 5 | ## Changes made in this commit | 5 | ## Changes made in this commit |
| 6 | * Added search filters | 6 | * Search support for video (Cannot display channel and playlists yet) |
| 7 | 7 | ||
| 8 | ## Installation | 8 | ## Installation |
| 9 | Requirements: Git, Cargo and the Rust Compiler | 9 | Requirements: Git, Cargo and the Rust Compiler |
| @@ -95,7 +95,7 @@ The config files are located in `~/.config/youtube-tui`, run the program once to | |||
| 95 | > | 95 | > |
| 96 | 96 | ||
| 97 | ## To Do | 97 | ## To Do |
| 98 | * Search | 98 | * Search (For all items) |
| 99 | * Adaptive config files (auto update when config files when there are new options in the config) | 99 | * Adaptive config files (auto update when config files when there are new options in the config) |
| 100 | 100 | ||
| 101 | ## Bugs | 101 | ## Bugs |
src/app/app.rs +34 −1
| @@ -18,7 +18,10 @@ use tui::{ | |||
| 18 | Frame, | 18 | Frame, |
| 19 | }; | 19 | }; |
| 20 | 20 | ||
| 21 | use super::{config::Config, pages::item_info::ItemInfo}; | 21 | use super::{ |
| 22 | config::Config, | ||
| 23 | pages::{item_info::ItemInfo, search::Search}, | ||
| 24 | }; | ||
| 22 | 25 | ||
| 23 | #[derive(Debug)] | 26 | #[derive(Debug)] |
| 24 | pub struct App { | 27 | pub struct App { |
| @@ -36,6 +39,8 @@ pub struct App { | |||
| 36 | pub history: Vec<AppHistory>, | 39 | pub history: Vec<AppHistory>, |
| 37 | pub watch_history: WatchHistory, | 40 | pub watch_history: WatchHistory, |
| 38 | pub search_settings: SearchSettings, | 41 | pub search_settings: SearchSettings, |
| 42 | pub search_text: String, | ||
| 43 | pub page_no: usize, | ||
| 39 | } | 44 | } |
| 40 | 45 | ||
| 41 | impl Default for App { | 46 | impl Default for App { |
| @@ -57,6 +62,8 @@ impl Default for App { | |||
| 57 | config: Config::load().unwrap(), | 62 | config: Config::load().unwrap(), |
| 58 | watch_history: WatchHistory::load(), | 63 | watch_history: WatchHistory::load(), |
| 59 | search_settings: SearchSettings::default(), | 64 | search_settings: SearchSettings::default(), |
| 65 | search_text: String::new(), | ||
| 66 | page_no: 1, | ||
| 60 | } | 67 | } |
| 61 | } | 68 | } |
| 62 | } | 69 | } |
| @@ -72,6 +79,7 @@ impl App { | |||
| 72 | Item::Global(item) => item.selectable(), | 79 | Item::Global(item) => item.selectable(), |
| 73 | Item::MainMenu(item) => item.selectable(), | 80 | Item::MainMenu(item) => item.selectable(), |
| 74 | Item::ItemInfo(item) => item.selectable(), | 81 | Item::ItemInfo(item) => item.selectable(), |
| 82 | Item::Search(item) => item.selectable(), | ||
| 75 | } { | 83 | } { |
| 76 | row_vec.push((x, y)); | 84 | row_vec.push((x, y)); |
| 77 | } | 85 | } |
| @@ -112,6 +120,14 @@ impl App { | |||
| 112 | self.state[y].items[x].item = Item::ItemInfo(item); | 120 | self.state[y].items[x].item = Item::ItemInfo(item); |
| 113 | } | 121 | } |
| 114 | } | 122 | } |
| 123 | Item::Search(item) => { | ||
| 124 | let mut item = item.clone(); | ||
| 125 | let hold = item.key_input(key, self); | ||
| 126 | self = hold.1; | ||
| 127 | if hold.0 { | ||
| 128 | self.state[y].items[x].item = Item::Search(item); | ||
| 129 | } | ||
| 130 | } | ||
| 115 | } | 131 | } |
| 116 | 132 | ||
| 117 | return self; | 133 | return self; |
| @@ -153,6 +169,13 @@ impl App { | |||
| 153 | self.selected = Some((x, y)); | 169 | self.selected = Some((x, y)); |
| 154 | } | 170 | } |
| 155 | } | 171 | } |
| 172 | Item::Search(mut item) => { | ||
| 173 | let held = item.select(self); | ||
| 174 | self = held.0; | ||
| 175 | if held.1 { | ||
| 176 | self.selected = Some((x, y)); | ||
| 177 | } | ||
| 178 | } | ||
| 156 | } | 179 | } |
| 157 | 180 | ||
| 158 | return self; | 181 | return self; |
| @@ -236,6 +259,7 @@ impl App { | |||
| 236 | let min = match self.page { | 259 | let min = match self.page { |
| 237 | Page::MainMenu(_) => MainMenu::min(), | 260 | Page::MainMenu(_) => MainMenu::min(), |
| 238 | Page::ItemDisplay(_) => ItemInfo::min(), | 261 | Page::ItemDisplay(_) => ItemInfo::min(), |
| 262 | Page::Search => Search::min(), | ||
| 239 | }; | 263 | }; |
| 240 | 264 | ||
| 241 | if size.width < min.0 || size.height < min.1 { | 265 | if size.width < min.0 || size.height < min.1 { |
| @@ -324,6 +348,7 @@ impl App { | |||
| 324 | false, | 348 | false, |
| 325 | &*self.message.lock().unwrap(), | 349 | &*self.message.lock().unwrap(), |
| 326 | &mut self.search_settings, | 350 | &mut self.search_settings, |
| 351 | &self.search_text, | ||
| 327 | ), | 352 | ), |
| 328 | Item::MainMenu(i) => { | 353 | Item::MainMenu(i) => { |
| 329 | i.render_item(frame, chunk, selected, hover, self.popup_focus, &self.page); | 354 | i.render_item(frame, chunk, selected, hover, self.popup_focus, &self.page); |
| @@ -334,6 +359,11 @@ impl App { | |||
| 334 | i.render_item(frame, chunk, selected, hover, self.popup_focus); | 359 | i.render_item(frame, chunk, selected, hover, self.popup_focus); |
| 335 | false | 360 | false |
| 336 | } | 361 | } |
| 362 | |||
| 363 | Item::Search(i) => { | ||
| 364 | i.render_item(frame, chunk, selected, hover, self.popup_focus); | ||
| 365 | false | ||
| 366 | } | ||
| 337 | } { | 367 | } { |
| 338 | popups.push((item, selected, hover, chunk)); | 368 | popups.push((item, selected, hover, chunk)); |
| 339 | } | 369 | } |
| @@ -351,6 +381,7 @@ impl App { | |||
| 351 | true, | 381 | true, |
| 352 | &*self.message.lock().unwrap(), | 382 | &*self.message.lock().unwrap(), |
| 353 | &mut self.search_settings, | 383 | &mut self.search_settings, |
| 384 | &self.search_text, | ||
| 354 | ); | 385 | ); |
| 355 | } | 386 | } |
| 356 | _ => {} // Item::MainMenu(i) => { | 387 | _ => {} // Item::MainMenu(i) => { |
| @@ -387,6 +418,8 @@ impl App { | |||
| 387 | watch_history: self.watch_history, | 418 | watch_history: self.watch_history, |
| 388 | search_settings: self.search_settings, | 419 | search_settings: self.search_settings, |
| 389 | popup_focus: app_history.popup_focus, | 420 | popup_focus: app_history.popup_focus, |
| 421 | search_text: app_history.search_text, | ||
| 422 | page_no: app_history.page_no, | ||
| 390 | }; | 423 | }; |
| 391 | 424 | ||
| 392 | (self, true) | 425 | (self, true) |
src/app/pages/global.rs +33 −10
| @@ -12,14 +12,16 @@ use tui::{ | |||
| 12 | use crate::{ | 12 | use crate::{ |
| 13 | app::app::App, | 13 | app::app::App, |
| 14 | functions::center_rect, | 14 | functions::center_rect, |
| 15 | structs::SearchSettings, | 15 | structs::{SearchSettings, Page}, |
| 16 | traits::{KeyInput, SelectItem}, | 16 | traits::{KeyInput, SelectItem}, |
| 17 | widgets::{force_clear::ForceClear, horizontal_split::HorizontalSplit}, | 17 | widgets::{force_clear::ForceClear, horizontal_split::HorizontalSplit}, |
| 18 | }; | 18 | }; |
| 19 | 19 | ||
| 20 | use super::search::Search; | ||
| 21 | |||
| 20 | #[derive(Debug, Clone)] | 22 | #[derive(Debug, Clone)] |
| 21 | pub enum GlobalItem { | 23 | pub enum GlobalItem { |
| 22 | SearchBar(String), | 24 | SearchBar, |
| 23 | SearchSettings, | 25 | SearchSettings, |
| 24 | MessageBar, | 26 | MessageBar, |
| 25 | } | 27 | } |
| @@ -51,7 +53,7 @@ impl Mode { | |||
| 51 | impl SelectItem for GlobalItem { | 53 | impl SelectItem for GlobalItem { |
| 52 | fn select(&mut self, mut app: App) -> (App, bool) { | 54 | fn select(&mut self, mut app: App) -> (App, bool) { |
| 53 | let selected = match self { | 55 | let selected = match self { |
| 54 | GlobalItem::SearchBar(_) => true, | 56 | GlobalItem::SearchBar => true, |
| 55 | GlobalItem::SearchSettings => { | 57 | GlobalItem::SearchSettings => { |
| 56 | app.popup_focus = true; | 58 | app.popup_focus = true; |
| 57 | true | 59 | true |
| @@ -64,7 +66,7 @@ impl SelectItem for GlobalItem { | |||
| 64 | 66 | ||
| 65 | fn selectable(&self) -> bool { | 67 | fn selectable(&self) -> bool { |
| 66 | match self { | 68 | match self { |
| 67 | GlobalItem::SearchBar(_) | GlobalItem::SearchSettings => true, | 69 | GlobalItem::SearchBar | GlobalItem::SearchSettings => true, |
| 68 | _ => false, | 70 | _ => false, |
| 69 | } | 71 | } |
| 70 | } | 72 | } |
| @@ -73,14 +75,34 @@ impl SelectItem for GlobalItem { | |||
| 73 | impl KeyInput for GlobalItem { | 75 | impl KeyInput for GlobalItem { |
| 74 | fn key_input(&mut self, key: KeyCode, mut app: App) -> (bool, App) { | 76 | fn key_input(&mut self, key: KeyCode, mut app: App) -> (bool, App) { |
| 75 | match self { | 77 | match self { |
| 76 | GlobalItem::SearchBar(search_bar) => match key { | 78 | GlobalItem::SearchBar => match key { |
| 77 | KeyCode::Char(c) => { | 79 | KeyCode::Char(c) => { |
| 78 | search_bar.push(c); | 80 | app.search_text.push(c); |
| 79 | } | 81 | } |
| 80 | KeyCode::Backspace => { | 82 | KeyCode::Backspace => { |
| 81 | search_bar.pop(); | 83 | app.search_text.pop(); |
| 84 | } | ||
| 85 | KeyCode::Enter => { | ||
| 86 | let state = Search::default(); | ||
| 87 | let mut history = app.history.clone(); | ||
| 88 | let search_text = app.search_text.clone(); | ||
| 89 | let search_settings = app.search_settings.clone(); | ||
| 90 | history.push(app.into()); | ||
| 91 | |||
| 92 | return ( | ||
| 93 | false, | ||
| 94 | App { | ||
| 95 | history, | ||
| 96 | page: Page::Search, | ||
| 97 | selectable: App::selectable(&state), | ||
| 98 | state, | ||
| 99 | search_text, | ||
| 100 | search_settings, | ||
| 101 | load: true, | ||
| 102 | ..Default::default() | ||
| 103 | }, | ||
| 104 | ); | ||
| 82 | } | 105 | } |
| 83 | KeyCode::Enter => {} | ||
| 84 | _ => {} | 106 | _ => {} |
| 85 | }, | 107 | }, |
| 86 | GlobalItem::SearchSettings => { | 108 | GlobalItem::SearchSettings => { |
| @@ -103,10 +125,11 @@ impl GlobalItem { | |||
| 103 | popup_render: bool, | 125 | popup_render: bool, |
| 104 | message: &Option<String>, | 126 | message: &Option<String>, |
| 105 | search_settings: &mut SearchSettings, | 127 | search_settings: &mut SearchSettings, |
| 128 | search_text: &String, | ||
| 106 | ) -> bool { | 129 | ) -> bool { |
| 107 | let area = frame.size(); | 130 | let area = frame.size(); |
| 108 | match self { | 131 | match self { |
| 109 | GlobalItem::SearchBar(search) => { | 132 | GlobalItem::SearchBar => { |
| 110 | let block = Block::default() | 133 | let block = Block::default() |
| 111 | .border_type(BorderType::Rounded) | 134 | .border_type(BorderType::Rounded) |
| 112 | .borders(Borders::ALL) | 135 | .borders(Borders::ALL) |
| @@ -119,7 +142,7 @@ impl GlobalItem { | |||
| 119 | })) | 142 | })) |
| 120 | .title("Search YouTube") | 143 | .title("Search YouTube") |
| 121 | .title_alignment(Alignment::Center); | 144 | .title_alignment(Alignment::Center); |
| 122 | let paragraph = Paragraph::new(search.clone()).block(block); | 145 | let paragraph = Paragraph::new(search_text.clone()).block(block); |
| 123 | frame.render_widget(paragraph, rect); | 146 | frame.render_widget(paragraph, rect); |
| 124 | } | 147 | } |
| 125 | GlobalItem::MessageBar => { | 148 | GlobalItem::MessageBar => { |
src/app/pages/item_info.rs +1 −1
| @@ -383,7 +383,7 @@ impl ItemInfo { | |||
| 383 | vec![ | 383 | vec![ |
| 384 | Row { | 384 | Row { |
| 385 | items: vec![RowItem { | 385 | items: vec![RowItem { |
| 386 | item: Item::Global(GlobalItem::SearchBar(String::new())), | 386 | item: Item::Global(GlobalItem::SearchBar), |
| 387 | constraint: Constraint::Percentage(100), | 387 | constraint: Constraint::Percentage(100), |
| 388 | }], | 388 | }], |
| 389 | centered: false, | 389 | centered: false, |
src/app/pages/main_menu.rs +4 −20
| @@ -171,10 +171,7 @@ impl LoadItem for MainMenuItem { | |||
| 171 | if let Ok(file) = file { | 171 | if let Ok(file) = file { |
| 172 | let item: ListItem = serde_json::from_str(&file).unwrap(); | 172 | let item: ListItem = serde_json::from_str(&file).unwrap(); |
| 173 | 173 | ||
| 174 | let title = match &item { | 174 | let title = item.to_string(); |
| 175 | ListItem::FullVideo(v) => v.title.clone(), | ||
| 176 | _ => unreachable!(), | ||
| 177 | }; | ||
| 178 | 175 | ||
| 179 | list.push_back(item); | 176 | list.push_back(item); |
| 180 | text_list.items.push(title); | 177 | text_list.items.push(title); |
| @@ -271,21 +268,8 @@ impl MainMenuItem { | |||
| 271 | } | 268 | } |
| 272 | } | 269 | } |
| 273 | 270 | ||
| 274 | fn textlist_from_video_list(original: &LinkedList<ListItem>) -> Vec<String> { | 271 | pub fn textlist_from_video_list(original: &LinkedList<ListItem>) -> Vec<String> { |
| 275 | original | 272 | original.iter().map(|item| item.to_string()).collect() |
| 276 | .iter() | ||
| 277 | .map(|item| match item { | ||
| 278 | ListItem::MiniVideo(video) => video | ||
| 279 | .title | ||
| 280 | .clone() | ||
| 281 | .chars() | ||
| 282 | .map(|c| if c.is_ascii() { c } else { '?' }) | ||
| 283 | .collect(), | ||
| 284 | _ => { | ||
| 285 | unreachable!() | ||
| 286 | } | ||
| 287 | }) | ||
| 288 | .collect() | ||
| 289 | } | 273 | } |
| 290 | 274 | ||
| 291 | #[derive(Debug, Clone, Copy, PartialEq)] | 275 | #[derive(Debug, Clone, Copy, PartialEq)] |
| @@ -317,7 +301,7 @@ impl MainMenu { | |||
| 317 | Row { | 301 | Row { |
| 318 | items: vec![ | 302 | items: vec![ |
| 319 | RowItem { | 303 | RowItem { |
| 320 | item: Item::Global(GlobalItem::SearchBar(String::new())), | 304 | item: Item::Global(GlobalItem::SearchBar), |
| 321 | constraint: Constraint::Min(16), | 305 | constraint: Constraint::Min(16), |
| 322 | }, | 306 | }, |
| 323 | RowItem { | 307 | RowItem { |
src/app/pages/mod.rs +1
| @@ -1,3 +1,4 @@ | |||
| 1 | pub mod global; | 1 | pub mod global; |
| 2 | pub mod item_info; | 2 | pub mod item_info; |
| 3 | pub mod main_menu; | 3 | pub mod main_menu; |
| 4 | pub mod search;\ No newline at end of file | ||
src/app/pages/search.rs added +308
| @@ -0,0 +1,308 @@ | |||
| 1 | use std::collections::LinkedList; | ||
| 2 | |||
| 3 | use crossterm::event::KeyCode; | ||
| 4 | use invidious::structs::hidden::SearchItem as InvidiousSearchItem; | ||
| 5 | use tui::{ | ||
| 6 | backend::Backend, | ||
| 7 | layout::{Constraint, Rect}, | ||
| 8 | style::{Color, Style}, | ||
| 9 | Frame, | ||
| 10 | }; | ||
| 11 | |||
| 12 | use crate::{ | ||
| 13 | app::app::App, | ||
| 14 | functions::download_all_thumbnails, | ||
| 15 | structs::{Item, ListItem, MiniVideo, Page, Row, RowItem}, | ||
| 16 | traits::{KeyInput, LoadItem, SelectItem}, | ||
| 17 | widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList}, | ||
| 18 | }; | ||
| 19 | |||
| 20 | use super::{ | ||
| 21 | global::GlobalItem, | ||
| 22 | item_info::{DisplayItem, ItemInfo}, | ||
| 23 | main_menu::textlist_from_video_list, | ||
| 24 | }; | ||
| 25 | |||
| 26 | #[derive(Debug, Clone)] | ||
| 27 | pub enum SearchItem { | ||
| 28 | Search { | ||
| 29 | results: Option<LinkedList<ListItem>>, | ||
| 30 | text_list: TextList, | ||
| 31 | }, | ||
| 32 | } | ||
| 33 | |||
| 34 | impl SelectItem for SearchItem { | ||
| 35 | fn select(&mut self, app: App) -> (App, bool) { | ||
| 36 | (app, true) | ||
| 37 | } | ||
| 38 | |||
| 39 | fn selectable(&self) -> bool { | ||
| 40 | true | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | impl KeyInput for SearchItem { | ||
| 45 | fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) { | ||
| 46 | match self { | ||
| 47 | SearchItem::Search { results, text_list } => match key { | ||
| 48 | KeyCode::Up => text_list.up(), | ||
| 49 | KeyCode::Down => text_list.down(), | ||
| 50 | KeyCode::PageUp => text_list.selected = 0, | ||
| 51 | KeyCode::PageDown => text_list.selected = text_list.items.len() - 1, | ||
| 52 | KeyCode::Enter => { | ||
| 53 | if let Some(results_unwrapped) = results { | ||
| 54 | match results_unwrapped.iter().nth(text_list.selected).unwrap() { | ||
| 55 | ListItem::PageTurner(b) => { | ||
| 56 | let new_page_no; | ||
| 57 | if *b { | ||
| 58 | new_page_no = app.page_no + 1; | ||
| 59 | } else { | ||
| 60 | new_page_no = app.page_no - 1; | ||
| 61 | } | ||
| 62 | |||
| 63 | *results = None; | ||
| 64 | *text_list = TextList::default(); | ||
| 65 | |||
| 66 | let state = Search::default(); | ||
| 67 | let mut history = app.history.clone(); | ||
| 68 | let search_text = app.search_text.clone(); | ||
| 69 | let search_settings = app.search_settings.clone(); | ||
| 70 | history.push(app.into()); | ||
| 71 | |||
| 72 | return ( | ||
| 73 | false, | ||
| 74 | App { | ||
| 75 | history, | ||
| 76 | page: Page::Search, | ||
| 77 | selectable: App::selectable(&state), | ||
| 78 | state, | ||
| 79 | search_text, | ||
| 80 | search_settings, | ||
| 81 | load: true, | ||
| 82 | page_no: new_page_no, | ||
| 83 | ..Default::default() | ||
| 84 | }, | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | |||
| 88 | ListItem::MiniVideo(video) => { | ||
| 89 | let state = ItemInfo::default(); | ||
| 90 | let mut history = app.history.clone(); | ||
| 91 | history.push(app.into()); | ||
| 92 | |||
| 93 | return ( | ||
| 94 | false, | ||
| 95 | App { | ||
| 96 | history, | ||
| 97 | page: Page::ItemDisplay(DisplayItem::Video( | ||
| 98 | video.video_id.clone(), | ||
| 99 | )), | ||
| 100 | selectable: App::selectable(&state), | ||
| 101 | state, | ||
| 102 | ..Default::default() | ||
| 103 | }, | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | _ => {} | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | _ => {} | ||
| 112 | }, | ||
| 113 | } | ||
| 114 | |||
| 115 | (true, app) | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | impl LoadItem for SearchItem { | ||
| 120 | fn load_item(&self, app: &App) -> Result<Box<Self>, Box<dyn std::error::Error>> { | ||
| 121 | let mut this = self.clone(); | ||
| 122 | |||
| 123 | match &mut this { | ||
| 124 | SearchItem::Search { results, text_list } => { | ||
| 125 | // panic!("got here"); | ||
| 126 | if app.search_text.len() == 0 { | ||
| 127 | *app.message.lock().unwrap() = | ||
| 128 | Some(String::from("Search term cannot be empty")); | ||
| 129 | } else { | ||
| 130 | let mut args = vec![app.search_text.clone()]; | ||
| 131 | args.extend(app.search_settings.clone().to_vec()); | ||
| 132 | |||
| 133 | let invidious_results = app | ||
| 134 | .client | ||
| 135 | .search(Some(&format!( | ||
| 136 | "page={}&q={}", | ||
| 137 | app.page_no, | ||
| 138 | args.join("&").as_str() | ||
| 139 | )))? | ||
| 140 | .items; | ||
| 141 | |||
| 142 | let mut items = LinkedList::new(); | ||
| 143 | |||
| 144 | for item in invidious_results { | ||
| 145 | match item { | ||
| 146 | InvidiousSearchItem::Video { | ||
| 147 | title: _, | ||
| 148 | videoId: _, | ||
| 149 | author: _, | ||
| 150 | authorId: _, | ||
| 151 | authorUrl: _, | ||
| 152 | lengthSeconds: _, | ||
| 153 | videoThumbnails: _, | ||
| 154 | description: _, | ||
| 155 | descriptionHtml: _, | ||
| 156 | viewCount: _, | ||
| 157 | published: _, | ||
| 158 | publishedText: _, | ||
| 159 | liveNow: _, | ||
| 160 | paid: _, | ||
| 161 | premium: _, | ||
| 162 | } => { | ||
| 163 | items.push_back(ListItem::MiniVideo(MiniVideo::from(item))); | ||
| 164 | } | ||
| 165 | |||
| 166 | _ => {} | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | let mut minimum_length = 0; | ||
| 171 | |||
| 172 | if app.page_no != 1 { | ||
| 173 | minimum_length += 1; | ||
| 174 | items.push_front(ListItem::PageTurner(false)); | ||
| 175 | } | ||
| 176 | |||
| 177 | if items.len() == minimum_length { | ||
| 178 | *app.message.lock().unwrap() = Some(String::from("No results found")); | ||
| 179 | } else { | ||
| 180 | items.push_back(ListItem::PageTurner(true)); | ||
| 181 | } | ||
| 182 | |||
| 183 | text_list.items = textlist_from_video_list(&items); | ||
| 184 | |||
| 185 | let mut thumbnails = LinkedList::new(); | ||
| 186 | |||
| 187 | for item in items.iter() { | ||
| 188 | match item { | ||
| 189 | ListItem::MiniVideo(video) => { | ||
| 190 | thumbnails.push_back(( | ||
| 191 | video.video_thumbnail.clone(), | ||
| 192 | video.video_id.clone(), | ||
| 193 | )); | ||
| 194 | } | ||
| 195 | |||
| 196 | _ => {} | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | let _ = download_all_thumbnails(thumbnails); | ||
| 201 | |||
| 202 | *results = Some(items); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | Ok(Box::new(this)) | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | impl SearchItem { | ||
| 212 | pub fn render_item<B: Backend>( | ||
| 213 | &mut self, | ||
| 214 | frame: &mut Frame<B>, | ||
| 215 | rect: Rect, | ||
| 216 | selected: bool, | ||
| 217 | hover: bool, | ||
| 218 | popup_focus: bool, | ||
| 219 | ) { | ||
| 220 | |||
| 221 | match self { | ||
| 222 | SearchItem::Search { results, text_list } => { | ||
| 223 | let split = HorizontalSplit::default() | ||
| 224 | .percentages(vec![60, 40]) | ||
| 225 | .border_style(Style::default().fg(if selected { | ||
| 226 | Color::LightBlue | ||
| 227 | } else if hover { | ||
| 228 | Color::LightRed | ||
| 229 | } else { | ||
| 230 | Color::Reset | ||
| 231 | })); | ||
| 232 | |||
| 233 | let chunks = split.inner(rect); | ||
| 234 | |||
| 235 | frame.render_widget(split, rect); | ||
| 236 | |||
| 237 | text_list.area(chunks[0]); | ||
| 238 | let mut text_list = text_list.clone(); | ||
| 239 | |||
| 240 | if let Some(result) = results { | ||
| 241 | if let Some(item) = result.iter().nth(text_list.selected) { | ||
| 242 | if !popup_focus { | ||
| 243 | frame.render_widget(ItemDisplay { item: item.clone() }, chunks[1]); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | if selected { | ||
| 249 | text_list.selected_style(Style::default().fg(Color::LightRed)); | ||
| 250 | } else { | ||
| 251 | text_list.selected_style(Style::default().fg(Color::LightYellow)); | ||
| 252 | } | ||
| 253 | |||
| 254 | frame.render_widget(text_list, chunks[0]); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | pub struct Search; | ||
| 261 | |||
| 262 | impl Search { | ||
| 263 | pub fn message() -> String { | ||
| 264 | String::from("Loading search results...") | ||
| 265 | } | ||
| 266 | |||
| 267 | pub fn min() -> (u16, u16) { | ||
| 268 | (45, 12) | ||
| 269 | } | ||
| 270 | |||
| 271 | pub fn default() -> Vec<Row> { | ||
| 272 | vec![ | ||
| 273 | Row { | ||
| 274 | items: vec![ | ||
| 275 | RowItem { | ||
| 276 | item: Item::Global(GlobalItem::SearchBar), | ||
| 277 | constraint: Constraint::Min(16), | ||
| 278 | }, | ||
| 279 | RowItem { | ||
| 280 | item: Item::Global(GlobalItem::SearchSettings), | ||
| 281 | constraint: Constraint::Length(5), | ||
| 282 | }, | ||
| 283 | ], | ||
| 284 | centered: false, | ||
| 285 | height: Constraint::Length(3), | ||
| 286 | }, | ||
| 287 | Row { | ||
| 288 | items: vec![RowItem { | ||
| 289 | item: Item::Search(SearchItem::Search { | ||
| 290 | results: None, | ||
| 291 | text_list: TextList::default(), | ||
| 292 | }), | ||
| 293 | constraint: Constraint::Percentage(100), | ||
| 294 | }], | ||
| 295 | centered: false, | ||
| 296 | height: Constraint::Min(6), | ||
| 297 | }, | ||
| 298 | Row { | ||
| 299 | items: vec![RowItem { | ||
| 300 | item: Item::Global(GlobalItem::MessageBar), | ||
| 301 | constraint: Constraint::Percentage(100), | ||
| 302 | }], | ||
| 303 | centered: false, | ||
| 304 | height: Constraint::Length(3), | ||
| 305 | }, | ||
| 306 | ] | ||
| 307 | } | ||
| 308 | } | ||
src/functions/download_all_thumbnails.rs +1 −1
| @@ -9,7 +9,7 @@ pub enum ItemType { | |||
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | pub fn download_all_thumbnails( | 11 | pub fn download_all_thumbnails( |
| 12 | list: LinkedList<(String, String)>, | 12 | list: LinkedList<(String, String)>, // (url, id) |
| 13 | ) -> Result<(), Box<dyn Error>> { | 13 | ) -> Result<(), Box<dyn Error>> { |
| 14 | thread::spawn(move || { | 14 | thread::spawn(move || { |
| 15 | let rt: Runtime = tokio::runtime::Runtime::new().unwrap(); | 15 | let rt: Runtime = tokio::runtime::Runtime::new().unwrap(); |
src/main.rs +17 −1
| @@ -11,7 +11,7 @@ use tui::{ | |||
| 11 | use youtube_tui::{ | 11 | use youtube_tui::{ |
| 12 | app::{ | 12 | app::{ |
| 13 | app::App, | 13 | app::App, |
| 14 | pages::{item_info::ItemInfo, main_menu::MainMenu}, | 14 | pages::{item_info::ItemInfo, main_menu::MainMenu, search::Search}, |
| 15 | }, | 15 | }, |
| 16 | structs::{Item, Page, Row, RowItem}, | 16 | structs::{Item, Page, Row, RowItem}, |
| 17 | traits::LoadItem, | 17 | traits::LoadItem, |
| @@ -53,6 +53,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<( | |||
| 53 | *app.message.lock().unwrap() = Some(match app.page { | 53 | *app.message.lock().unwrap() = Some(match app.page { |
| 54 | Page::MainMenu(_) => MainMenu::message(), | 54 | Page::MainMenu(_) => MainMenu::message(), |
| 55 | Page::ItemDisplay(_) => ItemInfo::message(), | 55 | Page::ItemDisplay(_) => ItemInfo::message(), |
| 56 | Page::Search => Search::message(), | ||
| 56 | }); | 57 | }); |
| 57 | terminal.clear()?; | 58 | terminal.clear()?; |
| 58 | ui(&mut terminal, &mut app)?; | 59 | ui(&mut terminal, &mut app)?; |
| @@ -91,6 +92,21 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<( | |||
| 91 | *app.message.lock().unwrap() = Some(e.to_string()); | 92 | *app.message.lock().unwrap() = Some(e.to_string()); |
| 92 | } | 93 | } |
| 93 | }, | 94 | }, |
| 95 | Item::Search(item) => match item.load_item(&app) { | ||
| 96 | Ok(new) => { | ||
| 97 | row_vec.push(RowItem { | ||
| 98 | item: Item::Search(*new), | ||
| 99 | ..*row_item | ||
| 100 | }); | ||
| 101 | } | ||
| 102 | Err(e) => { | ||
| 103 | row_vec.push(RowItem { | ||
| 104 | item: Item::Search(item.clone()), | ||
| 105 | ..*row_item | ||
| 106 | }); | ||
| 107 | *app.message.lock().unwrap() = Some(e.to_string()); | ||
| 108 | } | ||
| 109 | }, | ||
| 94 | _ => { | 110 | _ => { |
| 95 | row_vec.push(RowItem { | 111 | row_vec.push(RowItem { |
| 96 | item: row_item.item.clone(), | 112 | item: row_item.item.clone(), |
src/structs/apphistory.rs +8
| @@ -7,6 +7,8 @@ use crate::{ | |||
| 7 | structs::{Page, Row}, | 7 | structs::{Page, Row}, |
| 8 | }; | 8 | }; |
| 9 | 9 | ||
| 10 | use super::SearchSettings; | ||
| 11 | |||
| 10 | #[derive(Debug, Clone)] | 12 | #[derive(Debug, Clone)] |
| 11 | pub struct AppHistory { | 13 | pub struct AppHistory { |
| 12 | pub page: Page, | 14 | pub page: Page, |
| @@ -19,6 +21,9 @@ pub struct AppHistory { | |||
| 19 | pub load: bool, | 21 | pub load: bool, |
| 20 | pub render: bool, | 22 | pub render: bool, |
| 21 | pub popup_focus: bool, | 23 | pub popup_focus: bool, |
| 24 | pub search_text: String, | ||
| 25 | pub search_settings: SearchSettings, | ||
| 26 | pub page_no: usize, | ||
| 22 | } | 27 | } |
| 23 | 28 | ||
| 24 | impl From<App> for AppHistory { | 29 | impl From<App> for AppHistory { |
| @@ -34,6 +39,9 @@ impl From<App> for AppHistory { | |||
| 34 | load: original.load, | 39 | load: original.load, |
| 35 | render: original.render, | 40 | render: original.render, |
| 36 | popup_focus: original.popup_focus, | 41 | popup_focus: original.popup_focus, |
| 42 | search_text: original.search_text, | ||
| 43 | search_settings: original.search_settings, | ||
| 44 | page_no: original.page_no, | ||
| 37 | } | 45 | } |
| 38 | } | 46 | } |
| 39 | } | 47 | } |
src/structs/listitem.rs +31
| @@ -18,4 +18,35 @@ impl ListItem { | |||
| 18 | } | 18 | } |
| 19 | } | 19 | } |
| 20 | } | 20 | } |
| 21 | |||
| 22 | pub fn is_page_turner(&self) -> bool { | ||
| 23 | match self { | ||
| 24 | ListItem::PageTurner(_) => true, | ||
| 25 | _ => false, | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | pub fn to_string(&self) -> String { | ||
| 30 | match self { | ||
| 31 | ListItem::MiniVideo(video) => video | ||
| 32 | .title | ||
| 33 | .chars() | ||
| 34 | .into_iter() | ||
| 35 | .filter(|c| !c.is_ascii_control()) | ||
| 36 | .collect(), | ||
| 37 | ListItem::FullVideo(video) => video | ||
| 38 | .title | ||
| 39 | .chars() | ||
| 40 | .into_iter() | ||
| 41 | .filter(|c| !c.is_ascii_control()) | ||
| 42 | .collect(), | ||
| 43 | ListItem::PageTurner(turner) => { | ||
| 44 | if *turner { | ||
| 45 | String::from("Next Page") | ||
| 46 | } else { | ||
| 47 | String::from("Previous Page") | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 21 | } | 52 | } |
src/structs/minivideo.rs +35 −1
| @@ -1,5 +1,5 @@ | |||
| 1 | use chrono::{prelude::*, Utc}; | 1 | use chrono::{prelude::*, Utc}; |
| 2 | use invidious::structs::hidden::{PopularItem, TrendingVideo}; | 2 | use invidious::structs::hidden::{PopularItem, TrendingVideo, SearchItem}; |
| 3 | use thousands::Separable; | 3 | use thousands::Separable; |
| 4 | use serde::{Deserialize, Serialize}; | 4 | use serde::{Deserialize, Serialize}; |
| 5 | 5 | ||
| @@ -71,3 +71,37 @@ impl From<PopularItem> for MiniVideo { | |||
| 71 | } | 71 | } |
| 72 | } | 72 | } |
| 73 | } | 73 | } |
| 74 | |||
| 75 | impl From<SearchItem> for MiniVideo { | ||
| 76 | fn from(original: SearchItem) -> Self { | ||
| 77 | match original { | ||
| 78 | SearchItem::Video { title, videoId, author, authorId: _, authorUrl, lengthSeconds, videoThumbnails, description, descriptionHtml: _, viewCount, published, publishedText, liveNow: _, paid: _, premium: _ } => { | ||
| 79 | MiniVideo { | ||
| 80 | title: title, | ||
| 81 | video_id: videoId, | ||
| 82 | video_thumbnail: videoThumbnails[0].url.clone(), | ||
| 83 | length: hrtime::from_sec_padded(lengthSeconds as u64), | ||
| 84 | view_count: viewCount.separate_with_commas(), | ||
| 85 | author: author, | ||
| 86 | author_url: authorUrl, | ||
| 87 | published: format!( | ||
| 88 | "{}{}", | ||
| 89 | publishedText, | ||
| 90 | if Utc::now().timestamp() - published as i64 > 86400 { | ||
| 91 | let datetime: DateTime<Utc> = DateTime::from_utc( | ||
| 92 | NaiveDateTime::from_timestamp(published as i64, 0), | ||
| 93 | Utc, | ||
| 94 | ); | ||
| 95 | format!(" [{}]", datetime.format("%Y/%m/%d")) | ||
| 96 | } else { | ||
| 97 | String::new() | ||
| 98 | } | ||
| 99 | ), | ||
| 100 | description: description, | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | _ => unreachable!(), | ||
| 105 | } | ||
| 106 | } | ||
| 107 | }\ No newline at end of file | ||
src/structs/page.rs +1
| @@ -4,6 +4,7 @@ use crate::app::pages::{item_info::DisplayItem, main_menu::MainMenuSelector}; | |||
| 4 | pub enum Page { | 4 | pub enum Page { |
| 5 | MainMenu(MainMenuSelector), | 5 | MainMenu(MainMenuSelector), |
| 6 | ItemDisplay(DisplayItem), | 6 | ItemDisplay(DisplayItem), |
| 7 | Search, | ||
| 7 | } | 8 | } |
| 8 | 9 | ||
| 9 | impl Default for Page { | 10 | impl Default for Page { |
src/structs/search_settings.rs +3 −3
| @@ -108,9 +108,9 @@ impl SearchSettings { | |||
| 108 | *self = Self::default(); | 108 | *self = Self::default(); |
| 109 | self.text_list.selected = 4; | 109 | self.text_list.selected = 4; |
| 110 | self.update_left(); | 110 | self.update_left(); |
| 111 | } else { | ||
| 112 | self.row = false; | ||
| 113 | } | 111 | } |
| 112 | |||
| 113 | self.row = !self.row; | ||
| 114 | } | 114 | } |
| 115 | _ => {} | 115 | _ => {} |
| 116 | } | 116 | } |
| @@ -159,7 +159,7 @@ pub enum SearchSettingsSortBy { | |||
| 159 | impl AsUrlString for SearchSettingsSortBy { | 159 | impl AsUrlString for SearchSettingsSortBy { |
| 160 | fn as_url_string(&self) -> String { | 160 | fn as_url_string(&self) -> String { |
| 161 | format!( | 161 | format!( |
| 162 | "sort_by={}", | 162 | "sort={}", |
| 163 | match self { | 163 | match self { |
| 164 | SearchSettingsSortBy::Relevance => String::from("relevance"), | 164 | SearchSettingsSortBy::Relevance => String::from("relevance"), |
| 165 | SearchSettingsSortBy::Rating => String::from("rating"), | 165 | SearchSettingsSortBy::Rating => String::from("rating"), |
src/structs/states.rs +2 −1
| @@ -1,12 +1,13 @@ | |||
| 1 | use tui::layout::Constraint; | 1 | use tui::layout::Constraint; |
| 2 | 2 | ||
| 3 | use crate::app::pages::{global::GlobalItem, item_info::ItemInfoItem, main_menu::MainMenuItem}; | 3 | use crate::app::pages::{global::GlobalItem, item_info::ItemInfoItem, main_menu::MainMenuItem, search::SearchItem}; |
| 4 | 4 | ||
| 5 | #[derive(Debug, Clone)] | 5 | #[derive(Debug, Clone)] |
| 6 | pub enum Item { | 6 | pub enum Item { |
| 7 | Global(GlobalItem), | 7 | Global(GlobalItem), |
| 8 | MainMenu(MainMenuItem), | 8 | MainMenu(MainMenuItem), |
| 9 | ItemInfo(ItemInfoItem), | 9 | ItemInfo(ItemInfoItem), |
| 10 | Search(SearchItem), | ||
| 10 | } | 11 | } |
| 11 | 12 | ||
| 12 | #[derive(Debug, Clone)] | 13 | #[derive(Debug, Clone)] |