src/widgets/item_display.rs

6f8b64c4fa1b89868a2dbaa1bbd9fda20ecd0768
youtube-tui/src/widgets/item_display.rs history · blame · raw

353 lines · 13469 bytes

  1use tui::{
  2    buffer::Buffer,
  3    layout::{Alignment, Rect},
  4    style::{Color, Style},
  5    widgets::{Paragraph, Widget, Wrap},
  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                if let Some(s) = video.view_count {
 59                    to_print.push((
 60                        format!("Views: {}", s),
 61                        Style::default().fg(Color::LightYellow),
 62                    ));
 63                }
 64                to_print.push((
 65                    format!("Length: {}", video.length),
 66                    Style::default().fg(Color::LightCyan),
 67                ));
 68                if let Some(s) = video.published {
 69                    to_print.push((
 70                        format!("Published: {}", s),
 71                        Style::default().fg(Color::LightMagenta),
 72                    ));
 73                }
 74
 75                for (mut item, style) in to_print {
 76                    if y > area.height + area.y - 1 {
 77                        return;
 78                    }
 79
 80                    if item.len() > area.width as usize {
 81                        if item.len() > area.width as usize - 2 {
 82                            for _ in area.width as usize - 3..item.len() {
 83                                item.pop();
 84                            }
 85
 86                            item.push('…');
 87                        }
 88                    }
 89                    buf.set_string(area.x, y, item, style);
 90                    y += 1;
 91                }
 92            }
 93
 94            ListItem::FullVideo(video) => {
 95                let mut path = home::home_dir().expect("Cannot get your home directory");
 96                path.push(".cache");
 97                path.push("youtube-tui");
 98                path.push("thumbnails");
 99                path.push(format!("{}.png", video.video_id));
100                let exists = path.exists();
101                let mut image_transform = area;
102                if exists {
103                    if image_transform.width * 9 > image_transform.height * 2 * 16 {
104                        image_transform.width = image_transform.height * 2 * 16 / 9;
105                    } else {
106                        image_transform.height = image_transform.width * 9 / 32;
107                    }
108
109                    let conf = Config {
110                        width: Some(image_transform.width as u32),
111                        height: Some(image_transform.height as u32),
112                        x: image_transform.x,
113                        y: image_transform.y as i16,
114                        ..Default::default()
115                    };
116
117                    let _ = print_from_file(path.as_os_str(), &conf);
118                }
119
120                let mut y = if exists {
121                    image_transform.y + image_transform.height
122                } else {
123                    area.y
124                };
125
126                let mut to_print = Vec::new();
127                let mut tags = Vec::new();
128
129                if !video.nsfw {
130                    tags.push("Family Friendly");
131                }
132
133                if !video.public {
134                    tags.push("Unlisted")
135                }
136
137                if video.upcoming {
138                    tags.push("Upcoming")
139                }
140                if video.live {
141                    tags.push("Live")
142                }
143
144                tags.push("Video");
145
146                to_print.push((
147                    format!(" [{}]", tags.join(" ")),
148                    Style::default().fg(Color::Gray),
149                ));
150                to_print.push((video.title, Style::default().fg(Color::LightBlue)));
151                to_print.push((
152                    format!("Uploaded by {}", video.channel),
153                    Style::default().fg(Color::LightGreen),
154                ));
155                to_print.push((
156                    format!("Views: {}", video.views),
157                    Style::default().fg(Color::LightYellow),
158                ));
159                to_print.push((
160                    format!("Length: {}", video.length),
161                    Style::default().fg(Color::LightCyan),
162                ));
163                to_print.push((
164                    format!("Genre: {}", video.genre),
165                    Style::default().fg(Color::LightYellow),
166                ));
167                to_print.push((
168                    format!("Published {}", video.published),
169                    Style::default().fg(Color::LightMagenta),
170                ));
171
172                to_print.push((
173                    String::from("Description: "),
174                    Style::default().fg(Color::Gray),
175                ));
176
177                for (mut item, style) in to_print {
178                    if y > area.height + area.y - 1 {
179                        return;
180                    }
181
182                    if item.len() > area.width as usize {
183                        item = format!("{}...", &item[..area.width as usize - 3]);
184                    }
185                    buf.set_string(area.x, y, item, style);
186                    y += 1;
187                }
188
189                if y < area.height + area.y {
190                    let rect = Rect {
191                        x: area.x,
192                        y: y,
193                        width: area.width,
194                        height: area.height + area.y - y,
195                    };
196
197                    let paragraph = Paragraph::new(video.description.clone())
198                        .wrap(Wrap { trim: true })
199                        .alignment(Alignment::Left)
200                        .style(Style::default().fg(Color::Gray));
201
202                    paragraph.render(rect, buf);
203                }
204            }
205
206            ListItem::MiniPlayList(playlist) => {
207                let mut path = home::home_dir().expect("Cannot get your home directory");
208                path.push(".cache");
209                path.push("youtube-tui");
210                path.push("thumbnails");
211                path.push(format!("{}.png", playlist.playlist_id));
212                let exists = path.exists();
213                let mut image_transform = area;
214                if exists {
215                    if image_transform.width * 9 > image_transform.height * 2 * 16 {
216                        image_transform.width = image_transform.height * 2 * 16 / 9;
217                    } else {
218                        image_transform.height = image_transform.width * 9 / 32;
219                    }
220
221                    let conf = Config {
222                        width: Some(image_transform.width as u32),
223                        height: Some(image_transform.height as u32),
224                        x: image_transform.x,
225                        y: image_transform.y as i16,
226                        ..Default::default()
227                    };
228
229                    let _ = print_from_file(path.as_os_str(), &conf);
230                }
231
232                let mut y = if exists {
233                    image_transform.y + image_transform.height
234                } else {
235                    area.y
236                };
237
238                let mut to_print = Vec::new();
239                to_print.push((String::from("[PlayList]"), Style::default().fg(Color::Gray)));
240                to_print.push((playlist.title, Style::default().fg(Color::LightBlue)));
241                to_print.push((
242                    format!("Created by {}", playlist.author),
243                    Style::default().fg(Color::LightGreen),
244                ));
245                to_print.push((
246                    format!("Video Count: {}", playlist.video_count),
247                    Style::default().fg(Color::LightCyan),
248                ));
249
250                for (mut item, style) in to_print {
251                    if y > area.height + area.y - 1 {
252                        return;
253                    }
254
255                    if item.len() > area.width as usize {
256                        if item.len() > area.width as usize - 2 {
257                            for _ in area.width as usize - 3..item.len() {
258                                item.pop();
259                            }
260
261                            item.push('…');
262                        }
263                    }
264                    buf.set_string(area.x, y, item, style);
265                    y += 1;
266                }
267            }
268
269            ListItem::FullPlayList(playlist) => {
270                let mut path = home::home_dir().expect("Cannot get your home directory");
271                path.push(".cache");
272                path.push("youtube-tui");
273                path.push("thumbnails");
274                path.push(format!("{}.png", playlist.playlist_id));
275                let exists = path.exists();
276                let mut image_transform = area;
277                if exists {
278                    if image_transform.width * 9 > image_transform.height * 2 * 16 {
279                        image_transform.width = image_transform.height * 2 * 16 / 9;
280                    } else {
281                        image_transform.height = image_transform.width * 9 / 32;
282                    }
283
284                    let conf = Config {
285                        width: Some(image_transform.width as u32),
286                        height: Some(image_transform.height as u32),
287                        x: image_transform.x,
288                        y: image_transform.y as i16,
289                        ..Default::default()
290                    };
291
292                    let _ = print_from_file(path.as_os_str(), &conf);
293                }
294
295                let mut y = if exists {
296                    image_transform.y + image_transform.height
297                } else {
298                    area.y
299                };
300
301                let mut to_print = Vec::new();
302                to_print.push((String::from("[PlayList]"), Style::default().fg(Color::Gray)));
303                to_print.push((playlist.title, Style::default().fg(Color::LightBlue)));
304                to_print.push((
305                    format!("Created by {}", playlist.author),
306                    Style::default().fg(Color::LightGreen),
307                ));
308                to_print.push((
309                    format!("Video Count: {}", playlist.video_count),
310                    Style::default().fg(Color::LightCyan),
311                ));
312                to_print.push((
313                    format!("Views: {}", playlist.views),
314                    Style::default().fg(Color::LightYellow),
315                ));
316
317                for (mut item, style) in to_print {
318                    if y > area.height + area.y - 1 {
319                        return;
320                    }
321                    
322                    if item.len() > area.width as usize {
323                        if item.len() > area.width as usize - 2 {
324                            for _ in area.width as usize - 3..item.len() {
325                                item.pop();
326                            }
327
328                            item.push('…');
329                        }
330                    }
331
332                    buf.set_string(area.x, y, item, style);
333
334                    y += 1;
335                }
336            }
337
338            ListItem::PageTurner(b) => {
339                let paragraph = Paragraph::new(if b {
340                    "Press enter to go to the next page"
341                } else {
342                    "Press enter to go to the previous page"
343                })
344                .style(Style::default().bg(Color::DarkGray))
345                .wrap(Wrap { trim: true })
346                .alignment(Alignment::Center);
347
348                paragraph.render(area, buf);
349            }
350            
351        }
352    }
353}