src/widgets/item_display.rs

49bf7c8f8b3f8fc98bc4e79420265fbbd9736d67
youtube-tui/src/widgets/item_display.rs history · blame · raw

76 lines · 2908 bytes

 1use crate::app::pages::global::ListItem;
 2use thousands::Separable;
 3use tui::{
 4    buffer::Buffer,
 5    layout::{Margin, Rect},
 6    style::{Color, Style},
 7    widgets::{Block, Borders, Widget},
 8};
 9use viuer::{print_from_file, Config};
10
11#[derive(Clone)]
12pub struct ItemDisplay {
13    pub item: ListItem,
14}
15
16impl Widget for ItemDisplay {
17    fn render(self, area: Rect, buf: &mut Buffer) {
18        match self.item {
19            ListItem::Video(video) => {
20                let mut path = home::home_dir().expect("Cannot get your home directory");
21                path.push(".siriusmart");
22                path.push("youtube-tui");
23                path.push("cache");
24                path.push("thumbnails");
25                path.push("videos");
26                path.push(format!("{}.png", video.video_id));
27                let exists = path.exists();
28                let mut image_transform = area;
29                if  exists{
30                    if image_transform.width * 9 > image_transform.height * 2 * 16 {
31                        image_transform.width = image_transform.height * 2 * 16 / 9;
32                    } else {
33                        image_transform.height = image_transform.width * 9 / 32;
34                    }
35
36                    let conf = Config {
37                        width: Some(image_transform.width as u32),
38                        height: Some(image_transform.height as u32),
39                        x: image_transform.x,
40                        y: image_transform.y as i16,
41                        ..Default::default()
42                    };
43
44                    let _ = print_from_file(path.as_os_str(), &conf);
45                }
46
47                let mut y = if exists {
48                    image_transform.y + image_transform.height
49                } else {
50                    area.y
51                };
52
53                let mut to_print = Vec::new();
54                to_print.push((String::from("[Video]"), Style::default().fg(Color::Gray)));
55                to_print.push((video.title, Style::default().fg(Color::LightBlue)));
56                to_print.push((format!("Uploaded by {}", video.author), Style::default().fg(Color::LightGreen)));
57                to_print.push((format!("Views: {}", video.view_count.separate_with_commas()), Style::default().fg(Color::LightYellow)));
58                to_print.push((format!("Published {}", video.published), Style::default().fg(Color::LightMagenta)));
59
60
61                for (mut item, style) in to_print {
62                    if y > area.height + area.y - 1 {
63                        return;
64                    }
65
66                    if item.len() > area.width as usize {
67                        item = format!("{}...", &item[..area.width as usize - 3]);
68                    }
69                    buf.set_string(area.x, y, item, style);
70                    y += 1;
71                }
72            }
73            _ => {}
74        }
75    }
76}