src/app/pages/item_info.rs
846 lines · 37402 bytes
1use std::{
2 collections::LinkedList,
3 error::Error,
4 process::Command,
5 sync::{Arc, Mutex},
6 thread,
7};
8
9use crate::{
10 app::{app::App, config::EnvVar, pages::global::*},
11 functions::download_all_thumbnails,
12 structs::{FullPlayList, FullVideo, Item, ListItem, Page, Row, RowItem, WatchHistory},
13 traits::{KeyInput, SelectItem},
14 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
15};
16use crossterm::event::KeyCode;
17use tui::{
18 backend::Backend,
19 layout::{Constraint, Rect},
20 style::{Color, Style},
21 Frame,
22};
23
24use super::global::GlobalItem;
25
26#[derive(Debug, Clone)]
27pub enum ItemInfoItem {
28 Unknown,
29 Video(VideoItemInfo),
30 PlayList(PlayListItemInfo),
31}
32
33#[derive(Debug, Clone)]
34pub struct VideoItemInfo {
35 video: FullVideo,
36 list: TextList,
37 mode: Mode,
38}
39
40#[derive(Debug, Clone)]
41pub struct PlayListItemInfo {
42 playlist: FullPlayList,
43 video_view_list: TextList,
44 playlist_view_list: TextList,
45 display_view: PlayListDisplayView,
46 mode: Mode,
47}
48
49#[derive(Debug, Clone, Copy)]
50pub enum PlayListDisplayView {
51 VideoList,
52 PlayListView,
53}
54
55impl PlayListDisplayView {
56 pub fn toggle(self) -> Self {
57 match self {
58 PlayListDisplayView::VideoList => PlayListDisplayView::PlayListView,
59 PlayListDisplayView::PlayListView => PlayListDisplayView::VideoList,
60 }
61 }
62}
63
64impl SelectItem for ItemInfoItem {
65 fn select(&mut self, app: App) -> (App, bool) {
66 let selected = true;
67
68 (app, selected)
69 }
70
71 fn selectable(&self) -> bool {
72 true
73 }
74}
75
76impl KeyInput for ItemInfoItem {
77 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
78 match self {
79 ItemInfoItem::Video(videoinfo) => match key {
80 KeyCode::Up => videoinfo.list.up(),
81 KeyCode::Down => videoinfo.list.down(),
82 KeyCode::PageUp => videoinfo.list.selected = 0,
83 KeyCode::PageDown => videoinfo.list.selected = videoinfo.list.items.len() - 1,
84 KeyCode::Enter => {
85 if videoinfo.list.selected < 5 {
86 *app.message.lock().unwrap() = Some(String::from("Launched application"));
87 }
88
89 match videoinfo.list.selected {
90 0 => {
91 let command =
92 match app.config.commands.video_player.clone().as_command_vec(
93 EnvVar {
94 url: Some(match videoinfo.mode {
95 Mode::Youtube => {
96 format!(
97 "https://youtu.be/{}",
98 videoinfo.video.video_id
99 )
100 }
101 Mode::Invidious => {
102 format!(
103 "{}/embed/{}",
104 app.client.server, videoinfo.video.video_id
105 )
106 }
107 }),
108 ..Default::default()
109 },
110 &app.config,
111 ) {
112 Ok(command) => command,
113 Err(_) => {
114 *app.message.lock().unwrap() =
115 Some(String::from("Error in parsing launch command"));
116 return (false, app);
117 }
118 };
119
120 run_command(command, app.message.clone());
121 }
122 1 => {
123 let command =
124 match app.config.commands.audio_player.clone().as_command_vec(
125 EnvVar {
126 url: Some(match videoinfo.mode {
127 Mode::Youtube => {
128 format!(
129 "https://youtu.be/{}",
130 videoinfo.video.video_id
131 )
132 }
133 Mode::Invidious => {
134 format!(
135 "{}/embed/{}",
136 app.client.server, videoinfo.video.video_id
137 )
138 }
139 }),
140 ..Default::default()
141 },
142 &app.config,
143 ) {
144 Ok(command) => command,
145 Err(_) => {
146 *app.message.lock().unwrap() =
147 Some(String::from("Error in parsing launch command"));
148 return (false, app);
149 }
150 };
151
152 run_command(command, app.message.clone());
153 }
154 2 => {
155 let command =
156 match app.config.commands.video_downloader.clone().as_command_vec(
157 EnvVar {
158 url: Some(match videoinfo.mode {
159 Mode::Youtube => {
160 format!(
161 "https://youtu.be/{}",
162 videoinfo.video.video_id
163 )
164 }
165 Mode::Invidious => {
166 format!(
167 "{}/embed/{}",
168 app.client.server, videoinfo.video.video_id
169 )
170 }
171 }),
172 ..Default::default()
173 },
174 &app.config,
175 ) {
176 Ok(command) => command,
177 Err(_) => {
178 *app.message.lock().unwrap() =
179 Some(String::from("Error in parsing launch command"));
180 return (false, app);
181 }
182 };
183
184 run_command(command, app.message.clone());
185 }
186 3 => {
187 let command =
188 match app.config.commands.audio_downloader.clone().as_command_vec(
189 EnvVar {
190 url: Some(match videoinfo.mode {
191 Mode::Youtube => {
192 format!(
193 "https://youtu.be/{}",
194 videoinfo.video.video_id
195 )
196 }
197 Mode::Invidious => {
198 format!(
199 "{}/embed/{}",
200 app.client.server, videoinfo.video.video_id
201 )
202 }
203 }),
204 ..Default::default()
205 },
206 &app.config,
207 ) {
208 Ok(command) => command,
209 Err(_) => {
210 *app.message.lock().unwrap() =
211 Some(String::from("Error in parsing launch command"));
212 return (false, app);
213 }
214 };
215
216 run_command(command, app.message.clone());
217 }
218 4 => {
219 let mut dir = home::home_dir().unwrap();
220
221 dir.push(".cache");
222 dir.push("youtube-tui");
223 dir.push("thumbnails");
224 dir.push(format!("{}.png", videoinfo.video.video_id));
225
226 let command =
227 match app.config.commands.image_viewer.clone().as_command_vec(
228 EnvVar {
229 url: Some((*dir.as_path().to_string_lossy()).to_string()),
230 ..Default::default()
231 },
232 &app.config,
233 ) {
234 Ok(command) => command,
235 Err(_) => {
236 *app.message.lock().unwrap() =
237 Some(String::from("Error in parsing launch command"));
238 return (false, app);
239 }
240 };
241
242 run_command(command, app.message.clone());
243 }
244 5 => {
245 *app.message.lock().unwrap() = Some(String::from(
246 "Too lazy to implement that what do you expect?",
247 ));
248 }
249 6 => {
250 videoinfo.mode.toggle();
251 }
252
253 _ => {}
254 }
255 }
256 _ => {}
257 },
258
259 ItemInfoItem::PlayList(playlistinfo) => match playlistinfo.display_view {
260 PlayListDisplayView::PlayListView => match key {
261 KeyCode::Up => playlistinfo.playlist_view_list.up(),
262 KeyCode::Down => playlistinfo.playlist_view_list.down(),
263 KeyCode::PageUp => playlistinfo.playlist_view_list.selected = 0,
264 KeyCode::PageDown => {
265 playlistinfo.playlist_view_list.selected =
266 playlistinfo.playlist_view_list.items.len() - 1
267 }
268 KeyCode::Enter => {
269 if playlistinfo.playlist_view_list.selected > 0
270 && playlistinfo.playlist_view_list.selected < 7
271 {
272 *app.message.lock().unwrap() =
273 Some(String::from("Launched application"));
274 }
275
276 match playlistinfo.playlist_view_list.selected {
277 0 => {
278 playlistinfo.display_view = playlistinfo.display_view.toggle();
279 }
280
281 1 => {
282 let command = match app
283 .config
284 .commands
285 .playlist_video_all
286 .clone()
287 .as_command_vec(
288 EnvVar {
289 url: Some(match playlistinfo.mode {
290 Mode::Youtube => {
291 format!(
292 "https://www.youtube.com/playlist?list={}",
293 playlistinfo.playlist.playlist_id
294 )
295 }
296 Mode::Invidious => {
297 format!(
298 "{}/playlist?list={}",
299 app.client.server,
300 playlistinfo.playlist.playlist_id
301 )
302 }
303 }),
304 ..Default::default()
305 },
306 &app.config,
307 ) {
308 Ok(command) => command,
309 Err(_) => {
310 *app.message.lock().unwrap() =
311 Some(String::from("Error in parsing launch command"));
312 return (false, app);
313 }
314 };
315
316 run_command(command, app.message.clone());
317 }
318
319 2 => {
320 let command = match app
321 .config
322 .commands
323 .playlist_audio_all
324 .clone()
325 .as_command_vec(
326 EnvVar {
327 url: Some(match playlistinfo.mode {
328 Mode::Youtube => {
329 format!(
330 "https://www.youtube.com/playlist?list={}",
331 playlistinfo.playlist.playlist_id
332 )
333 }
334 Mode::Invidious => {
335 format!(
336 "{}/playlist?list={}",
337 app.client.server,
338 playlistinfo.playlist.playlist_id
339 )
340 }
341 }),
342 ..Default::default()
343 },
344 &app.config,
345 ) {
346 Ok(command) => command,
347 Err(_) => {
348 *app.message.lock().unwrap() =
349 Some(String::from("Error in parsing launch command"));
350 return (false, app);
351 }
352 };
353
354 run_command(command, app.message.clone());
355 }
356
357 3 => {
358 let command = match app
359 .config
360 .commands
361 .playlist_shuffle_audio_all
362 .clone()
363 .as_command_vec(
364 EnvVar {
365 url: Some(match playlistinfo.mode {
366 Mode::Youtube => {
367 format!(
368 "https://www.youtube.com/playlist?list={}",
369 playlistinfo.playlist.playlist_id
370 )
371 }
372 Mode::Invidious => {
373 format!(
374 "{}/playlist?list={}",
375 app.client.server,
376 playlistinfo.playlist.playlist_id
377 )
378 }
379 }),
380 ..Default::default()
381 },
382 &app.config,
383 ) {
384 Ok(command) => command,
385 Err(_) => {
386 *app.message.lock().unwrap() =
387 Some(String::from("Error in parsing launch command"));
388 return (false, app);
389 }
390 };
391
392 run_command(command, app.message.clone());
393 }
394
395 4 => {
396 let command = match app
397 .config
398 .commands
399 .download_all_video
400 .clone()
401 .as_command_vec(
402 EnvVar {
403 url: Some(match playlistinfo.mode {
404 Mode::Youtube => {
405 format!(
406 "https://www.youtube.com/playlist?list={}",
407 playlistinfo.playlist.playlist_id
408 )
409 }
410 Mode::Invidious => {
411 format!(
412 "{}/playlist?list={}",
413 app.client.server,
414 playlistinfo.playlist.playlist_id
415 )
416 }
417 }),
418 ..Default::default()
419 },
420 &app.config,
421 ) {
422 Ok(command) => command,
423 Err(_) => {
424 *app.message.lock().unwrap() =
425 Some(String::from("Error in parsing launch command"));
426 return (false, app);
427 }
428 };
429
430 run_command(command, app.message.clone());
431 }
432
433 5 => {
434 let command = match app
435 .config
436 .commands
437 .download_all_audio
438 .clone()
439 .as_command_vec(
440 EnvVar {
441 url: Some(match playlistinfo.mode {
442 Mode::Youtube => {
443 format!(
444 "https://www.youtube.com/playlist?list={}",
445 playlistinfo.playlist.playlist_id
446 )
447 }
448 Mode::Invidious => {
449 format!(
450 "{}/playlist?list={}",
451 app.client.server,
452 playlistinfo.playlist.playlist_id
453 )
454 }
455 }),
456 ..Default::default()
457 },
458 &app.config,
459 ) {
460 Ok(command) => command,
461 Err(_) => {
462 *app.message.lock().unwrap() =
463 Some(String::from("Error in parsing launch command"));
464 return (false, app);
465 }
466 };
467
468 run_command(command, app.message.clone());
469 }
470
471 6 => {
472 let mut dir = home::home_dir().unwrap();
473
474 dir.push(".cache");
475 dir.push("youtube-tui");
476 dir.push("thumbnails");
477 dir.push(format!("{}.png", playlistinfo.playlist.playlist_id));
478
479 let command =
480 match app.config.commands.image_viewer.clone().as_command_vec(
481 EnvVar {
482 url: Some(
483 (*dir.as_path().to_string_lossy()).to_string(),
484 ),
485 ..Default::default()
486 },
487 &app.config,
488 ) {
489 Ok(command) => command,
490 Err(_) => {
491 *app.message.lock().unwrap() = Some(String::from(
492 "Error in parsing launch command",
493 ));
494 return (false, app);
495 }
496 };
497
498 run_command(command, app.message.clone());
499 }
500
501 7 => {
502 *app.message.lock().unwrap() = Some(String::from(
503 "Too lazy to implement that what do you expect?",
504 ));
505 }
506
507 8 => {
508 playlistinfo.mode.toggle();
509 }
510
511 _ => {}
512 }
513 }
514
515 _ => {}
516 },
517 PlayListDisplayView::VideoList => match key {
518 KeyCode::Up => playlistinfo.video_view_list.up(),
519 KeyCode::Down => playlistinfo.video_view_list.down(),
520 KeyCode::PageUp => playlistinfo.video_view_list.selected = 0,
521 KeyCode::PageDown => {
522 playlistinfo.video_view_list.selected =
523 playlistinfo.video_view_list.items.len() - 1
524 }
525 KeyCode::Enter => match playlistinfo.video_view_list.selected {
526 0 => {
527 playlistinfo.display_view = playlistinfo.display_view.toggle();
528 }
529
530 x => {
531 let state = ItemInfo::default();
532 let mut history = app.history.clone();
533 history.push(app.into());
534
535 return (
536 false,
537 App {
538 history,
539 page: Page::ItemDisplay(DisplayItem::Video(
540 playlistinfo.playlist.videos[x - 1].video_id.clone(),
541 )),
542 selectable: App::selectable(&state),
543 state,
544 ..Default::default()
545 },
546 );
547 }
548 },
549
550 _ => {}
551 },
552 },
553 _ => {}
554 }
555
556 (true, app)
557 }
558}
559
560impl ItemInfoItem {
561 pub fn load_item(
562 &self,
563 app: &App,
564 watch_history: &mut WatchHistory,
565 ) -> Result<Box<Self>, Box<dyn Error>> {
566 let mut this = self.clone();
567
568 if let ItemInfoItem::Unknown = this {
569 match &app.page {
570 Page::ItemDisplay(display_item) => match display_item {
571 DisplayItem::Video(id) => {
572 let mut list = TextList::default();
573 let video: FullVideo = app.client.video(id, None)?.into();
574 let _ = download_all_thumbnails(LinkedList::from([(
575 video.video_thumbnail.clone(),
576 video.video_id.clone(),
577 )]));
578 list.items(vec![
579 String::from("Watch video"),
580 String::from("Play audio"),
581 String::from("Download video"),
582 String::from("Download audio"),
583 String::from("View thumbnail"),
584 String::from("Visit channel"),
585 String::from("Mode placeholder"),
586 ]);
587
588 let iteminfo = VideoItemInfo {
589 video,
590 list,
591 mode: Mode::Youtube,
592 };
593
594 watch_history.push(
595 iteminfo.video.video_id.clone(),
596 ListItem::FullVideo(iteminfo.video.clone()),
597 &app.config,
598 );
599
600 this = ItemInfoItem::Video(iteminfo);
601 }
602
603 DisplayItem::PlayList(id) => {
604 let mut videos_text_list = TextList::default();
605 let mut playlist_text_list = TextList::default();
606 let playlist: FullPlayList = app.client.playlist(id, None)?.into();
607 let _ = download_all_thumbnails(
608 playlist
609 .videos
610 .iter()
611 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone()))
612 .collect(),
613 );
614
615 videos_text_list.items = vec![String::from("Swtich view")];
616 videos_text_list
617 .items
618 .extend(playlist.videos.iter().map(|v| v.title.clone()));
619
620 playlist_text_list.items(vec![
621 String::from("Switch view"),
622 String::from("Watch all"),
623 String::from("Play all audio"),
624 String::from("Shuffle play all audio"),
625 String::from("Download all video"),
626 String::from("Download all audio"),
627 String::from("View thumbnail"),
628 String::from("Visit channel"),
629 String::from("Mode placeholder"),
630 ]);
631
632 let iteminfo = PlayListItemInfo {
633 playlist: playlist,
634 video_view_list: videos_text_list,
635 playlist_view_list: playlist_text_list,
636 mode: Mode::Youtube,
637 display_view: PlayListDisplayView::PlayListView,
638 };
639
640 watch_history.push(
641 iteminfo.playlist.playlist_id.clone(),
642 ListItem::FullPlayList(iteminfo.playlist.clone()),
643 &app.config,
644 );
645
646 this = ItemInfoItem::PlayList(iteminfo);
647 }
648 },
649
650 _ => {}
651 }
652 }
653 Ok(Box::new(this))
654 }
655
656 pub fn render_item<B: Backend>(
657 &mut self,
658 frame: &mut Frame<B>,
659 rect: Rect,
660 selected: bool,
661 hover: bool,
662 popup_focus: bool,
663 ) {
664 match self {
665 ItemInfoItem::Video(videoinfo) => {
666 let index = videoinfo.list.items.len() - 1;
667 videoinfo.list.items[index] = format!("Mode: {}", videoinfo.mode);
668 drop(index);
669
670 let split = HorizontalSplit::default()
671 .percentages(vec![40, 60])
672 .border_style(Style::default().fg(if selected {
673 Color::LightBlue
674 } else if hover {
675 Color::LightRed
676 } else {
677 Color::Reset
678 }));
679
680 let chunks = split.inner(rect);
681 let mut list = videoinfo.list.clone();
682
683 list.area(chunks[1]);
684 if selected {
685 list.selected_style(Style::default().fg(Color::LightRed));
686 } else {
687 list.selected_style(Style::default().fg(Color::LightYellow));
688 }
689
690 frame.render_widget(split, rect);
691
692 if !popup_focus {
693 let item_display = ItemDisplay {
694 item: ListItem::FullVideo(videoinfo.video.clone()),
695 };
696
697 frame.render_widget(item_display, chunks[0]);
698 }
699
700 frame.render_widget(list, chunks[1]);
701 }
702
703 ItemInfoItem::PlayList(playlistinfo) => {
704 let split = HorizontalSplit::default()
705 .percentages(vec![40, 60])
706 .border_style(Style::default().fg(if selected {
707 Color::LightBlue
708 } else if hover {
709 Color::LightRed
710 } else {
711 Color::Reset
712 }));
713
714 let chunks = split.inner(rect);
715
716 frame.render_widget(split, rect);
717
718 if !popup_focus {
719 let item_display = ItemDisplay {
720 item: ListItem::FullPlayList(playlistinfo.playlist.clone()),
721 };
722
723 frame.render_widget(item_display, chunks[0]);
724 }
725
726 match playlistinfo.display_view {
727 PlayListDisplayView::VideoList => {
728 let mut list = playlistinfo.video_view_list.clone();
729 list.area(chunks[1]);
730 if selected {
731 list.selected_style(Style::default().fg(Color::LightRed));
732 } else {
733 list.selected_style(Style::default().fg(Color::LightYellow));
734 }
735
736 frame.render_widget(list, chunks[1]);
737 }
738 PlayListDisplayView::PlayListView => {
739 let index = playlistinfo.playlist_view_list.items.len() - 1;
740 playlistinfo.playlist_view_list.items[index] =
741 format!("Mode: {}", playlistinfo.mode);
742 drop(index);
743
744 let mut list = playlistinfo.playlist_view_list.clone();
745 list.area(chunks[1]);
746 if selected {
747 list.selected_style(Style::default().fg(Color::LightRed));
748 } else {
749 list.selected_style(Style::default().fg(Color::LightYellow));
750 }
751
752 frame.render_widget(list, chunks[1]);
753 }
754 }
755 }
756
757 ItemInfoItem::Unknown => {
758 let split = HorizontalSplit::default()
759 .percentages(vec![40, 60])
760 .border_style(Style::default().fg(if selected {
761 Color::LightBlue
762 } else if hover {
763 Color::LightRed
764 } else {
765 Color::Reset
766 }));
767
768 frame.render_widget(split, rect);
769 }
770 }
771 }
772}
773
774#[derive(Debug, Clone, PartialEq)]
775pub enum DisplayItem {
776 Video(String),
777 PlayList(String),
778}
779
780pub struct ItemInfo;
781
782impl ItemInfo {
783 pub fn message() -> String {
784 String::from("Loading item info...")
785 }
786
787 pub fn min() -> (u16, u16) {
788 (21, 12)
789 }
790
791 pub fn default() -> Vec<Row> {
792 vec![
793 Row {
794 items: vec![
795 RowItem {
796 item: Item::Global(GlobalItem::SearchBar),
797 constraint: Constraint::Min(16),
798 },
799 RowItem {
800 item: Item::Global(GlobalItem::SearchSettings),
801 constraint: Constraint::Length(5),
802 },
803 ],
804 centered: false,
805 height: Constraint::Length(3),
806 },
807 Row {
808 items: vec![RowItem {
809 item: Item::ItemInfo(ItemInfoItem::Unknown),
810 constraint: Constraint::Percentage(100),
811 }],
812 centered: false,
813 height: Constraint::Min(6),
814 },
815 Row {
816 items: vec![RowItem {
817 item: Item::Global(GlobalItem::MessageBar),
818 constraint: Constraint::Percentage(100),
819 }],
820 centered: false,
821 height: Constraint::Length(3),
822 },
823 ]
824 }
825}
826
827fn run_command(args: Vec<String>, message: Arc<Mutex<Option<String>>>) {
828 thread::spawn(move || {
829 let mut args = args.into_iter();
830 let mut command = Command::new(args.next().unwrap());
831 for arg in args {
832 command.arg(arg);
833 }
834
835 let res = command.spawn();
836 if let Err(e) = res {
837 *message.lock().unwrap() = Some(
838 e.to_string()
839 .lines()
840 .next()
841 .unwrap_or("An error occured")
842 .to_string(),
843 );
844 }
845 });
846}