src/widgets/item_display.rs

641938ba1f255069a13382c2a2903a32612e3873
youtube-tui/src/widgets/item_display.rs history · blame · raw

178 lines · 6597 bytes

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