src/widgets/item_display.rs
522 lines · 20260 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 pub render_image: bool,
15}
16
17impl Widget for ItemDisplay {
18 fn render(self, area: Rect, buf: &mut Buffer) {
19 match self.item {
20 ListItem::MiniChannel(channel) => {
21 let mut path = home::home_dir().expect("Cannot get your home directory");
22 path.push(".cache");
23 path.push("youtube-tui");
24 path.push("thumbnails");
25 path.push(format!("{}.png", channel.author_id));
26
27 let exists = path.exists();
28 let mut image_transform = area;
29 if exists {
30 if image_transform.width * 2 > image_transform.height {
31 image_transform.width = image_transform.height / 2;
32 }
33 image_transform.height = image_transform.width / 2;
34 // image_transform.width = image_transform.height * 2;
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 if self.render_image {
45 let _ = print_from_file(path.as_os_str(), &conf);
46 }
47 }
48
49 let mut y = if exists {
50 image_transform.y + image_transform.height
51 } else {
52 area.y
53 };
54
55 let mut to_print = Vec::new();
56 to_print.push((String::from("[Channel]"), Style::default().fg(Color::Gray)));
57 to_print.push((channel.author, Style::default().fg(Color::LightBlue)));
58 to_print.push((
59 format!("Subscribers: {}", channel.sub_count),
60 Style::default().fg(Color::LightYellow),
61 ));
62 to_print.push((
63 format!("Video Count: {}", channel.video_count),
64 Style::default().fg(Color::LightCyan),
65 ));
66
67 to_print.push((
68 String::from("Description: "),
69 Style::default().fg(Color::Gray),
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 if y < area.height + area.y {
85 let rect = Rect {
86 x: area.x,
87 y,
88 width: area.width,
89 height: area.height + area.y - y,
90 };
91
92 let paragraph = Paragraph::new(channel.description.clone())
93 .wrap(Wrap { trim: true })
94 .alignment(Alignment::Left)
95 .style(Style::default().fg(Color::Gray));
96
97 paragraph.render(rect, buf);
98 }
99 }
100 ListItem::FullChannel(channel) => {
101 let mut path = home::home_dir().expect("Cannot get your home directory");
102 path.push(".cache");
103 path.push("youtube-tui");
104 path.push("thumbnails");
105 path.push(format!("{}.png", channel.author_id));
106
107 let exists = path.exists();
108 let mut image_transform = area;
109 if exists {
110 image_transform.height = image_transform.width * 9 / 64;
111 image_transform.width = image_transform.height * 2;
112
113 let conf = Config {
114 width: Some(image_transform.width as u32),
115 height: Some(image_transform.height as u32),
116 x: image_transform.x,
117 y: image_transform.y as i16,
118 ..Default::default()
119 };
120 if self.render_image {
121 let _ = print_from_file(path.as_os_str(), &conf);
122 }
123 }
124
125 let mut y = if exists {
126 image_transform.y + image_transform.height
127 } else {
128 area.y
129 };
130
131 let mut to_print = Vec::new();
132 to_print.push((String::from("[Channel]"), Style::default().fg(Color::Gray)));
133 to_print.push((channel.author, Style::default().fg(Color::LightBlue)));
134 to_print.push((
135 format!("Subscribers: {}", channel.sub_count),
136 Style::default().fg(Color::LightYellow),
137 ));
138
139 to_print.push((
140 String::from("Description: "),
141 Style::default().fg(Color::Gray),
142 ));
143
144 to_print.push((
145 format!("Total Views: {}", channel.total_views),
146 Style::default().fg(Color::LightCyan),
147 ));
148
149 to_print.push((
150 String::from("Description: "),
151 Style::default().fg(Color::Gray),
152 ));
153
154 for (mut item, style) in to_print {
155 if y > area.height + area.y - 1 {
156 return;
157 }
158
159 if item.len() > area.width as usize {
160 item = format!("{}...", &item[..area.width as usize - 3]);
161 }
162 buf.set_string(area.x, y, item, style);
163 y += 1;
164 }
165
166 if y < area.height + area.y {
167 let rect = Rect {
168 x: area.x,
169 y,
170 width: area.width,
171 height: area.height + area.y - y,
172 };
173
174 let paragraph = Paragraph::new(channel.description.clone())
175 .wrap(Wrap { trim: true })
176 .alignment(Alignment::Left)
177 .style(Style::default().fg(Color::Gray));
178
179 paragraph.render(rect, buf);
180 }
181 }
182 ListItem::MiniVideo(video) => {
183 let mut path = home::home_dir().expect("Cannot get your home directory");
184 path.push(".cache");
185 path.push("youtube-tui");
186 path.push("thumbnails");
187 path.push(format!("{}.png", video.video_id));
188 let exists = path.exists();
189 let mut image_transform = area;
190 if exists {
191 if image_transform.width * 9 > image_transform.height * 2 * 16 {
192 image_transform.width = image_transform.height * 2 * 16 / 9;
193 } else {
194 image_transform.height = image_transform.width * 9 / 32;
195 }
196
197 let conf = Config {
198 width: Some(image_transform.width as u32),
199 height: Some(image_transform.height as u32),
200 x: image_transform.x,
201 y: image_transform.y as i16,
202 ..Default::default()
203 };
204 if self.render_image{
205 let _ = print_from_file(path.as_os_str(), &conf);
206 }
207 }
208
209 let mut y = if exists {
210 image_transform.y + image_transform.height
211 } else {
212 area.y
213 };
214
215 let mut to_print = Vec::new();
216 to_print.push((String::from("[Video]"), Style::default().fg(Color::Gray)));
217 to_print.push((video.title, Style::default().fg(Color::LightBlue)));
218 to_print.push((
219 format!("Uploaded by {}", video.author),
220 Style::default().fg(Color::LightGreen),
221 ));
222 if let Some(s) = video.view_count {
223 to_print.push((
224 format!("Views: {}", s),
225 Style::default().fg(Color::LightYellow),
226 ));
227 }
228 to_print.push((
229 format!("Length: {}", video.length),
230 Style::default().fg(Color::LightCyan),
231 ));
232 if let Some(s) = video.published {
233 to_print.push((
234 format!("Published: {}", s),
235 Style::default().fg(Color::LightMagenta),
236 ));
237 }
238
239 for (mut item, style) in to_print {
240 if y > area.height + area.y - 1 {
241 return;
242 }
243
244 if item.len() > area.width as usize {
245 if item.len() > area.width as usize - 2 {
246 for _ in area.width as usize - 3..item.len() {
247 item.pop();
248 }
249
250 item.push('…');
251 }
252 }
253 buf.set_string(area.x, y, item, style);
254 y += 1;
255 }
256 }
257
258 ListItem::FullVideo(video) => {
259 let mut path = home::home_dir().expect("Cannot get your home directory");
260 path.push(".cache");
261 path.push("youtube-tui");
262 path.push("thumbnails");
263 path.push(format!("{}.png", video.video_id));
264 let exists = path.exists();
265 let mut image_transform = area;
266 if exists {
267 if image_transform.width * 9 > image_transform.height * 2 * 16 {
268 image_transform.width = image_transform.height * 2 * 16 / 9;
269 } else {
270 image_transform.height = image_transform.width * 9 / 32;
271 }
272
273 let conf = Config {
274 width: Some(image_transform.width as u32),
275 height: Some(image_transform.height as u32),
276 x: image_transform.x,
277 y: image_transform.y as i16,
278 ..Default::default()
279 };
280 if self.render_image {
281 let _ = print_from_file(path.as_os_str(), &conf);
282 }
283 }
284
285 let mut y = if exists {
286 image_transform.y + image_transform.height
287 } else {
288 area.y
289 };
290
291 let mut to_print = Vec::new();
292 let mut tags = Vec::new();
293
294 if !video.nsfw {
295 tags.push("Family Friendly");
296 }
297
298 if !video.public {
299 tags.push("Unlisted")
300 }
301
302 if video.upcoming {
303 tags.push("Upcoming")
304 }
305 if video.live {
306 tags.push("Live")
307 }
308
309 tags.push("Video");
310
311 to_print.push((
312 format!(" [{}]", tags.join(" ")),
313 Style::default().fg(Color::Gray),
314 ));
315 to_print.push((video.title, Style::default().fg(Color::LightBlue)));
316 to_print.push((
317 format!("Uploaded by {}", video.channel),
318 Style::default().fg(Color::LightGreen),
319 ));
320 to_print.push((
321 format!("Views: {}", video.views),
322 Style::default().fg(Color::LightYellow),
323 ));
324 to_print.push((
325 format!("Length: {}", video.length),
326 Style::default().fg(Color::LightCyan),
327 ));
328 to_print.push((
329 format!("Genre: {}", video.genre),
330 Style::default().fg(Color::LightYellow),
331 ));
332 to_print.push((
333 format!("Published {}", video.published),
334 Style::default().fg(Color::LightMagenta),
335 ));
336
337 to_print.push((
338 String::from("Description: "),
339 Style::default().fg(Color::Gray),
340 ));
341
342 for (mut item, style) in to_print {
343 if y > area.height + area.y - 1 {
344 return;
345 }
346
347 if item.len() > area.width as usize {
348 // item = format!("{}...", &item[..area.width as usize - 3]);
349 item = format!("{}...", &item.get(..area.width as usize - 3).unwrap_or_default());
350 }
351 buf.set_string(area.x, y, item, style);
352 y += 1;
353 }
354
355 if y < area.height + area.y {
356 let rect = Rect {
357 x: area.x,
358 y,
359 width: area.width,
360 height: area.height + area.y - y,
361 };
362
363 let paragraph = Paragraph::new(video.description.clone())
364 .wrap(Wrap { trim: true })
365 .alignment(Alignment::Left)
366 .style(Style::default().fg(Color::Gray));
367
368 paragraph.render(rect, buf);
369 }
370 }
371
372 ListItem::MiniPlayList(playlist) => {
373 let mut path = home::home_dir().expect("Cannot get your home directory");
374 path.push(".cache");
375 path.push("youtube-tui");
376 path.push("thumbnails");
377 path.push(format!("{}.png", playlist.playlist_id));
378 let exists = path.exists();
379 let mut image_transform = area;
380 if exists {
381 if image_transform.width * 9 > image_transform.height * 2 * 16 {
382 image_transform.width = image_transform.height * 2 * 16 / 9;
383 } else {
384 image_transform.height = image_transform.width * 9 / 32;
385 }
386
387 let conf = Config {
388 width: Some(image_transform.width as u32),
389 height: Some(image_transform.height as u32),
390 x: image_transform.x,
391 y: image_transform.y as i16,
392 ..Default::default()
393 };
394
395 if self.render_image{
396 let _ = print_from_file(path.as_os_str(), &conf);
397 }
398 }
399
400 let mut y = if exists {
401 image_transform.y + image_transform.height
402 } else {
403 area.y
404 };
405
406 let mut to_print = Vec::new();
407 to_print.push((String::from("[PlayList]"), Style::default().fg(Color::Gray)));
408 to_print.push((playlist.title, Style::default().fg(Color::LightBlue)));
409 to_print.push((
410 format!("Created by {}", playlist.author),
411 Style::default().fg(Color::LightGreen),
412 ));
413 to_print.push((
414 format!("Video Count: {}", playlist.video_count),
415 Style::default().fg(Color::LightCyan),
416 ));
417
418 for (mut item, style) in to_print {
419 if y > area.height + area.y - 1 {
420 return;
421 }
422
423 if item.len() > area.width as usize {
424 if item.len() > area.width as usize - 2 {
425 for _ in area.width as usize - 3..item.len() {
426 item.pop();
427 }
428
429 item.push('…');
430 }
431 }
432 buf.set_string(area.x, y, item, style);
433 y += 1;
434 }
435 }
436
437 ListItem::FullPlayList(playlist) => {
438 let mut path = home::home_dir().expect("Cannot get your home directory");
439 path.push(".cache");
440 path.push("youtube-tui");
441 path.push("thumbnails");
442 path.push(format!("{}.png", playlist.playlist_id));
443 let exists = path.exists();
444 let mut image_transform = area;
445 if exists {
446 if image_transform.width * 9 > image_transform.height * 2 * 16 {
447 image_transform.width = image_transform.height * 2 * 16 / 9;
448 } else {
449 image_transform.height = image_transform.width * 9 / 32;
450 }
451
452 let conf = Config {
453 width: Some(image_transform.width as u32),
454 height: Some(image_transform.height as u32),
455 x: image_transform.x,
456 y: image_transform.y as i16,
457 ..Default::default()
458 };
459
460 if self.render_image{
461 let _ = print_from_file(path.as_os_str(), &conf);
462 }
463 }
464
465 let mut y = if exists {
466 image_transform.y + image_transform.height
467 } else {
468 area.y
469 };
470
471 let mut to_print = Vec::new();
472 to_print.push((String::from("[PlayList]"), Style::default().fg(Color::Gray)));
473 to_print.push((playlist.title, Style::default().fg(Color::LightBlue)));
474 to_print.push((
475 format!("Created by {}", playlist.author),
476 Style::default().fg(Color::LightGreen),
477 ));
478 to_print.push((
479 format!("Video Count: {}", playlist.video_count),
480 Style::default().fg(Color::LightCyan),
481 ));
482 to_print.push((
483 format!("Views: {}", playlist.views),
484 Style::default().fg(Color::LightYellow),
485 ));
486
487 for (mut item, style) in to_print {
488 if y > area.height + area.y - 1 {
489 return;
490 }
491
492 if item.len() > area.width as usize {
493 if item.len() > area.width as usize - 2 {
494 for _ in area.width as usize - 3..item.len() {
495 item.pop();
496 }
497
498 item.push('…');
499 }
500 }
501
502 buf.set_string(area.x, y, item, style);
503
504 y += 1;
505 }
506 }
507
508 ListItem::PageTurner(b) => {
509 let paragraph = Paragraph::new(if b {
510 "Press enter to go to the next page"
511 } else {
512 "Press enter to go to the previous page"
513 })
514 .style(Style::default().bg(Color::DarkGray))
515 .wrap(Wrap { trim: true })
516 .alignment(Alignment::Center);
517
518 paragraph.render(area, buf);
519 }
520 }
521 }
522}