src/widgets/item_display.rs

2737f153106b44565bbbab1d0b08ab3067071283
youtube-tui/src/widgets/item_display.rs history · blame · raw

176 lines · 6496 bytes

  1use tui::{
  2    buffer::Buffer,
  3    layout::Rect,
  4    style::{Color, Style},
  5    widgets::Widget,
  6};
  7use viuer::{print_from_file, Config};
  8
  9use crate::structs::ListItem;
 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::MiniVideo(video) => {
 20                let mut path = home::home_dir().expect("Cannot get your home directory");
 21                path.push(".cache");
 22                path.push("youtube-tui");
 23                path.push("thumbnails");
 24                path.push(format!("{}.png", video.video_id));
 25                let exists = path.exists();
 26                let mut image_transform = area;
 27                if exists {
 28                    if image_transform.width * 9 > image_transform.height * 2 * 16 {
 29                        image_transform.width = image_transform.height * 2 * 16 / 9;
 30                    } else {
 31                        image_transform.height = image_transform.width * 9 / 32;
 32                    }
 33
 34                    let conf = Config {
 35                        width: Some(image_transform.width as u32),
 36                        height: Some(image_transform.height as u32),
 37                        x: image_transform.x,
 38                        y: image_transform.y as i16,
 39                        ..Default::default()
 40                    };
 41
 42                    let _ = print_from_file(path.as_os_str(), &conf);
 43                }
 44
 45                let mut y = if exists {
 46                    image_transform.y + image_transform.height
 47                } else {
 48                    area.y
 49                };
 50
 51                let mut to_print = Vec::new();
 52                to_print.push((String::from("[Video]"), Style::default().fg(Color::Gray)));
 53                to_print.push((video.title, Style::default().fg(Color::LightBlue)));
 54                to_print.push((
 55                    format!("Uploaded by {}", video.author),
 56                    Style::default().fg(Color::LightGreen),
 57                ));
 58                to_print.push((
 59                    format!("Views: {}", video.view_count),
 60                    Style::default().fg(Color::LightYellow),
 61                ));
 62                to_print.push((
 63                    format!("Length: {}", video.length),
 64                    Style::default().fg(Color::LightCyan),
 65                ));
 66                to_print.push((
 67                    format!("Published {}", video.published),
 68                    Style::default().fg(Color::LightMagenta),
 69                ));
 70
 71                for (mut item, style) in to_print {
 72                    if y > area.height + area.y - 1 {
 73                        return;
 74                    }
 75
 76                    if item.len() > area.width as usize {
 77                        item = format!("{}...", &item[..area.width as usize - 3]);
 78                    }
 79                    buf.set_string(area.x, y, item, style);
 80                    y += 1;
 81                }
 82            }
 83            ListItem::FullVideo(video) => {
 84                let mut path = home::home_dir().expect("Cannot get your home directory");
 85                path.push(".cache");
 86                path.push("youtube-tui");
 87                path.push("thumbnails");
 88                path.push(format!("{}.png", video.video_id));
 89                let exists = path.exists();
 90                let mut image_transform = area;
 91                if exists {
 92                    if image_transform.width * 9 > image_transform.height * 2 * 16 {
 93                        image_transform.width = image_transform.height * 2 * 16 / 9;
 94                    } else {
 95                        image_transform.height = image_transform.width * 9 / 32;
 96                    }
 97
 98                    let conf = Config {
 99                        width: Some(image_transform.width as u32),
100                        height: Some(image_transform.height as u32),
101                        x: image_transform.x,
102                        y: image_transform.y as i16,
103                        ..Default::default()
104                    };
105
106                    let _ = print_from_file(path.as_os_str(), &conf);
107                }
108
109                let mut y = if exists {
110                    image_transform.y + image_transform.height
111                } else {
112                    area.y
113                };
114
115                let mut to_print = Vec::new();
116                let mut tags = Vec::new();
117
118                if video.nsfw {
119                    tags.push("Non-Family Friendly");
120                }
121
122                if !video.public {
123                    tags.push("Unlisted")
124                }
125
126                if video.upcoming {
127                    tags.push("Upcoming")
128                }
129                if video.live {
130                    tags.push("Live")
131                }
132
133                tags.push("Video");
134
135                to_print.push((
136                    format!(" [{}]", tags.join(" ")),
137                    Style::default().fg(Color::Gray),
138                ));
139                to_print.push((video.title, Style::default().fg(Color::LightBlue)));
140                to_print.push((
141                    format!("Uploaded by {}", video.channel),
142                    Style::default().fg(Color::LightGreen),
143                ));
144                to_print.push((
145                    format!("Views: {}", video.views),
146                    Style::default().fg(Color::LightYellow),
147                ));
148                to_print.push((
149                    format!("Length: {}", video.length),
150                    Style::default().fg(Color::LightCyan),
151                ));
152                to_print.push((
153                    format!("Genre: {}", video.genre),
154                    Style::default().fg(Color::LightYellow),
155                ));
156                to_print.push((
157                    format!("Published {}", video.published),
158                    Style::default().fg(Color::LightMagenta),
159                ));
160
161                for (mut item, style) in to_print {
162                    if y > area.height + area.y - 1 {
163                        return;
164                    }
165
166                    if item.len() > area.width as usize {
167                        item = format!("{}...", &item[..area.width as usize - 3]);
168                    }
169                    buf.set_string(area.x, y, item, style);
170                    y += 1;
171                }
172            }
173            _ => {}
174        }
175    }
176}