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